JustGage
JustGage

Reputation: 259

Paypal REST PHP SDK giving me a 400 error (laravel library)

I'm using the paypal REST SDK for PHP, using the laravel wrapper which gives me a 400 error (bad input from what I understand) I mostly copied from the example found at: enter link description here Yet I'm getting a 400 error sent back. I can't figure out what I'm doing wrong: here's the code.

    $payer = Paypalpayment::Payer();
    $payer->setPayment_method("paypal");

    $item1 = Paypalpayment::Item();
    $item1->setName('Lavender 6 oz')
        ->setCurrency('USD')
        ->setQuantity(1)
        ->setPrice('7.50');

    $itemList = Paypalpayment::ItemLIst();
    $itemList->setItems(array($item1));


    $amount = Paypalpayment::Amount();
    $amount->setCurrency("USD")
        ->setTotal("20.00");


    $transaction = Paypalpayment::Transaction();
    $transaction->setAmount($amount)
        ->setItemList($itemList)
        ->setDescription("Buying from ButterflyOils.com");

    $redirectUrls = Paypalpayment::RedirectUrls();
    $baseUrl = Paypalpayment::getBaseUrl();
    $redirectUrls->setReturnUrl($baseUrl +  "/ExecutePayment.php?success=true&message='PayPal+Worked!'")
        ->setCancelUrl($baseUrl + "/ExecutePayment.php?success=false&message='PayPal+Cancel'");

    $payment = Paypalpayment:: Payment();
    $payment->setIntent("sale");
    $payment->setPayer($payer);
    $payment->setRedirectUrls($redirectUrls);
    $payment->setTransactions(array($transaction));

    try {
        $payment->create($this->_apiContext);
    } catch (\PPConnectionException $ex) {
        return "Exception: " . $ex->getMessage() . PHP_EOL;
        var_dump($ex->getData());
        exit(1);
    }

    foreach($payment->getLinks() as $link) {
        if($link->getRel() == 'approval_url') {
            $redirectUrl = $link->getHref();
            break;
        }
    }

    // yeah I know this is a bad idea
    $_SESSION['paymentId'] = $payment->getId();
    if(isset($redirectUrl)) {
        header("Location: $redirectUrl");
        exit;
    }

Upvotes: 0

Views: 971

Answers (3)

Emmanuel Orozco
Emmanuel Orozco

Reputation: 389

Change this

$item1 = Paypalpayment::Item();
    $item1->setName('Lavender 6 oz')
        ->setCurrency('USD')
        ->setQuantity(1)
        ->setPrice('7.50');

    $itemList = Paypalpayment::ItemLIst();
    $itemList->setItems(array($item1));


    $amount = Paypalpayment::Amount();
    $amount->setCurrency("USD")
        ->setTotal("7.50");

Thats it

Upvotes: 0

JustGage
JustGage

Reputation: 259

The Error was I had a returnURL and CancelURL with a value of '0' because I was trying to concatenate to strings with a + (Javascript habit) but also the baseUrl() returns an invalid address due to Laravel's routing.

It was helpful to look at the PayPal.log

Upvotes: 0

aydiv
aydiv

Reputation: 531

The API returns a detailed description of validation errors with HTTP 400 responses and you should be able to inspect the exception object to figure what was returned.

In this case, it looks like your item total does not match the overall payment amount for the transaction but the API error message should tell you for sure

Upvotes: 2

Related Questions