vendredi 3 juin 2016

Laravel 5.1 - Error payment Paypal Api

i have this error with method payment paypal.

Error:

PayPalConnectionException in PayPalHttpConnection.php line 178: Got Http response code 400 when accessing http://ift.tt/1f12BzJ.

My PaypalController

  • work well without insert my coupon price to array

My PaypalController

public function postPayment(Request $request)
    {


        // i deleted validation form
        // i deleted how i get price shipping 
        // $cart is my session with all products
        // i take my price coupon from my DB and not from my session 


        $cart = $request->session()->get('cart');
        $payer = new Payer();
        $payer->setPaymentMethod('paypal');

        $items = array();
        $subtotal = 0 ;
        $cart = \Session::get('cart'); //ottenere tutta l'informazione dalla session cart
        $currency = 'EUR';

        foreach($cart as $producto){
            $item = new Item();
            $item->setName($producto->name)
            ->setCurrency($currency)
            ->setDescription($producto->extract)
            ->setQuantity($producto->quantity)
            ->setPrice($producto->price);

            $items[] = $item;
            $subtotal += $producto->quantity * $producto->price;
        }

        $item_list = new ItemList();
        $item_list->setItems($items);
        $details = new Details();
        $details->setSubtotal($subtotal)
        ->setShipping($get_ship_total);

        // I need insert my price coupon discount ( it can be 0 or >0)

        $total = $subtotal + $get_ship_total - $price_coupon;

        $amount = new Amount();
        $amount->setCurrency($currency)
            ->setTotal($total)
            ->setDetails($details);

        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($item_list)
            ->setDescription('Order ecommerce');

        $redirect_urls = new RedirectUrls();
        $redirect_urls->setReturnUrl(\URL::route('payment.status'))
            ->setCancelUrl(\URL::route('payment.status'));

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

        try {
            $payment->create($this->_api_context);
        } catch (\PayPal\Exception\PPConnectionException $ex) {
            if (\Config::get('app.debug')) {
                echo "Exception: " . $ex->getMessage() . PHP_EOL;
                $err_data = json_decode($ex->getData(), true);
                exit;
            } else {
                die('Ups! error');
            }
        }


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

        \Session::put('paypal_payment_id', $payment->getId());

        if(isset($redirect_url)) {

            return \Redirect::away($redirect_url);
        }

        return \Redirect::route('cart-show')
            ->with('message', 'Ups! Error .');

        }

    } // fine metodo

I tryed to do some test: - Paypal work well If i delete $price_coupon from:

$total = $subtotal + $get_ship_total - $price_coupon;

Maybe is a issue with array, i read a question like this but i did not fix this issue.

Maybe because subTotal + taxshipping - coupon does not add up to the total correctly!

Thank you for your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire