Unable to get the transactions of specific subscription - paypal-sandbox

I am creating subscription from Axios in node js and after the user clicks the service, and i get the subscription id on success callback, i am unable to retrieve the last payment transaction that was executed in the current subscription
All i am getting is empty JSON object {}
PS: The URL i am using is /v1/billing/subscriptions/{id}/transactions
The image for the same with some URLs

The transactions of subscription won't be retrieved instantly. It requires some time, about 5 - 10 mins. The paypal needs some time to verify the transactions and then transfer funds.

Related

Optimum way for sending API calls in WordPress/WooCommerce Subscriptions

I have a high traffic WooCommerce Subscriptions site. I need to call a 3rd party API every time an order is generated and send information about the customer (if they still have an active subscription or not), this includes new and renewal orders.
For this purpose I choose the "woocommerce_subscription_status_updated" hook. It fires every time a subscription changes state, lets me know what the current state is and then I can send that info to the API.
The issue is that when a subscription gets renewed successfully, this hook is fired twice (once if the renewal fails). Subscription get changed from "active" to "on-hold" and then "on-hold" to "active" (this is how Wc Subscription renews a subscription).
The API gets called twice even though it did not needed to. What would be a better way to implement this?
I think you can set a CRON with storing the last checked date time and get a list of data whose status are updated since the last time and call an API in some interval through CRON if it works for you.

Google Calendar API: User exists as an attendee when the meeting does not exist in his calendar

I'm using calendar API to pull events of users in an organization that had installed my GSuite app as a service account (it just means that I have access to all the users in the organization).
I noticed some weird behavior - let's say I have an event with 2 participants - a and b (both are users in the company). when I use the calendar API events.list to pull event for a I get this event, and b is listed in the attendee list with responseStatus of 'accepted'.
When I pull the events for the same period of time for user b, I do not get this meeting, nor when I try to pull a specific event using the meeting id (event.get API). I also used showDeleted=True but still didn't get the event back when pulling the events of b (I'm pretty sure if the event would have been deleted then the responseStatus would have been 'decline').
What could be the reason for this behavior? I tried to reproduce it using two emails that I have access to, and I was not successful.
Since those are emails of my client I cannot just come up to them and ask what happened with this meeting.
This is the request I'm making (using python google API client):
from googleapiclient.discovery import build as Build
service = Build("calendar", "v3", credentials=credentials, cache_discovery=False)
service.events().list(calendarId='primary', timeMin=min_time, timeMax=max_time, pageToken=page_token, singleEvents=True).execute()
First I'm building the service using the service account credentials (delegating permissions to the current email), and then making the request to pull the events.
The min_time,max_time are strings like this - 2020-10-19T08:00:00+0000.
The pageToken starts as None but will change if there is a nextPageToken in the response.
This is the request I made to try and get the meeting of the user that appear as an attendee when pulling the event using another user's email & credentials:
service.events().get(calendarId='primary', eventId=<meeting_id>).execute()
But I'm getting 404 event not found response (This API returns deleted events - I've seen it)
I'm pulling all of the events correctly, and only saw this case on small number of the events (less than 5% of the meetings).
Any idea what could cause this?
Thanks!

Woocommerce order created webhook firing only sometimes and with incomplete payload

I've set up a WooCommerce webhook that fires when an order is created, but I've found that the webhook only fires sometimes, and even when it does fire, it often contains incomplete body data, meaning that instead of the full json structure of the order, the payload only contains "webhook_id xxx" (where xxx is the webhook id number value).
I've been testing by creating orders and setting the webhook Delivery URL to a hookbin.com temporary bin.
In the cases where the webhook fires but contains incomplete data, nothing gets written to the Webhook Log on the WooCommerce webhook settings pages. The only way that I know that it fired is by refreshing the hookbin and seeing the request go through.
In the rare cases where the webhook does fire with the complete json data of the order, the Webhook Log is updated.
How should I go about debugging this?

How can I test subscription renewal flows in stripe?

