PayPalHttpConnection.php第174行中的PayPalConnectionException:
嘿,我正在开发Laravel 5.3,我正在尝试实现PayPal Api,我在网上找到了一个教程,所以我遵循了每一个步骤,但后来我遇到了一个错误,我不知道如何解决它 这是我得到错误的代码:PayPalHttpConnection.php第174行中的PayPalConnectionException:,php,laravel,paypal,Php,Laravel,Paypal,嘿,我正在开发Laravel 5.3,我正在尝试实现PayPal Api,我在网上找到了一个教程,所以我遵循了每一个步骤,但后来我遇到了一个错误,我不知道如何解决它 这是我得到错误的代码: try{ $payment->create($this->_apiContext); } catch (\PayPal\Exception\PayPalConnectionException $ex){ if(\Config::get('app.debug'
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:
访问时获取Http响应代码400
有什么想法吗
代码:
您的请求负载中可能有语法错误您的请求负载中可能有语法错误是的,如果您可以共享,我正在尝试,但我不知道为什么它不允许我发布。您可以编辑您的帖子,并将其添加到其中。我正在查看您的代码,似乎一切都是正确的。奇怪的是你得到了400英镑。如果你确定配置正确,我的配置中应该有什么?是的,如果你可以共享我正在尝试,但我不知道为什么它不允许我发布。你可以编辑你的帖子,然后添加到那里。我正在查看你的代码,一切似乎都是正确的。奇怪的是你得到了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');
}