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

前端 未结 2 1718
花落未央
花落未央 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:13

    EDIT: This is insecure (and outdated)! You shouldn't send your user's card information directly to your own server. Instead, you should directly send it to Stripe. There's an up-to-date (using intents, etc) example here


    You need to POST to a PHP file in your $.ajax() function:

      $.ajax({
        type: 'POST',
        url: './stripe-payment.php',
        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);
        }
      })
    

    Your PHP should have something like the Stripe PHP bindings require()d to use the Stripe payment API, and that PHP file should look something like this, from this SO question:

     2000, // amount in cents, again
      "currency" => "usd",
      "card" => $tokenid,
      "description" => "payinguser@example.com")
    );
    echo 'success';
    } catch(Stripe_CardError $e) {
      // The card has been declined
        echo $tokenid;
    }
    
    ?>
    

    Refer to that Github's README for more, as well as the Stripe documentation.

提交回复
热议问题