根据表单公钥动态设置Stripe密钥


Dynamically set Stripe secret key based on form public key

我正在使用Stripe Checkout在我的网站上创建一个付款表单,该表单需要使用3个单独的Stripe帐户,基于单个表单中的下拉选择。

在HTML/JS方面,我已经成功地为Stripe支付动态设置了公钥。

在PHP方面,我想根据提交的公钥设置密钥。

除了这样做是否安全这个显而易见的问题之外,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需要将公钥更改为对应于适当帐户的公钥。生成令牌(并从stripe.js返回到您的代码中)后,您将其传递给您的PHP脚本,以及标识您使用的stripe帐户的内容,然后PHP脚本在后端使用适当的密钥进行实际收费。

我会在其中添加一些内容来处理来自Javascript的无效标识符,但最终(根据我对stripe.js的理解)令牌只能对应于一个帐户。如果$_POST['key']=a,并且使用"a"帐户的密钥找不到令牌,请确保处理得当。