Associate Stripe checkout session with subscription webhook - php

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.

Related

Stripe inactive subscription as Payment Intent

I have been following this guide to create a subscription service using Stripe's API:
https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements#create-subscription
Everything works, however as soon as the payment element is loaded, an inactive subscription is created due to the payment intent. As stated in the guide:
At this point the Subscription is inactive and awaiting payment
If the payment is successful, what is returned is the payment intent,
payment intent client secret, and redirect status status.
So here are my issues:
It seems terribly illogical to create an inactive subscription for every customer that visits the payment element. Let's say hypothetically I had the payment element on my landing page, my Stripe would be overwhelmed with inactive subscriptions. Even if the user reloads/revisits the payment page a new subscription is created. It just seems messy.
I want to store the active subscription ID in a database and only allow a user to subscribe once. But the response doesn't return the subscription ID, it only retuns the payment intent stuff listed above.
This just seems like not the best way to do this, am I doing something wrong?
Is there a way to get the active subscription ID with the success response?
AND/OR
Is there a way to initialize the payment element with a payment intent before creating the subscription, and then create the subscription after success?
In the beginning, the subscription is in incomplete status because it requires a payment method to pay its first invoice so that it can transition to active status. So if you want to make a subscription active upon creation, you need to specify a default_payment_method during creation.
You can use the SetupIntent API to collect a payment_method.
Create a customer
Create a SetupIntent with the customer
Use the SetupIntent's client_secret to render the PaymentElement
Call stripe.confirmSetup to confirm the SetupIntent so that the collected payment_method is attached to the customer
Now, you can pass the earlier collected payment_method as the default_payment_method when creating a subscription, and the resulting subscription will become active if the first invoice is paid successfully with the default_payment_method. You don't need to use PaymentElement in this step anymore because you already collected a payment_method through SetupIntent.

Shopify - How to get customer e-mail from Cart webhook response?

I've found one topic mentioning this problem - customer Id not return in shopify cart webhook, but it doesn't help at all.
I really need to connect cart event with customer, I'm writing integration app and it is essential for implementing external events for my service, but I cannot get to the customer e-mail from cart hooks response. I thought that, I could get customerId from hook response, and then use it to GET customer e-mail using Shopify Api, but Cart webhook response DOESN'T even have customerId - wth? I thought about creating cookie, so I could connect event with customer, but I can't get to the cookies with hook whatsoever.
Is there any way to connect cart event with customer at all? Shouldn't it be just passed in the response, like in checkout hook?
The customer info is available client side from cookies. You know you can get the customer ID anyway, and use that to callback to the server and get the email using an API call.
Since the cart event in the webhook has a token value, have you tried matching that to the token value in the customer cookie? You might find a match there. If not, you're SOL.

When is the best time to create a PayPal Webhook?

I am using the PayPal API to create a Webhook, and not the Developer site. When is the best time to do this? Do I create the webhook before the payment is created, after the payment is created, or after the payment is executed? According to the API documentation, the webhook needs to be created only once in the lifetime of the app. What happens if a new webhook is created each time the app is used?
What I have:
processPayment: the payment details are defined, the payment is created, the user is redirected to PayPal.
executePayment: the user is returned from PayPal, the payment is executed.
On notify:
verifyPayment: calls a function to validate Webhook signature.
Post-processing in the Site's database.
P.S. Where/how is the webhook Id generated?
Webhook needs to be created before making a payment transaction if you want to get notified of the payment. If there are multiple Webhooks configured on your app and if you have same set of events subscribed on both, you would receive notifications on both Webhooks.
Thanks!

Paypal API for checking status of recurring (subscription) payment

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.

Paypal: Standard Checkout Return

I have a custom button that is calling paypal, standard checkout method. It's just like this: Passing price variable to PayPal with custom button
If the customer uses a credit card and paypal accept, then paypal redirect to my site and I "mark" the product as paid. But, after a while the bank reject it. How can I get this information? If I'm using standard checkout.
Sounds like you're getting an e-check that hasn't cleared. Then a couple of days later when the payment should clear it fails for some reason.
The best way to handle this is to utilize Instant Payment Notification. Once configured, it will POST transaction data to a listener script that you have setup. This script can receive that data and update your database, send out email notifications, or anything else you want to automate based on different transaction types.
In the case of e-check the IPN data would have a payment_status of Pending, and then when that payment status changes you would get another IPN with the same transaction ID, but the new status. That way you can wait to deliver goods until the payment status of any transaction is actually Completed as opposed to anything else.

Categories