Search code examples
laravellaravel-5stripe-paymentslaravel-cashier

Laravel Cashier - How to Check if Stripe Payment was Successful


I am using Cashier in Laravel 5.1 for stripe payment. I want to check how to create a new database entry only when the stripe payment is successful. I am doing something like this:

if ($user->subscribed())
{
        $subscribe = $user->subscription()->increment($totalQuantity);
}
else {
        $subscribe = $user->subscription('monthly')->quantity($totalQuantity)->create($postData['token']);
}

if($subscribe) {
   // Insert into Database
}

I thought the stripe subscription will return true or false so I can check if the last payment was successful and proceed. But the $subscribe is empty.

How can I check if the last stripe payment was successful before I proceed with the DB insert queries? I have read the docs and it only mentioned about checking the overall subscription like using

`if ($user->subscribed())`

but it doesn't mention about how to check if the last stripe subscription charge was successful. How will I do that?


Solution

  • You'll probably want to look at the "handling failed subscriptions" and "handling other webhooks" sections. http://laravel.com/docs/5.0/billing#handling-failed-subscriptions

    Webhooks are where a 3rd party does a callback to a URL for your application. For example, with stripe someone has just subscribed and made the payment, stripe will then visit a URL of your application with various information about whether that payment was decline, successful, etc. See https://stripe.com/docs/webhooks

    You can register the route to your application in your stripe dashboard under Your Account > Account Settings > Webhooks > Add New Endpoint