diff --git a/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php b/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php index f9d28a0b984..fb400ecb7be 100644 --- a/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php +++ b/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php @@ -147,7 +147,7 @@ class InterfaceStripe if ($customer) { $namecleaned = $object->name ? $object->name : null; - $vatcleaned = $object->tva_intra ? $object->tva_intra : null; + $vatcleaned = $object->tva_intra ? $object->tva_intra : null; // Example of valid numbers are 'FR12345678901' or 'FR12345678902' $desccleaned = $object->name_alias ? $object->name_alias : null; $taxexemptcleaned = $object->tva_assuj ? 'none' : 'exempt'; $langcleaned = $object->default_lang ? array(substr($object->default_lang, 0, 2)) : null; @@ -173,10 +173,10 @@ class InterfaceStripe if ($desccleaned != $customer->description) $changerequested++; if (($customer->tax_exempt == 'exempt' && ! $object->tva_assuj) || (! $customer->tax_exempt == 'exempt' && empty($object->tva_assuj))) $changerequested++; if (! isset($customer->tax_ids['data']) && ! is_null($vatcleaned)) $changerequested++; - elseif (isset($customer->tax_ids['data']) && is_null($vatcleaned)) $changerequested++; - elseif (isset($customer->tax_ids['data']) && ! is_null($vatcleaned)) + elseif (isset($customer->tax_ids['data'])) { $taxinfo = reset($customer->tax_ids['data']); + if (empty($taxinfo) && ! empty($vatcleaned)) $changerequested++; if (isset($taxinfo->value) && $vatcleaned != $taxinfo->value) $changerequested++; } @@ -190,10 +190,42 @@ class InterfaceStripe $customer->description = $desccleaned; $customer->preferred_locales = $langcleaned; $customer->tax_exempt = $taxexemptcleaned; - if (! empty($vatcleaned)) $customer->tax_ids = array('object'=>'list', 'data'=>array('value'=>$vatcleaned)); - else $customer->tax_ids = null; - $customer->save(); + try { + // Update Tax info on Stripe + 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 + { + if (! empty($vatcleaned)) + { + $isineec=isInEEC($object); + if ($object->country_code && $isineec) + { + //$taxids = $customer->allTaxIds($customer->id); + $customer->createTaxId($customer->id, array('type'=>'eu_vat', 'value'=>$vatcleaned)); + } + } + else + { + $taxids = $customer->allTaxIds($customer->id); + if (is_array($taxids->data)) + { + foreach($taxids->data as $taxidobj) + { + $customer->deleteTaxId($customer->id, $taxidobj->id); + } + } + } + } + + // Update Customer on Stripe + $customer->save(); + } + catch(Exception $e) + { + //var_dump(\Stripe\Stripe::getApiVersion()); + $this->errors[] = $e->getMessage(); + $ok = -1; + } } } } @@ -206,7 +238,13 @@ class InterfaceStripe $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus); if ($customer) { - $customer->delete(); + try { + $customer->delete(); + } + catch(Exception $e) + { + dol_syslog("Failed to delete Stripe customer ".$e->getMessage(), LOG_WARNING); + } } $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account"; @@ -242,7 +280,7 @@ class InterfaceStripe if ($card) { $card->metadata=array('dol_id'=>$object->id, 'dol_version'=>DOL_VERSION, 'dol_entity'=>$conf->entity, 'ipaddress'=>(empty($_SERVER['REMOTE_ADDR'])?'':$_SERVER['REMOTE_ADDR'])); try { - $card->save($dataforcard); + $card->save(); } catch(Exception $e) { diff --git a/htdocs/public/payment/newpayment.php b/htdocs/public/payment/newpayment.php index 90646ef9b41..5d04b6335c7 100644 --- a/htdocs/public/payment/newpayment.php +++ b/htdocs/public/payment/newpayment.php @@ -430,6 +430,7 @@ if ($action == 'charge' && ! empty($conf->stripe->enabled)) 'dol_version' => DOL_VERSION, 'dol_entity' => $conf->entity, 'dol_company' => $mysoc->name, // Usefull when using multicompany + 'dol_tax_num' => $taxinfo, 'ipaddress'=> getUserRemoteIP() ); @@ -501,24 +502,40 @@ if ($action == 'charge' && ! empty($conf->stripe->enabled)) { $vatcleaned = $vatnumber ? $vatnumber : null; - $taxinfo = array('type'=>'vat'); + /*$taxinfo = array('type'=>'vat'); if ($vatcleaned) { $taxinfo["tax_id"] = $vatcleaned; } // We force data to "null" if not defined as expected by Stripe if (empty($vatcleaned)) $taxinfo=null; + */ dol_syslog("Create anonymous customer card profile", LOG_DEBUG, 0, '_stripe'); + $customer = \Stripe\Customer::create(array( 'email' => $email, 'description' => ($email?'Anonymous customer for '.$email:'Anonymous customer'), 'metadata' => $metadata, - 'tax_info' => $taxinfo, 'source' => $stripeToken // source can be a token OR array('object'=>'card', 'exp_month'=>xx, 'exp_year'=>xxxx, 'number'=>xxxxxxx, 'cvc'=>xxx, 'name'=>'Cardholder's full name', zip ?) )); // Return $customer = array('id'=>'cus_XXXX', ...) + // Create the VAT record in Stripe + /* We don't know country of customer, so we can't create tax + 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 + { + if (! empty($vatcleaned)) + { + $isineec=isInEEC($object); + if ($object->country_code && $isineec) + { + //$taxids = $customer->allTaxIds($customer->id); + $customer->createTaxId($customer->id, array('type'=>'eu_vat', 'value'=>$vatcleaned)); + } + } + }*/ + if (! empty($FULLTAG)) $metadata["FULLTAG"] = $FULLTAG; if (! empty($dol_id)) $metadata["dol_id"] = $dol_id; if (! empty($dol_type)) $metadata["dol_type"] = $dol_type; diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php index 27346a15f5e..d8d0c656dab 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -192,6 +192,7 @@ class Stripe extends CommonObject $vatcleaned = $object->tva_intra ? $object->tva_intra : null; + /* $taxinfo = array('type'=>'vat'); if ($vatcleaned) { @@ -199,8 +200,8 @@ class Stripe extends CommonObject } // We force data to "null" if not defined as expected by Stripe if (empty($vatcleaned)) $taxinfo=null; - $dataforcustomer["tax_info"] = $taxinfo; + */ //$a = \Stripe\Stripe::getApiKey(); //var_dump($a);var_dump($key);exit; @@ -215,6 +216,21 @@ class Stripe extends CommonObject $customer = \Stripe\Customer::create($dataforcustomer, array("stripe_account" => $key)); } + // Create the VAT record in Stripe + 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 + { + if (! empty($vatcleaned)) + { + $isineec=isInEEC($object); + if ($object->country_code && $isineec) + { + //$taxids = $customer->allTaxIds($customer->id); + $customer->createTaxId($customer->id, array('type'=>'eu_vat', 'value'=>$vatcleaned)); + } + } + } + + // Create customer in Dolibarr $sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_account (fk_soc, login, key_account, site, status, entity, date_creation, fk_user_creat)"; $sql .= " VALUES (".$object->id.", '', '".$this->db->escape($customer->id)."', 'stripe', " . $status . ", " . $conf->entity . ", '".$this->db->idate(dol_now())."', ".$user->id.")"; $resql = $this->db->query($sql);