gpt4 book ai didi

php - Stripe ,验证卡并仅在操作后充电

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:08:09 26 4
gpt4 key购买 nike

我正在为一个网站启用支付功能。我使用 Stripe 作为提供者。我想知道如果 2 个条件成立,我该如何从卡中扣款。当用户付款时,我想更改数据库中的一个值,并从卡中扣款。但是如果数据库查询失败我不想扣卡。同样,我不想查询卡是否无效。我需要两者,卡有效和查询成功。我该怎么做?

这是充值卡的代码

try {
$charge = \Stripe\Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "cad",
"source" => $token,
"description" => $description)
);
} catch(\Stripe\Error\Card $e) {
// The card has been declined
}

最佳答案

您应该考虑向客户收费,而不是向卡收费。我的意思是:

1. Create a customer

$customer = \Stripe\Customer::create(array(
"description" => "Customer for test@example.com",
"source" => "tok_15gDQhLIVeeEqCzasrmEKuv8" // obtained with Stripe.js
));

2. Create a card

$card = $customer->sources->create(array("source" => "tok_15gDQhLIVeeEqCzasrmEKuv8"));

来自 Stripe API 引用:

source | external_account REQUIRED When adding a card to a customer, the parameter name is source. The value can either be a token, like the ones returned by our Stripe.js, or a dictionary containing a user’s credit card details. Stripe will automatically validate the card.

通过创建卡片,Stripe 会自动验证它。因此,有了有效的信用卡对象,您就可以在数据库上执行任何想要的查询,如果成功,则向客户收费。

3. Charge

\Stripe\Charge::create(array(
"amount" => 400,
"currency" => "usd",
"source" => "tok_15gDQhLIVeeEqCzasrmEKuv8", // obtained with Stripe.js,
// "customer" => $cusomer->id // the customer created above
"metadata" => array("order_id" => "6735")
));

收费的时候可以传入source(通过Stripe.js获取的token)或者我们刚刚创建的customer id。

另外不要忘记try...catch一切。

关于php - Stripe ,验证卡并仅在操作后充电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31306967/

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