【问题标题】:PayPalConnectionException in PayPalHttpConnection.php line 174:PayPalHttpConnection.php 第 174 行中的 PayPalConnectionException:
【发布时间】:2017-06-15 21:50:01
【问题描述】:

嘿,我正在开发 Laravel 5.3,我正在尝试实现 PayPal Api,我在网上找到了一个教程,所以我按照每个步骤进行操作,但后来出现错误,我不知道如何解决它.

这是我得到错误的代码:

try{
        $payment->create($this->_apiContext);
    } catch (\PayPal\Exception\PayPalConnectionException $ex){
        if(\Config::get('app.debug')){
            echo 'Exception: ' . $ex->getMessage() . PHP_EOL;
            $err_data = json_decode($ex->getData(), true);
            echo $err_data;/*
            echo $ex->getCode();
            echo $ex->getData();*/
            exit;
        } else {
            die('Ups! Algo salio mal');
        }
    }

这是错误:

PayPalHttpConnection.php 第 174 行中的 PayPalConnectionException: 访问https://api.sandbox.paypal.com/v1/payments/payment/PAY-3SH87897C2313620PLCHWOOY/execute时得到Http响应码400。

有什么想法吗?

代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Foundation\Validation\ValidatesRequests;
use App\Http\Requests;
use App\Deal;
use App\Status;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\ExecutePayment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;
use Paypal;
use Session;

class PaypalController extends Controller
{
    private $_apiContext;

public function __construct()
{
    $paypal_conf = \Config::get('paypal');
    $this->_apiContext = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
    $this->_apiContext->setConfig($paypal_conf['settings']);
    /*$this->_apiContext = Paypal::ApiContext(
        config('services.paypal.client_id'),
        config('services.paypal.secret'));

    $this->_apiContext->setConfig(array(
        'mode' =>'sandbox',
        'service.EndPoint' => 'https://api.sandbox.paypal.com',
        'http.ConnectionTimeOut' => 30,
        'log.LogEnabled' => true,
        'log.FileName' => storage_path('logs/paypal.log'),
        'log.LogLevel' => 'FINE',
    ));*/
}

public function payPremium()
{
    return view('payPremium');
}

public function getCheckout(Request $request)
{
    $payer = Paypal::Payer();
    $payer->setPaymentMethod('paypal');

    $amount = Paypal::Amount();
    $amount->setCurrency('USD');
    $amount->setTotal($request->input('pay'));

    $transaction = Paypal::Transaction();
    $transaction->setAmount($amount);
    $transaction->setDescription('Compra de Plan '.$request->input('type').' Precio por mensualidad '.$request->input('pay'). 'Descripción del plan: '.$request->input('description'));

    $redirectUrls = Paypal::RedirectUrls();
    $redirectUrls->setReturnUrl(route('getDone'));
    $redirectUrls->setCancelUrl(route('getCancel'));

    $payment = Paypal::Payment();
    $payment->setIntent('sale');
    $payment->setPayer($payer);
    $payment->setRedirectUrls($redirectUrls);
    $payment->setTransactions(array($transaction));

    try{
        $payment->create($this->_apiContext);
    } catch (\PayPal\Exception\PayPalConnectionException $ex){
        if(\Config::get('app.debug')){
            echo 'Exception: ' . $ex->getMessage() . PHP_EOL;
            $err_data = json_decode($ex->getData(), true);
            echo $err_data;
            echo $ex->getMessage();
            echo $ex->getCode();
            echo $ex->getData();
            exit(1);
        } else {
            die('Ups! Algo salio mal');
        }
    }

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

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

    if(isset($redirectUrls)){
        return \Redirect::away($redirectUrls);
    }

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

    /*$response = $payment->create($this->_apiContext);*/
    /*$redirectUrl = $response->links[1]->href;*/

    /*$paymentExecution = Paypal::PaymentExecution();
    $id = $request->get('paymentId');
    $token = $request->get('token');
    $payer_id = $request->get('PayerID');

    $payment = Paypal::getById($id, $this->_apiContext);

    $paymentExecution->setPayerId($payer_id);
    $executePayment = $payment->execute($paymentExecution, $this->_apiContext);
    if ($executePayment->getState() == 'approved') {
        dd($executePayment);
        dd($amount);
        $transactions = new Transaction;
        $transactions->payer = $payer_id;
        $transactions->paymentID = $id;
        $transactions->amount = array_get($amount, 'setTotal');
        $transactions->description = array_get($transaction, 'setDescription');
        $transactions->statuses_id = 10;
        $transactions->save();
    }
    else
    {
        dd('Error en su pago');
        $transactions = new Transaction;
        $transactions->payer = $payer_id;
        $transactions->paymentID = $id;
        $transactions->amount = $amount;
        $transactions->description = $description;
        $transactions->statuses_id = 11;
        $transactions->save();
    }*/

    /*return redirect()->to( $redirectUrl );*/
}

public function getDone(Request $request)
{
    $payment_id = \Session::get('paypal_payment_id');
    \Session::forget('paypal_payment_id');

    $payerId = $request->get('paymentId');
    $token = $request->get('PayerID');

    if(empty($payerId) || empty($token)){
        return \Redirect::route('payPremium')->with('message', 'Hubo un problema al intentar pagar con Paypal');
    }

    $payment = Payment::get($payment_id, $this->_apiContext);

    $execution = new PaymentExecution();
    $execution->setPayerId($request->get('paymentId'));

    $result = $payment->execute($execution, $this->_apiContext);

    if ($result->getState() == 'approved') {
        return \Redirect::route('payPremium')->with('message', 'Compra realizada de forma correcta');
    } else {
        return \Redirect::route('payPremium')->with('message', 'La compra fue cancelada');
    }

    // $paymentExecution = Paypal::PaymentExecution();
    // $id = $request->get('paymentId');
    // $token = $request->get('token');
    // $payer_id = $request->get('PayerID');

    // $payment = Paypal::getById($id, $this->_apiContext);

    // $paymentExecution->setPayerId($payer_id);
    // $executePayment = $payment->execute($paymentExecution, $this->_apiContext);
    // if ($executePayment->getState() == 'approved') {
    //  /*dd($executePayment);*/
 //        dd($amount);
 //        $transactions = new Transaction;
 //        $transactions->payer = $payer_id;
 //        $transactions->paymentID = $id;
 //        $transactions->amount = $price;
 //        $transactions->description = $description;
 //        $transactions->statuses_id = 10;
 //        $transactions->save();
    // }
    // else
    // {
    //  dd('Error en su pago');
 //        $transactions = new Transaction;
 //        $transactions->payer = $payer_id;
 //        $transactions->paymentID = $id;
 //        $transactions->amount = $amount;
 //        $transactions->description = $description;
 //        $transactions->statuses_id = 11;
 //        $transactions->save();
    // }
}

public function getCancel()
{
    return redirect()->route('payPremium');
}

}

【问题讨论】:

    标签: php laravel paypal


    【解决方案1】:

    您的请求负载中可能存在语法错误

    【讨论】:

    • 是的,如果你可以分享的话
    • 我正在尝试,但我不知道为什么它不让我发布它。
    • 您可以编辑您的帖子并将其添加到那里
    • 我正在查看您的代码,一切似乎都是正确的。奇怪的是你得到了 400。如果你确定配置是正确的
    • 我的配置中应该有什么?
    猜你喜欢
    • 2017-12-26
    • 2021-06-08
    • 1970-01-01
    • 2019-08-17
    • 2020-10-25
    • 2014-02-12
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多