我正在使用编写器实例化条带,并尝试使用卡号4000 0000 0000 0341在测试模式下订阅客户到订阅计划。文档说这将被接受,但在用于创建订阅时会抛出错误。我的问题是,为什么创建订阅步骤总是会使用此卡号返回成功?
我的收费代码是
<form action="subscribe.php" method="post">
<input type="hidden" name="customer" value="<? echo $stripeID ?>">
<input type="hidden" name="plan" value="<? echo $thisPlan['id'] ?>">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-name="www.example.com"
data-description="<? echo $thisPlan['name'] ?>"
data-amount="<? echo $thisPlan['amount'] ?>"
data-locale="auto"
data-panel-label="Subscribe Now"
data-label="Subscribe"
data-allow-remember-me="false">
</script>
</form>
我的subscribe.php是
try {
$result = \Stripe\Subscription::create(array(
"customer" => $_POST['customer'],
"plan" => $_POST['plan']
));
echo "Subscription successful";
}
catch (Stripe\Error\Base $e) {
// Code to do something with the $e exception object when an error occurs
echo($e->getMessage());
} catch (Exception $e) {
// Catch any other non-Stripe exceptions
}
答案 0 :(得分:0)
问题是为什么测试卡号码不会产生错误响应。答案在评论中:客户已经分配了默认卡,即使在javascript表单中提供了新卡,条带始终使用默认卡。我的解决方案是使用令牌中返回的新卡号更新客户,然后应用订阅:
try {
$cu = \Stripe\Customer::retrieve($_POST['customer']);
$cu->source = $_POST['stripeToken']; // obtained with Stripe.js
$cu->save();
}
try {
$result = \Stripe\Subscription::create(array(
"customer" => $_POST['customer'],
"plan" => $_POST['plan']
));
echo&#34; \ n订阅成功&#34 ;; }