【问题标题】:How to make token based GET request to Paypal REST API using angularjs?如何使用 angularjs 向 Paypal REST API 发出基于令牌的 GET 请求?
【发布时间】:2018-07-27 06:00:33
【问题描述】:

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

我不知道这个请求出了什么问题。非常感谢任何帮助。

【问题讨论】:

    标签: angularjs rest paypal


    【解决方案1】:

    你需要这样调用,

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

    【讨论】:

    • 我只是检查状态而不执行付款。这是我正在尝试进行的 api 调用的链接。它的第三个。 developer.paypal.com/docs/api/overview/#hateoas-links
    • 从第一个 POST 方法我得到状态为“已批准”。我想检查它是否变为“完成”或“待定”状态。
    猜你喜欢
    • 2020-06-17
    • 2013-03-20
    • 2021-07-16
    • 2017-05-05
    • 2021-12-23
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多