Ive been searching for an answer for my error but still no luck, hoping for someone that can help me here.
So I have a website with two paypal SDK REST API SDK, two paypal account with two different API context. When the user confirm the payment, its not redirecting to my webiste but it got this error.
PayPalConnectionException in PayPalHttpConnection.php line 183:
Got Http response code 403 when accessing https://api.sandbox.paypal.com/v1/payments/payment/PAY-21W53712D7067391CLA35ZEA/execute.
So here is my route.php
// for local
Route::post('payment', array(
'as' => 'payment',
'uses' => 'IndexController@postPayment',
));
// this is after make the payment, PayPal redirect back to your site
Route::get('payment/status', array(
'as' => 'payment.status',
'uses' => 'IndexController@getPaymentStatus',
));
//for international
Route::post('international', array(
'as' => 'payment',
'uses' => 'IntIndexController@postPayment',
));
Route::get('international/status', array(
'as' => 'payment.status',
'uses' => 'IntIndexController@getPaymentStatus',
));
My IndexController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
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;
class IndexController extends Controller
{
private $_api_context;
public function __construct()
{
// setup PayPal api context
$this->_api_context = new ApiContext(
new OAuthTokenCredential(
'....',
'....'
)
);
$this->_api_context->setConfig(['mode' => 'sandbox']);
}
// local paypal
public function postPayment()
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$price = Input::get('value');
if($price == 'starter'){
$price = 465;
$item_1 = new Item();
$item_1->setName('STARTER PLAN') // item name
->setCurrency('USD')
->setQuantity(1)
->setPrice($price); // unit price
}
elseif($price == 'silver'){
$price = 700;
$item_1 = new Item();
$item_1->setName('SILVER PLAN') // item name
->setCurrency('USD')
->setQuantity(1)
->setPrice($price); // unit price
}
else{
$price = 1300;
$item_1 = new Item();
$item_1->setName('GOLD PLAN') // item name
->setCurrency('USD')
->setQuantity(1)
->setPrice($price); // unit price
}
// add item to list
$item_list = new ItemList();
// $item_list->setItems(array($item_1, $item_2, $item_3));
$item_list->setItems(array($item_1));
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($price);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Your transaction description');
$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('Some error occur, sorry for inconvenient');
}
}
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
// add payment ID to session
\Session::put('paypal_payment_id', $payment->getId());
\Session::put('value_price', $price);
if(isset($redirect_url)) {
// redirect to paypal
return Redirect::away($redirect_url);
}
return redirect('paypal');
}
public function getPaymentStatus()
{
// Get the payment ID before session clear
$payment_id = \Session::get('paypal_payment_id');
// clear the session payment ID
\Session::forget('paypal_payment_id');
if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
return redirect('failed');
}
$payment = Payment::get($payment_id, $this->_api_context);
// PaymentExecution object includes information necessary
// to execute a PayPal account payment.
// The payer_id is added to the request query parameters
// when the user is redirected from paypal back to your site
$execution = new PaymentExecution();
$execution->setPayerId(Input::get('PayerID'));
//Execute the payment
$result = $payment->execute($execution, $this->_api_context);
//echo '<pre>';print_r($result);echo '</pre>';exit; // DEBUG RESULT, remove it later
if ($result->getState() == 'approved') { // payment made
return redirect('success');
}else{
return redirect('failed');
}
}
}
My IntIndexController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
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;
class IntIndexController extends Controller
{
private $_api_context;
public function __construct()
{
// setup PayPal api context
$this->_api_context = new ApiContext(
new OAuthTokenCredential(
'...',
'...'
)
);
$this->_api_context->setConfig(['mode' => 'sandbox']);
}
// local paypal
public function postPayment()
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$price = Input::get('value');
if($price == 'starter'){
$price = 465;
$item_1 = new Item();
$item_1->setName('STARTER PLAN') // item name
->setCurrency('USD')
->setQuantity(1)
->setPrice($price); // unit price
}
elseif($price == 'silver'){
$price = 700;
$item_1 = new Item();
$item_1->setName('SILVER PLAN') // item name
->setCurrency('USD')
->setQuantity(1)
->setPrice($price); // unit price
}
else{
$price = 1300;
$item_1 = new Item();
$item_1->setName('GOLD PLAN') // item name
->setCurrency('USD')
->setQuantity(1)
->setPrice($price); // unit price
}
// add item to list
$item_list = new ItemList();
// $item_list->setItems(array($item_1, $item_2, $item_3));
$item_list->setItems(array($item_1));
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($price);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Your transaction description');
$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('Some error occur, sorry for inconvenient');
}
}
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
// add payment ID to session
\Session::put('paypal_payment_id', $payment->getId());
\Session::put('value_price', $price);
if(isset($redirect_url)) {
// redirect to paypal
return Redirect::away($redirect_url);
}
return redirect('paypal');
}
public function getPaymentStatus()
{
// Get the payment ID before session clear
$payment_id = \Session::get('paypal_payment_id');
// clear the session payment ID
\Session::forget('paypal_payment_id');
if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
return redirect('failed');
}
$payment = Payment::get($payment_id, $this->_api_context);
// PaymentExecution object includes information necessary
// to execute a PayPal account payment.
// The payer_id is added to the request query parameters
// when the user is redirected from paypal back to your site
$execution = new PaymentExecution();
$execution->setPayerId(Input::get('PayerID'));
//Execute the payment
$result = $payment->execute($execution, $this->_api_context);
//echo '<pre>';print_r($result);echo '</pre>';exit; // DEBUG RESULT, remove it later
if ($result->getState() == 'approved') { // payment made
return redirect('success');
}else{
return redirect('failed');
}
}
}
My paypal.php view
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4" style="margin-top: 20px;">
{!! Form::open(array('url'=>'payment','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('value', 'starter') }}
<div class="row" style="background-color: #423E3D; margin-left: 0px; margin-right: 0px;">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="color : #000000;">
<h2 style="color: #FFFFFF; text-align:center;">STARTER PLAN</h2>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="color : #000000;">
<h2 style="color: #FFFFFF; text-align:center; margin-top:0px;">USD 465</h2>
</div>
<center>
<div class="col-md-12">
<div style="margin-top:15px;" class="form-group">
{!! Form::submit('Buy Now',
array('class'=>'btn btn-primary')) !!}
</div>
</div>
</center>
</div>
{!! Form::close() !!}
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4" style="margin-top: 20px;">
{!! Form::open(array('url'=>'payment','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('value', 'silver') }}
<div class="row" style="background-color: #423E3D; margin-left: 0px; margin-right: 0px;">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="color : #000000;">
<h2 style="color: #FFFFFF; text-align:center;">SILVER PLAN</h2>
</div>
<center>
<div style="margin-top:15px;" class="col-md-12">
<div class="form-group">
{!! Form::submit('Buy Now',
array('class'=>'btn btn-primary')) !!}
</div>
</div>
</center>
</div>
{!! Form::close() !!}
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4" style="margin-top: 20px;">
{!! Form::open(array('url'=>'payment','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('value', 'gold') }}
<div class="row" style="background-color: #423E3D; margin-left: 0px; margin-right: 0px;">
<center>
<div style="margin-top:15px;" class="col-md-12">
<div class="form-group">
{!! Form::submit('Buy Now',
array('class'=>'btn btn-primary')) !!}
</div>
</div>
</center>
</div>
{!! Form::close() !!}
</div>
Then my intpaypal.php view
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4" style="margin-top: 20px;">
{!! Form::open(array('url'=>'international','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('value', 'starter') }}
<div class="row" style="background-color: #423E3D; margin-left: 0px; margin-right: 0px;">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="color : #000000;">
<h2 style="color: #FFFFFF; text-align:center;">STARTER PLAN</h2>
</div>
<center>
<div class="col-md-12">
<div style="margin-top:15px;" class="form-group">
{!! Form::submit('Buy Now',
array('class'=>'btn btn-primary')) !!}
</div>
</div>
</center>
</div>
{!! Form::close() !!}
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4" style="margin-top: 20px;">
{!! Form::open(array('url'=>'international','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('value', 'silver') }}
<div class="row" style="background-color: #423E3D; margin-left: 0px; margin-right: 0px;">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="color : #000000;">
<h2 style="color: #FFFFFF; text-align:center;">SILVER PLAN</h2>
</div>
<center>
<div class="col-md-12">
<div style="margin-top:15px;" class="form-group">
{!! Form::submit('Buy Now',
array('class'=>'btn btn-primary')) !!}
</div>
</div>
</center>
</div>
{!! Form::close() !!}
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4" style="margin-top: 20px;">
{!! Form::open(array('url'=>'international','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('value', 'gold') }}
<div class="row" style="background-color: #423E3D; margin-left: 0px; margin-right: 0px;">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="color : #000000;">
<h2 style="color: #FFFFFF; text-align:center;">GOLD PLAN</h2>
</div>
<center>
<div class="col-md-12">
<div style="margin-top:15px;" class="form-group">
{!! Form::submit('Buy Now',
array('class'=>'btn btn-primary')) !!}
</div>
</div>
</center>
</div>
{!! Form::close() !!}
</div>
I think the only problem here is in the route.php but i dont know how to fix it, my culprit is the
Route::get('international/status', array(
'as' => 'payment.status',
'uses' => 'IntIndexController@getPaymentStatus',
));
because i tried removing it then the local account works but the international account is not working, when i Put this back the local account is not working but the international is working.
Can anyone know the solution? Can you help me.