From 7d433e43fac407083b508e98b77ec6e7feaacb16 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 31 Dec 2022 16:24:32 +0100 Subject: [PATCH] Add hooks on payment pages --- htdocs/core/class/hookmanager.class.php | 1 + htdocs/core/lib/payments.lib.php | 11 +++++++++-- htdocs/public/payment/newpayment.php | 8 +------- htdocs/public/payment/paymentok.php | 23 ++++++++++++++++++++--- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/htdocs/core/class/hookmanager.class.php b/htdocs/core/class/hookmanager.class.php index 191887e1725..71a92ebb6b1 100644 --- a/htdocs/core/class/hookmanager.class.php +++ b/htdocs/core/class/hookmanager.class.php @@ -197,6 +197,7 @@ class HookManager 'getFormatedSupplierRef', 'getIdProfUrl', 'getInputIdProf', + 'isPaymentOK', 'menuDropdownQuickaddItems', 'menuLeftMenuItems', 'moveUploadedFile', diff --git a/htdocs/core/lib/payments.lib.php b/htdocs/core/lib/payments.lib.php index 44691f7c2c4..d5ddb158d6d 100644 --- a/htdocs/core/lib/payments.lib.php +++ b/htdocs/core/lib/payments.lib.php @@ -146,7 +146,7 @@ function payment_supplier_prepare_head(Paiement $object) */ function getValidOnlinePaymentMethods($paymentmethod = '') { - global $conf, $langs; + global $conf, $langs, $hookmanager, $action; $validpaymentmethod = array(); @@ -162,8 +162,15 @@ function getValidOnlinePaymentMethods($paymentmethod = '') $langs->load("stripe"); $validpaymentmethod['stripe'] = 'valid'; } - // TODO Add trigger + // This hook is used to complete the $validpaymentmethod array so an external payment modules + // can add its own key (ie 'payzen' for Payzen, ...) + $parameters = [ + 'paymentmethod' => $paymentmethod, + 'validpaymentmethod' => &$validpaymentmethod + ]; + $tmpobject = new stdClass(); + $hookmanager->executeHooks('doValidatePayment', $parameters, $tmpobject, $action); return $validpaymentmethod; } diff --git a/htdocs/public/payment/newpayment.php b/htdocs/public/payment/newpayment.php index e9823f2d0f7..c4b2d502d3d 100644 --- a/htdocs/public/payment/newpayment.php +++ b/htdocs/public/payment/newpayment.php @@ -281,15 +281,9 @@ if ((empty($paymentmethod) || $paymentmethod == 'stripe') && isModEnabled('strip } // Initialize $validpaymentmethod +// The list can be complete by the hook 'doValidatePayment' executed inside getValidOnlinePaymentMethods() $validpaymentmethod = getValidOnlinePaymentMethods($paymentmethod); -// This hook is used to push to $validpaymentmethod by external payment modules (ie Payzen, ...) -$parameters = [ - 'paymentmethod' => $paymentmethod, - 'validpaymentmethod' => &$validpaymentmethod -]; -$reshook = $hookmanager->executeHooks('doValidatePayment', $parameters, $object, $action); - // Check security token $tmpsource = $source; if ($tmpsource == 'membersubscription') { diff --git a/htdocs/public/payment/paymentok.php b/htdocs/public/payment/paymentok.php index 6d50d1c8c76..3bfdfc5f089 100644 --- a/htdocs/public/payment/paymentok.php +++ b/htdocs/public/payment/paymentok.php @@ -54,13 +54,14 @@ if (is_numeric($entity)) { require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; -require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php'; -require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorbooth.class.php'; - if (isModEnabled('paypal')) { require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypal.lib.php'; require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypalfunctions.lib.php'; } +// Hook to be used by external payment modules (ie Payzen, ...) +include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; +$hookmanager = new HookManager($db); +$hookmanager->initHooks(array('newpayment')); $langs->loadLangs(array("main", "other", "dict", "bills", "companies", "paybox", "paypal")); @@ -337,6 +338,16 @@ if (isModEnabled('stripe')) { } } +// Check status of the object to verify if it is paid by external payment modules +$action = ''; +$parameters = [ + 'paymentmethod' => $paymentmethod, +]; +$reshook = $hookmanager->executeHooks('isPaymentOK', $parameters, $object, $action); +if ($reshook >= 0) { + $ispaymentok = $hookmanager->resArray['ispaymentok']; +} + // If data not provided from back url, search them into the session env if (empty($ipaddress)) { @@ -1142,6 +1153,8 @@ if ($ispaymentok) { // (we need first that the donation module is able to generate a pdf document for the cerfa with pre filled content) } elseif (array_key_exists('ATT', $tmptag) && $tmptag['ATT'] > 0) { // Record payment for registration to an event for an attendee + require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php'; + require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorbooth.class.php'; include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; $object = new Facture($db); $result = $object->fetch($ref); @@ -1355,6 +1368,8 @@ if ($ispaymentok) { } } elseif (array_key_exists('BOO', $tmptag) && $tmptag['BOO'] > 0) { // Record payment for booth or conference + require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php'; + require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorbooth.class.php'; include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; $object = new Facture($db); $result = $object->fetch($ref); @@ -1461,6 +1476,8 @@ if ($ispaymentok) { if (!$error) { // Putting the booth to "suggested" state + require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php'; + require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorbooth.class.php'; $booth = new ConferenceOrBooth($db); $resultbooth = $booth->fetch((int) $tmptag['BOO']); if ($resultbooth < 0) {