简体   繁体   中英

PHP Loop over multiple cart items from PayPal response

I get the following response from Paypal after a successful transaction:

array (size=37)
  'payer_email' => string 'email@-buyer.com' (length=27)
  'payer_id' => string 'CM3HZRBZPR7ZS' (length=13)
  'payer_status' => string 'VERIFIED' (length=8)
  'first_name' => string 'First' (length=6)
  'last_name' => string 'Surname' (length=4)
  'address_name' => string 'xxxxxxx' (length=11)
  'address_street' => string 'xxxxxx' (length=14)
  'address_city' => string 'xxxxxx' (length=13)
  'address_state' => string 'xxxxxx' (length=13)
  'address_country_code' => string 'GB' (length=2)
  'address_zip' => string 'xxxxx' (length=7)
  'residence_country' => string 'GB' (length=2)
  'txn_id' => string '1NL36739Y22912102' (length=17)
  'mc_currency' => string 'GBP' (length=3)
  'mc_fee' => string '18.56' (length=5)
  'mc_gross' => string '540.00' (length=6)
  'protection_eligibility' => string 'ELIGIBLE' (length=8)
  'payment_fee' => string '18.56' (length=5)
  'payment_gross' => string '540.00' (length=6)
  'payment_status' => string 'Completed' (length=9)
  'payment_type' => string 'instant' (length=7)
  'item_name1' => string 'Front Row Seats' (length=15)
  'item_number1' => string '27' (length=2)
  'quantity1' => string '4' (length=1)
  'mc_gross_1' => string '480.00' (length=6)
  'item_name2' => string 'Middle seats' (length=12)
  'item_number2' => string '27' (length=2)
  'quantity2' => string '2' (length=1)
  'mc_gross_2' => string '60.00' (length=5)
  'num_cart_items' => string '2' (length=1)
  'txn_type' => string 'cart' (length=4)
  'payment_date' => string '2017-05-15T12:59:36Z' (length=20)
  'business' => string 'xxxxxxxxx' (length=21)
  'receiver_id' => string '5RV8VG77C2SCC' (length=13)
  'notify_version' => string 'UNVERSIONED' (length=11)
  'custom' => string '2' (length=1)
  'verify_sign' => string 'AFcWxV21C7fd0v3bYYYRCpSSRl31AI1bmOJ-4coNEpEu0juBQ7wxRWzV' (length=56)

How can I loop over the number of cart items from this response? I would expect a better structured response but this is PayPal.

Any Ideas?

My code for the success function:

    public function actionPaypalSuccess()
        {
            $request = Yii::$app->request;
            $params = $request->bodyParams;

            // SUCCESS
            if(isset( $params['payment_status']) && 
                 $params['payment_status'] == 'Completed'){

                    // loop goes here

            }
      }

Something like this?

for($i = 1; $i <= $params['num_cart_items']; $i++){
    echo $params['item_name'.$i];
}

How about

$products = [];
for ($productNr=1; $productNr<=(int)$params['num_cart_items']; $productNr++)  {
        $products[] = [
            'item_name' => $params['item_name' . $productNr],
            'item_number' => $params['item_number' . $productNr],
            'quantity' => $params['quantity' . $productNr],
            'mc_gross' => $params['mc_gross' . $productNr];
        ];
    }
}

print_r($products);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM