Reputation: 1673
I have a PHP array called $dataset
which has been split into two chunks. I am trying to create two Stripe subscription schedules based on each chunk.
The code below creates two schedules but only for the second part of the chunk schedule_batch[1]
which is created twice. The first 9 items in the first chunk are not created at all.
The first chunk is completely ignored and the second chunk is processed twice.
I have removed the full contents of my array to keep the code example below simple, but it is in the format below and there are 20 items in the array.
$dataset = array(
array(
"product" => $product->id,
"unit_amount" => "2020",
"end_date" => date_timestamp_get(date_create("2020-07-12")) // Payment 1 Date
),
array(
"product" => $product->id,
"unit_amount" => "2000",
"end_date" => date_timestamp_get(date_create("2020-07-15")) //Payment 2 Date
),
array(
"product" => $product->id,
"unit_amount" => "3400",
"end_date" => date_timestamp_get(date_create("2020-07-16")) //Payment 3 Date
),
array(
"product" => $product->id,
"unit_amount" => "3700",
"end_date" => date_timestamp_get(date_create("2020-07-18")) //Payment 4 Date
),
);
$schedule_batch = array_chunk($dataset, 9);
$numberofbatches = count($schedule_batch);
$phases = [];
foreach ($schedule_batch as $index => $data) {
$phases[] = [
'end_date' => $data[$index]["end_date"],
'transfer_data' => [
'amount_percent' => $fee_percentage,
'destination' => $account],
'proration_behavior' => 'none',
'plans' => [
[
'price_data' => [
'unit_amount' => $data[$index]["unit_amount"],
'currency' => 'usd',
'product' => $data[$index]["product"],
'recurring' => [
'interval' => 'month',
],
],
],
],
];
}
function createSchedule ($customer, $phases) {
$schedule = \Stripe\SubscriptionSchedule::create([
'customer' => $customer,
'expand' => ['subscription'],
'start_date' => date_timestamp_get(date_create("2020-07-10")),
'end_behavior' => 'cancel',
'phases' => $phases,
]);
}
foreach ($schedule_batch as $index => $data) {
createSchedule($customer->id, $phases);
}
Upvotes: 0
Views: 107
Reputation: 147206
It would be easier to apply the code which generates $phases
to $dataset
and then chunk it into $schedule_batch
:
$phases = [];
foreach ($dataset as $data) {
$phases[] = [
'end_date' => $data["end_date"],
'transfer_data' => [
'amount_percent' => 20,
'destination' => 10],
'proration_behavior' => 'none',
'plans' => [
[
'price_data' => [
'unit_amount' => $data["unit_amount"],
'currency' => 'usd',
'product' => $data["product"],
'recurring' => [
'interval' => 'month',
],
],
],
],
];
}
$schedule_batch = array_chunk($phases, 2);
$numberofbatches = count($schedule_batch);
Your final code to call createSchedule
would then be:
foreach ($schedule_batch as $phase) {
createSchedule($customer->id, $phase);
}
Upvotes: 1