#xfechx-subscription-maxoccurrences

1 messages · Page 1 of 1 (latest)

weak coveBOT
dusky thorn
#

@flint bear hello, what do you need?

flint bear
#

Hello @dusky thorn , I have the following code, that updates my subscriptions to payment plans whenever a subscription is created with metadata of payment plan flag and number of payments

#
        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
                );
#

I would like to replicate this , but with the new subscription schedules, as max_occurrences is getting deprecated

dusky thorn
#

Sure, what's the problem exactly though?

#

xfechx-subscription-maxoccurrences

flint bear
#

that, here I am using a subscription event

#

can I still use the subscription event when subscription is created to create a schedule as well?

dusky thorn
#

Yes

flint bear
#

and also what parameter is compulsory, can I pass the subscription to the subscripiton schedule, or what do I need to do?

#

Or do I need to discern into prices and plans, etc. That's my confusion

dusky thorn
weak coveBOT
flint bear
#

I got the email with this code:

#

$StripeSub = Stripe::SubscriptionSchedules.create({phases: [items: […], iterations: $metadata['No. of Payments']]}, end_behavior: ‘cancel’ });

#

It doesn't include customer, is customer a needed parameter?

loud adder
#

Hello! I'm taking over and catching up...

flint bear
#

Hi

#

Can I use the items array from the subscription created?

#

$StripeSub = Stripe::SubscriptionSchedules.create({phases: [items: $event->data->object->items, iterations: $metadata['No. of Payments']]}, end_behavior: ‘cancel’ });

#

through webhook

loud adder
#

Not sure I understand... you got that code from an email? What email?

flint bear
#

"
To migrate to Subscription Schedules, you’ll need to make the following update to your API calls:

Current: Stripe::Subscriptions.create({items: […], max_occurrences: 2})

Updated: Stripe::SubscriptionSchedules.create({phases: [items: […], iterations: 2]}, end_behavior: ‘cancel’ })"

#

my question is if the customer id is needed compulsory, or just with what I just shared is enough.

loud adder
#

When creating a new Subscription you need to specify the Customer. If you're creating a Subscription Schedule for an existing Subscription the Customer is already specified and doesn't need to be specified again. If the Subscription Schedule is creating a new Subscription then a Customer does need to be specified.

flint bear
#
        echo "--- EVENT: customer.subscription.created ---";
        
        // Check if 'No. of Payments' metadata is present
        $metadata = $event->data->object->metadata;
        if (!empty($metadata['No. of Payments'])) {
            try {
                // Create phases array for SubscriptionSchedules
                $phases = [];
                foreach ($event->data->object->items as $item) {
                    $phases[] = [
                        'items' => [['price' => $item->price->id]],
                        'iterations' => $metadata['No. of Payments'],
                    ];
                }

                // Create SubscriptionSchedule
                $subscriptionSchedule = \Stripe\SubscriptionSchedule::create([
                    'phases' => $phases,
                    'end_behavior' => 'cancel',
                ], $connected_account);

                // Update the subscription with the SubscriptionSchedule ID
                $updatedSubscription = \Stripe\Subscription::update(
                    $event->data->object->id,
                    ['schedule' => $subscriptionSchedule->id],
                    $connected_account
                );
            } catch (\Stripe\Exception\ApiErrorException $e) {
                echo '--- could not update subscription with phases (max occurrences) ---';
                // Handle exception appropriately
                echo 'Error: ' . $e->getMessage();
            }
        }```
#

are the above phases enough then?