dolibarr 19.0.3
interface_80_modStripe_Stripe.class.php
Go to the documentation of this file.
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
30require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
31
32
37{
43 public function __construct($db)
44 {
45 $this->db = $db;
46
47 $this->name = preg_replace('/^Interface/i', '', get_class($this));
48 $this->family = 'stripe';
49 $this->description = "Triggers of the module Stripe";
50 $this->version = self::VERSION_DOLIBARR; // 'development', 'experimental', 'dolibarr' or version
51 $this->picto = 'stripe';
52 }
53
66 public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
67 {
68 // Put here code you want to execute when a Dolibarr business event occurs.
69 // Data and type of action are stored into $object and $action
70 global $langs, $db, $conf;
71
72 if (empty($conf->stripe) || empty($conf->stripe->enabled)) {
73 return 0;
74 }
75
76 require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
77 $stripe = new Stripe($db);
78
79 $ok = 1;
80
81 $service = 'StripeTest';
82 $servicestatus = 0;
83 if (getDolGlobalString('STRIPE_LIVE') && !GETPOST('forcesandbox', 'alpha')) {
84 $service = 'StripeLive';
85 $servicestatus = 1;
86 }
87
88 // If customer is linked to Stripe, we update/delete Stripe too
89 if ($action == 'COMPANY_MODIFY') {
90 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
91
92 $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
93
94 if ($object->client != 0) {
95 $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus); // This make a network request
96 if ($customer) {
97 $namecleaned = $object->name ? $object->name : null;
98 $vatcleaned = $object->tva_intra ? $object->tva_intra : null; // Example of valid numbers are 'FR12345678901' or 'FR12345678902'
99 $desccleaned = $object->name_alias ? $object->name_alias : null;
100 $taxexemptcleaned = $object->tva_assuj ? 'none' : 'exempt';
101 $langcleaned = $object->default_lang ? array(substr($object->default_lang, 0, 2)) : null;
102 /*$taxinfo = array('type'=>'vat');
103 if ($vatcleaned)
104 {
105 $taxinfo["tax_id"] = $vatcleaned;
106 }
107 // We force data to "null" if not defined as expected by Stripe
108 if (empty($vatcleaned)) $taxinfo=null;*/
109
110 // Detect if we change a Stripe info (email, description, vat id)
111 $changerequested = 0;
112 if (!empty($object->email) && $object->email != $customer->email) {
113 $changerequested++;
114 }
115 /* if ($namecleaned != $customer->description) $changerequested++;
116 if (! isset($customer->tax_info['tax_id']) && ! is_null($vatcleaned)) $changerequested++;
117 elseif (isset($customer->tax_info['tax_id']) && is_null($vatcleaned)) $changerequested++;
118 elseif (isset($customer->tax_info['tax_id']) && ! is_null($vatcleaned))
119 {
120 if ($vatcleaned != $customer->tax_info['tax_id']) $changerequested++;
121 } */
122 if ($namecleaned != $customer->name) {
123 $changerequested++;
124 }
125 if ($desccleaned != $customer->description) {
126 $changerequested++;
127 }
128 if (($customer->tax_exempt == 'exempt' && !$object->tva_assuj) || (!$customer->tax_exempt == 'exempt' && empty($object->tva_assuj))) {
129 $changerequested++;
130 }
131 if (!isset($customer->tax_ids['data']) && !is_null($vatcleaned)) {
132 $changerequested++;
133 } elseif (isset($customer->tax_ids['data'])) {
134 $taxinfo = reset($customer->tax_ids['data']);
135 if (empty($taxinfo) && !empty($vatcleaned)) {
136 $changerequested++;
137 }
138 if (isset($taxinfo->value) && $vatcleaned != $taxinfo->value) {
139 $changerequested++;
140 }
141 }
142
143 if ($changerequested) {
144 /*if (!empty($object->email)) $customer->email = $object->email;
145 $customer->description = $namecleaned;
146 if (empty($taxinfo)) $customer->tax_info = array('type'=>'vat', 'tax_id'=>null);
147 else $customer->tax_info = $taxinfo; */
148 $customer->name = $namecleaned;
149 $customer->description = $desccleaned;
150 $customer->preferred_locales = $langcleaned;
151 $customer->tax_exempt = $taxexemptcleaned;
152
153 try {
154 // Update Tax info on Stripe
155 if (getDolGlobalString('STRIPE_SAVE_TAX_IDS')) { // We setup to save Tax info on Stripe side. Warning: This may result in error when saving customer
156 if (!empty($vatcleaned)) {
157 $isineec = isInEEC($object);
158 if ($object->country_code && $isineec) {
159 //$taxids = $customer->allTaxIds($customer->id);
160 $customer->createTaxId($customer->id, array('type'=>'eu_vat', 'value'=>$vatcleaned));
161 }
162 } else {
163 $taxids = $customer->allTaxIds($customer->id);
164 if (is_array($taxids->data)) {
165 foreach ($taxids->data as $taxidobj) {
166 $customer->deleteTaxId($customer->id, $taxidobj->id);
167 }
168 }
169 }
170 }
171
172 // Update Customer on Stripe
173 $customer->save();
174 } catch (Exception $e) {
175 //var_dump(\Stripe\Stripe::getApiVersion());
176 $this->errors[] = $e->getMessage();
177 $ok = -1;
178 }
179 }
180 }
181 }
182 }
183 if ($action == 'COMPANY_DELETE') {
184 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
185
186 if (getDolGlobalString('STRIPE_DELETE_STRIPE_ACCOUNT_WHEN_DELETING_THIRDPARTY')) {
187 // 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.
188 $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
189
190 $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus);
191 if ($customer) {
192 try {
193 $customer->delete();
194 } catch (Exception $e) {
195 dol_syslog("Failed to delete Stripe customer ".$e->getMessage(), LOG_WARNING);
196 }
197 }
198 }
199
200 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account";
201 $sql .= " WHERE site='stripe' AND fk_soc = ".((int) $object->id);
202 $this->db->query($sql);
203 }
204
205 // If payment mode is linked to Stripe, we update/delete Stripe too
206 if ($action == 'COMPANYPAYMENTMODE_CREATE' && $object->type == 'card') {
207 // For creation of credit card, we do not create in Stripe automatically
208 }
209 if ($action == 'COMPANYPAYMENTMODE_MODIFY' && $object->type == 'card') {
210 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
211
212 if (!empty($object->stripe_card_ref)) {
213 $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
214 $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc); // No need of network access for this
215
216 if ($stripecu) {
217 // Get customer (required to get a card)
218 if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
219 $customer = \Stripe\Customer::retrieve($stripecu);
220 } else {
221 $customer = \Stripe\Customer::retrieve($stripecu, array("stripe_account" => $stripeacc));
222 }
223
224 if ($customer) {
225 dol_syslog("We got the customer, so now we update the credit card", LOG_DEBUG);
226 $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
227 if ($card) {
228 $card->metadata = array('dol_id'=>$object->id, 'dol_version'=>DOL_VERSION, 'dol_entity'=>$conf->entity, 'ipaddress'=>(empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']));
229 try {
230 $card->save();
231 } catch (Exception $e) {
232 $ok = -1;
233 $this->error = $e->getMessages();
234 }
235 }
236 }
237 }
238 }
239 }
240 if ($action == 'COMPANYPAYMENTMODE_DELETE' && $object->type == 'card') {
241 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
242
243 if (!empty($object->stripe_card_ref)) {
244 $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
245 $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc); // No need of network access for this
246
247 if ($stripecu) {
248 // Get customer (required to get a card)
249 if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
250 $customer = \Stripe\Customer::retrieve($stripecu);
251 } else {
252 $customer = \Stripe\Customer::retrieve($stripecu, array("stripe_account" => $stripeacc));
253 }
254
255 if ($customer) {
256 $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
257 if ($card) {
258 if (method_exists($card, 'detach')) {
259 $card->detach();
260 } else {
261 $card->delete();
262 }
263 }
264 }
265 }
266 }
267 }
268
269 return $ok;
270 }
271}
Class to stock current configuration.
Class that all the triggers must extend.
Class of triggers for stripe module.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
Stripe class.
Class to manage translations.
Class to manage Dolibarr users.
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124