From cef4bdab7b388302168fe7d778222751f032b32a Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Thu, 22 Aug 2019 22:20:50 +0200 Subject: [PATCH 1/4] NEW update / delete stripe account for supplier --- htdocs/societe/paymentmodes.php | 48 ++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/htdocs/societe/paymentmodes.php b/htdocs/societe/paymentmodes.php index 9310ede87c5..cd8571fa869 100644 --- a/htdocs/societe/paymentmodes.php +++ b/htdocs/societe/paymentmodes.php @@ -600,6 +600,52 @@ if (empty($reshook)) $db->rollback(); } } + + if ($action == 'setkey_account_supplier') + { + $error = 0; + + $newsup = GETPOST('key_account_supplier', 'alpha'); + + $db->begin(); + + if (empty($newsup)) { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; + } else { + try { + $stripesup = \Stripe\Account::retrieve($db->escape(GETPOST('key_account_supplier', 'alpha'))); + + $tokenstring['stripe_user_id'] = $stripesup->id; + $tokenstring['type'] = $stripesup->type; + $sql = "UPDATE ".MAIN_DB_PREFIX."oauth_token"; + $sql.= " SET tokenstring = '".dol_json_encode($tokenstring)."'"; + $sql.= " WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; // Keep = here for entity. Only 1 record must be modified ! + } + catch(Exception $e) + { + $error++; + setEventMessages($e->getMessage(), null, 'errors'); + } + } + + $resql = $db->query($sql); + $num = $db->num_rows($resql); + if (empty($num) && !empty($newsup)) + { + //TODO CREATE ACCOUNT + } + + if (! $error) + { + $stripesupplieracc = $newsup; + $db->commit(); + } + else + { + $db->rollback(); + } + } + if ($action == 'setlocalassourcedefault') // Set as default when payment mode defined locally (and may be also remotely) { try { @@ -845,7 +891,7 @@ if ($socid && $action != 'edit' && $action != 'create' && $action != 'editcard' print $form->editfieldkey("StripeConnectAccount", 'key_account_supplier', $stripesupplieracc, $object, $permissiontowrite, 'string', '', 0, 2, 'socid'); print ''; print $form->editfieldval("StripeConnectAccount", 'key_account_supplier', $stripesupplieracc, $object, $permissiontowrite, 'string', '', null, null, '', 2, '', 'socid'); - if (! empty($conf->stripe->enabled) && $stripesupplieracc && $action != 'editkey_account') + if (! empty($conf->stripe->enabled) && $stripesupplieracc && $action != 'editkey_account_supplier') { $connect=''; From 4536b8618e9eeea91c0585868e907e7f2bf40b62 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Thu, 22 Aug 2019 22:48:37 +0200 Subject: [PATCH 2/4] Update paymentmodes.php --- htdocs/societe/paymentmodes.php | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/htdocs/societe/paymentmodes.php b/htdocs/societe/paymentmodes.php index cd8571fa869..374eff99766 100644 --- a/htdocs/societe/paymentmodes.php +++ b/htdocs/societe/paymentmodes.php @@ -612,27 +612,38 @@ if (empty($reshook)) if (empty($newsup)) { $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; } else { - try { + try { $stripesup = \Stripe\Account::retrieve($db->escape(GETPOST('key_account_supplier', 'alpha'))); - $tokenstring['stripe_user_id'] = $stripesup->id; - $tokenstring['type'] = $stripesup->type; + $tokenstring['type'] = $stripesup->type; $sql = "UPDATE ".MAIN_DB_PREFIX."oauth_token"; $sql.= " SET tokenstring = '".dol_json_encode($tokenstring)."'"; $sql.= " WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; // Keep = here for entity. Only 1 record must be modified ! - } - catch(Exception $e) - { - $error++; - setEventMessages($e->getMessage(), null, 'errors'); - } - } + } + catch(Exception $e) + { + $error++; + setEventMessages($e->getMessage(), null, 'errors'); + } + } $resql = $db->query($sql); $num = $db->num_rows($resql); if (empty($num) && !empty($newsup)) { - //TODO CREATE ACCOUNT + try { + $stripesup = \Stripe\Account::retrieve($db->escape(GETPOST('key_account_supplier', 'alpha'))); + $tokenstring['stripe_user_id'] = $stripesup->id; + $tokenstring['type'] = $stripesup->type; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_token (service, fk_soc, entity, tokenstring)"; + $sql .= " VALUES ('".$service."', ".$object->id.", ".$conf->entity.", '".dol_json_encode($tokenstring)."')"; + } + catch(Exception $e) + { + $error++; + setEventMessages($e->getMessage(), null, 'errors'); + } + $resql = $db->query($sql); } if (! $error) From 55d70004b1c1ff0ca75d1d01f3a22284437281ef Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Fri, 23 Aug 2019 09:00:11 +0200 Subject: [PATCH 3/4] Update paymentmodes.php --- htdocs/societe/paymentmodes.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/societe/paymentmodes.php b/htdocs/societe/paymentmodes.php index 374eff99766..958ba4810b7 100644 --- a/htdocs/societe/paymentmodes.php +++ b/htdocs/societe/paymentmodes.php @@ -612,10 +612,10 @@ if (empty($reshook)) if (empty($newsup)) { $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; } else { - try { + try { $stripesup = \Stripe\Account::retrieve($db->escape(GETPOST('key_account_supplier', 'alpha'))); $tokenstring['stripe_user_id'] = $stripesup->id; - $tokenstring['type'] = $stripesup->type; + $tokenstring['type'] = $stripesup->type; $sql = "UPDATE ".MAIN_DB_PREFIX."oauth_token"; $sql.= " SET tokenstring = '".dol_json_encode($tokenstring)."'"; $sql.= " WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; // Keep = here for entity. Only 1 record must be modified ! @@ -631,8 +631,8 @@ if (empty($reshook)) $num = $db->num_rows($resql); if (empty($num) && !empty($newsup)) { - try { - $stripesup = \Stripe\Account::retrieve($db->escape(GETPOST('key_account_supplier', 'alpha'))); + try { + $stripesup = \Stripe\Account::retrieve($db->escape(GETPOST('key_account_supplier', 'alpha'))); $tokenstring['stripe_user_id'] = $stripesup->id; $tokenstring['type'] = $stripesup->type; $sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_token (service, fk_soc, entity, tokenstring)"; From 0afc55ab51e16c3cb9021999cb208295325813be Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Fri, 23 Aug 2019 09:08:39 +0200 Subject: [PATCH 4/4] Update paymentmodes.php --- htdocs/societe/paymentmodes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/societe/paymentmodes.php b/htdocs/societe/paymentmodes.php index 958ba4810b7..4d49fc3792a 100644 --- a/htdocs/societe/paymentmodes.php +++ b/htdocs/societe/paymentmodes.php @@ -610,7 +610,7 @@ if (empty($reshook)) $db->begin(); if (empty($newsup)) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE fk_soc = ".$object->id." AND service = '".$service."' AND entity = ".$conf->entity; } else { try { $stripesup = \Stripe\Account::retrieve($db->escape(GETPOST('key_account_supplier', 'alpha'))); @@ -618,7 +618,7 @@ if (empty($reshook)) $tokenstring['type'] = $stripesup->type; $sql = "UPDATE ".MAIN_DB_PREFIX."oauth_token"; $sql.= " SET tokenstring = '".dol_json_encode($tokenstring)."'"; - $sql.= " WHERE fk_soc = ".$object->id." AND service = ".$servicestatus." AND entity = ".$conf->entity; // Keep = here for entity. Only 1 record must be modified ! + $sql.= " WHERE fk_soc = ".$object->id." AND service = '".$service."' AND entity = ".$conf->entity; // Keep = here for entity. Only 1 record must be modified ! } catch(Exception $e) {