Fix use correct stripe env

This commit is contained in:
Laurent Destailleur 2018-05-17 16:07:44 +02:00
parent 4f6b0fead6
commit ffb6a5db81
2 changed files with 40 additions and 20 deletions

View File

@ -135,7 +135,7 @@ class Stripe extends CommonObject
$sql.= " AND sa.site = 'stripe' AND sa.status = ".((int) $status); $sql.= " AND sa.site = 'stripe' AND sa.status = ".((int) $status);
$sql.= " AND key_account IS NOT NULL AND key_account <> ''"; $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); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
@ -143,6 +143,13 @@ class Stripe extends CommonObject
{ {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
$tiers = $obj->key_account; $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 { try {
if (empty($key)) { // If the Stripe connect account not set, we use common API usage if (empty($key)) { // If the Stripe connect account not set, we use common API usage
$customer = \Stripe\Customer::retrieve("$tiers"); $customer = \Stripe\Customer::retrieve("$tiers");
@ -167,6 +174,10 @@ class Stripe extends CommonObject
//$a = \Stripe\Stripe::getApiKey(); //$a = \Stripe\Stripe::getApiKey();
//var_dump($a);var_dump($key);exit; //var_dump($a);var_dump($key);exit;
try { 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 if (empty($key)) { // If the Stripe connect account not set, we use common API usage
$customer = \Stripe\Customer::create($dataforcustomer); $customer = \Stripe\Customer::create($dataforcustomer);
} else { } else {
@ -373,6 +384,10 @@ class Stripe extends CommonObject
); );
$return = new Stripe($this->db); $return = new Stripe($this->db);
try { try {
// Force to use the correct API key
global $stripearrayofkeysbyenv;
\Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']);
if (empty($conf->stripeconnect->enabled)) if (empty($conf->stripeconnect->enabled))
{ {
if (preg_match('/acct_/i', $source)) if (preg_match('/acct_/i', $source))
@ -410,17 +425,17 @@ class Stripe extends CommonObject
} }
$charge = \Stripe\Charge::create(array( $charge = \Stripe\Charge::create(array(
"amount" => "$stripeamount", "amount" => "$stripeamount",
"currency" => "$currency", "currency" => "$currency",
// "statement_descriptor" => " ", // "statement_descriptor" => " ",
"description" => "$description", "description" => "$description",
"metadata" => $metadata, "metadata" => $metadata,
"source" => "$source", "source" => "$source",
"customer" => "$customer", "customer" => "$customer",
"application_fee" => "$fee" "application_fee" => "$fee"
), array( ), array(
"idempotency_key" => "$ref", "idempotency_key" => "$ref",
"stripe_account" => "$account" "stripe_account" => "$account"
)); ));
} }
if (isset($charge->id)) {} if (isset($charge->id)) {}

View File

@ -30,22 +30,27 @@ require_once DOL_DOCUMENT_ROOT.'/includes/stripe/lib/Stripe.php';
global $stripe; global $stripe;
global $conf; 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(); $stripearrayofkeys = array();
if (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox','alpha')) if (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox','alpha'))
{ {
$stripearrayofkeys = array( $stripearrayofkeys = $stripearrayofkeysbyenv[0];
"secret_key" => $conf->global->STRIPE_TEST_SECRET_KEY,
"publishable_key" => $conf->global->STRIPE_TEST_PUBLISHABLE_KEY
);
} }
else else
{ {
$stripearrayofkeys = array( $stripearrayofkeys = $stripearrayofkeysbyenv[1];
"secret_key" => $conf->global->STRIPE_LIVE_SECRET_KEY,
"publishable_key" => $conf->global->STRIPE_LIVE_PUBLISHABLE_KEY
);
} }
\Stripe\Stripe::setApiKey($stripearrayofkeys['secret_key']); \Stripe\Stripe::setApiKey($stripearrayofkeys['secret_key']);