当前位置 : 主页 > 网页制作 > HTTP/TCP >

Meteor PayPal付款(使用Meteor.http)

来源:互联网 收集:自由互联 发布时间:2021-06-16
编辑:我修复了我原来的问题,并在我的回答中显示了一个metor示例. 尝试在Meteor中获取PayPal API应用程序的令牌时,我收到错误500: token = EJSON.stringify(Meteor.http.call "POST", "https://api.sandbox.
编辑:我修复了我原来的问题,并在我的回答中显示了一个metor示例.

尝试在Meteor中获取PayPal API应用程序的令牌时,我收到错误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调用的示例实现.

在程序启动时,获取令牌.始终用自己的替换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结账样式付款.

网友评论