使用Jquery AJAX进行Stripe付款? (仅限Javascript) [英] Make a Stripe payment with Jquery AJAX? (Javascript ONLY)

查看:99
本文介绍了使用Jquery AJAX进行Stripe付款? (仅限Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Stripe创建自定义付款表单,我想手动调用Stripe的AJAX。 (而不是提交事件)



然而,首先我很确定我将它发布到错误的地方。但我无法弄清楚我应该向这个帖子请求提供什么网址。



如果我使用的是正确的网址。我得到 405不允许回复。没有关于我的请求有什么问题的信息。



这是我得到的:

  Stripe.setPublishableKey( 'pk_test_12345'); 
Stripe.card.createToken({
number:ccNum,
cvc:ccCVC,
exp_month:ccMonth,
exp_year:ccYear
},stripeResponseHandler) ;

这部分工作正常,给我200 OK状态,我从服务器返回一个令牌。

  function stripeResponseHandler(status,response){
console.log('card status:',status);
console.log('token:',response.id);
$ .ajax({
type:'POST',
url:'https://checkout.stripe.com/checkout.js',
header:{
stripeToken:response.id
},
数据:{
数字:ccNum,
cvc:ccCVC,
exp_month:ccMonth,
exp_year: ccYear
},
成功:(响应)=> {
console.log('成功付款:',回复);
},
错误:(响应)=> {
console.log('错误付款:',响应);
}
})
}

然而,这给了我405 Not Allowed。对我来说,端点是一个.js文件似乎有点奇怪。这就是为什么我假设我的网址错误。



任何人都可以帮我弄清楚如何为Stripe付款提出手动发布请求吗?

解决方案

免责声明:这是有效的,但它是可怕的做法。不要将它用于实际项目。我需要它才能用于前端测试环境。正如本页上的其他用户指出的那样,您应该在后端执行此操作!



我终于找到了一些有用的文档: https://stripe.com/docs/api#create_charge



As我怀疑我使用的网址是错误的。



获取正确的网址后,以下ajax调用有效:



<希望这对其他人也有帮助!大多数答案都是PHP或其他后端语言。

  $ .ajax({
type:'POST',
url:'https://api.stripe.com/v1/charges',
标题:{
授权:'Bearer sk_test_YourSecretKeyHere'
},
数据:{
金额:3000,
货币:'usd',
来源:response.id,
描述:费用为madison.garcia@example.com
},
成功:(响应)=> {
console.log('成功付款:',响应);
},
错误:(响应)=> ; {
console.log('错误付款:',回复);
}
})


I am trying to make a custom payment form for Stripe, and I want to make the AJAX call to Stripe manually. (instead of a submit event)

However, first off I am pretty sure I am posting it to the wrong place. But I can't figure out what URL I'm supposed to make this post request to.

If I am using the right url. I am getting a 405 not allowed response. With no information on what is wrong with my request.

Here's what I got:

Stripe.setPublishableKey('pk_test_12345');
    Stripe.card.createToken({
      number: ccNum,
      cvc: ccCVC,
      exp_month: ccMonth,
      exp_year: ccYear
    }, stripeResponseHandler);

This part works fine, gives me a 200 OK status and I got a token back from the server.

function stripeResponseHandler(status, response) {
      console.log('card status: ', status);
      console.log('token: ', response.id);
      $.ajax({
        type: 'POST',
        url: 'https://checkout.stripe.com/checkout.js',
        headers: {
          stripeToken: response.id
        },
        data: {
          number: ccNum,
          cvc: ccCVC,
          exp_month: ccMonth,
          exp_year: ccYear
        },
        success: (response) => {
          console.log('successful payment: ', response);
        },
        error: (response) => {
          console.log('error payment: ', response);
        }
      })
    }

This however, gives me the 405 Not Allowed. It seems a bit weird to me that the endpoint would be a .js file. Which is why I am assuming I got the wrong URL.

Can anyone help me figure out how to make a manual post request for a Stripe payment?

解决方案

Disclaimer: This works, but it is TERRIBLE practice. Don't use this for a real project. I needed it for a front-end only testing environment. As other users on this page has pointed out, you should be doing this on the backend!

I finally found some useful documentation at: https://stripe.com/docs/api#create_charge

As I suspected the URL I was using was wrong.

after getting the right URL, the following ajax call works:

Hope That helps someone else as well! As most answers, are PHP or other backend languages.

$.ajax({
        type: 'POST',
        url: 'https://api.stripe.com/v1/charges',
        headers: {
          Authorization: 'Bearer sk_test_YourSecretKeyHere'
        },
        data: {
          amount: 3000,
          currency: 'usd',
          source: response.id,
          description: "Charge for madison.garcia@example.com"
        },
        success: (response) => {
          console.log('successful payment: ', response);
        },
        error: (response) => {
          console.log('error payment: ', response);
        }
      })

这篇关于使用Jquery AJAX进行Stripe付款? (仅限Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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