2016-11-25 7 views
0

Ive искал ответ на мою ошибку, но все равно не повезло, надеясь на кого-то, кто может мне помочь.Laravel 5.2 Paypal SDK REST API Код ответа SDK 403

У меня есть веб-сайт с двумя SDK SDK SDK SDK для PayPal, двумя учетными записями в PayPal с двумя различными API-интерфейсами. Когда пользователь подтверждает платеж, он не перенаправляется на мой веб-сайт, но он получил эту ошибку.

PayPalConnectionException in PayPalHttpConnection.php line 183: 
Got Http response code 403 when accessing https://api.sandbox.paypal.com/v1/payments/payment/PAY-21W53712D7067391CLA35ZEA/execute. 

Так вот мой route.php

// for local 
Route::post('payment', array(
    'as' => 'payment', 
    'uses' => '[email protected]', 
)); 


// this is after make the payment, PayPal redirect back to your site 
Route::get('payment/status', array(
    'as' => 'payment.status', 
    'uses' => '[email protected]', 
)); 

//for international 
Route::post('international', array(
    'as' => 'payment', 
    'uses' => '[email protected]', 
)); 

Route::get('international/status', array(
    'as' => 'payment.status', 
    'uses' => '[email protected]', 
)); 

Мои 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'); 
    } 
} 

} 

Мои 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'); 
    } 
} 

} 

Мой взгляд paypal.php

   <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> 

Тогда мой intpaypal.php вид

   <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> 

Я думаю, что единственная проблема здесь заключается в route.php, но я не знаю, как это исправить, мой виновником является

Route::get('international/status', array(
'as' => 'payment.status', 
'uses' => '[email protected]', 
)); 

, потому что я попытался удалить его, тогда локальная учетная запись работает, но международная учетная запись не работает, когда я помещаю ее обратно, локальная учетная запись не работает, но международный работает.

Может ли кто-нибудь знать решение? Вы можете мне помочь.

ответ

0

Прежде всего, вы не должны называть маршруты same.You должны заменить этот маршрут

Route::get('international/status', array('as' => 'payment.status','uses'=> '[email protected]'));

к чему-то вроде

Route::get('international/status', array('as' => 'international_payment.status','uses'=> '[email protected]'));

Затем измените Перенаправление в IntIndexController.php как

$redirect_urls->setReturnUrl(\URL::route('international_payment.status')) 
    ->setCancelUrl(\URL::route('international_payment.status')); 

Я думаю, что это решит проблему.

Смежные вопросы