Add-on Webhook Stripe payment integration

235 views
Skip to first unread message

Darren D'Mello

unread,
Feb 25, 2022, 3:50:59 AM2/25/22
to Google Apps Script Community
I want to use stripe payment system in the addons

The payment method can be Card/Wallet

Once the payment is successful (auto subscription or manual checkout) I need to send the activation code. The next year when the charge is successful I need to send the activation key again if the payment is successful

I see there are many events in the Stripe payment webhooks

I have used charge.succeeded event listener and it does send me the object (pasted below). I have one issue here. If I rely on this charge.succeeded object I find no information on which product the purchase is made.

The response does have user supplied email but there is no way I find the metadata or product code in the charge.succeeded object
How do I find the product name on which the charge was successful?

Test card number: 4242 4242 4242 4242


Any help is greatly appreciated

{ "id": "evt_1KWzCbHpIo9Nhh5aYEv7XlY8", "object": "event", "api_version": "2017-12-14", "created": 1645777861, "data": { "object": { "id": "ch_1KWzCaHpIo9Nhh5atK09dpaO", "object": "charge", "amount": 100, "amount_captured": 100, "amount_refunded": 0, "application": null, "application_fee": null, "application_fee_amount": null, "balance_transaction": "txn_1KWzCaHpIo9Nhh5anEebLs4X", "billing_details": { "address": { "city": null, "country": "IN", "line1": null, "line2": null, "postal_code": null, "state": null }, "email": "te...@email.com", "name": "CARDNAME", "phone": null }, "calculated_statement_descriptor": "DIGITALTHOUGHTS", "captured": true, "created": 1645777860, "currency": "inr", "customer": "cus_LDQ2DBhL2VkPOH", "description": "Subscription creation", "destination": null, "dispute": null, "disputed": false, "failure_code": null, "failure_message": null, "fraud_details": { }, "invoice": "in_1KWzCYHpIo9Nhh5ammBkFvU1", "livemode": false, "metadata": { }, "on_behalf_of": null, "order": null, "outcome": { "network_status": "approved_by_network", "reason": null, "risk_level": "normal", "risk_score": 58, "seller_message": "Payment complete.", "type": "authorized" }, "paid": true, "payment_intent": "pi_1KWzCYHpIo9Nhh5aj6Xgl3tS", "payment_method": "pm_1KWzCXHpIo9Nhh5aADMKyWPc", "payment_method_details": { "card": { "brand": "visa", "checks": { "address_line1_check": null, "address_postal_code_check": null, "cvc_check": "pass" }, "country": "US", "exp_month": 2, "exp_year": 2022, "fingerprint": "MxtsbEBU2BmJbOn4", "funding": "credit", "installments": null, "last4": "4242", "network": "visa", "three_d_secure": null, "wallet": null }, "type": "card" }, "receipt_email": null, "receipt_number": null, "receipt_url": "https://pay.stripe.com/receipts/acct_1BhpF1HpIo9Nhh5a/ch_1KWzCaHpIo9Nhh5atK09dpaO/rcpt_LDQ2FDPK6fwsEyBOISiDCItSv8JeNbl", "refunded": false, "refunds": { "object": "list", "data": [ ], "has_more": false, "total_count": 0, "url": "/v1/charges/ch_1KWzCaHpIo9Nhh5atK09dpaO/refunds" }, "review": null, "shipping": null, "source": null, "source_transfer": null, "statement_descriptor": null, "statement_descriptor_suffix": null, "status": "succeeded", "transfer_data": null, "transfer_group": null } }, "livemode": false, "pending_webhooks": 1, "request": { "id": "req_QzrolSFU0OA7D7", "idempotency_key": "228c5670-85ea-4047-9f9f-9d1e519ffc2c" }, "type": "charge.succeeded" }

John McGowan

unread,
Feb 25, 2022, 5:34:15 PM2/25/22
to Google Apps Script Community
Hi Darren,
I use Stripe and parse the Webhook after payment using a Cloud Function (Node). I previously used Apps Script but the logging is much easier for Webhook in cloud functions when I was troubleshooting code :)

I kickoff my order fulfillment (and renewal) on the checkout.session.completed event and the invoice.paid event.

From there you can grab the object.metadata.product_name if you created that in a self-hosted Checkout session or the object.session.subscription (which returns a response: https://stripe.com/docs/api/subscriptions/retrieve)
where I grab the productId using 

var subscription = await stripe.subscriptions.retrieve(subscriptionId);
var productId = subscription.items.data[0].price.product;

From there I do different things like assigning the correct license flags in Firebase and any other processes I do upon subscription purchase.

In the same cloud function I also parse when the subscription is cancelled (like when non-renewed or manually cancelled) to handle license flags/etc...

Hope that helps,
John

Darren D'Mello

unread,
Feb 26, 2022, 1:29:27 AM2/26/22
to Google Apps Script Community
Perfect John, Thank you so much.
That helps a lot.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/av_inEfXpHY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/56631121-af4c-439a-b3ef-8322a6743282n%40googlegroups.com.


--
Best,
Darren
Reply all
Reply to author
Forward
0 new messages