我正在尝试使用 paypal
Rest API 创建付款,但我不断收到此错误响应:
{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"}
这里是我的代码:
payment_url = 'https://api.sandbox.paypal.com/v1/payments/payment'
headers = {"Content-Type": "application/json", "Authorization": "Bearer %s" % access_token}
data = {
"intent": "sale",
"redirect_urls": {
"return_url": "http://localhost:8080/index.html",
"cancel_url": "http://localhost:8080/index.html"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "7.47",
"currency": "USD"
},
"details": {
"subtotal": "7.41",
"tax": "0.03",
"shipping": "0.03"
},
"description": "This is the payment transaction description.",
"item_list": {
"items": [
{
"quantity": "1",
"name": "item",
"price": "7.41",
"currency": "USD",
"sku": "item"
}]
}
}
]
}
print headers
print data
r = requests.post(payment_url, headers=headers, data=data)
print 'payment res', r.text
我只得到这样的回应:
{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"}
我看到了很多关于这种错误的问题,但都没有解决方案。 :( json 格式的 post 数据显然是有效的。否则,请求 post 方法将引发异常。并且响应是从 paypal 服务器返回的,但我无法从它提供的链接中找到任何信息。我检查了 Rest API 文档,我想我提出的请求与样本完全一样。我错过了什么?
如有任何建议或解决方案,我们将不胜感激。
最佳答案
您的return_url 和cancel_url 值需要用引号括起来。如消息所述,您的 JSON 格式不正确。
试试这个 - http://jsonlint.com/查看您的错误。
关于python - Paypal Rest API 不断返回格式错误的 json 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952451/