Browse Source

Save Stripe customer id when using online member subscription

Laurent Destailleur 7 years ago
parent
commit
c864dbd515

+ 7 - 3
htdocs/adherents/class/adherent.class.php

@@ -1331,7 +1331,7 @@ class Adherent extends CommonObject
 	 *	Do complementary actions after subscription recording.
 	 *
 	 *	@param	int			$subscriptionid			Id of created subscription
-	 *  @param	string		$option					Which action ('bankdirect', 'invoiceonly', ...)
+	 *  @param	string		$option					Which action ('bankdirect', 'bankviainvoice', 'invoiceonly', ...)
 	 *	@param	int			$accountid				Id bank account
 	 *	@param	int			$datesubscription		Date of subscription
 	 *	@param	int			$paymentdate			Date of payment
@@ -1341,7 +1341,7 @@ class Adherent extends CommonObject
 	 *	@param	string		$num_chq				Numero cheque (if Id bank account provided)
 	 *	@param	string		$emetteur_nom			Name of cheque writer
 	 *	@param	string		$emetteur_banque		Name of bank of cheque
-	 *  @param	string		$autocreatethirdparty	Auto create new thirdparty if member not linked to a thirdparty.
+	 *  @param	string		$autocreatethirdparty	Auto create new thirdparty if member not linked to a thirdparty and we request an option that generate invoice.
 	 *	@return int									<0 if KO, >0 if OK
 	 */
 	function subscriptionComplementaryActions($subscriptionid, $option, $accountid, $datesubscription, $paymentdate, $operation, $label, $amount, $num_chq, $emetteur_nom='', $emetteur_banque='', $autocreatethirdparty=0)
@@ -1352,6 +1352,8 @@ class Adherent extends CommonObject
 
 		$this->invoice = null;	// This will contains invoice if an invoice is created
 
