From c112c807ac747011789f01878611f675a86fb979 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 18 Dec 2020 19:02:36 +0100 Subject: [PATCH] Fix action after payment on member registration --- htdocs/public/payment/newpayment.php | 8 ++--- htdocs/public/payment/paymentok.php | 52 +++++++++++++++++++++------- htdocs/stripe/class/stripe.class.php | 8 ++--- htdocs/theme/eldy/global.inc.php | 2 +- htdocs/theme/md/style.css.php | 2 +- 5 files changed, 49 insertions(+), 23 deletions(-) diff --git a/htdocs/public/payment/newpayment.php b/htdocs/public/payment/newpayment.php index d90f8684673..1a643323dce 100644 --- a/htdocs/public/payment/newpayment.php +++ b/htdocs/public/payment/newpayment.php @@ -1660,19 +1660,19 @@ if ($action != 'dopayment') $reshook = $hookmanager->executeHooks('doCheckStatus', $parameters, $object, $action); if ($source == 'order' && $object->billed) { - print '

'.$langs->trans("OrderBilled").''; + print '

'.$langs->trans("OrderBilled").''; } elseif ($source == 'invoice' && $object->paye) { - print '

'.$langs->trans("InvoicePaid").''; + print '

'.$langs->trans("InvoicePaid").''; } elseif ($source == 'donation' && $object->paid) { - print '

'.$langs->trans("DonationPaid").''; + print '

'.$langs->trans("DonationPaid").''; } else { // Membership can be paid and we still allow to make renewal if ($source == 'membersubscription' && $object->datefin > dol_now()) { $langs->load("members"); - print '
'.$langs->trans("MembershipPaid", dol_print_date($object->datefin, 'day')).'
'; + print '
'.$langs->trans("MembershipPaid", dol_print_date($object->datefin, 'day')).'
'; print '
'.$langs->trans("PaymentWillBeRecordedForNextPeriod").'
'; } diff --git a/htdocs/public/payment/paymentok.php b/htdocs/public/payment/paymentok.php index a4600c57c22..ef4931c4b4a 100644 --- a/htdocs/public/payment/paymentok.php +++ b/htdocs/public/payment/paymentok.php @@ -471,8 +471,10 @@ if ($ispaymentok) $thirdparty = new Societe($db); $thirdparty->fetch($thirdparty_id); - include_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php'; + include_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php'; // This also set $stripearrayofkeysbyenv $stripe = new Stripe($db); + //$stripeacc = $stripe->getStripeAccount($service); Already defined previously + $customer = $stripe->customerStripe($thirdparty, $stripeacc, $servicestatus, 0); if (!$customer && $TRANSACTIONID) // Not linked to a stripe customer, we make the link @@ -480,21 +482,48 @@ if ($ispaymentok) dol_syslog("No stripe profile found, so we add it for TRANSACTIONID = ".$TRANSACTIONID, LOG_DEBUG, 0, '_payment'); try { - $ch = \Stripe\Charge::retrieve($TRANSACTIONID); // contains the charge id - $stripecu = $ch->customer; // value 'cus_....' + global $stripearrayofkeysbyenv; + \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$servicestatus]['secret_key']); - $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) - { + if (preg_match('/^pi_/', $TRANSACTIONID)) { + // This may throw an error if not found. + $chpi = \Stripe\PaymentIntent::retrieve($TRANSACTIONID); // payment_intent (pi_...) + } else { + // This throw an error if not found + $chpi = \Stripe\Charge::retrieve($TRANSACTIONID); // old method, contains the charge id (ch_...) + } + + if ($chpi) { + $stripecu = $chpi->customer; // value 'cus_....'. WARNING: This property may be empty if first payment was recorded before the stripe customer was created. + + if (empty($stripecu)) { + // This include the INSERT + $customer = $stripe->customerStripe($thirdparty, $stripeacc, $servicestatus, 1); + + // Link this customer to the payment intent + if (preg_match('/^pi_/', $TRANSACTIONID) && $customer) { + \Stripe\PaymentIntent::update($chpi->id, array('customer' => $customer->id)); + } + } else { + $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_account (fk_soc, login, key_account, site, site_account, status, entity, date_creation, fk_user_creat)"; + $sql .= " VALUES (".$object->fk_soc.", '', '".$db->escape($stripecu)."', 'stripe', '".$db->escape($stripearrayofkeysbyenv[$servicestatus]['publishable_key'])."', ".$servicestatus.", ".$conf->entity.", '".$db->idate(dol_now())."', 0)"; + $resql = $db->query($sql); + if (!$resql) { // should not happen + $error++; + $errmsg = 'Failed to insert customer stripe id in database : '.$db->lasterror(); + dol_syslog($errmsg, LOG_ERR, 0, '_payment'); + $postactionmessages[] = $errmsg; + $ispostactionok = -1; + } + } + } else { // should not happen $error++; - $errmsg = 'Failed to insert customer stripe id in database : '.$db->lasterror(); + $errmsg = 'Failed to retreive paymentintent or charge from id'; dol_syslog($errmsg, LOG_ERR, 0, '_payment'); $postactionmessages[] = $errmsg; $ispostactionok = -1; } - } catch (Exception $e) { + } catch (Exception $e) { // should not happen $error++; $errmsg = 'Failed to get or save customer stripe id in database : '.$e->getMessage(); dol_syslog($errmsg, LOG_ERR, 0, '_payment'); @@ -505,8 +534,7 @@ if ($ispaymentok) } } - if (!$error) - { + if (!$error) { $db->commit(); } else { $db->rollback(); diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php index e7856a82899..db225196ea7 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -142,7 +142,7 @@ class Stripe extends CommonObject /** - * Get the Stripe customer of a thirdparty (with option to create it if not linked yet). + * Get the Stripe customer of a thirdparty (with option to create it in Stripe if not linked yet). * Search on site_account = 0 or = $stripearrayofkeysbyenv[$status]['publishable_key'] * * @param Societe $object Object thirdparty to check, or create on stripe (create on stripe also update the stripe_account table for current entity) @@ -179,8 +179,7 @@ class Stripe extends CommonObject $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); - if ($num) - { + if ($num) { $obj = $this->db->fetch_object($resql); $tiers = $obj->key_account; @@ -199,8 +198,7 @@ class Stripe extends CommonObject // For exemple, we may have error: 'No such customer: cus_XXXXX; a similar object exists in live mode, but a test mode key was used to make this request.' $this->error = $e->getMessage(); } - } elseif ($createifnotlinkedtostripe) - { + } elseif ($createifnotlinkedtostripe) { $ipaddress = getUserRemoteIP(); $dataforcustomer = array( diff --git a/htdocs/theme/eldy/global.inc.php b/htdocs/theme/eldy/global.inc.php index 6a9cbf112df..337c72ec0d9 100644 --- a/htdocs/theme/eldy/global.inc.php +++ b/htdocs/theme/eldy/global.inc.php @@ -769,7 +769,7 @@ span.fa.fa-plus-circle.paddingleft { padding-bottom: 2px; } -.size15x { font-size: 1.5em; } +.size15x { font-size: 1.5em !important; } .fa-toggle-on, .fa-toggle-off, .size2x { font-size: 2em; } .websiteselectionsection .fa-toggle-on, .websiteselectionsection .fa-toggle-off, .asetresetmodule .fa-toggle-on, .asetresetmodule .fa-toggle-off, diff --git a/htdocs/theme/md/style.css.php b/htdocs/theme/md/style.css.php index 550f80216d2..0f251bb3067 100644 --- a/htdocs/theme/md/style.css.php +++ b/htdocs/theme/md/style.css.php @@ -840,7 +840,7 @@ body[class*="colorblind-"] .text-success{ color: rgb() !important; } -.size15x { font-size: 1.5em; } +.size15x { font-size: 1.5em !important; } .fa-toggle-on, .fa-toggle-off, .size2x { font-size: 2em; } .websiteselectionsection .fa-toggle-on, .websiteselectionsection .fa-toggle-off, .asetresetmodule .fa-toggle-on, .asetresetmodule .fa-toggle-off {