Add option to create stripe customer if not found (so we can use

function to find without side effect)
This commit is contained in:
Laurent Destailleur 2018-03-13 14:44:35 +01:00
parent a658f7c455
commit f7dd7e89de

View File

@ -124,25 +124,19 @@ class Stripe extends CommonObject
/** /**
* customerStripe * Get the Stripe customer of a thirdparty (with option to create it if not linked yet)
* *
* @param int $id Id of third party * @param int $id Id of third party
* @return string Stripe account ref 'acc_xxxxxxxxxxxxx' * @param string $key Stripe account acc_....
* @return \Stripe\StripeObject|\Stripe\ApiResource * @param int $status Status (0=test, 1=live)
* @param int $createifnotlinkedtostripe 1=Create the stripe customer and the link if the thirdparty is not yet linked to a stripe customer
* @return \Stripe\StripeObject|\Stripe\ApiResource|null Stripe Customer or null if not found
*/ */
public function customerStripe($id,$key) public function customerStripe($id, $key, $status=0, $createifnotlinkedtostripe=0)
{ {
global $conf; global $conf;
if (empty($conf->global->STRIPECONNECT_LIVE)) {
$mode = 0; $sql = "SELECT sa.key_account as key_account, sa.entity"; // key_account is cu_....
} else {
if (empty($conf->global->STRIPE_LIVE)) {
$mode = 0;
} else {
$mode = $conf->global->STRIPE_LIVE;
}
}
$sql = "SELECT sa.key_account as key_account, sa.entity";
$sql.= " FROM " . MAIN_DB_PREFIX . "societe_account as sa"; $sql.= " FROM " . MAIN_DB_PREFIX . "societe_account as sa";
$sql.= " WHERE sa.fk_soc = " . $id; $sql.= " WHERE sa.fk_soc = " . $id;
$sql.= " AND sa.entity IN (".getEntity('societe').")"; $sql.= " AND sa.entity IN (".getEntity('societe').")";
@ -164,24 +158,28 @@ class Stripe extends CommonObject
"stripe_account" => $key "stripe_account" => $key
)); ));
} }
} else { }
else
{
if ($conf->entity == 1) { if ($conf->entity == 1) {
$customer = \Stripe\Customer::create(array( $customer = \Stripe\Customer::create(array(
"email" => $soc->email, "email" => $soc->email,
"business_vat_id" => $soc->tva_intra,
"description" => $soc->name "description" => $soc->name
)); ));
} else { } else {
$customer = \Stripe\Customer::create(array( $customer = \Stripe\Customer::create(array(
"email" => $soc->email, "email" => $soc->email,
"business_vat_id" => $soc->tva_intra,
"description" => $soc->name "description" => $soc->name
), array( ), array(
"stripe_account" => $key "stripe_account" => $key
)); ));
} }
$customer_id = "" . $customer->id . ""; $customer_id = $customer->id;
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_account (fk_soc,key_account,site,status,entity)";
$sql .= " VALUES ($id,'$customer_id','stripe'," . $mode . "," . $conf->entity . ")"; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_account (fk_soc, key_account, site, status, entity)";
dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG); $sql .= " VALUES (".$id.", '".$this->db->escape($customer_id)."', 'stripe', " . $status . "," . $conf->entity . ")";
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
} }
} }
@ -189,25 +187,28 @@ class Stripe extends CommonObject
} }
/** /**
* createPaymentStripe Create charge with public/payment/newpayment.php, stripe/card.php, cronjobs or REST API * Create charge with public/payment/newpayment.php, stripe/card.php, cronjobs or REST API
* *
* @param int $amount amount to pay * @param int $amount Amount to pay
* @param string $currency EUR, GPB... * @param string $currency EUR, GPB...
* @param string $origin order, invoice, contract... * @param string $origin order, invoice, contract...
* @param int $item if of element to pay * @param int $item if of element to pay
* @param string $source src_xxxxx or card_xxxxx or ac_xxxxx * @param string $source src_xxxxx or card_xxxxx or ac_xxxxx
* @param string $customer Stripe account ref 'cu_xxxxxxxxxxxxx' via customerStripe() * @param string $customer Stripe account ref 'cu_xxxxxxxxxxxxx' via customerStripe()
* @param string $account Stripe account ref 'acc_xxxxxxxxxxxxx' via getStripeAccount() * @param string $account Stripe account ref 'acc_xxxxxxxxxxxxx' via getStripeAccount()
* @param int $status Status * @param int $status Status (0=test, 1=live)
* @return Stripe * @return Stripe
*/ */
public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status=0) public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status=0)
{ {
global $conf; global $conf;
if (empty($status)) $service = 'StripeTest';
else $service = 'StripeLive';
$sql = "SELECT sa.key_account as key_account, sa.entity"; $sql = "SELECT sa.key_account as key_account, sa.entity";
$sql.= " FROM " . MAIN_DB_PREFIX . "societe_account as sa"; $sql.= " FROM " . MAIN_DB_PREFIX . "societe_account as sa";
$sql.= " WHERE sa.key_account = " . $customer; $sql.= " WHERE sa.key_account = '" . $this->db->escape($customer) . "'";
//$sql.= " AND sa.entity IN (".getEntity('societe').")"; //$sql.= " AND sa.entity IN (".getEntity('societe').")";
$sql.= " AND sa.site = 'stripe' AND sa.status = ".((int) $status); $sql.= " AND sa.site = 'stripe' AND sa.status = ".((int) $status);
@ -223,7 +224,7 @@ class Stripe extends CommonObject
} else { } else {
$key = NULL; $key = NULL;
} }
$stripeamount = round($amount * 100); $stripeamount = round($amount * 100);
$societe = new Societe($this->db); $societe = new Societe($this->db);
$societe->fetch($fksoc); $societe->fetch($fksoc);
@ -293,9 +294,6 @@ class Stripe extends CommonObject
if (isset($charge->id)) {} if (isset($charge->id)) {}
} }
if (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox','alpha')) $service = 'StripeTest';
else $service = 'StripeLive';
$return->statut = 'success'; $return->statut = 'success';
$return->id = $charge->id; $return->id = $charge->id;
if ($charge->source->type == 'card') { if ($charge->source->type == 'card') {