Dynamically set Stripe secret key based on form public key
<p>I'm using Stripe Checkout to create a payment form on my website that needs to use 3 separate Stripe accounts, based on a dropdown selection in a single form.</p>

<p>On the HTML/JS side, I've successfully been able to dynamically set the public key for the Stripe payment.</p>

<p>On the PHP side of things, I want to set the secret key depending on the public key that is submitted.</p>

<p>Aside from the obvious question of whether it's safe to do so, what would that PHP code look like?</p>

<p>This is my reference at the moment: <a href="https://stripe.com/docs/checkout/php" rel="nofollow">https://stripe.com/docs/checkout/php</a></p>

<p>Below is what I had in mind for the PHP code. Also appreciate better solutions if my intuition is off.</p>

<pre><code>&lt;?php
require_once('vendor/autoload.php');

$stripeStore1 = array(
  "secret_key"      =&gt; "...",
  "publishable_key" =&gt; "..."
);
$stripeStore2 = array(
  "secret_key"      =&gt; "...",
  "publishable_key" =&gt; "..."
);
$stripeStore3 = array(
  "secret_key"      =&gt; "...",
  "publishable_key" =&gt; "..."
);
// 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']);
?&gt;
</code></pre>

<p>Here's how my JS code looks.</p>

<pre><code>$('#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();
});
</code></pre>