Skip to content

Commit

Permalink
FIX Management of vat info in stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jun 21, 2019
1 parent 0bc0f8a commit a7d7d44
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
54 changes: 46 additions & 8 deletions htdocs/core/triggers/interface_80_modStripe_Stripe.class.php
Expand Up @@ -147,7 +147,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
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;
Expand All @@ -173,10 +173,10 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
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++;
}

Expand All @@ -190,10 +190,42 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$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;
}
}
}
}
Expand All @@ -206,7 +238,13 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$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";
Expand Down Expand Up @@ -242,7 +280,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
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)
{
Expand Down
21 changes: 19 additions & 2 deletions htdocs/public/payment/newpayment.php
Expand Up @@ -430,6 +430,7 @@
'dol_version' => DOL_VERSION,
'dol_entity' => $conf->entity,
'dol_company' => $mysoc->name, // Usefull when using multicompany
'dol_tax_num' => $taxinfo,
'ipaddress'=> getUserRemoteIP()
);

Expand Down Expand Up @@ -501,24 +502,40 @@
{
$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;
Expand Down
18 changes: 17 additions & 1 deletion htdocs/stripe/class/stripe.class.php
Expand Up @@ -192,15 +192,16 @@ public function customerStripe(Societe $object, $key = '', $status = 0, $createi

$vatcleaned = $object->tva_intra ? $object->tva_intra : null;

/*
$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;

$dataforcustomer["tax_info"] = $taxinfo;
*/

//$a = \Stripe\Stripe::getApiKey();
//var_dump($a);var_dump($key);exit;
Expand All @@ -215,6 +216,21 @@ public function customerStripe(Societe $object, $key = '', $status = 0, $createi
$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);
Expand Down

0 comments on commit a7d7d44

Please sign in to comment.