Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Stripe payments Stripe PHP-如何检索新添加卡的ID?_Stripe Payments - Fatal编程技术网

Stripe payments Stripe PHP-如何检索新添加卡的ID?

Stripe payments Stripe PHP-如何检索新添加卡的ID?,stripe-payments,Stripe Payments,我希望允许我们的客户向其帐户添加多张卡。因此,在结账时,他们可以选择使用哪张卡或添加一张新卡 我可以通过拨打以下电话选择已添加的卡ID: $cardid = $customer->sources->data[0]->id; $cardid = $customer->sources->data[1]->id; $cardid = $customer->sources->data[2]->id; etc... 但我需要检索卡ID或新添加的卡 /

我希望允许我们的客户向其帐户添加多张卡。因此,在结账时,他们可以选择使用哪张卡或添加一张新卡

我可以通过拨打以下电话选择已添加的卡ID:

$cardid = $customer->sources->data[0]->id;
$cardid = $customer->sources->data[1]->id;
$cardid = $customer->sources->data[2]->id;
etc...
但我需要检索卡ID或新添加的卡

//Create Token
try {
$token = \Stripe\Token::create(
                array(
                        "card" => array(
                        "name" => $_POST['ccname'],
                        "number" => $_POST['ccnum'],
                        "exp_month" => $_POST['ccxpm'],
                        "exp_year" => $_POST['ccxpy'],
                        "cvc" => $_POST['cccvc'] )
)); }
catch(\Stripe\Error\Card $e) {
     $body = $e->getJsonBody();
     $err  = $body['error'];
    $status = $err['message'];
}       


// Add new Card to Custid
$customer = \Stripe\Customer::retrieve($_POST['custid']);
$customer->sources->create(
                array(
                        "source" => $token['id']
));

$cardid = $customer->sources->data[]->id; ???

//  Charge CustID
$mysum = $_POST['amount']*100;

$charge = \Stripe\Charge::create(array(
'customer' => $customer,
'amount'   => $mysum,
'currency' => 'usd',
'card' => $cardid
));
将返回新创建的,因此您只需从中获取ID:

$new_card = $customer->sources->create(array(
    "source" => $token['id']
));

$new_card_id = $new_card->id;
请注意,在向客户添加新卡时,Stripe将向发卡行验证该卡,如果验证失败,可能会返回
card\u错误。您应该将卡片创建请求包装在一个
try/catch
块中