Home » Php » php – Paypal item description with the rest api sdk

php – Paypal item description with the rest api sdk

Posted by: admin July 12, 2020 Leave a comment


I’m using https://github.com/paypal/rest-api-sdk-php

And i want to have a item description review to be displayed, here is the code:

  $amountDetails = new Details();

    $amount = new Amount();

    $transaction = new Transaction();
    $transaction->setDescription('This is the payment transaction description.');

    $RedirectUrls = new RedirectUrls();
    $RedirectUrls ->setReturnUrl('http://localhost/mrSurvey/public/#/pricing');
    $RedirectUrls ->setCancelUrl('http://localhost/mrSurvey/public/#/pricing');

    $payment = new Payment();

All i can see is the description, but i want to see item number and the subtotal, what am i missing?

So i read that i need to add a few things: so i did something like this:

 $item = new Item();

 $items = new ItemList();


$payment = new Payment();

$response = $payment->create($apiContext)->toarray();

return Response::json($response);

Now the code above gives me 400 error… because of the added item stuff, any clues?

How to&Answers:

It would be appear that your on the right track from the update you got. But it would also seem you have a few issues also, then just the stated problem

try adding your code into a catch and echo out message from paypal

  //your code here
catch(PayPal\Exception\PayPalConnectionException $e) {
  //This will show error from paypal

My guess cause I got similar error is your not doing the correct addition of your items (tax, shipping, subtotal, etc…) and so on. Try this sample @ https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payments/ExecutePayment.php and get it to work first, then modify code from sample, to sort your needs.

Below is some of my code modified and it works for me.

Note you got an item description and an transaction description

 //run x through loop with ++
 $x = 0;

 //for item 
 $items[$x] = new Item();

  $itemList = new ItemList();

  //for transaction
  $transaction = new Transaction();

function getTotals($quantity, $tax, $price){
  $tax = $tax * $quantity;
  $subtotal = $price * $quantity;
  $total = $tax + $subtotal;
total = getTotal($item_quantity, $item_tax, $item_price);


I found the answer, we need more details for create a transaction, and you have to set correct number to subTotal(), total() etc…

Each price item * Quantity = Subtotal

Subtotal +- Tax, Shipping etc... = total

This is what I have :

        $sdkConfig = array(
            "mode" => "sandbox"

        $cred = new OAuthTokenCredential($client_id, $secret, $sdkConfig);
        $apiContext = new ApiContext($cred, 'Request' . time());

        $payer = new Payer();

        foreach ($cartHasItems as $key => $order) {
            $item[$key] = new PayPalItem();

            $item[1] = new PayPalItem();
            $item[1]->setName('Promo code '.$cart->getPromoCode()->getCode())
                ->setPrice(-$this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'Promo'));

        $itemList = new ItemList();

        $details = new Details();
        $details->setTax($this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'HT')*20/100)
                ->setSubtotal($this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'HT'));

        $amount = new Amount();
            ->setTotal($this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'All'))

        $transaction = new Transaction();
            ->setDescription("Payment description")

        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl($this->generateUrl('paypal_success', array(), UrlGeneratorInterface::ABSOLUTE_URL));
        $redirectUrls->setCancelUrl($this->generateUrl('order_validation', array('type' => 'paypal'), UrlGeneratorInterface::ABSOLUTE_URL));

        $payment = new Payment();



Add this

$transaction->setDescription("Payment description")