#xfechx-subscription-maxoccurrences
1 messages · Page 1 of 1 (latest)
@flint bear hello, what do you need?
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
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?
Yes
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
Gotcha, we document all of this really clearly in step by step instructions in our docs https://stripe.com/docs/billing/subscriptions/subscription-schedules and https://stripe.com/docs/billing/subscriptions/subscription-schedules/use-cases#installment-plans
Please take the time to test this API end to end
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?
Hello! I'm taking over and catching up...
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
Not sure I understand... you got that code from an email? What email?
"
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.
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.
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?