使用 symfony 将多个产品发送到 paypal
sending multiple products to paypal using symfony
我正在使用 Symfony Payum 捆绑包向 PayPal 发送订单,到目前为止,它对单个产品运行良好
假设我的单一产品数组如下所示
$orderDetails = array(
'productID' => '10',
'fname' => 'First Name',
'lname' => 'Last Name',
'phone' => '111',
'clientemail' => 'xyz@abc.com',
'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' => 'xyz@abc.com',
'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' => 'xyz@abc.com',
'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'] = "etochavez-facilitator@gmail.com";
$paymentDetails[$lpayment.$i.'_AMT0'] = $orderDetails[$i]['price'];
$paymentDetails[$lpayment.$i.'_NAME0'] = $orderDetails[$i]['name'];
$paymentDetails[$lpayment.$i.'_DESC0'] = $orderDetails[$i]['description'];
}
我正在使用 Symfony Payum 捆绑包向 PayPal 发送订单,到目前为止,它对单个产品运行良好
假设我的单一产品数组如下所示
$orderDetails = array(
'productID' => '10',
'fname' => 'First Name',
'lname' => 'Last Name',
'phone' => '111',
'clientemail' => 'xyz@abc.com',
'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' => 'xyz@abc.com',
'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' => 'xyz@abc.com',
'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'] = "etochavez-facilitator@gmail.com";
$paymentDetails[$lpayment.$i.'_AMT0'] = $orderDetails[$i]['price'];
$paymentDetails[$lpayment.$i.'_NAME0'] = $orderDetails[$i]['name'];
$paymentDetails[$lpayment.$i.'_DESC0'] = $orderDetails[$i]['description'];
}