October 25, 2024
Chicago 12, Melborne City, USA
PHP

Omnipay Paypal php issue – adding multiple items to checkout


I’d like to be able to pass multiple items with their details through an associative array as shown in the following code:

<?php
require_once 'config.php';

if (isset($_POST['submit'])) {

    try {
        $response = $gateway->purchase([
            'amount'=>'60.00',
            'returnUrl' => 'http://localhost:3000/success.php',
            'cancelUrl' => 'http://localhost:3000/cancel.php'
        ])->setItems(array(
            array('name' => 'item1', 'quantity' => 2, 'price' => '10.00'),
            array('name' => 'item2', 'quantity' => 1, 'price' => '50.00')
        ))->send();

        if ($response->isRedirect()) {
            $response->redirect(); // this will automatically forward the customer
        } else {
            // not successful
            echo $response->getMessage();
        }
    } catch(Exception $e) {
        echo $e->getMessage();
    }
}

?>

And my response file code looks like this:

<?php
require_once 'config.php';
 
// Once the transaction has been approved, we need to complete it.
if (array_key_exists('paymentId', $_GET) && array_key_exists('PayerID', $_GET)) {
    $transaction = $gateway->completePurchase(array(
        'payer_id'             => $_GET['PayerID'],
        'transactionReference' => $_GET['paymentId'],
    ));
    $response = $transaction->send();
 
    if ($response->isSuccessful()) {
        // The customer has successfully paid.
        $arr_body = $response->getData();
 
        $payment_id = $db->real_escape_string($arr_body['id']);
        $payer_id = $db->real_escape_string($arr_body['payer']['payer_info']['payer_id']);
        $payer_email = $db->real_escape_string($arr_body['payer']['payer_info']['email']);
        $amount = $db->real_escape_string($arr_body['transactions'][0]['amount']['total']);
        $currency = PAYPAL_CURRENCY;
        $payment_status = $db->real_escape_string($arr_body['state']);
 
        $sql = sprintf("INSERT INTO payments(payment_id, payer_id, payer_email, amount, currency, payment_status) VALUES('%s', '%s', '%s', '%s', '%s', '%s')", $payment_id, $payer_id, $payer_email, $amount, $currency, $payment_status);
        $db->query($sql);
 
        echo "Payment is successful. Your transaction id is: ". $payment_id;
    } else {
        echo $response->getMessage();
    }
} else {
    echo 'Transaction is declined';
}


?>

I’d like for the buyer to be able to see all items that they’re about to purchase, along with their total cost. What exactly is the issue?

I tried the code above and the result is an error as shown below:

"Invalid request – see details"



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video