From 2e0e5496f65b1e3755c70ce7a6e36b4930ccfbac Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Sun, 24 Oct 2021 11:24:46 +0100
Subject: [PATCH] Fix for non-default Wordpress basepage and AJAX reload of
 payment elements

---
 js/crm.payment.js |  9 +++++----
 mjwshared.php     | 11 +++++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/js/crm.payment.js b/js/crm.payment.js
index 2b07ab6..e104b2f 100644
--- a/js/crm.payment.js
+++ b/js/crm.payment.js
@@ -285,10 +285,11 @@
       // /civicrm/payment/form? occurs when a payproc is selected on page
       // /civicrm/contact/view/participant occurs when payproc is first loaded on event credit card payment
       // On wordpress these are urlencoded
-      return (url.match("civicrm(\/|%2F)payment(\/|%2F)form") !== null) ||
-        (url.match("civicrm(\/|\%2F)contact(\/|\%2F)view(\/|\%2F)participant") !== null) ||
-        (url.match("civicrm(\/|\%2F)contact(\/|\%2F)view(\/|\%2F)membership") !== null) ||
-        (url.match("civicrm(\/|\%2F)contact(\/|\%2F)view(\/|\%2F)contribution") !== null);
+      var basePage = CRM.config.isFrontend ? CRM.vars.payment.basePage : 'civicrm';
+      return (url.match(basePage + "(\/|%2F)payment(\/|%2F)form") !== null) ||
+        (url.match(basePage + "(\/|\%2F)contact(\/|\%2F)view(\/|\%2F)participant") !== null) ||
+        (url.match(basePage + "(\/|\%2F)contact(\/|\%2F)view(\/|\%2F)membership") !== null) ||
+        (url.match(basePage + "(\/|\%2F)contact(\/|\%2F)view(\/|\%2F)contribution") !== null);
     },
 
     /**
diff --git a/mjwshared.php b/mjwshared.php
index 6fc9111..7f0458a 100644
--- a/mjwshared.php
+++ b/mjwshared.php
@@ -261,8 +261,19 @@ function mjwshared_civicrm_buildForm($formName, &$form) {
     return;
   }
 
+  // On Wordpress frontend we may have a different basePage (eg. mysite.com/mycrm/contribute/transact)
+  // CRM.payment.isAJAXPaymentForm requires the basePage to compare URLs.
+  $basePage = 'civicrm';
+  if (CRM_Core_Config::singleton()->userFramework === 'WordPress') {
+    $wpBasePage = \Civi::settings()->get('wpBasePage');
+    if (!empty($wpBasePage)) {
+      $basePage = $wpBasePage;
+    }
+  }
+
   $jsVars = [
     'jsDebug' => (boolean) \Civi::settings()->get('mjwshared_jsdebug'),
+    'basePage' => $basePage,
   ];
 
   \Civi::resources()->addVars('payment', $jsVars);
-- 
GitLab