1
votes

I'm learning PayPal SDK and it worked perfectly, when I was firstly working with one product. Now I have to work with an array of products. Thay's the code

$paypal = new ApiContext(
        new OAuthTokenCredential(
            '...',
            '...'
        )
    );

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

    $shipping = 2;
    $totalPriceWithShipping = 0;
    $totalPriceWithoutShipping = 0;


    $items = [];

    foreach(Controller::getData('productsInCart') as $product)
    {
        $totalPriceWithShipping += ($product->price + $shipping);
        $totalPriceWithoutShipping += $product->price;

        $item = new Item();

        $item->setName($product->name)
            ->setPrice($product->price)
            ->setCurrency('USD')
            ->setQuantity(1);

        $items[] = $item;
    }

    $itemList = new ItemList();
    $itemList->setItems($items);

    $details = new Details();
    $details->setShipping($shipping)
            ->setSubtotal($totalPriceWithoutShipping);


    $amount = new Amount();
    $amount->setCurrency('USD')
        ->setTotal($totalPriceWithShipping)
        ->setDetails($details);



    $transaction = new Transaction();
    $transaction->setItemList($itemList)
                ->setAmount($amount)
                ->setDescription('Your Order from Online Shop')
                ->setInvoiceNumber(uniqid());

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl('...')
                ->setCancelUrl('...');

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

    try {
        $payment->create($paypal);
    }
    catch(Exception $e) {
        die($e->getTrace());
    }

   die($payment->getApprovalLink());

But I am somehow getting this error:

Fatal error: Uncaught exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:180 Stack trace: #0 C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...') #1 C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(102): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL) #2 C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(422): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL) #3 C:\dev\htdocs\Shop\app\controllers\CartController.php(183): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 [internal functio in C:\dev\htdocs\Shop\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php on line 180

and just so you can take a look at $items array

$items array

1
So there is problem with shipping. If shipping equals 0 it works. - h0lend3r

1 Answers

0
votes

Getting 400 exception means, paypal is returning with some exception. To get more details on that exception. You need to catch that uncaught exception PayPal\Exception\PayPalConnectionException to get more details.

This page should help you give some idea. Below is the sample code to catch that exception:

try {
    $payment->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
    echo $ex->getCode(); // Prints the Error Code
    echo $ex->getData(); // Prints the detailed error message 
    die($ex);
} catch (Exception $ex) {
    die($ex);
}