繁体   English   中英

如何使用angularjs向Paypal REST API发出基于令牌的GET请求?

[英]How to make token based GET request to Paypal REST API using angularjs?

我正在尝试使用Paypal的REST API 文档通过付款ID获取付款状态。 我已经集成了Express Checkout,所以现在我想查看客户付款的状态。 为此,如文档中所述,我首先通过执行以下POST请求获取访问令牌:-

var basicAuthString = btoa('CLIENTID:SECRET');
$http({
    method: 'POST',
    url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ' + basicAuthString,
    },
    data: 'grant_type=client_credentials'
}).then(successCallback, errorCallback);
function successCallback(response){
    console.log(response.data);
    console.log(response.data.access_token);
    console.log(response.data.token_type);
    $scope.access_token = response.data.access_token;
    $scope.token_type = response.data.token_type;
    $scope.validate_payment();
};
function errorCallback(error){
    console.log(error);
};

现在,当我从上述请求中获取访问令牌时,我将通过调用方法$scope.validate_payment进行对Paypal REST API的连续调用,该方法的定义如下:-

$scope.validate_payment = function(){
    console.log("Validating Payment");
    console.log($scope.paymentId);
    console.log($scope.access_token);
    console.log($scope.token_type);
    $http({
        method: 'GET',
        url: 'https://api.sandbox.paypal.com/v1/payments/' + $scope.paymentId,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': $scope.token_type + ' ' + $scope.access_token,
        }, 
    }).then(successCallback, errorCallback);
    function successCallback(response){
        console.log("Payment Successful");
    };
    function errorCallback(error){
        console.log(error);
    };
}

但是在$scope.validate_payment's GET请求中,我得到这样的错误:

data:
{
    debug_id: "210153acc46b3"
    information_link: "https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST"
    message: "The requested resource was not found"
    name: "MALFORMED_REQUEST"
}

我没有得到这个要求出了什么问题。 任何追随感激。

你需要这样打

  var reqURL = 'https://api.sandbox.paypal.com/v1/payments/payment/'+$scope.paymentId+'/execute';

样本代码

var reqURL = 'https://api.sandbox.paypal.com/v1/payments/payment/'+$scope.paymentId+'/execute';
      var capture = new PaymentCaptureService({
        'headers': {
          'authorization': Authentication.paypal,
          'Content-Type': 'application/json',
        },
        'data' : {
          'transactions': [{
            'amount': {
              'currency': 'USD',
              'total': user.bidTotal.toFixed(2)
            }
          }],
          'payer_id': payerID,
        },
        'url': reqURL });

      console.log(capture);

      capture.then(function(response) {
        console.log('response from payment capture request:', response);
      });

暂无
暂无

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

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