dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
17  */
18 
30 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
31 
32 
37 {
41  public $db;
42 
48  public function __construct($db)
49  {
50  $this->db = $db;
51 
52  $this->name = preg_replace('/^Interface/i', '', get_class($this));
53  $this->family = 'stripe';
54  $this->description = "Triggers of the module Stripe";
55  $this->version = 'dolibarr'; // 'development', 'experimental', 'dolibarr' or version
56  $this->picto = 'stripe@stripe';
57  }
58 
64  public function getName()
65  {
66  return $this->name;
67  }
68 
69 
75  public function getDesc()
76  {
77  return $this->description;
78  }
79 
85  public function getVersion()
86  {
87  global $langs;
88  $langs->load("admin");
89 
90  if ($this->version == 'development') {
91  return $langs->trans("Development");
92  } elseif ($this->version == 'experimental') {
93  return $langs->trans("Experimental");
94  } elseif ($this->version == 'dolibarr') {
95  return DOL_VERSION;
96  } elseif ($this->version) {
97  return $this->version;
98  } else {
99  return $langs->trans("Unknown");
100  }
101  }
102 
115  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
116  {
117  // Put here code you want to execute when a Dolibarr business event occurs.
118  // Data and type of action are stored into $object and $action
119  global $langs, $db, $conf;
120 
121  // Load translation files required by the page
122  $langs->loadLangs(array("members","other","users","mails"));
123 
124  require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
125  $stripe = new Stripe($db);
126 
127  if (empty($conf->stripe->enabled)) return 0;
128 
129  $ok = 1;
130 
131  $service = 'StripeTest';
132  $servicestatus = 0;
133  if (! empty($conf->global->STRIPE_LIVE) && ! GETPOST('forcesandbox', 'alpha'))
134  {
135  $service = 'StripeLive';
136  $servicestatus = 1;
137  }
138 
139  // If customer is linked to Stripe, we update/delete Stripe too
140  if ($action == 'COMPANY_MODIFY') {
141  dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
142 
143  $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
144 
145  if ($object->client != 0) {
146  $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus); // This make a network request
147  if ($customer)
148  {
149  $namecleaned = $object->name ? $object->name : null;
150  $vatcleaned = $object->tva_intra ? $object->tva_intra : null;
151 
152  $taxinfo = array('type'=>'vat');
153  if ($vatcleaned)
154  {
155  $taxinfo["tax_id"] = $vatcleaned;
156  }
157  // We force data to "null" if not defined as expected by Stripe
158  if (empty($vatcleaned)) $taxinfo=null;
159 
160  // Detect if we change a Stripe info (email, description, vat id)
161  $changerequested = 0;
162  if (! empty($object->email) && $object->email != $customer->email) $changerequested++;
163  if ($namecleaned != $customer->description) $changerequested++;
164  if (! isset($customer->tax_info['tax_id']) && ! is_null($vatcleaned)) $changerequested++;
165  elseif (isset($customer->tax_info['tax_id']) && is_null($vatcleaned)) $changerequested++;
166  elseif (isset($customer->tax_info['tax_id']) && ! is_null($vatcleaned))
167  {
168  if ($vatcleaned != $customer->tax_info['tax_id']) $changerequested++;
169  }
170 
171  if ($changerequested)
172  {
173  if (! empty($object->email)) $customer->email = $object->email;
174  $customer->description = $namecleaned;
175  if (empty($taxinfo)) $customer->tax_info = array('type'=>'vat', 'tax_id'=>null);
176  else $customer->tax_info = $taxinfo;
177 
178  $customer->save();
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  $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
187 
188  $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus);
189  if ($customer)
190  {
191  $customer->delete();
192  }
193 
194  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account";
195  $sql.= " WHERE site='stripe' AND fk_soc = " . $object->id;
196  $this->db->query($sql);
197  }
198 
199  // If payment mode is linked to Stripee, we update/delete Stripe too
200  if ($action == 'COMPANYPAYMENTMODE_MODIFY' && $object->type == 'card') {
201 
202  // For creation of credit card, we do not create in Stripe automatically
203  }
204  if ($action == 'COMPANYPAYMENTMODE_MODIFY' && $object->type == 'card') {
205  dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
206 
207  if (! empty($object->stripe_card_ref))
208  {
209  $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined.
210  $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc); // No need of network access for this
211 
212  if ($stripecu)
213  {
214  // Get customer (required to get a card)
215  if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
216  $customer = \Stripe\Customer::retrieve($stripecu);
217  } else {
218  $customer = \Stripe\Customer::retrieve($stripecu, array("stripe_account" => $stripeacc));
219  }
220 
221  if ($customer)
222  {
223  $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
224  if ($card) {
225  $card->metadata=array('dol_id'=>$object->id, 'dol_version'=>DOL_VERSION, 'dol_entity'=>$conf->entity, 'ipaddress'=>(empty($_SERVER['REMOTE_ADDR'])?'':$_SERVER['REMOTE_ADDR']));
226  try {
227  $card->save($dataforcard);
228  }
229  catch(Exception $e)
230  {
231  $ok = -1;
232  $this->error = $e->getMessages();
233  }
234  }
235  }
236  }
237  }
238  }
239  if ($action == 'COMPANYPAYMENTMODE_DELETE' && $object->type == 'card') {
240  dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
241 
242  if (! empty($object->stripe_card_ref))
243  {
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  {
249  // Get customer (required to get a card)
250  if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
251  $customer = \Stripe\Customer::retrieve($stripecu);
252  } else {
253  $customer = \Stripe\Customer::retrieve($stripecu, array("stripe_account" => $stripeacc));
254  }
255 
256  if ($customer)
257  {
258  $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
259  if ($card) {
260  if (method_exists($card, 'detach')) $card->detach();
261  else $card->delete();
262  }
263  }
264  }
265  }
266  }
267 
268  return $ok;
269  }
270 }
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to stock current configuration.
Definition: conf.class.php:33
Class of triggers for stripe module.
</td >< td class="liste_titre" align="right"></td ></tr >< tr class="liste_titre">< input type="checkbox" onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< tr class="oddeven">< td >< input type="checkbox" class="check" name="' . $i . '"' . $disabled . '></td >< td >< input type="checkbox" class="check" name="choose'.$i.'"></td >< td class="nowrap"></td >< td >< input type="hidden" name="desc' . $i . '" value="' . dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:573
Class to manage Dolibarr users.
Definition: user.class.php:41
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
Stripe class.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage translations.