With this php code, I send a request for payment with Paypal sandbox account. However when I go to the submit page I cant see what amount I am paying: https://i.imgur.com/iTra1SQ.png -> here in the red shoulld be displayed - 10.00(amountPayable).
How can I send it so it's showed?

use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;

require 'bootstrap.php';

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

// Set some example data for the payment.
$currency = 'GBP';
$amountPayable = 10;
$invoiceNumber = uniqid();

$amount = new Amount();
$amount->setCurrency($currency)
->setTotal($amountPayable);

$transaction = new Transaction();
$transaction->setAmount($amount)
->setDescription('10 GBP payment')
->setInvoiceNumber($invoiceNumber);

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($paypalConfig['return_url'])
->setCancelUrl($paypalConfig['cancel_url']);

$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction])
->setRedirectUrls($redirectUrls);

try {
$payment->create($apiContext);
} catch (Exception $e) {
throw new Exception('Unable to create link for payment');
}

header('location:' . $payment->getApprovalLink());
exit(1);

Dani AI

Generated

Short expert summary for (and tying 's hint to the real cause)

Most likely cause: the PayPal payment object needs monetary values as strings with the correct decimal precision for the currency (GBP uses two decimals). Passing an integer (for example 10) or sending a breakdown whose parts do not sum exactly to the declared total will trigger a validation problem on PayPal's side. When that happens the approval page may not show the amount even though your server code called create() — the approval UI displays whatever the created payment object contains.

Quick, practical fixes to try now

  • Format totals as strings with two decimal places before calling the SDK method (for example use number_format($amountPayable, 2, '.', '')).
  • If you include details or an item list, make sure subtotal + tax + shipping exactly equals the total (all as strings with correct precision).
  • Capture and log PayPal's API response when create() fails so you can see the validation message. Example debug pattern:
try {
    $payment->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
    error_log($ex->getData()); // PayPal's validation JSON
    throw $ex;
}

Extra debugging and long-term notes

  • Inspect the sandbox API calls in the PayPal developer dashboard to see the exact request/response JSON. That will show mismatched amounts or validation errors.
  • is correct to check the cart UI, but fix the payload first: the approval page shows the payment object you send. If the server payload is wrong, refreshing the client cart won't help.
  • The old PayPal PHP REST SDK is deprecated; for new work consider PayPal Checkout / Orders V2. See the PayPal docs for the current APIs: PayPal Payments API (v2).

Your code seems fine. It does however not show how you are loading the cart menu. The code should be updated on that page to "refresh" your cart menu with the amount.

Please post the code where your cart is created.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.