lundi 11 septembre 2017

How to integrate Stripe Checkout server side code with Laravel (5.4) Cashier?

I'd like my site visitors to be able to sign up to one of three simple subscription plans, paying with a credit/debit card. I've set up Laravel Cashier on the back end, and the default Stripe Checkout on the frontend, and am now just trying to finish the back end Laravel code to tie the two together. I've been following an example Stripe 'recipe' and am looking at the bit where the server receives the Stripe token from the Stripe checkout popup on the front end, here: http://ift.tt/2y2lrqC and trying to match that up with the bit in the Laravel docs about creating a subscription: http://ift.tt/2xsnvv1

So I'm effectively trying to merge those two bits of code at those two links (amazed that I can't find anything online about it anywhere given it's using all the standard default Laravel and Stripe stuff!). I'm not sure exactly what code to write. Guessing it's something like the below?

\Stripe\Stripe::setApiKey("my_stripe_secret_key");
$user = User::find(1);
$user->newSubscription('main', 'premium')->create($stripeToken);

1.) What about the try...catch block in the Stripe example code? (at the aforementioned link) - if the payment fails, how does Laravel handle that, or how do I handle that in the Laravel code?

2.) Presumably I'm getting the $stripeToken from Laravel's Request::get('stripeToken') (using the field from the Stripe example code)

3.) The Stripe example code posts the customer's email from the Checkout form, back to Stripe to create the customer, but the Laravel example code doesn't supply email, so I'm not clear on whether this is really necessary or not (for Stripe, or for my database).

4.) What about Laravel's CSRF protection - is that likely to be an issue with receiving the POST from Stripe Checkout?

Basically, the end result I want is as advertised in the two docs - a new customer and subscription entered into the database on both my server, and in Stripe, and Stripe to bill the customer - i.e. all the default standard stuff :)

Thanks for any suggestions



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire