简体   繁体   中英

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

I am trying to get payment status through the payment ID using Paypal's documentation for REST API. I have integrated Express Checkout so now I want to see the status of the payment done by the client. To do so as mentioned in the documentation I first get the access token by doing the following POST request:-

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

Now as I get the access token from the above request I make a successive call to the Paypal's REST API by calling the method $scope.validate_payment which is defined like this:-

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

However in the $scope.validate_payment's GET request I am getting an error like this:-

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"
}

I am no getting what's going wrong with this requst. Any heelp is much appreciated.

You need to call like this,

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

SAMPLE CODE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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