From 19987f21728d84dea647387fd40a18488875282d Mon Sep 17 00:00:00 2001
From: "Matthew Wire (MJW Consulting)" <mjw@mjwconsult.co.uk>
Date: Thu, 3 Jan 2019 14:28:47 +0000
Subject: [PATCH] Resolve #26 - Make sure we clear processor specific metadata
 from payment form when changing processor

---
 CRM/Core/Payment/Stripe.php |  3 +++
 js/civicrm_stripe.js        | 10 ++++++++++
 2 files changed, 13 insertions(+)

diff --git a/CRM/Core/Payment/Stripe.php b/CRM/Core/Payment/Stripe.php
index e5b5a93e..27676a38 100644
--- a/CRM/Core/Payment/Stripe.php
+++ b/CRM/Core/Payment/Stripe.php
@@ -296,6 +296,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
         'title' => 'Stripe Token',
         'attributes' => array(
           'id' => 'stripe-token',
+          'class' => 'payproc-metadata',
         ),
         'cc_field' => TRUE,
         'is_required' => TRUE,
@@ -306,6 +307,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
         'title' => 'Stripe ID',
         'attributes' => array(
           'id' => 'stripe-id',
+          'class' => 'payproc-metadata',
         ),
         'cc_field' => TRUE,
         'is_required' => TRUE,
@@ -316,6 +318,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
         'title' => 'Stripe Public Key',
         'attributes' => array(
           'id' => 'stripe-pub-key',
+          'class' => 'payproc-metadata',
         ),
         'cc_field' => TRUE,
         'is_required' => TRUE,
diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index f520c0da..658e66e4 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -57,6 +57,16 @@ CRM.$(function($) {
     // We will re-add it if the transaction goes through.
     onclickAction = $submit.attr('onclick');
     $submit.removeAttr('onclick');
+
+    // Quickform doesn't add hidden elements via standard method. On a form where payment processor may
+    //  be loaded via initial form load AND ajax (eg. backend live contribution page with payproc dropdown)
+    //  the processor metadata elements will appear twice (once on initial load, once via AJAX).  The ones loaded
+    //  via initial load will not be removed when AJAX loaded ones are added and the wrong stripe-pub-key etc will
+    //  be submitted.  This removes all elements with the class "payproc-metadata" from the form each time the
+    //  dropdown is changed.
+    $('select#payment_processor_id').on('change', function() {
+      $('input.payproc-metadata').remove();
+    });
   });
 
   // Re-prep form when we've loaded a new payproc
-- 
GitLab