完全坚持使用 Stripe Checkout
Completely stuck with Stripe Checkout
目标:屏幕上有几个按钮,像这样–
[产品 1 – 20 美元]
[产品 2 – 35 美元]
等等...
用户可以 select 任意数量的产品。在 JavaScript 中,我有一个变量 amount
,当用户 select 购买它时,它会增加产品成本。所以流程是这样的:
用户 select 的产品 1 和 2 --> 数量 = 20 + 35 = 55
然后,用户可以按“用卡付款”按钮启动 Stripe Checkout 模式。填写,付款,完成。
问题:现在,我使用的是 Checkout 的自定义按钮版本,因此我可以按照自己的方式设置按钮的样式并将这个变量放入。所以,那个实现非常简单:
var handler = StripeCheckout.configure({
key: 'pk_test_XXXXXX',
image: 'assets/logo-stripe.png',
locale: 'auto',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
$.ajax({
url: 'php/charge.php',
type: 'post',
data: {tokenid: token.id, email: token.email, amount: amount},
success: function(data) {
if (data == 'success') {
console.log("Card successfully charged!");
}
else if (data == 'failure') {
console.log("Card error");
} else {
console.log("Other error");
}
},
error: function(data) {
console.log("AJAX error!");
console.log(data);
}
});
}
});
$('#pay-button').on('click', function(e) {
if (amount == 0) {
$('#pay-button').addClass('animated shake'); // Animates the button and refuses to open the modal if user didn't select any products
$('#pay-button').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', removeAnimation);
} else {
handler.open({
name: 'Company, Inc.',
description: "Description",
amount: amount // Here's the variable
});
e.preventDefault();
}
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
这就是JavaScript逻辑。我省略了对 checkout.js 的调用,因为这对这个问题没有任何影响。
从前端的角度来看,这是极好的。现在是令牌化和实际为卡充电。这是事情开始出现问题的地方。如果您查看令牌处理,我正在尝试 POST 将电子邮件、金额和令牌数据发送到 charge.php。基于我没有从控制台收到任何关于此的错误这一事实,我认为这是可行的。
在 charge.php 中,我有这个:(4 月 20 日更新:这是工作代码)
<?php
require_once('stripe-php/init.php');
// Set secret key
\Stripe\Stripe::setApiKey("sk_test_XXXXXXX");
// Get the credit card details submitted by the form
$token = $_POST['tokenid'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$customer = \Stripe\Customer::create(array(
'email' => $_POST['email'],
'source' => $token
));
$charge = \Stripe\Charge::create(array(
"amount" => $_POST['amount'],
"currency" => "usd",
"customer" => $customer->id
));
echo "success";
} catch(\Stripe\Error\Card $e) {
// The card has been declined
echo "failure";
}
?>
每当我尝试购买时,日志都会给我 "Success Error." 我大致遵循此处提供的代码 – How to create a stripe charge with a checkout token sent via AJAX to php。所以,我不知道 Success Error 是什么,但我的猜测是数据发送到 charge.php 没问题,但卡没有收费。我的 Stripe 仪表板没有显示任何付款记录这一事实支持了这一点。
我试过的:
1) 我认为错误可能是我的 config.php。我没有使用 Composer(因为我不知道那是什么,甚至不知道如何开始使用它),所以我从 stripe-php 调用 init.php。根据 Stripe 自己的文档,这是一个可接受的 Composer 替代品。
这是config.php:
<?php
require_once('php/stripe-php/init.php');
$stripe = array(
secret_key => getenv('sk_test_XXXXXX'),
publishable_key => getenv('pk_test_XXXXXX')
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>
不再使用config.php,全部合并为charge.php
2) 在我的 POST 调用中,我传递了一个 tokenid,但在我的 charge.php 中,我使用了 stripeToken。这可能是因为我尝试使用 Stack Overflow 答案和 Stripe 文档来制作完整的解决方案。我认为没有从我的前端发送 stripeToken 很奇怪。因此,我尝试在我的 charge.php 中使用 tokenid(将 stripeToken
替换为 tokenid
。更改为 tokenid 无效并存在安全问题(我宁愿坚持使用兼容的 stripeToken,但是我不知道如何实现它)。
3) 我担心将 amount 变量发送到 charge.php。我不希望用户在他们的控制台中更改它,但如果我不发送它,我就不知道如何进行可变定价。我需要可变定价,因为用户 select 使用不同价格的多种产品(参见:目标)。所以,这对我来说是另一个难题。
可能值得注意的是,我必须在生产中对此进行测试才能获得成功错误。在 localhost 中,我收到 500 Internal Server Error。不知道为什么,但猜测这与缺少 PHP 环境或文件结构有关。
如有任何帮助,我们将不胜感激!谢谢。
您的 token
回调函数检查字符串 "success"
是否 returned:
success: function(data) {
if (data == 'success') {
但是你的 charge.php
文件没有 return "success"
,它 returns
echo '<h1>Successfully charged [amount]!</h1>';
或
echo '<h1>Charge failed</h1>';
您应该将 charge.php
文件修改为 return 您的前端代码所需的数据。由于它是通过 AJAX 调用的,因此浏览器不会显示其输出。
其他几点:
您应该将客户创建调用 (\Stripe\Customer::create(...)
) 移到 try/catch
子句中。当 creating a customer 使用卡令牌时,Stripe 将检查卡是否有效,如果不是这样 return 则卡错误
除了客户应该自己明确选择金额的情况(例如接受客户指定的捐款),您不应该依赖客户浏览器发送的金额,因为这很容易更改它。
目标:屏幕上有几个按钮,像这样–
[产品 1 – 20 美元]
[产品 2 – 35 美元]
等等...
用户可以 select 任意数量的产品。在 JavaScript 中,我有一个变量 amount
,当用户 select 购买它时,它会增加产品成本。所以流程是这样的:
用户 select 的产品 1 和 2 --> 数量 = 20 + 35 = 55
然后,用户可以按“用卡付款”按钮启动 Stripe Checkout 模式。填写,付款,完成。
问题:现在,我使用的是 Checkout 的自定义按钮版本,因此我可以按照自己的方式设置按钮的样式并将这个变量放入。所以,那个实现非常简单:
var handler = StripeCheckout.configure({
key: 'pk_test_XXXXXX',
image: 'assets/logo-stripe.png',
locale: 'auto',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
$.ajax({
url: 'php/charge.php',
type: 'post',
data: {tokenid: token.id, email: token.email, amount: amount},
success: function(data) {
if (data == 'success') {
console.log("Card successfully charged!");
}
else if (data == 'failure') {
console.log("Card error");
} else {
console.log("Other error");
}
},
error: function(data) {
console.log("AJAX error!");
console.log(data);
}
});
}
});
$('#pay-button').on('click', function(e) {
if (amount == 0) {
$('#pay-button').addClass('animated shake'); // Animates the button and refuses to open the modal if user didn't select any products
$('#pay-button').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', removeAnimation);
} else {
handler.open({
name: 'Company, Inc.',
description: "Description",
amount: amount // Here's the variable
});
e.preventDefault();
}
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
这就是JavaScript逻辑。我省略了对 checkout.js 的调用,因为这对这个问题没有任何影响。
从前端的角度来看,这是极好的。现在是令牌化和实际为卡充电。这是事情开始出现问题的地方。如果您查看令牌处理,我正在尝试 POST 将电子邮件、金额和令牌数据发送到 charge.php。基于我没有从控制台收到任何关于此的错误这一事实,我认为这是可行的。
在 charge.php 中,我有这个:(4 月 20 日更新:这是工作代码)
<?php
require_once('stripe-php/init.php');
// Set secret key
\Stripe\Stripe::setApiKey("sk_test_XXXXXXX");
// Get the credit card details submitted by the form
$token = $_POST['tokenid'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$customer = \Stripe\Customer::create(array(
'email' => $_POST['email'],
'source' => $token
));
$charge = \Stripe\Charge::create(array(
"amount" => $_POST['amount'],
"currency" => "usd",
"customer" => $customer->id
));
echo "success";
} catch(\Stripe\Error\Card $e) {
// The card has been declined
echo "failure";
}
?>
每当我尝试购买时,日志都会给我 "Success Error." 我大致遵循此处提供的代码 – How to create a stripe charge with a checkout token sent via AJAX to php。所以,我不知道 Success Error 是什么,但我的猜测是数据发送到 charge.php 没问题,但卡没有收费。我的 Stripe 仪表板没有显示任何付款记录这一事实支持了这一点。
我试过的:
1) 我认为错误可能是我的 config.php。我没有使用 Composer(因为我不知道那是什么,甚至不知道如何开始使用它),所以我从 stripe-php 调用 init.php。根据 Stripe 自己的文档,这是一个可接受的 Composer 替代品。
这是config.php:
<?php
require_once('php/stripe-php/init.php');
$stripe = array(
secret_key => getenv('sk_test_XXXXXX'),
publishable_key => getenv('pk_test_XXXXXX')
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>
不再使用config.php,全部合并为charge.php
2) 在我的 POST 调用中,我传递了一个 tokenid,但在我的 charge.php 中,我使用了 stripeToken。这可能是因为我尝试使用 Stack Overflow 答案和 Stripe 文档来制作完整的解决方案。我认为没有从我的前端发送 stripeToken 很奇怪。因此,我尝试在我的 charge.php 中使用 tokenid(将 stripeToken
替换为 tokenid
。更改为 tokenid 无效并存在安全问题(我宁愿坚持使用兼容的 stripeToken,但是我不知道如何实现它)。
3) 我担心将 amount 变量发送到 charge.php。我不希望用户在他们的控制台中更改它,但如果我不发送它,我就不知道如何进行可变定价。我需要可变定价,因为用户 select 使用不同价格的多种产品(参见:目标)。所以,这对我来说是另一个难题。
可能值得注意的是,我必须在生产中对此进行测试才能获得成功错误。在 localhost 中,我收到 500 Internal Server Error。不知道为什么,但猜测这与缺少 PHP 环境或文件结构有关。
如有任何帮助,我们将不胜感激!谢谢。
您的 token
回调函数检查字符串 "success"
是否 returned:
success: function(data) {
if (data == 'success') {
但是你的 charge.php
文件没有 return "success"
,它 returns
echo '<h1>Successfully charged [amount]!</h1>';
或
echo '<h1>Charge failed</h1>';
您应该将 charge.php
文件修改为 return 您的前端代码所需的数据。由于它是通过 AJAX 调用的,因此浏览器不会显示其输出。
其他几点:
您应该将客户创建调用 (
\Stripe\Customer::create(...)
) 移到try/catch
子句中。当 creating a customer 使用卡令牌时,Stripe 将检查卡是否有效,如果不是这样 return 则卡错误除了客户应该自己明确选择金额的情况(例如接受客户指定的捐款),您不应该依赖客户浏览器发送的金额,因为这很容易更改它。