Home > database >  cURL request (with Stripe API) slowing down page load times - optimal way to make the request?
cURL request (with Stripe API) slowing down page load times - optimal way to make the request?

Time:12-24

I'm making a call at the beginning of each page of my site to Stripe (a payment/subscription service) using its API in PHP. I need to check if at any point the subscription has failed/changed so they lose access to the current page.

The problem is that it seems to be slowing down my pages a lot, causing noticeable issues with JavaScript that's hiding/showing elements. What's the best way to handle this? If I AJAX after the page is loaded then a user could disable JS in their browser and retain access. Is it unusual for a cURL to be noticeably slow?

CodePudding user response:

There can be various reasons behind why pages are loading slowly and/or the API response time is high. In your use case, it does make sense to retrieve the subscription object beforehand. An alternative approach would be to store subscription status in your DB so that you don’t have to make a network request to look it up every time. You can use customer.subscription.updated webhook [0] to keep a track of subscriptions statuses.

To verify if Stripe is the one responding to your GET requests slowly, you can profile your request by logging timestamps before/after the GET to retrieve a Subscription completes. If it is not that high, then your latency might lie elsewhere in your code. Otherwise I’d reach out to Stripe Support with specifics such as the request IDs for those GET requests you made to Stripe’s API [1].

[0] https://stripe.com/docs/api/events/types#event_types-customer.subscription.updated

[1] https://dashboard.stripe.com/test/logs

  • Related