访问 https://api.sandbox.paypal.com/v1/payments/billing-plans/ 时得到 Http 响应码 403

Posted

技术标签:

【中文标题】访问 https://api.sandbox.paypal.com/v1/payments/billing-plans/ 时得到 Http 响应码 403【英文标题】:Got Http response code 403 when accessing https://api.sandbox.paypal.com/v1/payments/billing-plans/ 【发布时间】:2019-12-04 10:54:24 【问题描述】:

我正在集成贝宝计费 api。但收到 HTTP 403 错误代码。

我调用了 createPlan() 方法。 我在调用 api 时收到 HTTP 403 状态。 我还实现了完美运行的快速结帐,但我不知道它会导致错误。

请查看以下链接以获取有关此通话的更多信息。

<?php
namespace App\Http\Controllers;

use Request as Requests;
use DB;
use Illuminate\Http\Request;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Redirect;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\PaymentDefinition;
use PayPal\Api\Currency;
use PayPal\Api\Plan;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\ChargeModel;



class SubscriptionPlan extends PaypalPaymentController

    public function __construct()
    
        parent::__construct();
    

    /**
     * Create subscription plan
     * @param $planInfo array
     * return $plan object
     */ 

    public function plan($planInfo=array())
    
        $plan = new Plan();
        $plan->setName($planInfo['planName'])
        ->setDescription($planInfo['planDesc'])
        ->setType($planInfo['planType']);

        return $plan;
    

    /**
     * Billing cycle 
     * @param $planInfo array
     * 
     */ 

    public function paymentDefinition($paymentDefination=array())
    
        $paymentDefinition = new PaymentDefinition();
        $paymentDefinition->setName($paymentDefination['name'])
        ->setType($paymentDefination['type'])
        ->setFrequency($paymentDefination['frequency'])
        ->setFrequencyInterval($paymentDefination['interval'])
        ->setCycles($paymentDefination['cycle'])
        ->setAmount(new Currency(array('value' => $paymentDefination['amount'], 'currency' => $paymentDefination['currency'])));

        return $paymentDefinition;
    


    /**
     * Billing cycle 
     * @param $charge deatails array
     * 
     */ 

    public function charge($charge=array())
    
        $chargeModel = new ChargeModel();
        $chargeModel->setType($charge['type'])
                    ->setAmount(new Currency(array('value' => $charge['amount'], 'currency' => $charge['currency'])));
        return array($chargeModel);            
    

    /**
     * Set merchant preferences 
     * @param $merchantPref deatails array
     * 
     */ 

    public function merchantPreferences($merchantPref=array())
    
        $merchantPreferences = new MerchantPreferences();

        $merchantPreferences->setReturnUrl($merchantPref['successUrl'])
        ->setCancelUrl($merchantPref['failUrl'])
        ->setAutoBillAmount($merchantPref['autoBillAmount'])
        ->setInitialFailAmountAction($merchantPref['initialFailAmountAction'])
        ->setMaxFailAttempts($merchantPref['maxFailAttempts'])
        ->setSetupFee(new Currency(array('value' => $merchantPref['amount'], 'currency' => $merchantPref['currency'])));

        return $merchantPreferences;


    

    /**
     * Create subscription Plan
     * @param $palnDetails
     * return
     */ 

    public function createPlan()
    

        $palnDetails['planInfo'] = array('planName'=>"Silver Plan",
                                        'planDesc'=>"Taskenator silver plan ",
                                        'planType'=>"INFINITE"
                                      );

        $palnDetails['paymentDefination'] = array('name'=>'Taskenator Plan Payment',
                                               'type'=>'REGULAR',
                                               'frequency'=>'MONTH',
                                               'interval'=>1,
                                               'cycle'=>0,
                                               'amount'=>10,
                                               'currency'=>'USD'
                                               );
        //$palnDetails['charge']=array();
        $palnDetails['merchantPref']    = array('successUrl'=>'https://www.example.com/subscription/true',
                                          'failUrl'=>'https://www.example.com/subscription/false',
                                          'autoBillAmount'=>'yes',
                                          'initialFailAmountAction'=>'CONTINUE',
                                          'maxFailAttempts'=>0,
                                          'amount'=>0,
                                          'currency'=>'USD'
                                          );



        $plan              = $this->plan($palnDetails['planInfo']); 
        $paymentDefinition = $this->paymentDefinition($palnDetails['paymentDefination']);
        //$charge          = $this->charge($palnDetails['charge']);
        //$paymentDefinition->setChargeModels($charge);

        $merchantPreferences = $this->merchantPreferences($palnDetails['merchantPref']);
        $plan->setPaymentDefinitions(array($paymentDefinition));
        $plan->setMerchantPreferences($merchantPreferences);

        //preDump($this->_api_context,true);

        $request = clone $plan;

        try 
                $output = $plan->create($this->_api_context);
             catch (Exception $ex) 
                ResultPrinter::printError("Created Plan", "Plan", null, $request, $ex);
                exit(1);
            

        ResultPrinter::printResult("Created Plan", "Plan", $output->getId(), $request, $output);

        return $output;    

    

    public function listPlans()
    
        try
            $params = array('page_size' => '10');
            $planList = Plan::all($params, $this->_api_context);
         catch (Exception $ex) 
            ResultPrinter::printError("List of Plans", "Plan", null, $params, $ex);
            exit(1);
        
        ResultPrinter::printResult("List of Plans", "Plan", null, $params, $planList);
        return $planList;

    

    public function testPlan()
    
        $plan = new Plan();
        $plan->setName('T-Shirt of the Month Club Plan')
            ->setDescription('Template creation.')
            ->setType('fixed');

        $paymentDefinition = new PaymentDefinition();

        $paymentDefinition->setName('Regular Payments')
            ->setType('REGULAR')
            ->setFrequency('Month')
            ->setFrequencyInterval("2")
            ->setCycles("12")
            ->setAmount(new Currency(array('value' => 100, 'currency' => 'USD')));

        $chargeModel = new ChargeModel();
        $chargeModel->setType('SHIPPING')
            ->setAmount(new Currency(array('value' => 10, 'currency' => 'USD')));

        $paymentDefinition->setChargeModels(array($chargeModel));

        $merchantPreferences = new MerchantPreferences();
        $baseUrl = "https://www.testtaskenator.com";

        $merchantPreferences->setReturnUrl("$baseUrl/ExecuteAgreement.php?success=true")
            ->setCancelUrl("$baseUrl/ExecuteAgreement.php?success=false")
            ->setAutoBillAmount("yes")
            ->setInitialFailAmountAction("CONTINUE")
            ->setMaxFailAttempts("0")
            ->setSetupFee(new Currency(array('value' => 1, 'currency' => 'USD')));


        $plan->setPaymentDefinitions(array($paymentDefinition));
        $plan->setMerchantPreferences($merchantPreferences);
        //create plan
        try 
                $createdPlan = $plan->create($apiContext);
                $patch = new Patch();
                $value = new PayPalModel('"state":"ACTIVE"');
                $patch->setOp('replace')
                  ->setPath('/')
                  ->setValue($value);
                $patchRequest = new PatchRequest();
                $patchRequest->addPatch($patch);
                $createdPlan->update($patchRequest, $apiContext);
                $plan = Plan::get($createdPlan->getId(), $apiContext);

                // Output plan id
                echo $plan->getId();
             catch (PayPal\Exception\PayPalConnectionException $ex) 
                    echo $ex->getCode();
                    echo $ex->getData();
                    die($ex);
             catch (Exception $ex) 
                    die($ex);
             catch (PayPal\Exception\PayPalConnectionException $ex) 
                  echo $ex->getCode();
                  echo $ex->getData();
                  die($ex);
             catch (Exception $ex) 
                  die($ex);
            
    





