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

时间:2016-11-25 07:10:11

标签: php laravel paypal laravel-5.2

我一直在寻找我的错误的答案,但仍然没有运气,希望有人可以帮助我。

所以我有一个网站,有两个paypal SDK REST API SDK,两个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' => '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',
));

我的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' => 'IntIndexController@getPaymentStatus',
));

因为我试图删除它然后本地帐户工作但国际帐户不起作用,当我把它放回去本地帐户不起作用但国际工作正在运作。

任何人都可以知道解决方案吗?你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

首先,你不应该将路线命名为相同的。你应该替换这条路线

$minCC

类似

$alpha = ["A","B","C","D"]; $arrival = ["1","1","1","4"]; $cpu = ["10","2","2","4"]; $final = array(); //start + finish $inCC = array(); //final cc of inline related to falp $outCC = array(); //final cc of inline2 $falp = array(); // final a $start = 0; $tempc = array(); // => temp for leftc $tempa = array(); // => temp for lefta $leftc = array(); // =>merge inCC & outCC $lefta = array(); // =>merge falp $hashed = mysql_query("SELECT Alpha FROM fcfs", $connection); while($row = mysql_fetch_assoc($hashed)){ $hash[] = $row['Alpha']; } /////test do{ $min = min($arrival); $findmin = mysql_query("SELECT Alpha,CPU FROM fcfs WHERE Arrival='$min'", $connection); while($fm = mysql_fetch_assoc($findmin)){ $ALPHA[] = $fm['Alpha']; $minCC[] = $fm['CPU']; } /*while(count($minCC) > 0){*/ $keys = array_keys($arrival, $min); foreach($keys as $k) { unset($arrival[$k]); } $min2 = min($arrival); $diff = $min2 - $min; if(count($minCC) > 1){ /// else fasfas el min of mincc for($i=0; $i<$diff; $i++){ if($minCC[$i] == 1){ array_push($inCC, $minCC[$i]); array_push($falp, $ALPHA[$i]); unset($minCC[$i]); unset($ALPHA[$i]); unset($hash[$i]); }else{ array_push($inCC, 1); array_push($tempc, ($minCC[$i] - 1)); array_push($tempa, $ALPHA[$i]); unset($minCC[$i]); unset($ALPHA[$i]); unset($hash[$i]); } } }else{ array_push($inCC, $minCC[0]); array_push($falp, $ALPHA[0]); unset($minCC[0]); unset($ALPHA[0]); unset($hash[0]); } }while(count($hash) > 0); <=== think this goes infinite and page stop working /* testing each array */ print_r($hash); echo '<br>'; print_r($tempc); echo '<br>'; print_r($tempa); echo '<br>'; print_r($inCC);

然后更改IntIndexController.php中的重定向URL,如

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

我认为它会解决问题。