Make a Stripe payment with Jquery AJAX? (Javascript ONLY)

前端 未结 2 1721
花落未央
花落未央 2021-01-06 19:36

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

2条回答
  •  天涯浪人
    2021-01-06 20:14

    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);
            }
          })
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题