2
0
forked from Wavyzz/dolibarr

FIX Management of vat info in stripe

This commit is contained in:
Laurent Destailleur
2019-06-21 19:58:28 +02:00
parent 6f35c53f3a
commit b6266953e9
3 changed files with 82 additions and 11 deletions

View File

@@ -147,7 +147,7 @@ class InterfaceStripe
if ($customer) if ($customer)
{ {
$namecleaned = $object->name ? $object->name : null; $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; $desccleaned = $object->name_alias ? $object->name_alias : null;
$taxexemptcleaned = $object->tva_assuj ? 'none' : 'exempt'; $taxexemptcleaned = $object->tva_assuj ? 'none' : 'exempt';
$langcleaned = $object->default_lang ? array(substr($object->default_lang, 0, 2)) : null; $langcleaned = $object->default_lang ? array(substr($object->default_lang, 0, 2)) : null;
@@ -173,10 +173,10 @@ class InterfaceStripe
if ($desccleaned != $customer->description) $changerequested++; if ($desccleaned != $customer->description) $changerequested++;
if (($customer->tax_exempt == 'exempt' && ! $object->tva_assuj) || (! $customer->tax_exempt == 'exempt' && empty($object->tva_assuj))) $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++; 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']))
elseif (isset($customer->tax_ids['data']) && ! is_null($vatcleaned))
{ {
$taxinfo = reset($customer->tax_ids['data']); $taxinfo = reset($customer->tax_ids['data']);
if (empty($taxinfo) && ! empty($vatcleaned)) $changerequested++;
if (isset($taxinfo->value) && $vatcleaned != $taxinfo->value) $changerequested++; if (isset($taxinfo->value) && $vatcleaned != $taxinfo->value) $changerequested++;
} }
@@ -190,11 +190,43 @@ class InterfaceStripe
$customer->description = $desccleaned; $customer->description = $desccleaned;
$customer->preferred_locales = $langcleaned; $customer->preferred_locales = $langcleaned;
$customer->tax_exempt = $taxexemptcleaned; $customer->tax_exempt = $taxexemptcleaned;
if (! empty($vatcleaned)) $customer->tax_ids = array('object'=>'list', 'data'=>array('value'=>$vatcleaned));
else $customer->tax_ids = null;
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(); $customer->save();
} }
catch(Exception $e)
{
//var_dump(\Stripe\Stripe::getApiVersion());
$this->errors[] = $e->getMessage();
$ok = -1;
}
}
} }
} }
} }
@@ -206,8 +238,14 @@ class InterfaceStripe
$customer = $stripe->customerStripe($object, $stripeacc, $servicestatus); $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus);
if ($customer) if ($customer)
{ {
try {
$customer->delete(); $customer->delete();
} }
catch(Exception $e)
{
dol_syslog("Failed to delete Stripe customer ".$e->getMessage(), LOG_WARNING);
}
}
$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account";
$sql.= " WHERE site='stripe' AND fk_soc = " . $object->id; $sql.= " WHERE site='stripe' AND fk_soc = " . $object->id;
@@ -242,7 +280,7 @@ class InterfaceStripe
if ($card) { 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'])); $card->metadata=array('dol_id'=>$object->id, 'dol_version'=>DOL_VERSION, 'dol_entity'=>$conf->entity, 'ipaddress'=>(empty($_SERVER['REMOTE_ADDR'])?'':$_SERVER['REMOTE_ADDR']));
try { try {
$card->save($dataforcard); $card->save();
} }
catch(Exception $e) catch(Exception $e)
{ {

View File

@@ -430,6 +430,7 @@ if ($action == 'charge' && ! empty($conf->stripe->enabled))
'dol_version' => DOL_VERSION, 'dol_version' => DOL_VERSION,
'dol_entity' => $conf->entity, 'dol_entity' => $conf->entity,
'dol_company' => $mysoc->name, // Usefull when using multicompany 'dol_company' => $mysoc->name, // Usefull when using multicompany
'dol_tax_num' => $taxinfo,
'ipaddress'=> getUserRemoteIP() 'ipaddress'=> getUserRemoteIP()
); );
@@ -501,24 +502,40 @@ if ($action == 'charge' && ! empty($conf->stripe->enabled))
{ {
$vatcleaned = $vatnumber ? $vatnumber : null; $vatcleaned = $vatnumber ? $vatnumber : null;
$taxinfo = array('type'=>'vat'); /*$taxinfo = array('type'=>'vat');
if ($vatcleaned) if ($vatcleaned)
{ {
$taxinfo["tax_id"] = $vatcleaned; $taxinfo["tax_id"] = $vatcleaned;
} }
// We force data to "null" if not defined as expected by Stripe // We force data to "null" if not defined as expected by Stripe
if (empty($vatcleaned)) $taxinfo=null; if (empty($vatcleaned)) $taxinfo=null;
*/
dol_syslog("Create anonymous customer card profile", LOG_DEBUG, 0, '_stripe'); dol_syslog("Create anonymous customer card profile", LOG_DEBUG, 0, '_stripe');
$customer = \Stripe\Customer::create(array( $customer = \Stripe\Customer::create(array(
'email' => $email, 'email' => $email,
'description' => ($email?'Anonymous customer for '.$email:'Anonymous customer'), 'description' => ($email?'Anonymous customer for '.$email:'Anonymous customer'),
'metadata' => $metadata, '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 ?) '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', ...) // 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($FULLTAG)) $metadata["FULLTAG"] = $FULLTAG;
if (! empty($dol_id)) $metadata["dol_id"] = $dol_id; if (! empty($dol_id)) $metadata["dol_id"] = $dol_id;
if (! empty($dol_type)) $metadata["dol_type"] = $dol_type; if (! empty($dol_type)) $metadata["dol_type"] = $dol_type;

View File

@@ -192,6 +192,7 @@ class Stripe extends CommonObject
$vatcleaned = $object->tva_intra ? $object->tva_intra : null; $vatcleaned = $object->tva_intra ? $object->tva_intra : null;
/*
$taxinfo = array('type'=>'vat'); $taxinfo = array('type'=>'vat');
if ($vatcleaned) if ($vatcleaned)
{ {
@@ -199,8 +200,8 @@ class Stripe extends CommonObject
} }
// We force data to "null" if not defined as expected by Stripe // We force data to "null" if not defined as expected by Stripe
if (empty($vatcleaned)) $taxinfo=null; if (empty($vatcleaned)) $taxinfo=null;
$dataforcustomer["tax_info"] = $taxinfo; $dataforcustomer["tax_info"] = $taxinfo;
*/
//$a = \Stripe\Stripe::getApiKey(); //$a = \Stripe\Stripe::getApiKey();
//var_dump($a);var_dump($key);exit; //var_dump($a);var_dump($key);exit;
@@ -215,6 +216,21 @@ class Stripe extends CommonObject
$customer = \Stripe\Customer::create($dataforcustomer, array("stripe_account" => $key)); $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 = "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.")"; $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); $resql = $this->db->query($sql);