KellyM
KellyM

Reputation: 2522

Paypal PHP REST API - No Payment Value Shown At Checkout

I am very new to the PayPal API and REST request/responses. As such, I have been trying to follow along with online samples (mostly from GitHub) about how to use PayPal's REST Api to process payments.

However, I have encountered a problem. When I click on the link that is generated, I am successfully redirected to PayPal's site. However, at the checkout page, there is nothing that indicates the amount of the purchase. What might the issue be? Thanks!

enter image description here

<?php
require __DIR__ . '/bootstrap.php';

use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;

$payer = new Payer();
$payer->setPaymentMethod("paypal");

$amount = new Amount();
$amount->setCurrency("USD");
$amount->setTotal('5.55');

$transaction = new Transaction();
$transaction->setAmount($amount)->setDescription("Purchase from Leisurely Diversion")->setInvoiceNumber(uniqid());

$redirectURLs = new RedirectUrls();
$redirectURLs->setReturnUrl("http://localhost/leisurelydiversion/confirmation.php")->setCancelUrl("http://localhost/leisurelydiversion/confirmation.php");

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

try {
$payment->create($apiContext);
} catch(Exception $e) {
echo "<h2> Error Sending Payment! $e</h2>";
}

$url = $payment->getApprovalLink();
echo $url;
?>

Upvotes: 1

Views: 766

Answers (2)

Shahin ShamS
Shahin ShamS

Reputation: 508

Hope this will help

$item = new Item();
$item->setSku('Product Id');
$item->setName('Product Name');
$item->setPrice('5.55');
$item->setCurrency('USD');
$item->setQuantity(1);

$itemList = new ItemList();
$itemList->setItems(array($item));

$transaction = new Transaction();
$transaction->setItemList($itemList);
$transaction->setAmount($amount);

Upvotes: 1

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

You have to add an item list to the payment:
http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/OrderCreateUsingPayPal.html

You can also change the action by appending &useraction=commit to the approval url:
Does PayPal Rest API have the classic useraction equivalent

Upvotes: 1

Related Questions