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
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.