Search code examples
stripe-paymentsstripe-connectstripe.net

Making test transactions in an activated stripe account


I have a stripe standalone account that is activated and is accepting live transactions and connected to a platform stripe account. I am giving below the code I am using for accepting live payments.

 \Stripe\Stripe::setApiKey("LIVE_PLATFORM_API_KEY");
 $strtok = \Stripe\Token::create(
             array(
              "card" => array(
                      "number" => $cardnumber,
                      "exp_month" => $cardexpmonth,
                      "exp_year" => $cardexpyear,
                      "cvc" => $creditcardcvv
                  )
                ),
                array('stripe_account' => "live_account_header")
              );
 $strtoken = $strtok->id;
 $charge = \Stripe\Charge::create(array(
                  'amount' => $amts,
                  'currency' => 'usd',
                  'application_fee' => $appfee,
                  'source' => $strtoken
            ), array('stripe_account' => "live_account_header"));

I want to set up a debug mode in my code that will use the stripe test keys to accept test transactions even though both the stripe standalone and platform accounts are activated and in the live mode. I want the stripe calls I should use when I am debugging instead of making live transactions. I mean how should I change the above code and use test keys when I suddenly want to make a test transaction instead of a live one even though both the platform and the stand alone account are activated and live?


Solution

  • Whether a transaction is processed in test mode or live mode depends entirely on which set of API keys you use. If you use your test API keys, the transaction will be processed in test mode. If you use your live API keys, the transaction will be processed in live mode.

    So what you need to do is decide which set of keys you're going to use based on some condition that you can trigger.

    Basically, you'd need to replace this:

    \Stripe\Stripe::setApiKey("LIVE_PLATFORM_API_KEY");
    

    with something like this:

    if ($test_condition) {
        \Stripe\Stripe::setApiKey("TEST_PLATFORM_API_KEY");
    } else {
        \Stripe\Stripe::setApiKey("LIVE_PLATFORM_API_KEY");
    }
    

    Keep in mind that you also need to use your test publishable key in your frontend code (for creating tokens with Checkout or Elements) -- trying to create a charge in test mode if the token was created with a live key will not work.

    In the sample code you provided, you're creating the token from your backend code (via \Stripe\Token::create(...)). This means that your server is directly providing the card data. This is fine when testing, but in live mode, tokens should always be created frontend-side, via Checkout or Elements. Otherwise, you would no longer be eligible for PCI SAQ A and would need to get your solution audited for PCI compliance.