+		dol_syslog("subscriptionComplementaryActions subscriptionid=".$subscriptionid." option=".$option." accountid=".$accountid." datesubscription=".$datesubscription." paymentdate=".$paymentdate." label=".$label." amount=".$amount." num_chq=".$num_chq." autocreatethirdparty=".$autocreatethirdparty);
+
 		// Insert into bank account directlty (if option choosed for) + link to llx_subscription if option is 'bankdirect'
 		if ($option == 'bankdirect' && $accountid)
 		{
@@ -1407,7 +1409,7 @@ class Adherent extends CommonObject
 
 			if (! $error)
 			{
-				if (! ($this->fk_soc > 0))
+				if (! ($this->fk_soc > 0))	// If not yet linked to a company
 				{
 					if ($autocreatethirdparty)
 					{
@@ -1894,6 +1896,8 @@ class Adherent extends CommonObject
 			$label.= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
 		if (! empty($this->firstname) || ! empty($this->lastname))
 			$label.= '<br><b>' . $langs->trans('Name') . ':</b> ' . $this->getFullName($langs);
+		if (! empty($this->societe))
+			$label.= '<br><b>' . $langs->trans('Company') . ':</b> ' . $this->societe;
 		$label.='</div>';
 
 		$url = DOL_URL_ROOT.'/adherents/card.php?rowid='.$this->id;

+ 1 - 1
htdocs/adherents/list.php

@@ -593,7 +593,6 @@ while ($i < min($num, $limit))
 	$memberstatic->ref=$obj->rowid;
 	$memberstatic->lastname=$obj->lastname;
 	$memberstatic->firstname=$obj->firstname;
-	$memberstatic->societe=$obj->company;
 	$memberstatic->statut=$obj->statut;
 	$memberstatic->datefin= $datefin;
 	$memberstatic->socid = $obj->fk_soc;
@@ -605,6 +604,7 @@ while ($i < min($num, $limit))
 	} else {
 		$companyname=$obj->company;
 	}
+	$memberstatic->societe = $companyname;
 
 	print '<tr class="oddeven">';
 

+ 1 - 1
htdocs/core/class/html.form.class.php

@@ -6462,7 +6462,7 @@ class Form
 		else if ($object->element == 'member')
 		{
 			$fullname=$object->getFullName($langs);
-			if ($object->morphy == 'mor') {
+			if ($object->morphy == 'mor' && $object->societe) {
 				$ret.= dol_htmlentities($object->societe) . ((! empty($fullname) && $object->societe != $fullname)?' ('.dol_htmlentities($fullname).')':'');
 			} else {
 				$ret.= dol_htmlentities($fullname) . ((! empty($object->societe) && $object->societe != $fullname)?' ('.dol_htmlentities($object->societe).')':'');

+ 2 - 2
htdocs/public/payment/newpayment.php

@@ -397,7 +397,7 @@ if ($action == 'charge' && ! empty($conf->stripe->enabled))
 
 	$stripeToken = GETPOST("stripeToken",'alpha');
 	$email = GETPOST("email",'alpha');
-	$thirdparty_id=GETPOST('thirdparty_id', 'int');
+	$thirdparty_id=GETPOST('thirdparty_id', 'int');		// Note that for payment following online registration for members, this is empty because thirdparty is created once payment is confirmed by paymentok.php
 	$vatnumber = GETPOST('vatnumber','alpha');
 
 	dol_syslog("stripeToken = ".$stripeToken, LOG_DEBUG, 0, '_stripe');
@@ -419,7 +419,7 @@ if ($action == 'charge' && ! empty($conf->stripe->enabled))
 
 		if ($thirdparty_id > 0)
 		{
-			dol_syslog("Search existing customer profile for thirdparty_id=".$thirdparty_id, LOG_DEBUG, 0, '_stripe');
+			dol_syslog("Search existing Stripe customer profile for thirdparty_id=".$thirdparty_id, LOG_DEBUG, 0, '_stripe');
 
 			$service = 'StripeTest';
 			$servicestatus = 0;

+ 51 - 1
htdocs/public/payment/paymentok.php

@@ -281,6 +281,11 @@ if ($ispaymentok)
 
 	if (in_array('MEM', array_keys($tmptag)))
 	{
+		// Validate member
+		// Create subscription
+		// Create complementary actions (this include creation of thirdparty)
+		// Send confirmation email
+
 		$defaultdelay=1;
 		$defaultdelayunit='y';
 
@@ -384,7 +389,9 @@ if ($ispaymentok)
 
 				if (! $error)
 				{
-					$result = $object->subscriptionComplementaryActions($crowid, $option, $accountid, $datesubscription, $paymentdate, $operation, $label, $amount, $num_chq, $emetteur_nom, $emetteur_banque, 1);
+					$autocreatethirdparty = 1;
+
+					$result = $object->subscriptionComplementaryActions($crowid, $option, $accountid, $datesubscription, $paymentdate, $operation, $label, $amount, $num_chq, $emetteur_nom, $emetteur_banque, $autocreatethirdparty);
 					if ($result < 0)
 					{
 						$error++;
@@ -403,6 +410,49 @@ if ($ispaymentok)
 					}
 				}
 
+				if (! $error)
+				{
+					if ($paymentmethod == 'stripe' && $autocreatethirdparty && $option == 'bankviainvoice')
+					{
+						$thirdparty_id = $object->fk_soc;
+
+						dol_syslog("Search existing Stripe customer profile for thirdparty_id=".$thirdparty_id, LOG_DEBUG, 0, '_stripe');
+
+						$service = 'StripeTest';
+						$servicestatus = 0;
+						if (! empty($conf->global->STRIPE_LIVE) && ! GETPOST('forcesandbox','alpha'))
+						{
+							$service = 'StripeLive';
+							$servicestatus = 1;
+						}
+						$stripeacc = null;	// No Oauth/connect use for public pages
+
+						$thirdparty = new Societe($db);
+						$thirdparty->fetch($thirdparty_id);
+
+						include_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
+						$stripe = new Stripe($db);
+						$customer = $stripe->customerStripe($thirdparty, $stripeacc, $servicestatus, 0);
+
+						if (! $customer && $TRANSACTIONID)	// Not linked to a stripe customer, we make the link
+						{
+							$ch = \Stripe\Charge::retrieve($TRANSACTIONID);		// contains the charge id
+							$stripecu = $ch->customer;							// value 'cus_....'
+
+							$sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_account (fk_soc, login, key_account, site, status, entity, date_creation, fk_user_creat)";
+							$sql .= " VALUES (".$object->fk_soc.", '', '".$db->escape($stripecu)."', 'stripe', " . $servicestatus . ", " . $conf->entity . ", '".$db->idate(dol_now())."', 0)";
+							$resql = $db->query($sql);
+							if (! $resql)
+							{
+								$error++;
+								$errmsg='Failed to save customer stripe id in database ; '.$db->lasterror();
+								$postactionmessages[] = $errmsg;
+								$ispostactionok = -1;
+							}
+						}
+					}
+				}
+
 				if (! $error)
 				{
 					$db->commit();