0
votes

I'm using Stripe Checkout on the frontend and collecting user's card details then using Stripe's PHP library on the backend to subscribe the user to a pre-defined Stripe plan. I get the error

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

and checking the Stripe Dashboard shows the user (Stripe customer) does not have any card details associated even though I pass the token through to the subscription call as per docs etc.

Front end code (Laravel template):

<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>

and back end code:

\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]]]);

Not sure where I am going wrong. Thanks for any suggestions.

2

2 Answers

1
votes

You have to use the below code to make successful subscription, while creating subscriptions

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

You have skip trial_end parameter in your request.

0
votes

Most likely $request->input['stripeToken'] is empty. I'd log that server-side and confirm that it's got something in it.