Stripe PHP订阅错误 - 客户没有附加的付款来源

时间:2018-05-28 22:07:00

标签: php stripe-payments

我在前端使用Stripe Checkout并收集用户的卡片详细信息,然后在后端使用Stripe的PHP库来订阅用户预定义的Stripe计划。我收到了错误

(1/1) InvalidRequest This customer has no attached payment source

并检查条带仪表板显示用户(条带客户)没有关联任何卡详细信息,即使我按照文档等将令牌传递到订阅调用。

前端代码(Laravel模板):

<form action="{{url('subscribe')}}" method="POST" id="stripeform1" class="nodisplay">
{!! csrf_field() !!}
<input type="hidden" name="plan" value="individual">
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="my_stripe_test_key"
        data-image="https://myapp.co/public/images/stripeicon.png"
        data-name="MyApp"
        data-zip-code="true"
        data-locale="auto"
        data-email="testemail@test.com"
        data-description="Subscription for Individual Plan"
        data-currency="gbp"
        data-amount="999"
        data-label="Subscribe!">
    </script>
</form>

和后端代码:

\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

// Create customer that we can then create a subscription or one-off charges for:
$customer = \Stripe\Customer::create(['email' => $request->input['stripeEmail'], 'source'  => $request->input['stripeToken']]);

// Attach user (as Stripe customer) to existing subscription plan defined in Stripe:
if($request->input('plan') == 'individual') $planID = env('STRIPE_INDIVIDUAL_PLAN_ID');
else if($request->input('plan') == 'business') $planID = env('STRIPE_BUSINESS_PLAN_ID');
$subscription = \Stripe\Subscription::create(['customer' => $customer->id, 'items' => [['plan' => $planID]]]);

不确定我哪里出错了。感谢您的任何建议。

2 个答案:

答案 0 :(得分:0)

最有可能$request->input['stripeToken']为空。我记录了服务器端,并确认它中有内容。

答案 1 :(得分:0)

在创建订阅时,您必须使用以下代码进行成功订阅

$subscription = \Stripe\Subscription::create(['customer' => $customer->id,"trial_end" => null, 'items' => [['plan' => $planID]]]);

您已在请求中跳过trial_end参数。