#xfechx
1 messages · Page 1 of 1 (latest)
Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- xfechx-subscription-maxoccurrences, 9 hours ago, 21 messages
- xfechx, 11 hours ago, 7 messages
- xfechx-subscription-maxoccurrences, 12 hours ago, 29 messages
Could you elaborate? Which request id?
I don't have the request id I dont think. Last night I had to actually delete the customer because I was ending in loops
Creating each second a subscription
I can share my code
The ppl helping me did not really resolve well my questions
In our implementation, max_occurrences gets added on the event customer.subscription_created
We need to do the same for subscription schedule, please
So, my guess is that we have to copy accross all the info from the subscription object to the subscription schedule object (annoying) . We cannot just make a subscription schedule and apply it to the created subscription? I need clarity
I spent hours yesterday testing and whatnot under the API and docs
Please just dont refer me there, there is support is for a reason
Sorry I don't really follow. Could you summarize your current issue with a step by step bullet points? It being hard to follow
What is difficult?
Ok, need to migrare max_occurrences
To subscription schedule
Max occcurrences is gtting deprecated
Right now, our code uses a webhook
To listen to customer.subscription_created
If the subscription has metadata['No. of Payments'] then it applies max_ocurrences to the subscription
We want TO DO THE SAME with subscription schedules
Using webhook + listening to the newly created subscription. If subscription has that metadata, then just apply the schedule to it (instead of max occurrences)
Last night we were ending up in endless customer subscription creation loops.
When customer.subscription_created then look for metadata['No. Of Payments'] and apply a schedule with iterations as the no. Of payments.
Also, all data should be kept the same, for example application fee percent
But it is frustrating and annoying that we have to copy everything accross
Why cannot we just apply a schedule to an existing subscription, simple?
Or how can we achieve this?
I need instructions, steps, on how to apply a schedule from a subscription creation event.
Without having infinite loops of subscription creations
The part infinitive loop that I haven't follow. But before that how do you apply "max_ocurrences" to a Subscription? Any example request id?
Yes give me a moment to share it
obviously here won't work, because it on this specific account, max_occurrences has not being enabled, but like this:
req_LUhDfH5sSCnFch
here is the code:
echo "--- EVENT: customer.subscription.created ---";
$metadata = $event->data->object->metadata;
if(!empty($metadata['No. of Payments'])){
try{
$StripeSub = \Stripe\Subscription::update(
$event->data->object->id,
['max_occurrences' => $metadata['No. of Payments']],
$connected_account
);
}
catch(\Stripe\Error\InvalidRequest $e){
echo '--- could not update subscription with max occurrences ---';
}
}
else{
try{
$subscription_object = \Stripe\Subscription::retrieve($event->data->object->id, $connected_account);
$customer =\Stripe\Customer::retrieve($event->data->object->customer, $connected_account);
if(!empty($subscription_object)){
subscription_notification($subscription_object);
}
} catch(\Stripe\Error\InvalidRequest $e){
echo 'Catched a InvalidRequest Error on "transfer.created"';
error_messages($e);
} catch (\Stripe\Error\Card $e){
echo 'Catched a Card Error on "transfer.created"';
error_messages($e);
} catch (Exception $e){
echo 'Catched a Exception Error on "transfer.created"';
error_messages($e);
}
}
}```
I just want to keep doing the same. Get the event for the subscription created, and apply the subscription schedule iterations to it, nothing else, and keep all metadata the same, and all application fees the same, etc.
just simple, like max_occurrences.
was great, not sure why is getting deprecated
Well, you can: https://stripe.com/docs/api/subscription_schedules/create#create_subscription_schedule-from_subscription
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
hmm again reference
reference is not so complet.
I read this already
I am asking for help because yesterday the api generated an infinite subscription creation loop
i am not asking for links to documentation or api , i already have those
ok, I haven't seen this parameter
Migrate an existing subscription to be managed by a subscription schedule. If this parameter is set, a subscription schedule will be created using the subscription’s item(s), set to auto-renew using the subscription’s interval. When using this parameter, other parameters (such as phase values) cannot be set. To create a subscription schedule with other modifications, we recommend making two separate API calls.
what do I need to pass if I pass this
I need clarity , it doesn't say which elements can or cannot be added, just (such as phase values)
I just need to translate my current code to sub schedules, nothing else.
I want the subscription to keep the application fee, and everything inherited from subscription
Please can you phrase your questions more coherently. It's really hard to follow that the actual ask is
what is incoherent about my questions, please confirm first.
what are you not understanding about my questions
have you read since the beginning of the chat?
You're just sending a wall of text that references infinite loops and alkl sorts of other issues.
I haven't no, it's a bit busy right now so a concise summary of the actual issue at hand would be helpful
It is not my fault that is busy on your job and you haven't read, sorry.
I will rephrase it again, so that you perceive it to be more 'coherent'
- Listening to the event: customer.subscription_created.
- Getting the value of the metadata "No. of Payments"
- Applying a schedule to the event subscription it is referring to.
- Keep the subscription values exactly how they are, and ALL of them.
- Have finally a subscription with limits and intervals, as it was originally defined in it's creation, but with the max_occurrences alternative (iterations) applied to it.
Is that more coherent?
Thanks
Ok, great. You should be able to achieve 3 with the API parameter I shared
4, the sub_xxx should remain untouched (other than schedule being set). Easy enough to confirm with a test
5, what do you mean by 'limits' specifically? What is the behaviour you want to implement?
Nothing special, I only want to translate what max_occurrences was doing. that is all.
if I add from_subscription, how can I add the phase and intervals, this is the super confusing part. Because it is way more complicated than just a single parameter like max_occurrences
I only need to pass a number, which is the number of iterations, that's all.
and update subscription with the iterations, keep everything the same about the subscription.
You'd need to then make a follow-up API call to update the created sub_sched_xxx where you can pass phases: https://stripe.com/docs/api/subscription_schedules/update#update_subscription_schedule-phases
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
subscription schedules have rules, they are picky and some parameters do not agree when other parameters are there, etc. I need to know this
see, this is where it gets so much more complicated
ok, I will try
Yep, it is a pretty complex API I agree
But I can help you with any issue you run into
what else do i need to pass on to the subscription schedule
if I am passing 'from_subscription'
Nothing in the creation request as it'll prevent you passing phases. But after creation you should have sub_sched_xxx with a single phase and then you can set the interations on that in an update call: https://stripe.com/docs/api/subscription_schedules/create#create_subscription_schedule-phases-iterations
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
so, that will be the ONLY parameter to pass to it?
also, will I need to pass, price, etc etc. to phases?
Depends if you want to change them. They will already be inherited from what was set on the sub_xxx you used to create the schedule
what about application fee percent?
on phases?
will that be inherited from subscription?
👋 taking over for my colleague. Let me know if there's any follow-up Qs I can answer!
phases of a subscription schedule is an array
you're passing an object
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
what is the problem?
$subscriptionSchedule = \Stripe\SubscriptionSchedule::create([
'from_subscription' => $stripeSub,
], $connected_account);
if($subscriptionSchedule){
$uddatedSchedule = \Stripe\SubscriptionSchedule::update(
$subscriptionSchedule->id,
[
'phases' => [
'iterations' => $metadata['No. of Payments'],
],
], $connected_account);
}```
I can't see the object
'iterations' => $metadata['No. of Payments'],
],```
needs to be
```'phases' => [[
'iterations' => $metadata['No. of Payments'],
]],```
ahhh, now is givving me more errors:
Error Message: Missing required param: phases[0][items].
Code: 0
HTTP Status Code: 400
Request ID: req_r6aIfgEXOIDYEt
I thought items were inherited from subscription??? 😵💫
man, this seems like an endless loop. Did you have a chance to read everything above before you jumped in?
the highlighted line explicitly say
You need to pass in all current and future phases when you update a subscription schedule
I am using 'from_subscription'
even if you are
so how can I take the info from the event (the event is a subscription object)
I need to copy it accross, exactly
please
even with application fee percent, etc. your colleague claimed that it should all be inherited, but it isn't
you can use the subscription schedule object you just created from the subscription and use the info inside the phases to fill in the phases param of the update function
hmm?
can you give me an example?
what do I need to fill in phases
to keep the subscription exactly like it was but with max_occurrences
(i.e iterations)
$subscriptionSchedule = \Stripe\SubscriptionSchedule::create([
'from_subscription' => $stripeSub,
], $connected_account);
if($subscriptionSchedule){
$uddatedSchedule = \Stripe\SubscriptionSchedule::update(
$subscriptionSchedule->id,
[
'phases' => [
[
'iterations' => $metadata['No. of Payments'],
'items' => $subscriptionSchedule->phases->items,
]
],
], $connected_account);
}```
what else?
e.g.
$subscriptionSchedule =$stripe->subscriptionSchedules->create(['from_subscription' => 'sub_xxx']);
$stripe->subscriptionSchedules->update(
$subscriptionSchedule->id,
[
'phases' => [[
'price'=> $subscriptionSchedule->phases[0]->price,
'quantity'=> $subscriptionSchedule->phases[0]->quantity,
'iterations' => $metadata['No. of Payments'],
]]
]
}
something like that basically
do i need price, and quantity?
oh I forgot start_date and end_date that you need to pass as well
I want the bare minimum editing of subscription please
[
'phases' => [
[
'items' => [
[
'price' => '{{PRICE_ID}}',
'quantity' => 2,
],
],
'start_date' => 1577865600,
'end_date' => 1580544000,
],
],
]
the api is being sassy about items
what about start date?
I need the start date of exactly when subscription started
these are the needed fields
and end date??????
no need for end_date
end date that's why iterations....
with your example
with this
you are not adding items
then in the second one you are not using iterations
the correct code is in the docs I shared
I can't really code on your behalf @somber vault
I can give you pointers and direct you in the right direction
I asked for an example that is correct
this means I need to write a working code for you
first they tell me no need to do all this
then obviously errors come on, and slowly it is basically copying things accross subscription to a shedule, or schedule to schedule update
when I only wanted to apply iterations to a subscription, before it was so simple using max_occurrences
what about start date?
to use the date the subscription is created?
$originalSubscription->current_period_start, ?
exactly
$subscriptionSchedule = \Stripe\SubscriptionSchedule::create([
'from_subscription' => $stripeSub,
], $connected_account);
if($subscriptionSchedule){
$uddatedSchedule = \Stripe\SubscriptionSchedule::update(
$subscriptionSchedule->id,
[
'phases' => [
[
'items' => $subscriptionSchedule->phases->items,
'iterations' => $metadata['No. of Payments'],
'start_date' => $stripeSub->current_period_start,
]
],
], $connected_account);
}```
still getting this error:
Error Message: Missing required param: phases[0][items].
Code: 0
HTTP Status Code: 400
Request ID: req_Wtx2gIDqUHCCiO
I don't think that will work since items is not exactly the same as a field as as a value(instead you have to manually pass the parameters you can't just copy from the exising object directly), but you could try it.
what is it that I have to manually pass?
// schedule a change from gold to silver at the end of the period
$sched = \Stripe\SubscriptionSchedule::create([
'from_subscription' => $subscription->id,
]);
\Stripe\SubscriptionSchedule::update($sched->id, [
'phases' => [
[
"start_date" => $subscription->current_period_start,
"end_date" => $subscription->current_period_end,
'items' => [
[
'price' => $gold->id,
'quantity' => 1,
],
],
],
[
"start_date" => $subscription->current_period_end,
'items' => [
[
'price' => $silver->id,
'quantity' => 1,
],
],
],
],
"end_behavior" => "release"
]);
this is what I use for a basic "change to X at the end of the period" if that helps
i want to keep it EXACTLY as the subscription
so like this?
$phases = [];
foreach ($stripeSub->items->data as $item) {
$phases[] = [
'items' => [['price' => $item->price->id]],
'iterations' => $metadata['No. of Payments'],
'application_fee_percent' => $stripeSub->application_fee_percent,
'metadata' => $metadataArray, // Use the corrected metadata array
'description' => $stripeSub->description,
'on_behalf_of' => $stripeSub->on_behalf_of,
// Add any other relevant data from the subscription item
];
}```
then you have to do things like
'phases' => [
# ...
'items' => [
[
'price' => $subscription->items->data[0]->price,
'quantity' => $subscription->items->data[0]->quantity,
],
[
'price' => $subscription->items->data[1]->price,
'quantity' => $subscription->items->data[1]->quantity,
],
],
# ...
]
for instance, it's very manual
SUPER manual, and annoying
sure, I agree
what about application fee percent?
if you're not changing it from what was on the subscription then 'application_fee_percent' => $stripeSub->application_fee_percent, seems to make sense yes
ok, almost there. With this:
$phases = [];
foreach ($stripeSub->items->data as $item) {
$phases[] = [
'items' => [['price' => $item->price->id]],
'iterations' => $metadata['No. of Payments'],
'application_fee_percent' => $stripeSub->application_fee_percent,
'metadata' => $metadataArray, // Use the corrected metadata array
'description' => $stripeSub->description,
'on_behalf_of' => $stripeSub->on_behalf_of,
// Add any other relevant data from the subscription item
];
}
// Create SubscriptionSchedule
$subscriptionSchedule = \Stripe\SubscriptionSchedule::create([
'from_subscription' => $stripeSub,
], $connected_account);
if($subscriptionSchedule){
$uddatedSchedule = \Stripe\SubscriptionSchedule::update(
$subscriptionSchedule->id,
[
/*'phases' => [
[
'items' => $subscriptionSchedule->phases->items,
'iterations' => $metadata['No. of Payments'],
'start_date' => $stripeSub->current_period_start,
]
],*/
'phases' => $phases,
], $connected_account);
}```
I get the following error:
Error Message: The subscription schedule update is missing at least one phase with a start_date to anchor end dates to.
Code: 0
HTTP Status Code: 400
Request ID: req_PZT4ps8RaK9vPj
you need to pass "start_date" => $subscription->current_period_start, (see the example I posted earlier)
the key thing to understand is the phases need to explicitly "declare" the state of the subscription at all times, including the current time; nothing is inferred from the existing phases. In the Update Schedule API call you are entirely replacing existing phases with what you pass, hence you need to re-declare the currently-happening phase and its start/end dates
ok, it worked. I now just need to check that all parameters are kept
the metadata of the schedule is all empty
remember to clarify if you mean the Schedule object $subscriptionSchedule->metadata or the phase object $subscriptionSchedule->phases[0]->metadata