Я работаю с пакетом Symfony Payum для отправки заказов в PayPal, и пока он работает нормально с одним продуктом.
Предположим, что мой единственный массив продуктов выглядит следующим образом
$orderDetails = array(
'productID' => '10',
'fname' => 'First Name',
'lname' => 'Last Name',
'phone' => '111',
'clientemail' => '[email protected]',
'name' => 'Product Name',
'description' => 'The Product description.',
'price' => 8.64,
'currency_symbol' => '$',
'currency' => 'USD',
'clientId' => '222',
'payment_option' => $paymentName);
Тогда следующий код работает отлично
$storage = $this->get('payum')->getStorage('ClickTeck\featuresBundle\Entity\Orders');
/** @var $paymentDetails Orders */
$paymentDetails = $storage->create();$paymentDetails->setClientFname($orderDetails['fname']);
$paymentDetails->setClientLname($orderDetails['lname']);
$paymentDetails->setClientPhone($orderDetails['phone']);
$paymentDetails->setClientEmail($orderDetails['clientemail']);
$paymentDetails->setNumber($orderDetails['productID']);
$paymentDetails->setDescription($orderDetails['description']);
$paymentDetails->setCurrencyCode($orderDetails['currency']);
$paymentDetails->setTotalAmount($orderDetails['price']);
$paymentDetails->setClientId($orderDetails['clientId']);
$paymentDetails->setPaymentOption($orderDetails['payment_option']);$paymentDetails['PAYMENTREQUEST_0_CURRENCYCODE'] = $orderDetails['currency'];
$paymentDetails['PAYMENTREQUEST_0_AMT'] = $orderDetails['price'];
$paymentDetails['NOSHIPPING'] = Api::NOSHIPPING_NOT_DISPLAY_ADDRESS;
$paymentDetails['REQCONFIRMSHIPPING'] = Api::REQCONFIRMSHIPPING_NOT_REQUIRED;
$paymentDetails['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = Api::PAYMENTREQUEST_ITERMCATEGORY_DIGITAL;
$paymentDetails['L_PAYMENTREQUEST_0_AMT0'] = $orderDetails['price'];
$paymentDetails['L_PAYMENTREQUEST_0_NAME0'] = $orderDetails['name'];
$paymentDetails['L_PAYMENTREQUEST_0_DESC0'] = $orderDetails['description'];
$storage->update($paymentDetails);
$captureToken = $this->getTokenFactory()->createCaptureToken(
$paymentName,
$paymentDetails,
'payment_done'
);
$paymentDetails['INVNUM'] = $paymentDetails->getId();
$storage->update($paymentDetails);
return $this->redirect($captureToken->getTargetUrl());
Однако, если у меня есть несколько продуктов, таких как массив ниже, то как с этим справиться?
$paymentName = 'paypal';
$orderDetails = array(
array(
'productID' => '10',
'fname' => 'First Name',
'lname' => 'Last Name',
'phone' => '111',
'clientemail' => '[email protected]',
'name' => 'Product Name',
'description' => 'The Product description.',
'price' => 8.64,
'currency_symbol' => '$',
'currency' => 'USD',
'clientId' => '222',
'payment_option' => $paymentName
),
array(
'productID' => '10',
'fname' => 'First Name',
'lname' => 'Last Name',
'phone' => '111',
'clientemail' => '[email protected]',
'name' => 'Product Name',
'description' => 'The Product description.',
'price' => 8.64,
'currency_symbol' => '$',
'currency' => 'USD',
'clientId' => '222',
'payment_option' => $paymentName
),
);
Вам просто нужно пройтись по элементам, чтобы динамически генерировать эту часть запроса API. Так было бы что-то вроде этого.
$i = 0;
foreach($orderDetails as $orderItem)
{
$paymentDetails['L_PAYMENTREQUEST_0_ITEMCATEGORY' . $i] = Api::PAYMENTREQUEST_ITERMCATEGORY_DIGITAL;
$paymentDetails['L_PAYMENTREQUEST_0_AMT' . $i] = $orderItem['price'];
$paymentDetails['L_PAYMENTREQUEST_0_NAME' . $i] = $orderItem['name'];
$paymentDetails['L_PAYMENTREQUEST_0_DESC' . $i] = $orderItem['description'];
$i++;
}
Просто так:
for ($i = 0; $i < count($orderDetails); $i++) {
$paymentDetails->setClientEmail($orderDetails[$i]['clientemail']);
$paymentDetails->setDescription($orderDetails[$i]['description']);
$paymentDetails->setCurrencyCode($orderDetails[$i]['currency']);
$paymentDetails->setTotalAmount($orderDetails[$i]['price']);
$paymentDetails[$payment.$i.'_CURRENCYCODE'] = $orderDetails[$i]['currency'];
$paymentDetails[$payment.$i.'_AMT'] = $orderDetails[$i]['price'];
$paymentDetails['NOSHIPPING'] = Api::NOSHIPPING_NOT_DISPLAY_ADDRESS;
$paymentDetails['REQCONFIRMSHIPPING'] = Api::REQCONFIRMSHIPPING_NOT_REQUIRED;
$paymentDetails[$lpayment.$i.'_ITEMCATEGORY0'] = Api::PAYMENTREQUEST_ITERMCATEGORY_DIGITAL;
$paymentDetails[$payment . $i . '_PAYMENTREQUESTID'] = "CART26488-PAYMENT".$i;
$paymentDetails[$payment . $i . '_SELLERPAYPALACCOUNTID'] = "[email protected]";
$paymentDetails[$lpayment.$i.'_AMT0'] = $orderDetails[$i]['price'];
$paymentDetails[$lpayment.$i.'_NAME0'] = $orderDetails[$i]['name'];
$paymentDetails[$lpayment.$i.'_DESC0'] = $orderDetails[$i]['description'];
}