#M Awais - Subscriptions
1 messages · Page 1 of 1 (latest)
Hello! Stripe itself won't prevent creation of two Subscriptions in that timeframe, but you can write code on your end to prevent it from happening.
i have to create subscription if there are two employees of a companies...
But if someone sends two requests at the same time, then both request tries to create the subscription in around 5 seconds...
Is that being caused by someone clicking twice on a subscribe button rapidly?
Or is something else causing it?
I am not sure about that... let's say if I have two browsers open with same account(of a portal) then refresh both of those pages
I am using laravel middleware to ensure that companies have a subscription if they have more than 1 employee.
Now that middleware apply to both request and tries to create the subscription. In result there will be two subscription instead of one....
It sounds like you need to add a check server side to confirm there's no existing Subscription before creating a new one.
I am hitting two requests at the same time, so there is no way to know if there is a active subscription as it will take time to create a subscription on stripe (curl/api request will take some seconds, in that time I don't know how I can check if there is already a request gone to stripe.)
Are the requests being sent to the same server or different servers?
same server..
Can you implement a queue system on the server that processes incoming Subscription creation requests one at a time? That way you can check for an existing Subscription before creating a new one safely.
I have not worked with queues but have some information, so if i add an item to queue then what should I display to customer?
A processing/loading indicator or similar is the usual approach.
Or a "creating Subscription" message.
ok thanks, I will try it....
Another option is to make an entry in your database or other shared storage location that indicates a Subscription is being created for a particular company/employee/whatever combo of information you need, then start the Subscription creation process once that data is set. Then, at the beginning of every Subscription creation, you check to see if a Subscription creation is already in flight or if an existing Subscription already exists and stop if it does.
I am not sure about all cases where a subscription creation can failed. and then i will keep getting from database that a subscription is already being created...
That depends on how you build your integration. This is a good place to start to figure out the best approach for your use case and the possible failures you might encounter: https://stripe.com/docs/billing/subscriptions/overview
Again, depends on your integration and your specific use case. Maybe?
I think i can check something in
Customer Subscription Updated event right?
What do you mean by "check something"?
like if a subscription is created or failed?
and then update the flag in database to show or hide the spinner/loader
Subscriptions have more statuses than that, and may have a more complex lifecycle than you're expecting, especially when async methods of payment are involved.
I recommend reading through the link I provided above and then letting us know if you have questions after that.
that's why I was asking if there is something i can check from that webhook event?
I have read the subscription many time already...
What does "something" mean though?
And when are you checking it?
What point in the Subscription lifecycle?
like maybe I can check the subscription status...
on every subscription update event?
I need more information. You can certainly do that, but I'm not clear why you want to do what/what your goal is with doing that. Are you asking about checking if the Subscription "failed" or not? Meaning if it was created or not?
if two request comes to server, I can create a flag in one of these requests, that a subscription request is sent to the stripe server for this customer.
Then second request and first will show a spinner until webhook event arrives where If i see subscription status as active then i can change database flag to 1 maybe to indicate that subscription was created and then both of those request where spinner was, can then show the normal page?
Yes, you could technically do it that way, but I'm confused about why you would take that approach? Specifically, waiting for the webhook to tell you the Subscription is active and then using that to trigger display of the normal page implies you're not using Stripe.js to confirm the initial Subscription Setup or Payment Intent, which means you're not handling things like 3D Secure or async methods of payment. Is that the case?
I already have payment method as default when a customer signup, and also in webhook even I can check if subscription status is incomplete, I can display(redirect) them a page where they can confirm the payment or add a new card in case issue like low balance etc...
Are you charging the customer when the Subscription begins, or is it free initially?
there are two subscription
portal subscription that began when adding the card (company signup). so this one charge the customer $49
then there is another one which will start when every they add two employees...
Okay, so what if authentication (like 3D Secure) is required when the Subscription begins?
...................
$user->newSubscription('qcsEmployee', 'qcsEmployee')
->quantity($noOfEmployees)->create(null, [], $subscriptionOptions);
} catch (IncompletePayment $exception) {
$obj->status = 2;
$obj->response = redirect()->route('cashier.payment', $exception->payment->id);
return $obj;
} catch (\Exception $e) {
$obj->status = 0;
$obj->message = $e->getMessage();
return $obj;
}
If an incomplete payment /3d secure error is thrown I redirect the user to a page where they will be asked to confirm the payment or add a new payment method