将信用卡添加到现有用户

时间:2017-08-09 12:42:14

标签: php stripe-payments

我在客户首次注册时添加此信用卡

// CREDIT CARD CODE (STRIPE)

$q_get_user = "select * from users where `id` = '$user_id' ";
$r_get_user = mysqli_query($conn,$q_get_user);
$get_user = mysqli_fetch_assoc($r_get_user);


if(1) {
    \Stripe\Stripe::setApiKey("sk_live_9N676756776");


    try {

        $t = \Stripe\Token::create(
                array(
                        "card" => array(
                                "name" => $get_user['first_name']." ".$get_user['last_name'],
                                "number" => $credit_num,
                                "exp_month" => $credit_month,
                                "exp_year" => $credit_year,
                                "cvc" => $credit_ccv
                        )
                )
        );

        if($t->id != '') {
            try {
                $c = \Stripe\Customer::create(array(
                        "description" => "Customer for ".$get_user['email'],
                        "source" => $t->id)
                );
                if($c->id != '') {
                    $stripe_token_response = mysqli_real_escape_string($conn, json_encode($t));
                    $stripe_token_id = mysqli_real_escape_string($conn, $t->id);
                    $stripe_customer_response = mysqli_real_escape_string($conn, json_encode($c));
                    $stripe_customer_id = mysqli_real_escape_string($conn, $c->id);
                    $stripe_card_id = mysqli_real_escape_string($conn, $c->default_source);
                }

            } catch (Exception $e) {
                //print_r($e->getMessage());
                header('Location: /credits?error=cc&message='.urlencode($e->getMessage()));die;
            }
        }

    } catch (Exception $e) {
        //print_r($e->getMessage());
        header('Location: /credits?error=cc&message='.urlencode($e->getMessage()));die;

    }



}

// END - CREDIT CARD CODE (STRIPE)

如何让新客户加入现有客户?因此,客户正在添加一张新卡(他们将拥有多张卡)

1 个答案:

答案 0 :(得分:1)

您正在通过API直接发送卡详细信息,这可能不是您想要做的事情。这意味着您在服务器上获得了一些serious PCI compliance implications的卡号。我强烈建议您修改集成,以便始终使用Stripe.jsStripe Checkout客户端将卡详细信息直接发送到Stripe,并将卡详细信息直接发送到Stripe并获取唯一的卡令牌(tok_XXX) )然后,您可以安全地发送到您的服务器以创建客户或添加为卡。

您可以找到“卡片更新”流程here的说明;你需要的唯一区别是,而不是这样做来取代卡:

$cu->source = $_POST['stripeToken']; // obtained with Checkout

您希望do this添加新的

$customer->sources->create(array("source" => $t->id));