From 3aa0bdfe5c4a6dfe8e18311f28398ed3eb175854 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Tue, 13 Mar 2018 12:17:57 +0100 Subject: [PATCH 1/5] ADD trigger when modify, delete thirdparty More fix todo with new class of stripe.class.php --- .../nterface_99_modStripe_Stripe.class.php | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 htdocs/core/triggers/nterface_99_modStripe_Stripe.class.php diff --git a/htdocs/core/triggers/nterface_99_modStripe_Stripe.class.php b/htdocs/core/triggers/nterface_99_modStripe_Stripe.class.php new file mode 100644 index 00000000000..e75d87c36f6 --- /dev/null +++ b/htdocs/core/triggers/nterface_99_modStripe_Stripe.class.php @@ -0,0 +1,159 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/triggers/interface_50_modStripe_Stripe.class.php + * \ingroup core + * \brief Fichier + * \remarks Son propre fichier d'actions peut etre cree par recopie de celui-ci: + * - Le nom du fichier doit etre: interface_99_modMymodule_Mytrigger.class.php + * ou: interface_99_all_Mytrigger.class.php + * - Le fichier doit rester stocke dans core/triggers + * - Le nom de la classe doit etre InterfaceMytrigger + * - Le nom de la propriete name doit etre Mytrigger + */ +require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php'; +dol_include_once('/stripe/class/stripe.class.php'); +$path=dirname(__FILE__).'/'; +/** + * Class of triggers for stripe module + */ +class InterfaceStripe +{ + public $db; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + + $this->name = preg_replace('/^Interface/i', '', get_class($this)); + $this->family = 'Stripeconnect'; + $this->description = "Triggers of the module Stripeconnect"; + $this->version = 'dolibarr'; // 'development', 'experimental', 'dolibarr' or version + $this->picto = 'stripe@stripe'; + } + + /** + * Trigger name + * + * @return string Name of trigger file + */ + public function getName() + { + return $this->name; + } + + + /** + * Trigger description + * + * @return string Description of trigger file + */ + public function getDesc() + { + return $this->description; + } + + /** + * Trigger version + * + * @return string Version of trigger file + */ + public function getVersion() + { + global $langs; + $langs->load("admin"); + + if ($this->version == 'development') { + return $langs->trans("Development"); + } elseif ($this->version == 'experimental') { + return $langs->trans("Experimental"); + } elseif ($this->version == 'dolibarr') { + return DOL_VERSION; + } elseif ($this->version) { + return $this->version; + } else { + return $langs->trans("Unknown"); + } + } + + /** + * Function called when a Dolibarrr business event is done. + * All functions "runTrigger" are triggered if file + * is inside directory core/triggers + * + * @param string $action Event action code + * @param CommonObject $object Object + * @param User $user Object user + * @param Translate $langs Object langs + * @param Conf $conf Object conf + * @return int <0 if KO, 0 if no triggered ran, >0 if OK + */ + + public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf) + { + // Put here code you want to execute when a Dolibarr business events occurs. + // Data and type of action are stored into $object and $action +global $langs,$db,$conf; +$langs->load("members"); +$langs->load("users"); +$langs->load("mails"); +$langs->load('other'); +/** Users */ +$ok=0; +$stripe=new Stripe($db); +if ($action == 'COMPANY_MODIFY') { + dol_syslog( + "Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id + ); +if ($stripe->GetStripeAccount($conf->entity)&&$object->client!=0) { +$cu=$stripe->CustomerStripe($object->id,$stripe->GetStripeAccount($conf->entity)); +if ($cu) { + if ($conf->entity=='1'){ +$customer = \Stripe\Customer::retrieve("$cu->id"); + }else{ +$customer = \Stripe\Customer::retrieve("$cu->id",array("stripe_account" => $stripe->GetStripeAccount($conf->entity))); + } +if (!empty($object->email)) {$customer->email = "$object->email";} +$customer->description = "$object->name"; +$customer->save(); +}} + } +elseif ($action == 'COMPANY_DELETE') { + dol_syslog( + "Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id + ); +$cu=$stripe->CustomerStripe($object->id,$stripe->GetStripeAccount($conf->entity)); +if ($cu) { + if ($conf->entity==1){ + $customer = \Stripe\Customer::retrieve("$cu->id"); + }else{ + $customer = \Stripe\Customer::retrieve("$cu->id",array("stripe_account" => $stripe->GetStripeAccount($conf->entity))); + } +$customer->delete(); +} + } + + return $ok; + } +} From 3dd90a1f1a8b9f3ef25c7e4ed4bed9700a9d6245 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Tue, 13 Mar 2018 13:21:36 +0100 Subject: [PATCH 2/5] FIX name of file --- ...e_Stripe.class.php => interface_99_modStripe_Stripe.class.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename htdocs/core/triggers/{nterface_99_modStripe_Stripe.class.php => interface_99_modStripe_Stripe.class.php} (100%) diff --git a/htdocs/core/triggers/nterface_99_modStripe_Stripe.class.php b/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php similarity index 100% rename from htdocs/core/triggers/nterface_99_modStripe_Stripe.class.php rename to htdocs/core/triggers/interface_99_modStripe_Stripe.class.php From d6e8872af4a37b985481507b0a499c6c3060abdf Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 13 Mar 2018 14:14:33 +0100 Subject: [PATCH 3/5] Update interface_99_modStripe_Stripe.class.php --- .../interface_99_modStripe_Stripe.class.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php b/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php index e75d87c36f6..0fff7b6a58e 100644 --- a/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php +++ b/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php @@ -121,18 +121,27 @@ $langs->load("mails"); $langs->load('other'); /** Users */ $ok=0; -$stripe=new Stripe($db); +$stripe=new Stripe($db); +if (! empty($conf->stripe->enabled) && (empty($conf->global->STRIPE_LIVE) || empty($conf->global->STRIPECONNECT_LIVE) || GETPOST('forcesandbox','alpha'))) +{ + $service = 'StripeTest'; +} +else +{ + $service = 'StripeLive'; +} + if ($action == 'COMPANY_MODIFY') { dol_syslog( "Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id ); -if ($stripe->GetStripeAccount($conf->entity)&&$object->client!=0) { -$cu=$stripe->CustomerStripe($object->id,$stripe->GetStripeAccount($conf->entity)); +if ($stripe->getStripeAccount($service) && $object->client!=0) { +$cu=$stripe->CustomerStripe($object->id,$stripe->getStripeAccount($service)); if ($cu) { if ($conf->entity=='1'){ $customer = \Stripe\Customer::retrieve("$cu->id"); }else{ -$customer = \Stripe\Customer::retrieve("$cu->id",array("stripe_account" => $stripe->GetStripeAccount($conf->entity))); +$customer = \Stripe\Customer::retrieve("$cu->id",array("stripe_account" => $stripe->getStripeAccount($service))); } if (!empty($object->email)) {$customer->email = "$object->email";} $customer->description = "$object->name"; @@ -143,12 +152,12 @@ elseif ($action == 'COMPANY_DELETE') { dol_syslog( "Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id ); -$cu=$stripe->CustomerStripe($object->id,$stripe->GetStripeAccount($conf->entity)); +$cu=$stripe->CustomerStripe($object->id,$stripe->getStripeAccount($service)); if ($cu) { if ($conf->entity==1){ $customer = \Stripe\Customer::retrieve("$cu->id"); }else{ - $customer = \Stripe\Customer::retrieve("$cu->id",array("stripe_account" => $stripe->GetStripeAccount($conf->entity))); + $customer = \Stripe\Customer::retrieve("$cu->id",array("stripe_account" => $stripe->getStripeAccount($service))); } $customer->delete(); } From 4b7f4d29d1d5d29d0d0d10ccde26acf6b895e4ce Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 13 Mar 2018 14:19:26 +0100 Subject: [PATCH 4/5] Update interface_99_modStripe_Stripe.class.php --- htdocs/core/triggers/interface_99_modStripe_Stripe.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php b/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php index 0fff7b6a58e..f639695cc40 100644 --- a/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php +++ b/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php @@ -122,7 +122,9 @@ $langs->load('other'); /** Users */ $ok=0; $stripe=new Stripe($db); -if (! empty($conf->stripe->enabled) && (empty($conf->global->STRIPE_LIVE) || empty($conf->global->STRIPECONNECT_LIVE) || GETPOST('forcesandbox','alpha'))) +if (empty($conf->stripe->enabled)) return 0; + +if (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox','alpha'))) { $service = 'StripeTest'; } From 59b7187501f26acac7077fe62279c775343ec8f7 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 13 Mar 2018 14:20:23 +0100 Subject: [PATCH 5/5] Rename interface_99_modStripe_Stripe.class.php to interface_80_modStripe_Stripe.class.php --- ...e_Stripe.class.php => interface_80_modStripe_Stripe.class.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename htdocs/core/triggers/{interface_99_modStripe_Stripe.class.php => interface_80_modStripe_Stripe.class.php} (100%) diff --git a/htdocs/core/triggers/interface_99_modStripe_Stripe.class.php b/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php similarity index 100% rename from htdocs/core/triggers/interface_99_modStripe_Stripe.class.php rename to htdocs/core/triggers/interface_80_modStripe_Stripe.class.php