diff --git a/htdocs/install/mysql/migration/10.0.0-11.0.0.sql b/htdocs/install/mysql/migration/10.0.0-11.0.0.sql index 4bacfa9b765..5d31d3de9dc 100644 --- a/htdocs/install/mysql/migration/10.0.0-11.0.0.sql +++ b/htdocs/install/mysql/migration/10.0.0-11.0.0.sql @@ -59,6 +59,8 @@ ALTER TABLE llx_emailcollector_emailcollectoraction ADD COLUMN position integer -- For v11 +ALTER TABLE llx_societe_account ADD COLUMN site_account varchar(128); + UPDATE llx_holiday SET ref = rowid WHERE ref IS NULL; -- VMYSQL4.3 ALTER TABLE llx_holiday MODIFY COLUMN ref varchar(30) NOT NULL; -- VPGSQL8.2 ALTER TABLE llx_holiday ALTER COLUMN ref SET NOT NULL; diff --git a/htdocs/install/mysql/tables/llx_societe_account.sql b/htdocs/install/mysql/tables/llx_societe_account.sql index 605a3d85313..feffc7c9bd6 100644 --- a/htdocs/install/mysql/tables/llx_societe_account.sql +++ b/htdocs/install/mysql/tables/llx_societe_account.sql @@ -20,13 +20,14 @@ CREATE TABLE llx_societe_account( -- BEGIN MODULEBUILDER FIELDS rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL, entity integer DEFAULT 1, - key_account varchar(128), + key_account varchar(128), -- the id of third party in external web site (for site_account if site_account defined) login varchar(128) NOT NULL, pass_encoding varchar(24), pass_crypted varchar(128), pass_temp varchar(128), -- temporary password when asked for forget password fk_soc integer, site varchar(128), -- name of external web site + site_account varchar(128), -- a key to identify the account on external web site fk_website integer, -- id of local web site note_private text, date_last_login datetime, diff --git a/htdocs/societe/class/societeaccount.class.php b/htdocs/societe/class/societeaccount.class.php index abfe4cad716..49ee793b285 100644 --- a/htdocs/societe/class/societeaccount.class.php +++ b/htdocs/societe/class/societeaccount.class.php @@ -86,7 +86,8 @@ class SocieteAccount extends CommonObject 'pass_temp' => array('type'=>'varchar(128)', 'label'=>'Temp', 'visible'=>0, 'enabled'=>0, 'position'=>32, 'notnull'=>-1,), 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1, 'index'=>1), 'site' => array('type'=>'varchar(128)', 'label'=>'Site', 'visible'=>-1, 'enabled'=>1, 'position'=>41), - 'fk_website' => array('type'=>'integer:Website:website/class/website.class.php', 'label'=>'WebSite', 'visible'=>1, 'enabled'=>1, 'position'=>42, 'notnull'=>-1, 'index'=>1), + 'site_account' => array('type'=>'varchar(128)', 'label'=>'SiteAccount', 'visible'=>-1, 'enabled'=>1, 'position'=>42, 'help'=>'A key to identify the account on external web site'), + 'fk_website' => array('type'=>'integer:Website:website/class/website.class.php', 'label'=>'WebSite', 'visible'=>1, 'enabled'=>1, 'position'=>43, 'notnull'=>-1, 'index'=>1), 'date_last_login' => array('type'=>'datetime', 'label'=>'LastConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>50, 'notnull'=>0,), 'date_previous_login' => array('type'=>'datetime', 'label'=>'PreviousConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>51, 'notnull'=>0,), //'note_public' => array('type'=>'text', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>45, 'notnull'=>-1,), @@ -121,6 +122,7 @@ class SocieteAccount extends CommonObject public $fk_soc; public $site; + public $site_account; /** * @var integer|string date_last_login @@ -294,21 +296,22 @@ class SocieteAccount extends CommonObject /** * Try to find the external customer id of a thirdparty for another site/system. * - * @param int $id Id of third party - * @param string $site Site (example: 'stripe', '...') - * @param int $status Status (0=test, 1=live) - * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or '' + * @param int $id Id of third party + * @param string $site Site (example: 'stripe', '...') + * @param int $status Status (0=test, 1=live) + * @param string $site_account Value to use to identify with account to use on site when site can offer several accounts. For example: 'pk_live_123456' when using Stripe service. + * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or '' * @see getThirdPartyID() */ - public function getCustomerAccount($id, $site, $status = 0) + public function getCustomerAccount($id, $site, $status = 0, $site_account = '') { $sql = "SELECT sa.key_account as key_account, sa.entity"; - $sql.= " FROM " . MAIN_DB_PREFIX . "societe_account as sa"; - $sql.= " WHERE sa.fk_soc = " . $id; - $sql.= " AND sa.entity IN (".getEntity('societe').")"; - $sql.= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status); - $sql.= " AND key_account IS NOT NULL AND key_account <> ''"; - //$sql.= " ORDER BY sa.key_account DESC"; + $sql .= " FROM " . MAIN_DB_PREFIX . "societe_account as sa"; + $sql .= " WHERE sa.fk_soc = " . $id; + $sql .= " AND sa.entity IN (".getEntity('societe').")"; + $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status); + $sql .= " AND key_account IS NOT NULL AND key_account <> ''"; + $sql .= " ORDER BY sa.site_account DESC"; // To get the entry with a site_account defined in priority dol_syslog(get_class($this) . "::getCustomerAccount Try to find the first system customer id for ".$site." of thirdparty id=".$id." (exemple: cus_.... for stripe)", LOG_DEBUG); $result = $this->db->query($sql); @@ -327,7 +330,7 @@ class SocieteAccount extends CommonObject } /** - * Try to find the thirdparty id for an another site/system external id. + * Try to find the thirdparty id from an another site/system external id. * * @param string $id Id of customer in external system (example: 'cu_xxxxxxxxxxxxx', ...) * @param string $site Site (example: 'stripe', '...') diff --git a/htdocs/societe/paymentmodes.php b/htdocs/societe/paymentmodes.php index 23a7c30f77e..e10603a2ba6 100644 --- a/htdocs/societe/paymentmodes.php +++ b/htdocs/societe/paymentmodes.php @@ -79,9 +79,14 @@ if (!empty($conf->stripe->enabled)) $servicestatus = 1; } + // Force to use the correct API key + global $stripearrayofkeysbyenv; + $site_account = $stripearrayofkeysbyenv[$servicestatus]['public_key']; + //var_dump($site_account); + $stripe = new Stripe($db); - $stripeacc = $stripe->getStripeAccount($service); // Get Stripe OAuth connect account (no network access here) - $stripecu = $stripe->getStripeCustomerAccount($object->id, $servicestatus); // Get remote Stripe customer 'cus_...' (no network access here) + $stripeacc = $stripe->getStripeAccount($service); // Get Stripe OAuth connect account (no remote access to Stripe here) + $stripecu = $stripe->getStripeCustomerAccount($object->id, $servicestatus, $site_account); // Get remote Stripe customer 'cus_...' (no remote access to Stripe here) } diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php index 5f8e264e9ab..e425b54de6f 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -125,15 +125,16 @@ class Stripe extends CommonObject /** * getStripeCustomerAccount * - * @param int $id Id of third party - * @param int $status Status - * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or '' + * @param int $id Id of third party + * @param int $status Status + * @param string $site_account Value to use to identify with account to use on site when site can offer several accounts. For example: 'pk_live_123456' when using Stripe service. + * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or '' */ - public function getStripeCustomerAccount($id, $status = 0) + public function getStripeCustomerAccount($id, $status = 0, $site_account = '') { include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; $societeaccount = new SocieteAccount($this->db); - return $societeaccount->getCustomerAccount($id, 'stripe', $status); // Get thirdparty cus_... + return $societeaccount->getCustomerAccount($id, 'stripe', $status, $site_account); // Get thirdparty cus_... }