How to get the charge Id after subscribing a customer to a plan with recurring payment? - stripe-payments

I have successfully created a customer in stripe with a subscription of plan .After successful payment I am not able to get the charge Id because after subscribing a customer to a plan.It automatically generates an invoice.So what is the way that I can get that particular charge stripe Id from stripe and store it in my system database.

Getting the charge ID for a subscription (or an invoice) can be done via their webhooks integration. You need to expose an API in your app where Stripe can fire these requests. You can subscribe to various events there, but in your case you'd need to do so on charge.succeeded. The charge ID will be in the data structure of the event object.
Take a look at the various other lifecycle events you can use with webhooks.

Related

Does Stripe have webhook for usage record?

Stripe uses webhooks to notify application when an event happens in the account. Webhooks are particularly useful for asynchronous events like when a customer’s bank confirms a payment, a customer disputes a charge, a recurring payment succeeds, or when collecting subscription payments etc.
But, does stripe have any webhook to inform application for usage record pricing model?
There is no webhook for usage record today in Stripe's API. This API is used at a higher volume that most APIs and reporting an event for every usage record would likely be unsustainable on your servers and Stripe's.
The best option is to either track the records yourself as you create those or use the summary API: https://stripe.com/docs/api/usage_records/subscription_item_summary_list

Provision subscription in checkout.session.completed for first payment

When I create a checkout session, I include the user_id in the metadata. This allows me to retrieve the user_id when checkout.session.completed event is fired which I will need in order to save the stripe customer id in the database for that user.
Is this the correct approach?
The issue I am having now, is that given this logic whenever the first payment is created I will need to provision the subscription in the checkout.session.completed event, future payments will be handled by invoice.paid event. This is because invoice.paid will can only recognise the user by customer id, which gets populated by checkout.session.completed.
Question:
Is my approach for storing the customer id correct?
How can I provision the subscription from the checkout.session.completed event for the first payment and then future payments are provisioned by invoice.paid?
To provision and monitor a subscription, Stripe recommends listening to three events: checkout.session.completed, invoice.paid, and invoice.payment_failed.
If you need a user_id to provision the subscription, then yes adding it on the Checkout Session metadata makes sense.
When you get the invoice.paid event you can retrieve the associated subscription with the subscription property of the invoice. And then, if needed, you can find the Checkout Session for that subscription with the list checkout sessions endpoint and passing the subscription ID.

How to test stripe charge.failed webhook on a subscription

I've got a test subscription set up that is active. I'm trying to test what happens when the payment fails. I need to be able to pass some metadata (cognito user ID) to the webhook endpoint, I think I know how to do this but can't work out how to test it.
Using the test webhook doesn't work because it sends a fake charge ID. I've tried using the payment failure card but not having much luck as it doesn't seem to be triggering my endpoint.
Here is what I've been reading already:
https://stripe.com/docs/billing/testing
Stripe charge after subscription, get metadata from subscription
Stripe subscription webhooks missing metadata and client_reference_id
How can I test subscription renewal flows in stripe?
Getting charge.failed event, but struggling to associate this charge to subscription, because the charge has customerId and the customer has a list of subscriptions.
Now I think below code can help
From event can get charge id and then retrieve charge obj with 'expand' property.
params := &stripe.ChargeParams{}
params.AddExpand("customer")
params.AddExpand("invoice.subscription")
ch, err := charge.Get("ch_3K4yJG2eZvKYlo2C14TZsir9", params)

Testing Stripe webhooks that require data on localhost

The guide here says to monitor the invoice.paid event via webhook in order to provision a customers account when the payment is successful. How can I test fire the invoice.paid event with data? I need to be able to access the customer id and see what plan they purchased to provision it correctly.
I am able to test fire these events via the Stripe CLI, but I don't know how to fire them with mock data.
With the Stripe CLI you can trigger a invoice.payment_suceeded webhook event (which is almost exactly the same as invoice.paid):
stripe trigger invoice.payment_succeeded
That event will contain mock data. If you want to provide your own data, you'd have to mimic the event yourself. In that case you could do that by creating a new subscription in test mode, which will create an invoice and trigger the invoice.paid event.

How to create invoices using Stripe Connect Express, on Stripe Subscriptions invoices are automatically created, but not on Stripe Connect payments

I charge Customers using this guide https://stripe.com/docs/connect/destination-charges adding amount, application fee and Service provider accound_id, after that I listen for "charge.succeeded" event with webhooks. If the payment was successful I generate invoice using this this guide - https://stripe.com/docs/billing/invoices/connect, but I get this error
"Nothing to invoice for customer"
Is it possible Stripe automatically to create Invoice for me?
Do I have to manually create this invoice and then make it paid.
Should I generate the Invoice before Payment or after Payment?
When you process a payment, Stripe will send your customers a receipt automatically per your email settings [1]. In some respects, this receipt serves as an invoice of services rendered, and might be sufficient for your needs. You can see an example of what a receipt will look like for a given payment by looking at a given payment in the dashboard (under the 'Receipt history' heading).
Now, if the receipt automatically generated by Stripe doesn't meet your needs (e.g., you want to be able to list more customer info on there), then you'll want to use the Invoices API [2] instead of the Payment Intents API to process the payment. Using the Invoices API also allows you send the invoice to the customer so they can initiate payment on their own [3].
So, with that out of the way, if you intend to create Invoices for your customers you need to first create Invoice Items on the Customer and then create an invoice off that Customer. Once you finalize the Invoice, Stripe will either process a payment automatically under the hood, or send the Invoice to your Customers via email so they can initiate the payment (through a Stripe hosted page). At that point you can send your customers a copy of the Invoice PDF. With this integration there is no need to process the payment on your own or deal with any other APIs. The Invoices API takes care of everything for you. These steps are documented in detail here:
https://stripe.com/docs/billing/invoices/create
To answer your questions more directly:
No. Stripe will only automatically create invoices when you make subscriptions. But, Stripe does automatically create receipts as described above.
Yes, you manually would need to add Invoice Items to the Customer, then create an Invoice off that Customer. Note that this step will also charge the customer, so you don't need to do that separately.
Invoices process the payment under the hood once they are finalized.
[1] https://stripe.com/docs/receipts
[2] https://stripe.com/docs/billing/invoices/create
[3] https://stripe.com/docs/api/invoices/create#create_invoice-collection_method

Resources