Angular and Stripe,无法使用$ http创建Card令牌,但可以使用CURL [英] Angular and Stripe, Can't create Card token using $http, but can using CURL

查看:53
本文介绍了Angular and Stripe,无法使用$ http创建Card令牌,但可以使用CURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,如果您认为这含糊不清,请原谅我,这是我第一次在Stack Exchange上发帖.

First off, please forgive me if this is considered vague, this is my first time posting to Stack Exchange.

希望这不是一个格式错误的问题,尽管它假定您熟悉CURL,Stripe和Angular,但我确实考虑了一下.

Hopefully, this is not a poorly formed question, I did give it some thought, though it assumes familiarity with CURL, Stripe and Angular.

问题所在:

我试图用Angular.js的$ http重新创建Stripe API的CURL的结果,但是这样做有些麻烦.

I am trying to recreate the results of a CURL to the Stripe API with Angular.js's $http and having some trouble doing so.

使用CURL,我可以如下创建卡令牌:

Using CURL, I am able to create a card token as follows:

curl -X POST https://api.stripe.com/v1/tokens \
   -u MY_TEST_KEY: \
   -d "card[number]"=4242424242424242 \
   -d "card[exp_month]"=12 \
   -d "card[exp_year]"=2017 \
   -d "card[cvc]"=123

这给了我类似"tok_blahblahbcryptnonsense"的

This gives me something like "tok_blahblahbcryptnonsense"

但是,我似乎无法将此CURL转换为Angular $ http函数,并且我返回状态代码400,并显示消息您必须传递完整的卡详细信息才能创建令牌."

However, I cannot seem to translate this CURL into an Angular $http function, and I am getting back a status code 400, with the message "You must pass full card details to create a token."

$http({
    method: 'POST',
    url: 'https://api.stripe.com/v1/tokens',
    headers: {
      'content-type': 'application/json',
      'Authorization': 'Bearer MY_TEST_KEY'
    },
    params: {
       card: {
          "number": '4242424242424242', // I have tried this as
                                       // an integer and string
                                      // Stripe docs say string
          "exp_month": 12,
          "exp_year": 2017,

          "cvc": '123'    // I have tried this as
                         // an integer and string
                        // Stripe docs don't specify but I think string
       }
    }
  }).then(function(success){
      console.log('success ', success)
  }, function(error){
      console.log('error ', error) // gets here, this is where the message is
  })

据我所知,这完全有可能.我需要做的就是为所述卡创建令牌.天色已经晚了,这可能是一个很明显的解决方案,我太累了.

As far as my understanding goes, this is totally possible. All I need is to create a token for said card. It is getting late and it might be a totally obvious solution and I'm too tired.

推荐答案

也许Stripe API也接受JSON,但是您在curl命令中发送的不是JSON.是表格数据.

Maybe the Stripe API also accepts JSON, but what you're sending in your curl command is not JSON. It's form data.

此外, params 用于在查询字符串中传递数据.您想要此数据在POST正文中.

Moreover, params is used to pass data in the query string. You want this data in the POST body.

正确的代码应为:

var myData = {
  card: {
    "number": '4242424242424242', 
    "exp_month": 12,
    "exp_year": 2017,
    "cvc": '123'
  }
};
$http({
  method: 'POST',
  url: 'https://api.stripe.com/v1/tokens',
  headers: {
    'Authorization': 'Bearer MY_TEST_KEY',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data: $httpParamSerializerJQLike(myData),
});

这篇关于Angular and Stripe,无法使用$ http创建Card令牌,但可以使用CURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