Search code examples
phpsymfonyloopspaypalpayum

sending multiple products to paypal using symfony


I am working with Symfony Payum bundle to send orders to PayPal and so far it is working fine with single product

Suppose my single product array looks like this

$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


);

Then the following code works perfectly fine

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

However if I have multiple products like the array below, then how to handle this?

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

Solution

  • You just need to loop through the items to generate that portion of the API request dynamically. So it would be something like this.

    $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++;
    }