8

I'm using Laravel Cashier along with Stripe to manage subscriptions. The user will supply their credit card information when signing up, but they won't be subscribed in a specific plan at this point. So I can successfully use Stripe Checkout to create a Stripe customer object and save the Stripe customer ID in my database. But when it comes time for the user to enroll in a plan, I can't see a way to use the Stripe customer ID to enroll them in the plan they want.

Of course, I could ask for their credit card information again and get a Stripe token to use with Laravel Cashier, but I'd like to avoid this since the app already created a Stripe customer object when they signed up and I'd like to simply use the existing customer object to charge their credit card rather than asking for their card number again.

To try illustrate what I'm trying to do, here is some sample code from the Laravel docs:

$user->newSubscription('main', 'monthly')->create($creditCardToken);

But what I'd like to be able to do is something like this (note the change to the create method:

$user->newSubscription('main', 'monthly')->create($user->stripe_id);

Any advice?

JasonJensenDev
  • 2,377
  • 21
  • 30
  • If the user already has a stripe_id, does that mean that they previously had a subscription? If so, you can just resume the subscription, and if you want to change it, change it. Or are you trying to accomplish something else? – Sehael Jul 13 '16 at 17:03
  • You can create a stripe user without a plan, but with a credit card. In fact, I'm trying to do something quite similar, so have added a bounty. – Chris Aug 21 '16 at 14:04

2 Answers2

11

If there is a stripe ID for the user, you don't have to supply the token

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

Have a look at the SubscriptionBuilder class.

Tosho Trajanov
  • 790
  • 8
  • 19
  • This works as expected but it leaves the ends_at date in table as it is. Is this normal or there is something wrong here? – sanky Feb 06 '18 at 10:13
  • ends_at should remain null. If you want to cancel it, you can explicitly do that by using the cancel() function from the Subscription class. https://github.com/laravel/cashier/blob/6.0/src/Subscription.php – Tosho Trajanov Feb 06 '18 at 10:17
0

You can try this.

$user->setStripeKey(env("STRIPE_KEY"));

# card details
$card = [
'card_number' => 'xxxxxxxx',
'card_cvc' => 'xxx',
'exp_month' => 'xx',
'exp_year' => 'xxxx',
'name' => 'xxx',
];
# generate token
$token = $this->generateAccessToken($card);

private function generateAccessToken($card)
{

    $client = new \GuzzleHttp\Client();
    $url = 'https://api.stripe.com/v1/tokens';
    $pubKey = env("STRIPE_SECRET");
    $postBody = [
        'key' => $pubKey,
        'payment_user_agent' => 'stripe.js/Fbebcbe6',
        'card' => [
            'number' => $card['card_number'],
            'cvc' => $card['card_cvc'],
            'exp_month' => $card['exp_month'],
            'exp_year' => $card['exp_year'],
            'name' => $card['name']
        ]
    ];

    $response = $client->post($url, [
        'form_params' => $postBody
    ]);

    $response_obj = json_decode($response->getbody()->getContents());

    return $response_obj->id;
}
# main or primary
$subscription_obj = $user->newSubscription('subscription_name', 'stripe_plan_id')->create($token);
Pratik Mehta
  • 707
  • 8
  • 27