使用symfony将多个产品发送到paypal

时间:2015-02-05 14:04:49

标签: php symfony loops paypal payum

我正在使用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
    ),
);

2 个答案:

答案 0 :(得分:0)

您只需循环遍历项目即可动态生成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++;
}

答案 1 :(得分:-1)

就像那样:

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'];
}