Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paypal item description with the rest api sdk

Tags:

php

paypal

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();
    $amountDetails->setSubtotal('7.41');
    $amountDetails->setTax('0.03');
    $amountDetails->setShipping('0.03');

    $amount = new Amount();
    $amount->setCurrency('USD');
    $amount->setTotal('7.47');
    $amount->setDetails($amountDetails);

    $transaction = new Transaction();
    $transaction->setAmount($amount);
    $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();
    $payment->setIntent('sale');
    $payment->setPayer($payer);
    $payment->setTransactions(array($transaction));
    $payment->setRedirectUrls($RedirectUrls);

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

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

 $item = new Item();
 $item->setQuantity('1');
 $item->setName('benny');
 $item->setPrice('7.41');
 $item->setCurrency('USD');
 $item->setSku('blah');


 $items = new ItemList();
 $items->addItem(array($item));

...

$transaction->setItemList($items);

...

$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));
$payment->setRedirectUrls($RedirectUrls);

$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?

like image 731
totothegreat Avatar asked Sep 20 '14 11:09

totothegreat


People also ask

Does PayPal use restful API?

The PayPal REST API is organized around transaction workflows, including: orders, payments, subscriptions, invoicing, and disputes. The API uses standard verbs and returns HTTP response codes and JSON-encoded responses.

How do I get my PayPal REST API access token?

Enter the https://api-m.sandbox.paypal.com/v1/oauth2/token request URL. On the Authorization tab, select the Basic Auth type. Type your client ID in the Username box, and type your secret in the Password box. On the Body tab, select x-www-form-urlencoded .


1 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

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

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();
 $items->setName($item_name)
 ->setDescription($item_description)
 ->setCurrency($currency)
 ->setQuantity($item_quantity)
 ->setTax($item_tax)
 ->setPrice($item_price)
 ->setSku($item_sku);

  $itemList = new ItemList();
  $itemList->setItems($items);

  //for transaction
  $transaction = new Transaction();
  $transaction
  ->setAmount($amount)
  ->setItemList($itemList)
  ->setDescription($payment_description)
  ->setInvoiceNumber($invoice_number);

function getTotals($quantity, $tax, $price){
  $tax = $tax * $quantity;
  $subtotal = $price * $quantity;
  $total = $tax + $subtotal;
} 
total = getTotal($item_quantity, $item_tax, $item_price);
like image 193
Sergio Ramalli Gomes Avatar answered Sep 19 '22 12:09

Sergio Ramalli Gomes