Laravel Cashier - where does $stripeToken come from?
Asked Answered
T

2

6

The documentation on Laravel Cashier is quite vague and misses some very important details like what the $stripeToken is and where does it come from?

So to create a new subscription we do this:

$user->newSubscription('main', 'premium')->create($stripeToken);

This is the first time a user will be subscribing so where does $stripeToken come from exactly?

In the docs it says:

The create method, which accepts a Stripe credit card / source token, will begin the subscription as well as update your database with the customer ID and other relevant billing information.

Does this mean I have to manually create the customer object in Stripe first and then pass the customer id as the $stripeToken? It mentions card details but how do I pass them? What is the format and what do I expect in return?

If $stripeToken is the customer id in Stripe then Cashier is assuming that we already have customers created in Stripe which we won't have the first time.

Can anyone shed some light on this?

Thermocouple answered 13/6, 2018 at 11:20 Comment(2)
If you use the Stripe Checkout JS, it will automatically add a hidden input stripeToken to the form.Eruptive
@Eruptive What if you're not using Stripe JS? Lets say you are doing basic REST API driven checkout forms?Thermocouple
T
5

It turns out that the stripeToken is usually generated by stripe.js forms when they are submitted.

As I am using API driven checkout forms and not standard html submission forms I need to use the Stripe API to create the token from the card details provided.

$stripeToken = Token::create(array(
                       "card" => array(
                           "number"    => $request->get('number'),
                           "exp_month" => str_before($request->get('expiry'), '/'),
                           "exp_year"  => str_after($request->get('expiry'), '/'),
                           "cvc"       => $request->get('cvc'),
                           "name"      => $request->get('name')
                       )
                   ));

Then I use $stripeToken->id and pass it:

$user->newSubscription('main', 'premium')->create($stripeToken->id);
Thermocouple answered 14/6, 2018 at 10:41 Comment(3)
From where you are getting this Token (Token::create())Grocer
I am using Braintree and I am unable to find this solution for braintree. There is nothing much information about $token for Braintree. Can anyone explain this pleaseMassimo
@YasirIjaz Token is coming from the Stripe API namespaceThermocouple
A
0

You can use the Stripe JS to get the stripeToken but if you are using the custom form then you can use Stripe checkout method.

Using both the ways you will get stripeToken in javascript and then you have to pass this token to your REST API for further processing.

Audition answered 14/6, 2018 at 8:23 Comment(1)
Nope, I'm not using Stripe JS. I'm using API driven checkout so it is not a standard checkout form that can use JS. The JS library is redundant in this situation and probably adds more resources that aren't required. See my answer for a more suitable solution for API driven forms.Thermocouple

© 2022 - 2024 — McMap. All rights reserved.