Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding multiple items to checkout - help needed.... #710

Open
AaronCatolico1 opened this issue Oct 25, 2024 · 0 comments
Open

Adding multiple items to checkout - help needed.... #710

AaronCatolico1 opened this issue Oct 25, 2024 · 0 comments

Comments

@AaronCatolico1
Copy link

AaronCatolico1 commented Oct 25, 2024

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant