使用 Jquery AJAX 进行 Stripe 付款? (仅 Javascript)
Make a Stripe payment with Jquery AJAX? (Javascript ONLY)
我正在尝试为 Stripe 制作自定义付款表格,我想手动对 Stripe 进行 AJAX 调用。 (而不是提交事件)
但是,首先我很确定我post把它放到了错误的地方。但是我不知道 URL 我应该向什么人发出这个 post 请求。
如果我用对了url。我收到 405 not allowed
回复。没有关于我的请求有什么问题的信息。
这是我得到的:
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',
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);
}
})
}
然而,这给了我 405 Not Allowed。端点是一个 .js 文件对我来说似乎有点奇怪。这就是为什么我假设我弄错了 URL.
谁能帮我弄清楚如何手动 post 请求 Stripe 付款?
编辑:这是不安全的(且已过时)!您不应将用户的卡信息直接发送到您自己的服务器。相反,您应该直接将其发送到 Stripe。有一个最新的(使用意图等)示例 here
您需要在 $.ajax()
函数中 POST
到 PHP 文件:
$.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);
}
})
您的 PHP 应该有类似于 Stripe PHP bindings require()
d to use the Stripe payment API, and that PHP file should look something like this, from this SO question:
<?php
require_once('Stripe.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://manage.stripe.com/account
Stripe::setApiKey("sk_test_APIKEYREDACTED");
// Get the credit card details submitted by the form
$token = json_decode($_POST['chargeData']);
$tokenid = $token['id'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = Stripe_Charge::create(array(
"amount" => 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;
}
?>
请参阅 Github 的自述文件以及 Stripe documentation。
免责声明:这有效,但这是一种糟糕的做法。不要将其用于实际项目。我需要它用于仅前端测试环境。正如此页面上的其他用户所指出的,您应该在后端执行此操作!
我终于在以下位置找到了一些有用的文档:https://stripe.com/docs/api#create_charge
因为我怀疑我使用的 URL 是错误的。
获得正确的 URL 后,以下 ajax 调用有效:
希望这对其他人也有帮助!作为大多数答案,是 PHP 或其他后端语言。
$.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);
}
})
我正在尝试为 Stripe 制作自定义付款表格,我想手动对 Stripe 进行 AJAX 调用。 (而不是提交事件)
但是,首先我很确定我post把它放到了错误的地方。但是我不知道 URL 我应该向什么人发出这个 post 请求。
如果我用对了url。我收到 405 not allowed
回复。没有关于我的请求有什么问题的信息。
这是我得到的:
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',
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);
}
})
}
然而,这给了我 405 Not Allowed。端点是一个 .js 文件对我来说似乎有点奇怪。这就是为什么我假设我弄错了 URL.
谁能帮我弄清楚如何手动 post 请求 Stripe 付款?
编辑:这是不安全的(且已过时)!您不应将用户的卡信息直接发送到您自己的服务器。相反,您应该直接将其发送到 Stripe。有一个最新的(使用意图等)示例 here
您需要在 $.ajax()
函数中 POST
到 PHP 文件:
$.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);
}
})
您的 PHP 应该有类似于 Stripe PHP bindings require()
d to use the Stripe payment API, and that PHP file should look something like this, from this SO question:
<?php
require_once('Stripe.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://manage.stripe.com/account
Stripe::setApiKey("sk_test_APIKEYREDACTED");
// Get the credit card details submitted by the form
$token = json_decode($_POST['chargeData']);
$tokenid = $token['id'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = Stripe_Charge::create(array(
"amount" => 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;
}
?>
请参阅 Github 的自述文件以及 Stripe documentation。
免责声明:这有效,但这是一种糟糕的做法。不要将其用于实际项目。我需要它用于仅前端测试环境。正如此页面上的其他用户所指出的,您应该在后端执行此操作!
我终于在以下位置找到了一些有用的文档:https://stripe.com/docs/api#create_charge
因为我怀疑我使用的 URL 是错误的。
获得正确的 URL 后,以下 ajax 调用有效:
希望这对其他人也有帮助!作为大多数答案,是 PHP 或其他后端语言。
$.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);
}
})