2017-08-10 69 views
0

我尝试将PayPal PLUS API集成到我的NativeScript移动应用程序中。当创建付款(调用此API:https://developer.paypal.com/docs/api/payments/#create_payment)与侧面显示的缩短呼叫时,我不断收到500错误。可悲的是我没有找到如何显示它的消息。PayPal REST API尝试创建付款对象时返回500错误

我通过NativeScript在iOS模拟器(Xcode 7)上运行我的代码。

我尝试以下卷曲调用,这工作对我来说:

我试着用卷曲什么:

curl -v -X POST https://api.sandbox.paypal.com/v1/payments/payment \ 
-H "Content-Type:application/json" \ 
-H "Authorization: Bearer <my-token>„ \ 
-d '{ 
    "intent": "sale", 
    "payer": { 
     "payment_method": "paypal" 
    }, 
    "transactions": [ 
    { 
     "amount": { 
     "total": "30.11", 
     "currency": "EUR", 
     "details": { 
      "subtotal": "30.00", 
      "tax": "0.07", 
      "shipping": "0.03", 
      "handling_fee": "1.00", 
      "shipping_discount": "-1.00", 
      "insurance": "0.01" 
     } 
    }, 
    "item_list": { 
     "items": [ 
     { 
      "name": "hat", 
      "description": "Brown hat.", 
      "quantity": "5", 
      "price": "3", 
      "tax": "0.01", 
      "sku": "1", 
      "currency": "EUR" 
     }, 
     { 
      "name": "handbag", 
      "description": "Black handbag.", 
      "quantity": "1", 
      "price": "15", 
      "tax": "0.02", 
      "sku": "product34", 
      "currency": "EUR" 
     } 
    ]} 
    } 
    ], 
    "redirect_urls": { 
    "return_url": "http://www.paypal.com/return", 
    "cancel_url": "http://www.paypal.com/cancel" 
    } 
}' 

响应一个201创建的响应。

现在我创建了一个带有相同元素的TypeScript中的XHR对象。我的代码到目前为止:

var oReq = new XMLHttpRequest();  
    oReq.open("POST", PayPalEndpoints.getPaymentEndpoint, true); 
    oReq.setRequestHeader('Authorization', "Bearer " + access_token); 
    oReq.setRequestHeader('Accept', "application/json"); 
    oReq.onreadystatechange = function() { 
     console.log("state changed - new state: " + oReq.readyState + " and Status: " + oReq.status); 
     if (oReq.readyState === 4) {  
      if (oReq.status === 200) {   
       var response = JSON.parse(oReq.responseText);   
       console.log("response = " + response); 
      } else { 
       console.log("Error: " + oReq.status + " Message: " + oReq.responseText); 
      } 
     }  
    }; 

    oReq.send({ 
     "intent": "sale", 
     "payer": { 
      "payment_method": "paypal" 
     }, 
     "transactions": [ 
     { 
      "amount": { 
       "total": "30.11", 
       "currency": "EUR", 
       "details": { 
       "subtotal": "30.00", 
       "tax": "0.07", 
       "shipping": "0.03", 
       "handling_fee": "1.00", 
       "shipping_discount": "-1.00", 
       "insurance": "0.01" 
      } 
      }, 
      "item_list": { 
       "items": [ 
        { 
         "name": "hat", 
         "description": "Brown hat.", 
         "quantity": "5", 
         "price": "3", 
         "tax": "0.01", 
         "sku": "1", 
         "currency": "EUR" 
        }, 
        { 
         "name": "handbag", 
         "description": "Black handbag.", 
         "quantity": "1", 
         "price": "15", 
         "tax": "0.02", 
         "sku": "product34", 
         "currency": "EUR" 
        } 
       ] 
      } 
     } 
     ], 
     "redirect_urls": { 
      "return_url":"http://www.paypal.com/return", 
      "cancel_url":"http://www.paypal.com/cancel" 
     } 
    }); 

这是(如上所述)不适合我。有人知道,如何显示来自该XHR对象的错误信息?或者,甚至有人甚至可以看到我的代码可能有什么问题。如果你需要的工作(通过XHR)API调用:

var oReq = new XMLHttpRequest();  
    var encodedString = base64.encode(this.authorizationBasic); 
    oReq.open("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", false); 
    oReq.setRequestHeader('Authorization', "Basic " + encodedString); 

    oReq.setRequestHeader('Content-type', "application/x-www-form-urlencoded"); 
    oReq.setRequestHeader('Accept', "application/json"); 
    oReq.setRequestHeader('Accept-Language', "en_US"); 

    oReq.onreadystatechange = function() { 
     console.log("state changed - new state: " + oReq.readyState + " and Status: " + oReq.status); 
     if (oReq.readyState === 4) { 
      if (oReq.status === 200) {   
       var response = JSON.parse(oReq.responseText);   
       OAuthAccessor.access_token = response.access_token; 
       console.log("access_token set"); 
      } else { 
       console.log("Error: " + oReq.status + " Message: " + oReq.responseText); 
      } 
     }  
    }; 

    oReq.send('grant_type=client_credentials'); 

API参考:

集成PDF: https://www.paypalobjects.com/webstatic/de_DE/downloads/PayPal-PLUS-IntegrationGuide.pdf

呼叫参考: https://developer.paypal.com/docs/integration/direct/make-your-first-call/

我缩短呼叫: https://developer.paypal.com/docs/api/payments/#create_payment

回答

0

如果某些一个是做研究上的平等问题:

您需要更改

oReq.setRequestHeader('Accept', "application/json"); 

oReq.setRequestHeader('Grant-Type', "application/json"); 

,你需要在send-方法传递数据作为JSON字符串,请致电:

oReq.send(JSON.stringify(data)); 

“data”是您的JSON字符串。