编辑:我已经解决了原来的问题,并在我的回答中展示了一个 metor 示例。
尝试在 Meteor 中为我的 PayPal API 应用程序获取 token 时出现错误 500:
token = EJSON.stringify(Meteor.http.call "POST", "https://api.sandbox.paypal.com/v1/oauth2/token",
headers:
"Accept": "application/json"
"Accept-Language": "en_US"
auth: "user:pass"
params:
"grant_type":"client_credentials"
);
console.log("Token: "+token);
这段代码的输出:
Token: {"statusCode":500,"headers":{"server":"Apache-Coyote/1.1","date":"Fri, 15 Mar 2013 05:04:43 GMT","content-length":"0","connection":"close"},"data":null,"error":{}}
显然 PayPal 向我返回错误 500。我不知道是什么原因造成的。当然 Auth 是实际数据,而不是 user:pass。
为什么我会收到错误 500?
编辑:编译的 Javascript var token ;
token = EJSON.stringify(Meteor.http.call("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", {
headers: {
"Accept": "application/json",
"Accept-Language": "en_US"
},
auth: "user:pass",
params: {
"grant_type": "client_credentials"
}
}));
console.log("Token: " + token);
最佳答案
下面是一个示例实现使用 meteor 调用 paypal API。
在您的程序启动时,获取您的 token 。始终将 clientid
和 clientsecret
替换为您自己的。
token = EJSON.parse(Meteor.http.post("https://api.sandbox.paypal.com/v1/oauth2/token",
headers:
"Accept": "application/json"
"Accept-Language":"en_US"
auth: "clientid:clientsecret"
params:
"grant_type":"client_credentials"
#encoding: "base64"
).content).access_token;
现在,创建一个付款,显示在此处的 Meteor.methods
方法中(并返回一个 URL 供客户端访问):
buySingleItem: () ->
console.log "Starting new payment, user id: "+Meteor.userId()
result = Meteor.http.post("https://api.sandbox.paypal.com/v1/payments/payment",
headers:
"Authorization":"Bearer "+token
"Content-Type": "application/json"
data:
{
"intent":"sale"
"redirect_urls":
"return_url":"http://mysite.herokuapp.com/done",
"cancel_url":"http://mysite.herokuapp.com/cancel"
"payer":
"payment_method":"paypal"
"transactions":[
{
"amount":
"total":"3.00",
"currency":"USD"
"description":"My item description."
}
]
}
)
payment = result.data
console.log "PayPal redirect: "+payment.links[1].href
return payment.links[1].href
这将在 Meteor 中创建一个 PayPal 结账式支付。
关于Meteor PayPal 支付(使用 Meteor.http),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15425189/