<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Redirect;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api;
use Request as Requests;
use DB;

class PaypalPaymentController extends Controller


    public function __construct()
    
        session_start();
        /** PayPal api context **/
        $paypal_conf = \Config::get('paypal');
        $this->_api_context = new ApiContext(new OAuthTokenCredential(
            $paypal_conf['client_id'],
            $paypal_conf['secret'])
        );

        $this->_api_context->setConfig($paypal_conf['settings']);

    
  

【问题讨论】:

403 错误通常意味着您无权执行某项操作。检查您正在使用的 api 密钥是否具有执行此操作的权限/范围。 感谢@Rwd 的回复。但是我可以使用相同的 API 密钥访问另一个 API,并且它可以工作。仅在此 api 调用中专门引发的错误。 仅仅因为您可以访问一个或多个资源并不一定意味着您可以访问所有资源。 developer.paypal.com/docs/api/reference/api-responses/… -- 403 - NOT_AUTHORIZED. Authorization failed due to insufficient permissions. 这可能会为您指明正确的方向:developer.paypal.com/docs/integration/paypal-here/… 谢谢@Rwd 是的,我已经联系了贝宝支持,他们已经为此 API 分配了权限! :) 很高兴你把它整理好了! :) 【参考方案1】:

您没有此 API 的权限

【讨论】:

以上是关于访问 https://api.sandbox.paypal.com/v1/payments/billing-plans/ 时得到 Http 响应码 403的主要内容,如果未能解决你的问题,请参考以下文章

如何设置MSSQL数据库访问用户及访问权限

在SQL数据访问中如何对不同级别设置访问权限

有效访问时间和平均访问时间

一个网站用域名可以访问,用ip不能访问,是为啥?

C盘 位置不可用,无法访问C:\ 拒绝访问

文件拒绝访问无法访问怎么办