interface_80_modStripe_Stripe.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /*
  3. * Copyright (C) 2018 ptibogxiv <support@ptibogxiv.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/triggers/interface_80_modStripe_Stripe.class.php
  20. * \ingroup core
  21. * \brief Fichier
  22. * \remarks Son propre fichier d'actions peut etre cree par recopie de celui-ci:
  23. * - Le nom du fichier doit etre: interface_99_modMymodule_Mytrigger.class.php
  24. * ou: interface_99_all_Mytrigger.class.php
  25. * - Le fichier doit rester stocke dans core/triggers
  26. * - Le nom de la classe doit etre InterfaceMytrigger
  27. * - Le nom de la propriete name doit etre Mytrigger
  28. */
  29. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  30. /**
  31. * Class of triggers for stripe module
  32. */
  33. class InterfaceStripe extends DolibarrTriggers
  34. {
  35. /**
  36. * Constructor
  37. *
  38. * @param DoliDB $db Database handler
  39. */
  40. public function __construct($db)
  41. {
  42. $this->db = $db;
  43. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  44. $this->family = 'stripe';
  45. $this->description = "Triggers of the module Stripe";
  46. $this->version = self::VERSION_DOLIBARR; // 'development', 'experimental', 'dolibarr' or version
  47. $this->picto = 'stripe';
  48. }
  49. /**
  50. * Function called when a Dolibarrr business event is done.
  51. * All functions "runTrigger" are triggered if file
  52. * is inside directory core/triggers
  53. *
  54. * @param string $action Event action code
  55. * @param CommonObject $object Object
  56. * @param User $user Object user
  57. * @param Translate $langs Object langs
  58. * @param Conf $conf Object conf
  59. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  60. */
  61. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  62. {
  63. // Put here code you want to execute when a Dolibarr business event occurs.
  64. // Data and type of action are stored into $object and $action
  65. global $langs, $db, $conf;
  66. if (empty($conf->stripe) || empty($conf->stripe->enabled)) {
  67. return 0;
  68. }
  69. require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
  70. $stripe = new Stripe($db);
  71. $ok = 1;
  72. $service = 'StripeTest';
  73. $servicestatus = 0;
  74. if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) {
  75. $service = 'StripeLive';
  76. $servicestatus = 1;
  77. }
  78. // If customer is linked to Stripe, we update/delete Stripe too
  79. if ($action == 'COMPANY_MODIFY') {
  80. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  81. $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
  82. if ($object->client != 0) {
  83. $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus); // This make a network request
  84. if ($customer) {
  85. $namecleaned = $object->name ? $object->name : null;
  86. $vatcleaned = $object->tva_intra ? $object->tva_intra : null; // Example of valid numbers are 'FR12345678901' or 'FR12345678902'
  87. $desccleaned = $object->name_alias ? $object->name_alias : null;
  88. $taxexemptcleaned = $object->tva_assuj ? 'none' : 'exempt';
  89. $langcleaned = $object->default_lang ? array(substr($object->default_lang, 0, 2)) : null;
  90. /*$taxinfo = array('type'=>'vat');
  91. if ($vatcleaned)
  92. {
  93. $taxinfo["tax_id"] = $vatcleaned;
  94. }
  95. // We force data to "null" if not defined as expected by Stripe
  96. if (empty($vatcleaned)) $taxinfo=null;*/
  97. // Detect if we change a Stripe info (email, description, vat id)
  98. $changerequested = 0;
  99. if (!empty($object->email) && $object->email != $customer->email) {
  100. $changerequested++;
  101. }
  102. /* if ($namecleaned != $customer->description) $changerequested++;
  103. if (! isset($customer->tax_info['tax_id']) && ! is_null($vatcleaned)) $changerequested++;
  104. elseif (isset($customer->tax_info['tax_id']) && is_null($vatcleaned)) $changerequested++;
  105. elseif (isset($customer->tax_info['tax_id']) && ! is_null($vatcleaned))
  106. {
  107. if ($vatcleaned != $customer->tax_info['tax_id']) $changerequested++;
  108. } */
  109. if ($namecleaned != $customer->name) {
  110. $changerequested++;
  111. }
  112. if ($desccleaned != $customer->description) {
  113. $changerequested++;
  114. }
  115. if (($customer->tax_exempt == 'exempt' && !$object->tva_assuj) || (!$customer->tax_exempt == 'exempt' && empty($object->tva_assuj))) {
  116. $changerequested++;
  117. }
  118. if (!isset($customer->tax_ids['data']) && !is_null($vatcleaned)) {
  119. $changerequested++;
  120. } elseif (isset($customer->tax_ids['data'])) {
  121. $taxinfo = reset($customer->tax_ids['data']);
  122. if (empty($taxinfo) && !empty($vatcleaned)) {
  123. $changerequested++;
  124. }
  125. if (isset($taxinfo->value) && $vatcleaned != $taxinfo->value) {
  126. $changerequested++;
  127. }
  128. }
  129. if ($changerequested) {
  130. /*if (!empty($object->email)) $customer->email = $object->email;
  131. $customer->description = $namecleaned;
  132. if (empty($taxinfo)) $customer->tax_info = array('type'=>'vat', 'tax_id'=>null);
  133. else $customer->tax_info = $taxinfo; */
  134. $customer->name = $namecleaned;
  135. $customer->description = $desccleaned;
  136. $customer->preferred_locales = $langcleaned;
  137. $customer->tax_exempt = $taxexemptcleaned;
  138. try {
  139. // Update Tax info on Stripe
  140. if (!empty($conf->global->STRIPE_SAVE_TAX_IDS)) { // We setup to save Tax info on Stripe side. Warning: This may result in error when saving customer
  141. if (!empty($vatcleaned)) {
  142. $isineec = isInEEC($object);
  143. if ($object->country_code && $isineec) {
  144. //$taxids = $customer->allTaxIds($customer->id);
  145. $customer->createTaxId($customer->id, array('type'=>'eu_vat', 'value'=>$vatcleaned));
  146. }
  147. } else {
  148. $taxids = $customer->allTaxIds($customer->id);
  149. if (is_array($taxids->data)) {
  150. foreach ($taxids->data as $taxidobj) {
  151. $customer->deleteTaxId($customer->id, $taxidobj->id);
  152. }
  153. }
  154. }
  155. }
  156. // Update Customer on Stripe
  157. $customer->save();
  158. } catch (Exception $e) {
  159. //var_dump(\Stripe\Stripe::getApiVersion());
  160. $this->errors[] = $e->getMessage();
  161. $ok = -1;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. if ($action == 'COMPANY_DELETE') {
  168. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  169. if (!empty($conf->global->STRIPE_DELETE_STRIPE_ACCOUNT_WHEN_DELETING_THIRDPARTY)) {
  170. // By default, we do not delete the stripe account. We may need to reuse it with its payment_intent, for example if delete is for a merge of thirdparties.
  171. $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
  172. $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus);
  173. if ($customer) {
  174. try {
  175. $customer->delete();
  176. } catch (Exception $e) {
  177. dol_syslog("Failed to delete Stripe customer ".$e->getMessage(), LOG_WARNING);
  178. }
  179. }
  180. }
  181. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account";
  182. $sql .= " WHERE site='stripe' AND fk_soc = ".((int) $object->id);
  183. $this->db->query($sql);
  184. }
  185. // If payment mode is linked to Stripe, we update/delete Stripe too
  186. if ($action == 'COMPANYPAYMENTMODE_CREATE' && $object->type == 'card') {
  187. // For creation of credit card, we do not create in Stripe automatically
  188. }
  189. if ($action == 'COMPANYPAYMENTMODE_MODIFY' && $object->type == 'card') {
  190. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  191. if (!empty($object->stripe_card_ref)) {
  192. $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
  193. $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc); // No need of network access for this
  194. if ($stripecu) {
  195. // Get customer (required to get a card)
  196. if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
  197. $customer = \Stripe\Customer::retrieve($stripecu);
  198. } else {
  199. $customer = \Stripe\Customer::retrieve($stripecu, array("stripe_account" => $stripeacc));
  200. }
  201. if ($customer) {
  202. dol_syslog("We got the customer, so now we update the credit card", LOG_DEBUG);
  203. $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
  204. if ($card) {
  205. $card->metadata = array('dol_id'=>$object->id, 'dol_version'=>DOL_VERSION, 'dol_entity'=>$conf->entity, 'ipaddress'=>(empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']));
  206. try {
  207. $card->save();
  208. } catch (Exception $e) {
  209. $ok = -1;
  210. $this->error = $e->getMessages();
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. if ($action == 'COMPANYPAYMENTMODE_DELETE' && $object->type == 'card') {
  218. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  219. if (!empty($object->stripe_card_ref)) {
  220. $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
  221. $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc); // No need of network access for this
  222. if ($stripecu) {
  223. // Get customer (required to get a card)
  224. if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
  225. $customer = \Stripe\Customer::retrieve($stripecu);
  226. } else {
  227. $customer = \Stripe\Customer::retrieve($stripecu, array("stripe_account" => $stripeacc));
  228. }
  229. if ($customer) {
  230. $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
  231. if ($card) {
  232. if (method_exists($card, 'detach')) {
  233. $card->detach();
  234. } else {
  235. $card->delete();
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return $ok;
  243. }
  244. }