From db15f04ab05efb95970f55676d0fe097abbe36ea Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 30 Jul 2023 14:00:22 +0200 Subject: [PATCH] FIX fatal error on Stripe setup page and security when terminal not used --- htdocs/stripe/admin/stripe.php | 59 ++++++++++++++++++++-------------- htdocs/stripe/ajax/ajax.php | 16 +++++++-- htdocs/takepos/pay.php | 31 +++++++++++------- 3 files changed, 68 insertions(+), 38 deletions(-) diff --git a/htdocs/stripe/admin/stripe.php b/htdocs/stripe/admin/stripe.php index 1c5a507b619..4c4fa48f759 100644 --- a/htdocs/stripe/admin/stripe.php +++ b/htdocs/stripe/admin/stripe.php @@ -394,35 +394,44 @@ if ($conf->global->MAIN_FEATURES_LEVEL >= 2) { // TODO Not used by current code $service = 'StripeLive'; $servicestatus = 1; } - global $stripearrayofkeysbyenv; - $site_account = $stripearrayofkeysbyenv[$servicestatus]['secret_key']; - if (!empty($site_account)) { - \Stripe\Stripe::setApiKey($site_account); - } - if (isModEnabled('stripe') && (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox', 'alpha'))) { - $service = 'StripeTest'; - $servicestatus = '0'; - dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode', 'Stripe'), '', 'warning'); - } else { - $service = 'StripeLive'; - $servicestatus = '1'; - } - $stripe = new Stripe($db); - if (!empty($site_account)) { - // If $site_account not defined, then key not set and no way to call API Location - $stripeacc = $stripe->getStripeAccount($service); - if ($stripeacc) { - $locations = \Stripe\Terminal\Location::all('', array("stripe_account" => $stripeacc)); + + try { + global $stripearrayofkeysbyenv; + $site_account = $stripearrayofkeysbyenv[$servicestatus]['secret_key']; + if (!empty($site_account)) { + \Stripe\Stripe::setApiKey($site_account); + } + if (isModEnabled('stripe') && (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox', 'alpha'))) { + $service = 'StripeTest'; + $servicestatus = '0'; + dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode', 'Stripe'), '', 'warning'); } else { - $locations = \Stripe\Terminal\Location::all(); + $service = 'StripeLive'; + $servicestatus = '1'; + } + $stripe = new Stripe($db); + if (!empty($site_account)) { + // If $site_account not defined, then key not set and no way to call API Location + $stripeacc = $stripe->getStripeAccount($service); + if ($stripeacc) { + $locations = \Stripe\Terminal\Location::all('', array("stripe_account" => $stripeacc)); + } else { + $locations = \Stripe\Terminal\Location::all(); + } + } + } catch (Exception $e) { + print $e->getMessage().'
'; + } + + // Define the array $location + $location = array(); + $location[""] = $langs->trans("NotDefined"); + if (!empty($locations)) { + foreach ($locations as $tmplocation) { + $location[$tmplocation->id] = $tmplocation->display_name; } } - $location = array(); - $location[""] = $langs->trans("NotDefined"); - foreach ($locations as $tmplocation) { - $location[$tmplocation->id] = $tmplocation->display_name; - } print $form->selectarray("STRIPE_LOCATION", $location, getDolGlobalString('STRIPE_LOCATION')); print ''; } diff --git a/htdocs/stripe/ajax/ajax.php b/htdocs/stripe/ajax/ajax.php index 5fa4da88ae7..664e21303dd 100644 --- a/htdocs/stripe/ajax/ajax.php +++ b/htdocs/stripe/ajax/ajax.php @@ -18,6 +18,11 @@ /** * \file htdocs/stripe/ajax/ajax.php * \brief Ajax action for Stipe ie: Terminal + * + * Calling with + * action=getConnexionToken return a token of Stripe terminal + * action=createPaymentIntent generates a payment intent + * action=capturePaymentIntent generates a payment */ if (!defined('NOTOKENRENEWAL')) { @@ -49,7 +54,12 @@ $servicestatus = GETPOST('servicestatus', 'int'); $amount = GETPOST('amount', 'int'); if (empty($user->rights->takepos->run)) { - accessforbidden(); + accessforbidden('Not allowed to use TakePOS'); +} + +$usestripeterminals = getDolGlobalString('STRIPE_LOCATION'); +if (! $usestripeterminals) { + accessforbidden('Feature to use Stripe terminals not enabled'); } @@ -68,7 +78,9 @@ if ($action == 'getConnexionToken') { // The ConnectionToken's secret lets you connect to any Stripe Terminal reader // and take payments with your Stripe account. $array = array(); - if (isset($location) && !empty($location)) $array['location'] = $location; + if (isset($location) && !empty($location)) { + $array['location'] = $location; + } if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage $connectionToken = \Stripe\Terminal\ConnectionToken::create($array); } else { diff --git a/htdocs/takepos/pay.php b/htdocs/takepos/pay.php index 0f2a54ced68..fdc2a4f319d 100644 --- a/htdocs/takepos/pay.php +++ b/htdocs/takepos/pay.php @@ -96,28 +96,34 @@ if (isModEnabled('stripe')) { $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) $keyforstripeterminalbank = "CASHDESK_ID_BANKACCOUNT_STRIPETERMINAL".$_SESSION["takeposterminal"]; - ?> + + $usestripeterminals = getDolGlobalString('STRIPE_LOCATION'); + + if ($usestripeterminals) { + ?> -global->STRIPE_LIVE) || GETPOST('forcesandbox', 'alpha'))) { dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode', 'Stripe'), '', 'warning', 1);