Stripe has so many different methods it's kinda confusing which one to used. I have a credit card form for the user after he/she has selected the subscription they want to purchase. From this point, I want to create the customer, setup payment and subscription all in one step.
Do I do:
Create customer
Create payment method
Create subscription
OR
Create Intent
Create Session
Or does creating a session handle create customer, create payment method and create subscription all in one?
If you want to use the hosted Checkout solution, you would not collect payment details yourself. Stripe does that for you as part of the Checkout flow. You can create a customer ahead of time, but by default Checkout will create one for you.
Most likely using Checkout with mode=subscription to start a subscription is going to be your most straightforward option. If you do use Checkout this way, you would not create any Subscriptions/Intents directly yourself. You can also use the Customer Portal to manage changes to the subscriptions later.
You can also go with the custom flow you described, creating the customer, attaching payment methods and starting a subscription, but this is more involved.
I'm using Stripe to handle payments for a subscription service I'm setting up.
I gather the relevant information from my customer, then on the server side, I use the Stripe PHP IDE to set up a new customer and create a checkout session for a price object which I've set up as a subscription. I save the checkout session ID to my database, then use that same session ID client side to take payment from the customer, via a redirect to Stripe.
The webhook checkout.session.completed, then lets me link up the previous checkout session ID with the subscription ID. Then I need the second webhook customer.subscription.updated to get the status of the subscription from the subscription id.
It feels like I'm doing something wrong here. I'm using two webhooks to get the information I need. If the checkout.session.completed webhook were to arrive after the customer.subscription.updated webhook, then my logic will fail.
Is there a better/correct way to manage this flow?
You only need checkout.session.completed here. That event indicates a successful Checkout and payment.
I would ignore the initial customer.subscription.updated event and instead, if you need that status, fetch the Subscription with https://stripe.com/docs/api/subscriptions/retrieve when you receive the checkout.session.completed event.
I have a paid membership site which takes payments via Stripe Checkout (server integration). When a user registers, they are redirected to the Stripe checkout (with their ID passed as the 'client_reference_id') where they enter their card details. For the checkout fulfillment, I have a webhook setup that is linked to the event 'checkout.session.complete'.
Everything is working fine, the webhook triggers and the endpoint processes the data and marks the corresponding account as activated, sends an email to the account holder as well as the site admin to let them both know the account has been created and a payment has been successful.
However, if a payment fails, I don't know what event to attach to the webhook to detect failed payments via Stripe Checkout. The only checkout event documented is 'checkout.session.complete', there isn't one for something like 'checkout.session.failed'. There's 'invoice.payment_failed' but this is used in subscriptions not one-off checkout payments.
We're wanting to basically notify the site admin when a payment fails so they are aware an account has been created but it still awaiting payment.
Is there a checkout event I'm missing or another way to go about finding failed checkout transactions?
To charge a credit or a debit card, you create a Charge object. You can retrieve and refund individual charges as well as list all charges. Charges are identified by a unique, random ID.
-- From the Stripe API Documentation.
You can then detect a webhook response of charge.failed to show that an attempted use of a charge object has failed.
Stripe used to be very cleanly documented, but they've lost their way a little, recently.
Stripe card payments ALWAYS use Charge objects, sometimes these are set by you (invoices, subscriptions, etc.) and sometimes these are set behind the scenes by Stripe ("Checkout Process", etc) -- but they are always set, so your webhook can always detect a charge.failed event.
Addendum
This does work, however, since the client_reference_id is not in the charge.failed data, I can't link the failed payment to a specific account anyway. But in regards to my actual question, this event does work and is the correct answer, just unfortunate that it doesn't solve my specific situation.
To solve this; either using the Stipe interface or by coding:
1) Create a Customer object and keep some sort of record of this customer Id.
2) When the Stripe Charge runs, an associated $charge->customer is set. This is available to your webhook.
3) When the webhook result appears, use this (typically) $event->data->object->customer value to cross reference the charge with the correct customer on your server.
4) As mentioned in comments, Stripe Webhook Testing does not populate the webhooks with example data such as ->customer... :-(
I know this is a bit late for a reply - but I have a working solution!!
When I create a "Session" in Stripe, I make sure to fill out the SessionCreateOptions so that the PaymentIntentData.Metadata field is setup to have my customer's ID and stuff in it. When I do that, the metadata not only shows up in payment_intent.created and payment_intent.succeeded webhooks, but it ALSO shows up in charge.succeeded/failed!
This means that when a payment fails I can look up the attempted payment that I keep track of and email them back letting them know that it failed and why, and I can also log it for myself for reference when I go look up that customer in my customer service tools.
It's not clear from the documentation and I spent a lot of time grokking how it works, so I hope this will save you some time.
The checkout session CANNOT fail (!)
A Checkout Session can have one of 3 states: "open" is an active session which can be paid, "completed" is paid ("happy path"), and "expired" if within the expiration time the session was not completed.
A session can only expire with two options:
After 24 hours if not paid (or sooner, if you set a shorter expiration time https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-expires_at)
Manually by calling https://stripe.com/docs/api/checkout/sessions/expire
A session's URL can be accessed unlimited number of times for payment attempts until it is either paid or expires. This is by design, so a user can have a couple of failed attempts to pay until she actually pays. With a session, you do not care about those attempts, and in most cases you do not need to track them.
So one option for your workflow can be the following, for example:
For creating a new order, create a new session, save it's ID in the order's record in the DB, and redirect the user to the session's URL
On the back-end, listen for "session completed" event, which is the happy path - everything is paid (watch out for the async payments, if you accept any! See herehttps://stripe.com/docs/payments/checkout/fulfill-orders#delayed-notification)
Also listen for "session expired" event. This will tell you that the user bailed and hasn't paid the order. You can send them a "failed payment, try again" email in this case (see below for Recovery workflow)
If the user returns to your website before paying for the order, you can display "NOT PAID, TRY AGAIN" message next to the unpaid order.
Attention: it is perfectly safe for the user to open a dozen of the session's URL and attempt to pay as Stripe won't allow the double payment after card information is submitted.
So you are thinking about creating expiring the old session and creating a new one for this unpaid order? Wrong! You can retrieve it by ID (you saved it in the DB, right?) and send the same URL to the user.
However, if the retrieved session is "expired", there will be no URL to send, so you will be forced to create a new session, save it in DB, and the whole cycle repeats.
Another possibility is for the repayment workflow to happen in rare state between the payment and "session is completed" webhook event (the user already paid in another tab, but you haven't received the "paid" event yet), you will get that the retrieved session state is "completed", in this case you will need the capability for the front-end not only to redirect the customer to a session's URL, but instead sometimes show a message like "nope, it's already paid".
As you can see, there is no "failed" state for the checkout session.
Now, another thing for recovering the unpaid and expired sessions is the Recovery workflow (https://stripe.com/docs/payments/checkout/abandoned-carts). The gist of it is the following:
When creating a session, you set "recovery" to true, so if the session expires, you will have access to a special URL that will "revive the same session with the same parameters" that will be active for 30 days after the original session expiration. You then send this URL to users with possibly some discount to recover their abandoned cart. Also, you can track whether the session is "original" or "revived". However, I did not use this way, and instead opted to simply creating a new session manually if the old one is expired.
I have integrated stripe in to my application.
Now i want to store the payment history in to my database.
Using stripe APIs, i can create new subscription as well as update that subscription.
Now say i have one subscription for one month for $20 and after few days i am updating to sixmonth for $60. So as per stripe logs it will deduct $40 while updating plan.
I want to store all these payment process along with subscription periods in to my database.
How can i do that. Is that any API for it?
You can use webhooks to get notification from Stripe whenever any event happen.
So you need to create a webhook for charge.succeeded. So that you can get notification for all the successful charges happen. You can get amount, customer data inside the event data which you can store to your DB. I assume you are already storing subscription along with customer, so that you can map subscription from customer data from event data.
Does anyone know is there any way to check status of recurring (subscription) payment at PayPal.
I just want to submit transaction ID and to see is payment canceled (suspended) or it is still active.
If you're using the Recurring Payments API you can use GetRecurringPaymentsProfileDetails to obtain details including the current status of the profile.
If you're working with Standard Subscriptions, though, then there is no API to obtain the details, unfortunately.
In either case, I'd recommend looking into Instant Payment Notification (IPN). Using it you could automate the process of sending email notifications, updating your own database, etc. when new profiles are created, suspended, re-activated, etc.