[英]PHP Loop over multiple cart items from PayPal response
I get the following response from Paypal after a successful transaction: 成功交易后,我从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)
How can I loop over the number of cart items from this response? 如何从该响应中循环选择购物车的数量? I would expect a better structured response but this is PayPal. 我希望有一个更好的结构化响应,但这就是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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.