samedi 27 juin 2020

Iterating through array chunk for a function call

I have an array that has 20 objects in it (I have reduced this to 3 in this example to keep things simple).

    $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
      ),
)

I can split the array by using chunks:

$schedule_batch = array_chunk($dataset, 9);

Now I want to process each chunk as seperate calls

  foreach ($schedule_batch as $key => $value){
    foreach ($schedule_batch[$key] as $key2 => $value2) {
         createSchedule($customer->id, $phases);
      }
    }

However all this does it just process all of the 20 array items in one call. My goal is to allow it to run twice creating two schedules each with 10 items (not a schedule of 20 items).

Any help is really appreciated.

UPDATED WITH FUNCTION*

function createSchedule ($customer, $phases) {
$schedule = \Stripe\SubscriptionSchedule::create([
    'customer' => $customer,
    'expand' => ['subscription'],
    'start_date' => 'now',
    'end_behavior' => 'cancel',
    'phases' => $phases,
  ]);
}


$phases = [];
foreach ($dataset as $data) {
$phases[] = [
  'end_date' => $data["end_date"],
  'transfer_data' => [
    'amount_percent' => $fee_percentage,
    'destination' => $account],        
  'proration_behavior' => 'none',
  'plans' => [
      [
        'price_data' => [
          'unit_amount' => $data["unit_amount"],
          'currency' => 'usd',
          'product' => $data["product"],
          'recurring' => [
              'interval' => 'month',
          ],
        ],
        
      ],
    ],
];
}

Some additional context as requested:

This piece of code is a proof of concept to create some schedules on Stripe. The goal is here is two have a list of payment schedules items in an array ($dataset). The Stripe API in this case does not support more than 10 payments in the phase, so I have broken up the array into two chunks. I just need the foreach loop to iterate through each array chunk.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3g1J2g7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire