根据表单 public 密钥动态设置 Stripe 密钥
Dynamically set Stripe secret key based on form public key
我正在使用 Stripe Checkout 在我的网站上创建一个付款表单,该表单需要使用 3 个独立的 Stripe 帐户,基于单个表单中的下拉选择。
在 HTML/JS 方面,我已经成功地为 Stripe 支付动态设置了 public 密钥。
在 PHP 方面,我想根据提交的 public 密钥设置密钥。
除了这样做是否安全这个显而易见的问题,PHP 代码会是什么样子?
这是我目前的参考资料:https://stripe.com/docs/checkout/php
下面是我对 PHP 代码的想法。如果我的直觉不对,还要感谢更好的解决方案。
<?php
require_once('vendor/autoload.php');
$stripeStore1 = array(
"secret_key" => "...",
"publishable_key" => "..."
);
$stripeStore2 = array(
"secret_key" => "...",
"publishable_key" => "..."
);
$stripeStore3 = array(
"secret_key" => "...",
"publishable_key" => "..."
);
// if form name='key' == a
\Stripe\Stripe::setApiKey($stripeStore1['secret_key']);
// if form name='key' == b
\Stripe\Stripe::setApiKey($stripeStore2['secret_key']);
// if form name='key' == c
\Stripe\Stripe::setApiKey($stripeStore3['secret_key']);
?>
这是我的 JS 代码的样子。
$('#customButton').on('click', function(e) {
// Open Checkout with further options
if (validateForm()) {
var store = selectStore();
var handler = StripeCheckout.configure({
key: store.testKey, // toggle Stripe mode here
name: store.name,
panelLabel: 'Pay {{amount}}',
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
handler.open({
zipCode: true,
amount: 1500
});
}
e.preventDefault();
});
我觉得你的直觉很正确。
因为看起来您正在使用 Checkout(它负责使用 stripe.js 在客户端提交卡号,然后给您一个令牌以在您的代码中使用),您的 Javascript 需要将 public 密钥更改为与相应帐户对应的密钥。令牌生成后(并从 stripe.js 返回到您的代码),您将其传递给您的 PHP 脚本,连同一些东西来识别您使用的是哪个 Stripe 帐户,然后是 PHP 脚本在后端使用适当的密钥进行实际收费。
我会在其中添加一些内容来处理来自您的 Javascript 的无效标识符,但最终(根据我对 stripe.js 的理解)令牌只能对应一个帐户。如果 $_POST['key']
= a
并且您无法使用 "a" 帐户的密钥找到令牌,请确保正确处理。
我正在使用 Stripe Checkout 在我的网站上创建一个付款表单,该表单需要使用 3 个独立的 Stripe 帐户,基于单个表单中的下拉选择。
在 HTML/JS 方面,我已经成功地为 Stripe 支付动态设置了 public 密钥。
在 PHP 方面,我想根据提交的 public 密钥设置密钥。
除了这样做是否安全这个显而易见的问题,PHP 代码会是什么样子?
这是我目前的参考资料:https://stripe.com/docs/checkout/php
下面是我对 PHP 代码的想法。如果我的直觉不对,还要感谢更好的解决方案。
<?php
require_once('vendor/autoload.php');
$stripeStore1 = array(
"secret_key" => "...",
"publishable_key" => "..."
);
$stripeStore2 = array(
"secret_key" => "...",
"publishable_key" => "..."
);
$stripeStore3 = array(
"secret_key" => "...",
"publishable_key" => "..."
);
// if form name='key' == a
\Stripe\Stripe::setApiKey($stripeStore1['secret_key']);
// if form name='key' == b
\Stripe\Stripe::setApiKey($stripeStore2['secret_key']);
// if form name='key' == c
\Stripe\Stripe::setApiKey($stripeStore3['secret_key']);
?>
这是我的 JS 代码的样子。
$('#customButton').on('click', function(e) {
// Open Checkout with further options
if (validateForm()) {
var store = selectStore();
var handler = StripeCheckout.configure({
key: store.testKey, // toggle Stripe mode here
name: store.name,
panelLabel: 'Pay {{amount}}',
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
handler.open({
zipCode: true,
amount: 1500
});
}
e.preventDefault();
});
我觉得你的直觉很正确。
因为看起来您正在使用 Checkout(它负责使用 stripe.js 在客户端提交卡号,然后给您一个令牌以在您的代码中使用),您的 Javascript 需要将 public 密钥更改为与相应帐户对应的密钥。令牌生成后(并从 stripe.js 返回到您的代码),您将其传递给您的 PHP 脚本,连同一些东西来识别您使用的是哪个 Stripe 帐户,然后是 PHP 脚本在后端使用适当的密钥进行实际收费。
我会在其中添加一些内容来处理来自您的 Javascript 的无效标识符,但最终(根据我对 stripe.js 的理解)令牌只能对应一个帐户。如果 $_POST['key']
= a
并且您无法使用 "a" 帐户的密钥找到令牌,请确保正确处理。