简体   繁体   English

Laravel 5.2 Paypal SDK REST API SDK响应代码403

[英]Laravel 5.2 Paypal SDK REST API SDK response code 403

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. 因此,我有一个网站,其中包含两个paypal SDK REST API SDK,两个具有两个不同API上下文的paypal帐户。 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 所以这是我的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 我的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 我的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 我的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>

Then my intpaypal.php view 然后我的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>

I think the only problem here is in the route.php but i dont know how to fix it, my culprit is the 我认为这里唯一的问题是route.php,但我不知道如何解决,我的罪魁祸首是

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. 你能帮助我吗。

First of all you should not name routes same.You should replace this route 首先,您不应将路由命名为相同。您应该替换此路由

Route::get('international/status', array('as' => 'payment.status','uses'=> 'IntIndexController@getPaymentStatus'));

to something like

Route::get('international/status', array('as' => 'international_payment.status','uses'=> 'IntIndexController@getPaymentStatus'));

Then change the Redirect Urls in IntIndexController.php like 然后像这样在IntIndexController.php中更改Redirect Urls

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

I think it will solve the problem. 我认为它将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM