From ffb6a5db8142a5895cf9d08fb60777f9c9fc5e6a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 17 May 2018 16:07:44 +0200 Subject: [PATCH] Fix use correct stripe env --- htdocs/stripe/class/stripe.class.php | 39 +++++++++++++++++++--------- htdocs/stripe/config.php | 21 +++++++++------ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php index 3b53b4572f0..c28567c8416 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -135,7 +135,7 @@ class Stripe extends CommonObject $sql.= " AND sa.site = 'stripe' AND sa.status = ".((int) $status); $sql.= " AND key_account IS NOT NULL AND key_account <> ''"; - dol_syslog(get_class($this) . "::fetch search stripe customer id for thirdparty id=".$object->id, LOG_DEBUG); + dol_syslog(get_class($this) . "::customerStripe search stripe customer id for thirdparty id=".$object->id, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); @@ -143,6 +143,13 @@ class Stripe extends CommonObject { $obj = $this->db->fetch_object($resql); $tiers = $obj->key_account; + + dol_syslog(get_class($this) . "::customerStripe found stripe customer key_account = ".$tiers); + + // Force to use the correct API key + global $stripearrayofkeysbyenv; + \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); + try { if (empty($key)) { // If the Stripe connect account not set, we use common API usage $customer = \Stripe\Customer::retrieve("$tiers"); @@ -167,6 +174,10 @@ class Stripe extends CommonObject //$a = \Stripe\Stripe::getApiKey(); //var_dump($a);var_dump($key);exit; try { + // Force to use the correct API key + global $stripearrayofkeysbyenv; + \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); + if (empty($key)) { // If the Stripe connect account not set, we use common API usage $customer = \Stripe\Customer::create($dataforcustomer); } else { @@ -373,6 +384,10 @@ class Stripe extends CommonObject ); $return = new Stripe($this->db); try { + // Force to use the correct API key + global $stripearrayofkeysbyenv; + \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); + if (empty($conf->stripeconnect->enabled)) { if (preg_match('/acct_/i', $source)) @@ -410,17 +425,17 @@ class Stripe extends CommonObject } $charge = \Stripe\Charge::create(array( - "amount" => "$stripeamount", - "currency" => "$currency", - // "statement_descriptor" => " ", - "description" => "$description", - "metadata" => $metadata, - "source" => "$source", - "customer" => "$customer", - "application_fee" => "$fee" - ), array( - "idempotency_key" => "$ref", - "stripe_account" => "$account" + "amount" => "$stripeamount", + "currency" => "$currency", + // "statement_descriptor" => " ", + "description" => "$description", + "metadata" => $metadata, + "source" => "$source", + "customer" => "$customer", + "application_fee" => "$fee" + ), array( + "idempotency_key" => "$ref", + "stripe_account" => "$account" )); } if (isset($charge->id)) {} diff --git a/htdocs/stripe/config.php b/htdocs/stripe/config.php index db982e878f9..1e9eb7a5181 100644 --- a/htdocs/stripe/config.php +++ b/htdocs/stripe/config.php @@ -30,22 +30,27 @@ require_once DOL_DOCUMENT_ROOT.'/includes/stripe/lib/Stripe.php'; global $stripe; global $conf; +global $stripearrayofkeysbyenv; +$stripearrayofkeysbyenv = array( + 0=>array( + "secret_key" => $conf->global->STRIPE_TEST_SECRET_KEY, + "publishable_key" => $conf->global->STRIPE_TEST_PUBLISHABLE_KEY + ), + 1=>array( + "secret_key" => $conf->global->STRIPE_LIVE_SECRET_KEY, + "publishable_key" => $conf->global->STRIPE_LIVE_PUBLISHABLE_KEY + ) +); $stripearrayofkeys = array(); if (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox','alpha')) { - $stripearrayofkeys = array( - "secret_key" => $conf->global->STRIPE_TEST_SECRET_KEY, - "publishable_key" => $conf->global->STRIPE_TEST_PUBLISHABLE_KEY - ); + $stripearrayofkeys = $stripearrayofkeysbyenv[0]; } else { - $stripearrayofkeys = array( - "secret_key" => $conf->global->STRIPE_LIVE_SECRET_KEY, - "publishable_key" => $conf->global->STRIPE_LIVE_PUBLISHABLE_KEY - ); + $stripearrayofkeys = $stripearrayofkeysbyenv[1]; } \Stripe\Stripe::setApiKey($stripearrayofkeys['secret_key']);