Laravel PayPal payment return 400

2019-09-19 13:33发布

I am using PayPal paypal/rest-api-sdk-php for payment gateway for my laravel application.

But when the link is click for confirmation, I get Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.

Following is my method of implementation in Laravel 5.1 and AngularJS:

angular.module('MyApp')
    .factory('Account', function($http){
        return {

            buyNow: function(pid){
                console.log(pid)
                return $http.put('/api/buynow',pid);
            }

        }   
    });

     public function buyNowAPI(Request $request)
        {
            $pid = $request->input('pid');
            $product = Product::find($pid);
            $baseUrl = url().'/#/app/product?orderId=$pid';     
            $payment = $this->makePaymentUsingPayPal($product->price,'MYR',$product->title,"$baseUrl&success=true", "$baseUrl&success=false");

            dd($payment->getLinks());
            return response()->json($pid);
        }

    public function makePaymentUsingPayPal($total, $currency, $paymentDesc, $returnUrl, $cancelUrl)
        {
            $payer = new Payer();
            $payer->setPaymentMethod("paypal");

            // Specify the payment amount.
            $amount = new Amount();
            $amount->setCurrency($currency);
            $amount->setTotal($total);

            $transaction = new Transaction();
            $transaction->setAmount($amount);
            $transaction->setDescription($paymentDesc);

            $redirectUrls = new RedirectUrls();
            $redirectUrls->setReturnUrl($returnUrl);
            $redirectUrls->setCancelUrl($cancelUrl);

            $payment = new Payment();
            $payment->setRedirectUrls($redirectUrls);
            $payment->setIntent("sale");
            $payment->setPayer($payer);
            $payment->setTransactions(array($transaction));
            $payment->create($this->getApiContext());
            return $payment;
        }

The laravel code implementation is based on example that available in git for paypal/rest-api-sdk-php based example called The Pizza App rest-api-sample-app . Thanks!!

1条回答
看我几分像从前
2楼-- · 2019-09-19 13:50

You need to follow the instructions mentioned here:

https://github.com/paypal/PayPal-PHP-SDK/wiki/exception-%27PayPal%5CException%5CPayPalConnectionException%27-with-message-%27Got-Http-response-code-400-when-accessing

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);
}
查看更多
登录 后发表回答