PHP从PayPal响应中循环多个购物车项目

时间:2017-05-15 13:13:24

标签: php paypal

在成功交易后,我从Paypal获得以下回复:

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)

如何循环此响应中的购物车数量?我希望有更好的结构化响应,但这是PayPal。

任何想法?

我的成功函数代码:

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

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

                    // loop goes here

            }
      }

2 个答案:

答案 0 :(得分:0)

这样的东西?

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

答案 1 :(得分:0)

怎么样

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