From a7c40820e3a3a23900093b25b319112ea8dedf45 Mon Sep 17 00:00:00 2001
From: Richard Burton <burtons@mosquitonet.com>
Date: Mon, 2 Feb 2015 11:58:56 -0900
Subject: [PATCH] Better multiple processor handling; reuse existing stripe
 token

---
 js/civicrm_stripe.js | 28 +++++++++++++++++++++++++++-
 stripe.php           |  3 +++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index 8fdd6257..d914500d 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -61,7 +61,7 @@
 
     if (isWebform) {
       if (!($('#action').length)) {
-        $form.append($('<input type="hidden" name="op" id="action" />'));
+        $form.append('<input type="hidden" name="op" id="action" />');
       }
       $(document).keypress(function(event) {
         if (event.which == 13) {
@@ -75,6 +75,18 @@
       $('#billingcheckbox:input').hide();
       $('label[for="billingcheckbox"]').hide();
     }
+    else {
+      // This is native civicrm form - check for existing token
+      if ($form.find("input#stripe-token").val()) {
+        $('.credit_card_info-group').hide();
+        $('#billing-payment-block').append('<input type="button" value="Edit CC details" id="ccButton" />');
+        $('#ccButton').click(function() {
+          $('.credit_card_info-group').show();
+          $('#ccButton').hide();
+          $form.find('input#stripe-token').val('');
+        });
+      }
+    }
 
     $submit.removeAttr('onclick');
 
@@ -117,12 +129,26 @@
         if (!($form.find('input[name="hidden_processor"]').length > 0)) {
           return true;
         }
+        if ($form.find('input[name="payment_processor"]:checked').length) {
+          processorId=$form.find('input[name="payment_processor"]:checked').val();
+          if (!($form.find('input[name="stripe_token"]').length) || ($('#stripe-id').length && $('#stripe-id').val() != processorId)) {
+            return true;
+          }
+        }
       }
 
       // Handle pay later (option value '0' in payment_processor radio group)
       if ($form.find('input[name="payment_processor"]:checked').length && !parseInt($form.find('input[name="payment_processor"]:checked').val())) {
         return true;
       }
+
+      // Handle reuse of existing token
+      if ($form.find("input#stripe-token").val()) {
+        $form.find("input#credit_card_number").removeAttr('name');
+        $form.find("input#cvv2").removeAttr('name');
+        return true;
+      }
+
       event.preventDefault();
       event.stopPropagation();
 
diff --git a/stripe.php b/stripe.php
index 28bd7635..c3d0fe49 100644
--- a/stripe.php
+++ b/stripe.php
@@ -152,6 +152,7 @@ function stripe_civicrm_buildForm($formName, &$form) {
 *        $form->_attributes['class'] .= ' stripe-payment-form';
 */
         $form->addElement('hidden', 'stripe_token', NULL, array('id' => 'stripe-token'));
+        $form->addElement('hidden', 'stripe_id', $form->_paymentProcessor['id'], array('id' => 'stripe-id'));
         stripe_add_stripe_js($form);
       }
     }
@@ -165,6 +166,8 @@ function stripe_civicrm_buildForm($formName, &$form) {
       if (!empty($params['stripe_token'])) {
         // Stash the token (including its value) in Confirm, in case they go backwards.
         $form->addElement('hidden', 'stripe_token', $params['stripe_token'], array('id' => 'stripe-token'));
+        // Stash stripe payment processor id
+        $form->addElement('hidden', 'stripe_id', $form->_paymentProcessor['id'], array('id' => 'stripe-id'));
       }
     }
   }
-- 
GitLab