70 global $langs, $db, $conf;
72 if (empty($conf->stripe) || empty($conf->stripe->enabled)) {
76 require_once DOL_DOCUMENT_ROOT.
'/stripe/class/stripe.class.php';
81 $service =
'StripeTest';
83 if (!empty($conf->global->STRIPE_LIVE) && !
GETPOST(
'forcesandbox',
'alpha')) {
84 $service =
'StripeLive';
89 if ($action ==
'COMPANY_MODIFY') {
90 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
92 $stripeacc = $stripe->getStripeAccount($service);
94 if ($object->client != 0) {
95 $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus);
97 $namecleaned = $object->name ? $object->name :
null;
98 $vatcleaned = $object->tva_intra ? $object->tva_intra :
null;
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;
111 $changerequested = 0;
112 if (!empty($object->email) && $object->email != $customer->email) {
122 if ($namecleaned != $customer->name) {
125 if ($desccleaned != $customer->description) {
128 if (($customer->tax_exempt ==
'exempt' && !$object->tva_assuj) || (!$customer->tax_exempt ==
'exempt' && empty($object->tva_assuj))) {
131 if (!isset($customer->tax_ids[
'data']) && !is_null($vatcleaned)) {
133 } elseif (isset($customer->tax_ids[
'data'])) {
134 $taxinfo = reset($customer->tax_ids[
'data']);
135 if (empty($taxinfo) && !empty($vatcleaned)) {
138 if (isset($taxinfo->value) && $vatcleaned != $taxinfo->value) {
143 if ($changerequested) {
148 $customer->name = $namecleaned;
149 $customer->description = $desccleaned;
150 $customer->preferred_locales = $langcleaned;
151 $customer->tax_exempt = $taxexemptcleaned;
155 if (!empty($conf->global->STRIPE_SAVE_TAX_IDS)) {
156 if (!empty($vatcleaned)) {
158 if ($object->country_code && $isineec) {
160 $customer->createTaxId($customer->id, array(
'type'=>
'eu_vat',
'value'=>$vatcleaned));
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);
176 $this->errors[] = $e->getMessage();
183 if ($action ==
'COMPANY_DELETE') {
184 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
186 if (!empty($conf->global->STRIPE_DELETE_STRIPE_ACCOUNT_WHEN_DELETING_THIRDPARTY)) {
188 $stripeacc = $stripe->getStripeAccount($service);
190 $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus);
195 dol_syslog(
"Failed to delete Stripe customer ".$e->getMessage(), LOG_WARNING);
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);
206 if ($action ==
'COMPANYPAYMENTMODE_CREATE' && $object->type ==
'card') {
209 if ($action ==
'COMPANYPAYMENTMODE_MODIFY' && $object->type ==
'card') {
210 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
212 if (!empty($object->stripe_card_ref)) {
213 $stripeacc = $stripe->getStripeAccount($service);
214 $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc);
218 if (empty($stripeacc)) {
219 $customer = \Stripe\Customer::retrieve($stripecu);
221 $customer = \Stripe\Customer::retrieve($stripecu, array(
"stripe_account" => $stripeacc));
225 dol_syslog(
"We got the customer, so now we update the credit card", LOG_DEBUG);
226 $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
228 $card->metadata = array(
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>(empty($_SERVER[
'REMOTE_ADDR']) ?
'' : $_SERVER[
'REMOTE_ADDR']));
233 $this->error = $e->getMessages();
240 if ($action ==
'COMPANYPAYMENTMODE_DELETE' && $object->type ==
'card') {
241 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
243 if (!empty($object->stripe_card_ref)) {
244 $stripeacc = $stripe->getStripeAccount($service);
245 $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc);
249 if (empty($stripeacc)) {
250 $customer = \Stripe\Customer::retrieve($stripecu);
252 $customer = \Stripe\Customer::retrieve($stripecu, array(
"stripe_account" => $stripeacc));
256 $card = $stripe->cardStripe($customer, $object, $stripeacc, $servicestatus);
258 if (method_exists($card,
'detach')) {