Got Http response code 400. Problem with Amount. Error using PAYPAL API

0

is the first time I try to make a page implementing paypal and it is giving me problems, I use Laravel too.

This is the function that gives me the error

private $_api_context;

    public function __construct()
    {

        // setup PayPal api context
        $paypal_conf = \Config::get('paypal');
        $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));

        $this->_api_context->setConfig($paypal_conf['settings']);
    }

    public function postPayment()
    {
          $payer = new Payer();
         $payer->setPaymentMethod('paypal');
         $elementos = array();
         $subtotal = 0;
         $carrito = \Session::get('carrito');
         $monedaActual = 'EUR';

         foreach ($carrito as $producto):

            $Item = new item();
            $Item->setName($producto->nombreproducto)
                        ->setCurrency($monedaActual)
                        ->setDescription($producto->descbreve)
                        ->setQuantity($producto->cantidad)
                        ->setPrice($producto->precio);
                 $elementos[] = $Item;
                  $subtotal += $producto->cantidad + $producto->precio;     
         endforeach;



        $listaElementos = new ItemList();
        $listaElementos->setItems($elementos);

        $detalles = new Details();
        $detalles->setSubtotal($subtotal)
                     ->setShipping(100);

        $total = $subtotal + 100;

        $amount = new Amount();
        $amount->setCurrency($monedaActual)
                   ->setTotal($total)
                   ->setDetails($detalles);

        $transaccion = new Transaction();
        $transaccion->setAmount($amount)
                    ->setItemList($listaElementos)
                    ->setDescription('Pedido de prueba de la aplicacion Laravel ');

      $urlsRedirecionadas  = new RedirectUrls();
      $urlsRedirecionadas->setReturnUrl(\URL::to('payment/status'))
                          ->setCancelUrl(\URL::to('payment/status'));

      $Pago = new Payment();
      $Pago->setIntent('sale')
            ->setPayer($payer)
            ->setRedirectUrls( $urlsRedirecionadas)
            ->setTransactions(array($transaccion));


      try
       {
          $Pago->create($this->_api_context);
        } catch (\PayPal\Exception\PayPalConnectionException $ex)
       {
          if (\Config::get('app.debug')) :
               echo 'Excepcion '.$ex->getMessage().PHP_EOL;
                $err_data = json_decode($ex->getData(), true);



                exit;
           else :
              die('Algo ha fallado, compruebe si estan correctos todos los pasos');
           endif;


  return \Redirect::away('/carrito')
                        ->with('mensaje','Hay un error desconocido');




    }

The error of the paypal log is this

[23-08-2016 10:26:21] PayPal\Core\PayPalHttpConnection : ERROR: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. {"name":"VALIDATION_ERROR","details":[{"field":"transactions[0]","issue":"Item amount must add up to specified amount subtotal (or total if amount details not specified)"}],"message":"Invalid request - see details","information_link":"https://developer.paypal.com/docs/api/#VALIDATION_ERROR","debug_id":"c486cbbedfec1"}

and the debug of the variable $ Payment in json is this

"{"intent":"sale","payer":{"payment_method":"paypal"},"redirect_urls":{"return_url":"http://localhost/HTDOCS_PHP/laravel_tienda_overfiso/public/payment/status","cancel_url":"http://localhost/HTDOCS_PHP/laravel_tienda_overfiso/public/payment/status"},"transactions":[{"amount":{"currency":"EUR","total":"601.28","details":{"subtotal":"501.28","shipping":"100"}},"item_list":{"items":[{"name":"perico_2","currency":"EUR","description":"mensaje descripcion 2","quantity":1,"price":"500.28"}]},"description":"Pedido de prueba de la aplicacion Laravel "}]}"
    
asked by KurodoAkabane 24.08.2016 в 11:22
source

1 answer

1

The error message makes it clear, the problem is with the API key, check that you are using the correct API key, for tests must be the sandbox.

    
answered by 27.08.2016 в 17:44