Meteor PayPal付款(使用Meteor.http)

时间:2013-03-15 05:11:05

标签: meteor paypal

编辑:我修复了原来的问题并在答案中显示了一个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);

3 个答案:

答案 0 :(得分:7)

以下是使用meteor进行paypal API调用的示例实现

在程序启动时,获取令牌。始终将clientidclientsecret替换为您自己的。

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方法(并返回客户端要访问的网址):

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结账样式付款。

答案 1 :(得分:0)

我会提供示例代码,但我不熟悉Meteor。

基本上你有2个问题:

在您的标头中

,您没有传递客户端ID或客户端密码。这应该是:

Authorization: Basic clientid:clientsecret

此外,在您的请求中,您的请求应如下所示:     RESPONSE_TYPE =令牌安培; grant_type = client_credentials

看起来你的json然后把它串起来,所以无论你需要什么样的方式来获取POST请求我只是把它放在那里,一旦你得到它,你应该是好的。

[编辑] PayPal的doc没有你base64编码客户端id或秘密[/ edit]

答案 2 :(得分:0)

然后,当您需要执行付款时,您可以执行以下操作。 See the whole payment process here

Meteor.methods
  'executePaypalPayment': (payerId) ->
    payment = PaypalPayments.findOne({ userId: @userId },
      { sort: { 'create_time': -1 } })

    token = Meteor.call 'getPaypalToken'

    url = 'https://api.sandbox.paypal.com/v1/payments/payment/' +
           payment.id + '/execute'

    res = Meteor.http.post url,
      headers:
        Authorization: 'Bearer ' + token.access_token
        'Content-Type': 'application/json'
      data:
        payer_id: payerId

    payment = res.data
    payment['userId'] = @userId

    if payment.state is 'approved' 
      # we insert the sucessful payment here
      PaypalPayments.insert payment

    return if payment.state is 'approved' then true else false