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

时间:2018-07-27 06:00:33

标签: angularjs rest paypal

我正在尝试使用Paypal的documentation for 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"
}

我没有得到这个要求出什么问题。任何脚跟都非常感谢。

1 个答案:

答案 0 :(得分:0)

您需要这样打电话,

  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);
      });