I want to test my application's handling of webhook events from stripe when a subscription payment has been made (or failed). Here is what I've tried so far:
Set up a new subscription
Update user's credit card to be the one that can be added to an account, but will fail to actually be charged
Change the trial end date to be in one second
Wait a few seconds expecting the webhook to be sent
However, According to the documentation:
If you have configured webhooks, the invoice will wait until one hour after the last webhook is successfully sent (or the last webhook times out after failing).
One hour is a long time to wait, since I am trying to do this as part of an automated integration test suite.
One suggestion (from IRC) is to fake out the webhook request, so that my integration test sends the event, instead of Stripe sending it. However, since Stripe doesn't include any sort of HMAC in the webhooks, I can't trust the data in the payload. So, my application just takes the event ID from the webhook payload and fetches the event from the Stripe API:
If security is a concern, or if it's important to confirm that Stripe sent the webhook, you should only use the ID sent in your webhook and should request the remaining details from the API directly.
This will obviously not work if I am trying to inject fake events for my test (by design).
What are the best practices for testing this sort of scenario?
It seems there isn't a perfect way to do this. As suggested by #koopajah in a comment, I added a configuration value in my application that will disable fetching the event from Stripe, and instead just trust the event data in the webhook. This allows me to test my flow in almost the same way as it would work on production, since the event data in the webhook and the event fetched from Stripe are identical (assuming it is an authentic webhook request :)
Unless/until Stripe includes an HMAC signature in the webhook request to authenticate that it came from them, I think this is the best way to solve the problem.
One hour is a long time to wait, since I am trying to do this as part of an automated integration test suite.
You can shorten the wait by going to the invoice and selecting the "Charge customer" button, as shown below.

Stripe Webhook events Renewal of subscription

I am using STRIPE for credit card payments in my Asp.net Application. Application have an monthly subscription plan. First time when user subscribe the webhook event 'customer.subscription.created' fired. my question is what happened which events will be fired when next month subscription is renewed ? Can anyone tell me the flow on subscription renewal webhook events?
Thanks
The webhook 'invoice.payment_succeeded' actually does distinguish between the first charge of a new subscription and subsequent renewal charges.
The webhook sends an invoice object, which includes 'billing_reason' - the possible values of which are noted in the Stripe Docs - The Invoice object:
billing_reason
(string)
"Indicates the reason why the invoice was created.
subscription_cycle indicates an invoice created by a subscription advancing into a new period. subscription_create indicates an invoice created due to creating a subscription. subscription_update indicates an invoice created due to updating a subscription. subscription is set for all old invoices to indicate either a change to a subscription or a period advancement. manual is set for all invoices unrelated to a subscription (for example: created via the invoice editor). The upcoming value is reserved for simulated invoices per the upcoming invoice endpoint."
If billing_reason == 'subscription_cycle' the webhook is for a subscription
renewal.
If billing_reason == 'subscription_create' the webhook is for a brand new subscription.
Renewing Subscriptions:
When a customer's subscription is renewed in Stripe a number of things happen, each with a corresponding event:
An invoice is created - invoice.created
The subscription billing period is updated - customer.subscription.updated
After an hour (giving you time to add any additional charges) Stripe attempts to charge the customer.
Given payment is successful an invoice.payment_succeeded event is raised.
The way to handle these events within your own application is to register a webhook; a HTTP endpoint that Stripe will send details of the event to.
Find the customer subscription using the Stripe identifier (included in the event payload).
Retrieve the subscription details from the Stripe API.
Update our subscription's CurrentPeriodStart and CurrentPeriodEnd with the Stripe subscription's period_start and period_end.
Create a customer invoice using the details from the Stripe event.
As well as customer.subscription.created you will also receive a invoice.created followed by invoice.payment_succeeded (or invoice.payment_failed)
From the documentation:
If you are using webhooks, Stripe will wait one hour after they have all succeeded to attempt to pay the invoice; the only exception here is on the first invoice, which gets created and paid immediately when you subscribe a customer to a plan.
So, this means, invoice.created event will also fire next month.
Stripe will then wait an hour before charging the customers card, then firing charge.succeeded (if the charge is succeeded) or charge.failed (if the charge fails)
The hour wait is to allow invoice items to be added to the invoice if you so wish.
See my answer on this question for more information on why you might need to do that...
You would want to watch for a invoice.payment_succeeded event:
Have a look at: https://stripe.com/docs/api#event_types
This event is triggered any time an invoice is paid. A charge.succeeded event also occurs, but the difference is that invoice.payment_succeeded only occurs for payments on invoices, whereas charge.succeeded also occurs for standalone charges.
There is no event distinction between the first charge on a subscription and a recurring one, although the logic you store on your end (e.g., when the subscription was created, when it
expires, etc.) should help you disambiguate them.

Resources