Monday, March 6, 2017

Got Http response code 400 when accessing https://api.sandbox.paypal.com

i'm working on Laravel 5.2 with Paypal-SDK sandbox

I get this error during call mainly paypal class

Exception: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.

this is my code:

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

    $inputs = (object) $request->all();
    $items = [];

    for ($i = 1; $i <= $request->input('items_number'); $i++) 
    { 
        $item_name = $request->input("item_name_$i");
        $item_quantity = $request->input("item_quantity_$i");
        $item_price = $request->input("item_price_$i");

        if(empty($item_price) || $item_price == 0){
            return redirect('/');
        }

        $item = new Item();
        $item->setName($item_name)
             ->setQuantity($item_quantity)
             ->setPrice($item_price)
             ->setCurrency('USD');

        $items[] = $item;
    }

    // add item to list
    $item_list = new ItemList();
    $item_list->setItems($items);


    // price of all items together
    $amount = new Amount();
    $amount->setCurrency('USD')
           ->setTotal($request->input('total_price'));

    // transaction
    $transaction = new Transaction();
    $transaction->setAmount($amount)
                ->setItemList($item_list)
                ->setDescription('This is just a demo transaction.');

    $redirect_urls = new RedirectUrls();
    $redirect_urls->setReturnUrl(URL::to('/paypal-payment/done'))
                  ->setCancelUrl(URL::to('/paypal-payment/cancel'));

    $payment = new Payment();
    $payment->setIntent('sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirect_urls)
            ->setTransactions(array($transaction));

    try {
        $payment->create($this->_api_context);
    } catch (\PayPal\Exception\PayPalConnectionException $ex) {
        if (config('app.debug')) {
            echo "Exception: " . $ex->getMessage() . PHP_EOL;
            $err_data = json_decode($ex->getData(), true);
            exit;
        } else {
            die('Some error occur, Sorry for inconvenient');
        }
    }

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

    /* here you could already add a database entry that a person started buying stuff (not finished of course) */
    Session::put('paypal_payment_id', $payment->getId());

    if(isset($redirect_url)) {
        // redirect to paypal
        return redirect()->away($redirect_url);
    }

    return 'This is some error';

How can i solve this?



via Ahmed Sk

Advertisement