Stripe/PHP:处理客户对象成功但充电失败(4000000000000341)
问题描述:
在条带中,测试号码4000 0000 0000 0341
根据Stripe Testing page模拟了“将此卡连接到客户对象成功但尝试向客户收费失败“。在我的情况下,我想将这种情况视为错误,并将其发送给错误处理程序,而不是向客户报告收费成功。Stripe/PHP:处理客户对象成功但充电失败(4000000000000341)
我在PHP中这样做,所以我没有从API获取JSON对象,而是为客户获取PHP对象。我是Stripe API的新手,所以我不知道我应该在这里做什么。我试图寻找有关这种情况的信息,并找不到任何有用的信息。看起来这个案件不是由现有的SO问题处理的。
摘自代码如下。如果客户没有收费,我需要将$charge_failed
设置为true
。
\Stripe\Stripe::setApiKey(STRIPE_SECRET_KEY);
// Create a customer.
try {
$customer = \Stripe\Customer::create(array("source" => $my_token, 'email' => $customers_email, "description" => $my_description));
$charge_failed = false; //TODO Set this boolean according to whether the charge passed or failed.
if ($charge_failed) {
//TODO Report to the user that the charge failed and they need to resubmit.
die();
}
}
catch(\Stripe\Error\Card $e) {
// Card was declined. Report the card declined to the user.
die();
}
catch (\Stripe\Error\RateLimit $e) {
// Report the error appropriately.
die();
} catch (\Stripe\Error\InvalidRequest $e) {
// Report the error appropriately.
die();
} catch (\Stripe\Error\Authentication $e) {
// Report the error appropriately.
die();
} catch (\Stripe\Error\ApiConnection $e) {
// Report the error appropriately.
die();
} catch (\Stripe\Error\Base $e) {
// Report the error appropriately.
die();
} catch (Exception $e) {
// Report the error appropriately.
die();
}
答
在某些日子里,我成功实现了条带。希望这会有所帮助。
if ($params['testmode'] == "on") {
\Stripe\Stripe::setApiKey($params['private_test_key']);
$pubkey = $params['public_test_key'];
} else {
\Stripe\Stripe::setApiKey($params['private_live_key']);
$pubkey = $params['public_live_key'];
}
include_once 'Stripe/init.php';
include_once 'Stripe/lib/Stripe.php';
//Set Stripe keys
$params = array(
"testmode" => "off",
"private_live_key" => "sk_live_",
"public_live_key" => "pk_live_",
"private_test_key" => "sk_test_",
"public_test_key" => "pk_test_"
);
if ($params['testmode'] == "on") {
\Stripe\Stripe::setApiKey($params['private_test_key']);
$pubkey = $params['public_test_key'];
} else {
\Stripe\Stripe::setApiKey($params['private_live_key']);
$pubkey = $params['public_live_key'];
}
if (isset($_POST['stripeToken'])) {
try {
$customer = \Stripe\Customer::create(array(
'email' => $varUserEmail,
'source' => $_POST['stripeToken']
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $price,
'currency' => 'usd',
'description' => $description,
));
$result = "success";
//Save information in the database and send an email with status
//Ends here
} catch (\Stripe\Error\Card $e) {
$error = $e->getMessage();
$result = "declined";
//Save information in the database and send an email with status
}
答
我可能是错的,但我认为,这种情况只有当你已经有一个现成的客户对象和要尝试更新自己的付款方式发生。在尝试创建客户之前,您用于创建客户的令牌将无效,并且客户创建将失败。如果这是初始费用,则JavaScript方法将返回失败。