#xfechx

1 messages · Page 1 of 1 (latest)

hasty ironBOT
#

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.

glossy lantern
#

Could you elaborate? Which request id?

somber vault
#

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

glossy lantern
#

Sorry I don't really follow. Could you summarize your current issue with a step by step bullet points? It being hard to follow

somber vault
#

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

glossy lantern
#

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?

somber vault
#

Yes give me a moment to share it

hasty ironBOT
somber vault
#

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

ebon spade
somber vault
#

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

ebon spade
#

Please can you phrase your questions more coherently. It's really hard to follow that the actual ask is

somber vault
#

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?

ebon spade
ebon spade
somber vault
#

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'

  1. Listening to the event: customer.subscription_created.
  2. Getting the value of the metadata "No. of Payments"
  3. Applying a schedule to the event subscription it is referring to.
  4. Keep the subscription values exactly how they are, and ALL of them.
  5. 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?

ebon spade
#

Please don't be rude or disrespectful, see #📖rules . I'll read your question soon

somber vault
#

Thanks

ebon spade
somber vault
#

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.

ebon spade
somber vault
#

subscription schedules have rules, they are picky and some parameters do not agree when other parameters are there, etc. I need to know this

somber vault
#

ok, I will try

ebon spade
#

Yep, it is a pretty complex API I agree

#

But I can help you with any issue you run into

somber vault
#

what else do i need to pass on to the subscription schedule

#

if I am passing 'from_subscription'

ebon spade
#

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

somber vault
#

so, that will be the ONLY parameter to pass to it?

#

also, will I need to pass, price, etc etc. to phases?

ebon spade
somber vault
#

what about application fee percent?

#

on phases?

#

will that be inherited from subscription?

ebon spade
#

It should be yes

#

Test it and let me know if something is unexpected

hasty ironBOT
sturdy gale
#

👋 taking over for my colleague. Let me know if there's any follow-up Qs I can answer!

somber vault
#

getting an error

#

req_iqDzKhXjXe82P2

sturdy gale
#

phases of a subscription schedule is an array

#

you're passing an object

somber vault
#

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

sturdy gale
#
    'iterations' => $metadata['No. of Payments'],
],```
 needs to be
```'phases' => [[
    'iterations' => $metadata['No. of Payments'],
]],```
somber vault
#

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??? 😵‍💫

somber vault
#

man, this seems like an endless loop. Did you have a chance to read everything above before you jumped in?

sturdy gale
#

it's not an endless loop

#

it's pretty simple

somber vault
#

sure

#

it is an endless loop explaining every single person who joins the chat.

sturdy gale
#

the highlighted line explicitly say

You need to pass in all current and future phases when you update a subscription schedule

somber vault
#

I am using 'from_subscription'

sturdy gale
#

even if you are

somber vault
#

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

sturdy gale
#

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

somber vault
#

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?

sturdy gale
#

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

somber vault
#

do i need price, and quantity?

sturdy gale
#

oh I forgot start_date and end_date that you need to pass as well

somber vault
#

I want the bare minimum editing of subscription please

sturdy gale
#

[
'phases' => [
[
'items' => [
[
'price' => '{{PRICE_ID}}',
'quantity' => 2,

      ],
    ],
    'start_date' => 1577865600,
    'end_date' => 1580544000,
  ],
],

]

somber vault
#

the api is being sassy about items

#

what about start date?

#

I need the start date of exactly when subscription started

sturdy gale
#

these are the needed fields

somber vault
#

and end date??????

sturdy gale
#

no need for end_date

somber vault
#

end date that's why iterations....

sturdy gale
#

since you're using iterations

#

right

somber vault
#

with your example

somber vault
#

you are not adding items

sturdy gale
#

my code isn't correct

#

it was just to give you an idea

somber vault
#

then in the second one you are not using iterations

sturdy gale
#

the correct code is in the docs I shared

somber vault
#

ahh, so frustrating

#

it becomes confusing

sturdy gale
#

I can't really code on your behalf @somber vault

somber vault
#

I am not asking you to?

#

who asked for that?

sturdy gale
#

I can give you pointers and direct you in the right direction

somber vault
#

I asked for an example that is correct

sturdy gale
#

this means I need to write a working code for you

somber vault
#

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, ?

sly moon
#

exactly

somber vault
#
                $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

sly moon
#

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.

somber vault
#

what is it that I have to manually pass?

sly moon
#
// 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

somber vault
#

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
                    ];
                }```
sly moon
#

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

somber vault
#

SUPER manual, and annoying

sly moon
#

sure, I agree

somber vault
#

what about application fee percent?

sly moon
#

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

somber vault
#

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

sly moon
#

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

somber vault
#

ok, it worked. I now just need to check that all parameters are kept

#

the metadata of the schedule is all empty

sly moon
#

remember to clarify if you mean the Schedule object $subscriptionSchedule->metadata or the phase object $subscriptionSchedule->phases[0]->metadata