gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:12:12 24 4
gpt4 key购买 nike

我想允许我们的客户将多张卡添加到他们的帐户中。因此,在结账时,他们可以选择要使用的卡或添加新卡。

我可以通过调用来选择已经添加的卡ID:

$cardid = $customer->sources->data[0]->id;
$cardid = $customer->sources->data[1]->id;
$cardid = $customer->sources->data[2]->id;
etc...

但是我需要取回卡号或者新添加的卡。

//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
));

最佳答案

card creation request将返回新创建的 card object ,所以您可以简单地从那里获取 ID:

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

$new_card_id = $new_card->id;

请注意,在向客户添加新卡时,Stripe 会与发卡行验证卡,如果验证失败,可能会返回 card_error。您应该将卡片创建请求包装在一个 try/catch block 中以 handle possible errors .

关于stripe-payments - Stripe PHP - 如何检索新添加的卡的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46336287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com