From c89be67e6d925fc36d80c142f433a203d23e749a Mon Sep 17 00:00:00 2001
From: drastik <jwjoshuawalker@gmail.com>
Date: Sun, 12 Aug 2012 03:28:48 -0700
Subject: [PATCH] New features.  Directory restructure

---
 .../README.txt                                |  9 ++-----
 .../CRM/Core/BillingBlock.tpl                 |  6 ++++-
 .../extern/civicrm_stripe_cron_live.php       |  3 ++-
 .../extern/civicrm_stripe_cron_test.php       |  3 ++-
 .../stripe.php                                | 26 +++++++++++++++----
 .../README.txt                                |  8 ++----
 .../CRM/Core/BillingBlock.tpl                 |  6 ++++-
 .../stripe.php                                | 21 ++++++++++-----
 source/CRM/Core/Payment/Stripe.php            | 20 +++++++++-----
 source/README.txt                             | 13 +++-------
 source/civicrm_stripe.sql                     |  3 ++-
 .../CRM/Core/BillingBlock.tpl                 |  6 ++++-
 source/extern/civicrm_stripe_cron_live.php    |  3 ++-
 source/extern/civicrm_stripe_cron_test.php    |  3 ++-
 14 files changed, 82 insertions(+), 48 deletions(-)
 rename extension-4.1/{ => com.drastikbydesign.payment.stripe}/README.txt (79%)
 rename extension-4.1/{ => com.drastikbydesign.payment.stripe}/civicrm_templates/CRM/Core/BillingBlock.tpl (98%)
 rename extension-4.1/{ => com.drastikbydesign.payment.stripe}/extern/civicrm_stripe_cron_live.php (95%)
 rename extension-4.1/{ => com.drastikbydesign.payment.stripe}/extern/civicrm_stripe_cron_test.php (95%)
 rename extension-4.2/{ => com.drastikbydesign.payment.stripe}/README.txt (76%)
 rename extension-4.2/{ => com.drastikbydesign.payment.stripe}/civicrm_templates/CRM/Core/BillingBlock.tpl (98%)

diff --git a/extension-4.1/README.txt b/extension-4.1/com.drastikbydesign.payment.stripe/README.txt
similarity index 79%
rename from extension-4.1/README.txt
rename to extension-4.1/com.drastikbydesign.payment.stripe/README.txt
index 8e538b17..b55fc914 100644
--- a/extension-4.1/README.txt
+++ b/extension-4.1/com.drastikbydesign.payment.stripe/README.txt
@@ -1,11 +1,7 @@
 ------------
 Please Read:
-
-There are 3 versions included by directory.  Installation instructions for each further below:
+There are 3 versions of this extension available.  This is:
 extension-4.1:  Extension for CiviCRM 4.1 and earlier.
-extension-4.2:  Extension for CiviCRM 4.2.
-source:  Pre-extension method, folder structure is in tact, place files accordingly.
-
 
 You also need a corresponding module for your CMS.  Here is where the modules can be found:
 Drupal:  git clone --recursive --branch master http://git.drupal.org/sandbox/drastik/1719796.git civicrm_stripe
@@ -22,8 +18,7 @@ Installation Instructions:
 
 ------------
 
-For CiviCRM 4.1
-extension-4.1 instructions:
+For CiviCRM 4.1:
 
 Install extension
 
diff --git a/extension-4.1/civicrm_templates/CRM/Core/BillingBlock.tpl b/extension-4.1/com.drastikbydesign.payment.stripe/civicrm_templates/CRM/Core/BillingBlock.tpl
similarity index 98%
rename from extension-4.1/civicrm_templates/CRM/Core/BillingBlock.tpl
rename to extension-4.1/com.drastikbydesign.payment.stripe/civicrm_templates/CRM/Core/BillingBlock.tpl
index 96a007b2..77dd3cc1 100644
--- a/extension-4.1/civicrm_templates/CRM/Core/BillingBlock.tpl
+++ b/extension-4.1/com.drastikbydesign.payment.stripe/civicrm_templates/CRM/Core/BillingBlock.tpl
@@ -57,7 +57,11 @@
 		function stripeResponseHandler(status, response) {		  
 		  if (response.error) {
         	// show the errors on the form
-			cj("form.stripe-payment-form").prepend('<div class="messages crm-error">'
+        	if(cj(".messages.crm-error.stripe-message").length > 0) {
+        		cj(".messages.crm-error.stripe-message").slideUp();
+        		cj(".messages.crm-error.stripe-message").remove();
+        	}
+			cj("form.stripe-payment-form").prepend('<div class="messages crm-error stripe-message">'
 				+'<strong>Payment Error Response:</strong>'
 	   			  +'<ul id="errorList">'
 	         		+'<li>Error: ' + response.error.message + '</li>'
diff --git a/extension-4.1/extern/civicrm_stripe_cron_live.php b/extension-4.1/com.drastikbydesign.payment.stripe/extern/civicrm_stripe_cron_live.php
similarity index 95%
rename from extension-4.1/extern/civicrm_stripe_cron_live.php
rename to extension-4.1/com.drastikbydesign.payment.stripe/extern/civicrm_stripe_cron_live.php
index 4208094e..719b4fb3 100644
--- a/extension-4.1/extern/civicrm_stripe_cron_live.php
+++ b/extension-4.1/com.drastikbydesign.payment.stripe/extern/civicrm_stripe_cron_live.php
@@ -19,6 +19,7 @@ $query = "
   SELECT  customer_id, invoice_id 
   FROM    civicrm_stripe_subscriptions 
   WHERE   end_time <= '$time' 
+  AND		is_live = '1'
 ";
 
 $end_date = date("Y-m-d H:i:s");
@@ -32,4 +33,4 @@ while($end_recur_query->fetch()) {
     //Delete the Stripe Subscription from our cron watch list.
     CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_subscriptions WHERE invoice_id = '$end_recur_query->invoice_id'");
   }
-}
+}
\ No newline at end of file
diff --git a/extension-4.1/extern/civicrm_stripe_cron_test.php b/extension-4.1/com.drastikbydesign.payment.stripe/extern/civicrm_stripe_cron_test.php
similarity index 95%
rename from extension-4.1/extern/civicrm_stripe_cron_test.php
rename to extension-4.1/com.drastikbydesign.payment.stripe/extern/civicrm_stripe_cron_test.php
index 4318bb55..852a41d9 100644
--- a/extension-4.1/extern/civicrm_stripe_cron_test.php
+++ b/extension-4.1/com.drastikbydesign.payment.stripe/extern/civicrm_stripe_cron_test.php
@@ -19,6 +19,7 @@ $query = "
   SELECT  customer_id, invoice_id 
   FROM    civicrm_stripe_subscriptions 
   WHERE   end_time <= '$time' 
+  AND	  is_live = '0'
 ";
 
 $end_date = date("Y-m-d H:i:s");
@@ -32,4 +33,4 @@ while($end_recur_query->fetch()) {
     //Delete the Stripe Subscription from our cron watch list.
     CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_subscriptions WHERE invoice_id = '$end_recur_query->invoice_id'");
   }
-}
+}
\ No newline at end of file
diff --git a/extension-4.1/com.drastikbydesign.payment.stripe/stripe.php b/extension-4.1/com.drastikbydesign.payment.stripe/stripe.php
index 443d3aa1..cc3fb589 100644
--- a/extension-4.1/com.drastikbydesign.payment.stripe/stripe.php
+++ b/extension-4.1/com.drastikbydesign.payment.stripe/stripe.php
@@ -129,11 +129,20 @@ class com_drastikbydesign_payment_stripe extends CRM_Core_Payment {
     CRM_Core_DAO::executeQuery("
 		CREATE TABLE IF NOT EXISTS `civicrm_stripe_subscriptions` (
 			`customer_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
-  			`invoice_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
-  			`end_time` int(11) NOT NULL DEFAULT '0',
-  			KEY `end_time` (`end_time`)
+			`invoice_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
+			`end_time` int(11) NOT NULL DEFAULT '0',
+			`is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
+			KEY `end_time` (`end_time`)
 			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-    	");
+		");
+    CRM_Core_Error::debug('Stripe Database tables created.');
+  }
+  
+  /*
+   * CiviCRM extension enable()
+   */
+  public function enable() {
+    CRM_Core_Session::setStatus("Stripe Payment Processor Message: <br />Don't forget to create a cron entry for CiviCRM's Job Scheduler so recurring contributions are ended!");
   }
 
   /*
@@ -265,6 +274,13 @@ class com_drastikbydesign_payment_stripe extends CRM_Core_Payment {
   }
   
   function doRecurPayment(&$params, $amount, $stripe_customer) {
+    switch($this->_mode) {
+      case 'test':
+        $transaction_mode = 0;
+        break;
+      case 'live':
+        $transaction_mode = 1;
+    }
     $frequency = $params['frequency_unit'];
     $installments = $params['installments'];
     $plan_id = "$frequency-$amount";
@@ -298,7 +314,7 @@ class com_drastikbydesign_payment_stripe extends CRM_Core_Payment {
     //Calculate timestamp for the last installment
     $end_time = strtotime("+$installments $frequency");
     $invoice_id = $params['invoiceID'];
-    CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions (customer_id, invoice_id, end_time) VALUES ('$stripe_customer->id', '$invoice_id', '$end_time')");
+    CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions (customer_id, invoice_id, end_time, is_live) VALUES ('$stripe_customer->id', '$invoice_id', '$end_time', '$transaction_mode')");
     
     $trxn_id = $stripe_customer->id . '-' . $end_time;
     $params['trxn_id'] = $trxn_id;
diff --git a/extension-4.2/README.txt b/extension-4.2/com.drastikbydesign.payment.stripe/README.txt
similarity index 76%
rename from extension-4.2/README.txt
rename to extension-4.2/com.drastikbydesign.payment.stripe/README.txt
index 06ebd230..6bf20360 100644
--- a/extension-4.2/README.txt
+++ b/extension-4.2/com.drastikbydesign.payment.stripe/README.txt
@@ -1,10 +1,7 @@
 ------------
 Please Read:
-
-There are 3 versions included by directory.  Installation instructions for each further below:
-extension-4.1:  Extension for CiviCRM 4.1 and earlier.
+There are 3 versions of this extension available.  This is:
 extension-4.2:  Extension for CiviCRM 4.2.
-source:  Pre-extension method, folder structure is in tact, place files accordingly.
 
 You also need a corresponding module for your CMS.  Here is where the modules can be found:
 Drupal:  git clone --recursive --branch master http://git.drupal.org/sandbox/drastik/1719796.git civicrm_stripe
@@ -20,8 +17,7 @@ Installation Instructions:
 
 ------------
 
-For CiviCRM 4.2
-extension-4.2 instructions:
+For CiviCRM 4.2:
 
 Install extension
 
diff --git a/extension-4.2/civicrm_templates/CRM/Core/BillingBlock.tpl b/extension-4.2/com.drastikbydesign.payment.stripe/civicrm_templates/CRM/Core/BillingBlock.tpl
similarity index 98%
rename from extension-4.2/civicrm_templates/CRM/Core/BillingBlock.tpl
rename to extension-4.2/com.drastikbydesign.payment.stripe/civicrm_templates/CRM/Core/BillingBlock.tpl
index 96a007b2..77dd3cc1 100644
--- a/extension-4.2/civicrm_templates/CRM/Core/BillingBlock.tpl
+++ b/extension-4.2/com.drastikbydesign.payment.stripe/civicrm_templates/CRM/Core/BillingBlock.tpl
@@ -57,7 +57,11 @@
 		function stripeResponseHandler(status, response) {		  
 		  if (response.error) {
         	// show the errors on the form
-			cj("form.stripe-payment-form").prepend('<div class="messages crm-error">'
+        	if(cj(".messages.crm-error.stripe-message").length > 0) {
+        		cj(".messages.crm-error.stripe-message").slideUp();
+        		cj(".messages.crm-error.stripe-message").remove();
+        	}
+			cj("form.stripe-payment-form").prepend('<div class="messages crm-error stripe-message">'
 				+'<strong>Payment Error Response:</strong>'
 	   			  +'<ul id="errorList">'
 	         		+'<li>Error: ' + response.error.message + '</li>'
diff --git a/extension-4.2/com.drastikbydesign.payment.stripe/stripe.php b/extension-4.2/com.drastikbydesign.payment.stripe/stripe.php
index 4b64f60d..29b991e7 100644
--- a/extension-4.2/com.drastikbydesign.payment.stripe/stripe.php
+++ b/extension-4.2/com.drastikbydesign.payment.stripe/stripe.php
@@ -96,15 +96,16 @@ class com_drastikbydesign_payment_stripe extends CRM_Core_Payment {
   			UNIQUE KEY `plan_id` (`plan_id`)
 			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
 		");
-      
+    
     CRM_Core_DAO::executeQuery("
 		CREATE TABLE IF NOT EXISTS `civicrm_stripe_subscriptions` (
 			`customer_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
-  			`invoice_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
-  			`end_time` int(11) NOT NULL DEFAULT '0',
-  			KEY `end_time` (`end_time`)
+			`invoice_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
+			`end_time` int(11) NOT NULL DEFAULT '0',
+			`is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
+			KEY `end_time` (`end_time`)
 			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-    	");
+		");
     
     CRM_Core_DAO::executeQuery("
 		INSERT INTO civicrm_job (
@@ -166,6 +167,7 @@ class com_drastikbydesign_payment_stripe extends CRM_Core_Payment {
 		SELECT  customer_id, invoice_id 
 		FROM    civicrm_stripe_subscriptions 
 		WHERE   end_time <= '$time' 
+		AND		is_live = '1'
 	";
 
     $end_date = date("Y-m-d H:i:s");
@@ -299,6 +301,13 @@ class com_drastikbydesign_payment_stripe extends CRM_Core_Payment {
   }
   
   function doRecurPayment(&$params, $amount, $stripe_customer) {
+    switch($this->_mode) {
+      case 'test':
+        $transaction_mode = 0;
+        break;
+      case 'live':
+        $transaction_mode = 1;
+    }
     $frequency = $params['frequency_unit'];
     $installments = $params['installments'];
     $plan_id = "$frequency-$amount";
@@ -332,7 +341,7 @@ class com_drastikbydesign_payment_stripe extends CRM_Core_Payment {
     //Calculate timestamp for the last installment
     $end_time = strtotime("+$installments $frequency");
     $invoice_id = $params['invoiceID'];
-    CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions (customer_id, invoice_id, end_time) VALUES ('$stripe_customer->id', '$invoice_id', '$end_time')");
+    CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions (customer_id, invoice_id, end_time, is_live) VALUES ('$stripe_customer->id', '$invoice_id', '$end_time', '$transaction_mode')");
     
     $trxn_id = $stripe_customer->id . '-' . $end_time;
     $params['trxn_id'] = $trxn_id;
diff --git a/source/CRM/Core/Payment/Stripe.php b/source/CRM/Core/Payment/Stripe.php
index e955cb3f..43026e15 100644
--- a/source/CRM/Core/Payment/Stripe.php
+++ b/source/CRM/Core/Payment/Stripe.php
@@ -79,12 +79,13 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
       
       CRM_Core_DAO::executeQuery("
 		CREATE TABLE IF NOT EXISTS `civicrm_stripe_subscriptions` (
-  			`customer_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
-  			`invoice_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
-  			`end_time` int(11) NOT NULL DEFAULT '0',
-  			KEY `end_time` (`end_time`)
+			`customer_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
+			`invoice_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
+			`end_time` int(11) NOT NULL DEFAULT '0',
+			`is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
+			KEY `end_time` (`end_time`)
 			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-    	");
+		");
       CRM_Core_Error::debug('Stripe Database tables created.');
     }
 
@@ -221,6 +222,13 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
   }
   
   function doRecurPayment(&$params, $amount, $stripe_customer) {
+    switch($this->_mode) {
+      case 'test':
+        $transaction_mode = 0;
+        break;
+      case 'live':
+        $transaction_mode = 1;
+    }
     $frequency = $params['frequency_unit'];
     $installments = $params['installments'];
     $plan_id = "$frequency-$amount";
@@ -254,7 +262,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
     //Calculate timestamp for the last installment
     $end_time = strtotime("+$installments $frequency");
     $invoice_id = $params['invoiceID'];
-    CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions (customer_id, invoice_id, end_time) VALUES ('$stripe_customer->id', '$invoice_id', '$end_time')");
+    CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions (customer_id, invoice_id, end_time, is_live) VALUES ('$stripe_customer->id', '$invoice_id', '$end_time', '$transaction_mode')");
     
     $trxn_id = $stripe_customer->id . '-' . $end_time;
     $params['trxn_id'] = $trxn_id;
diff --git a/source/README.txt b/source/README.txt
index c8693d06..5703b230 100644
--- a/source/README.txt
+++ b/source/README.txt
@@ -1,10 +1,8 @@
 ------------
 Please Read:
 
-There are 3 versions included by directory.  Installation instructions for each further below:
-extension-4.1:  Extension for CiviCRM 4.1 and earlier.
-extension-4.2:  Extension for CiviCRM 4.2.
-source:  Pre-extension method, folder structure is in tact, place files accordingly.
+There are 3 versions of this extension available.  This is:
+source:  Pre-extension method, folder structure is in tact, manually place files accordingly.
 
 
 You also need a corresponding module for your CMS.  Here is where the modules can be found:
@@ -36,11 +34,6 @@ Make cron entry to hit the file(s) (daily preferred).
 Copy Stripe's PHP library folder 'stripe-php' to civicrm/packages/stripe-php  
 You can get Stripe's PHP library here: https://github.com/stripe/stripe-php
 
-Run the included SQL file "civicrm_stripe.sql" to handle the DB-related needs.  It will:  
-Insert Stripe into civicrm_payment_processor_type (makes it available as an option within CiviCRM's payment processor settings)
-It will create the required tables:
-civicrm_stripe_customers
-civicrm_stripe_plans
-civicrm_stripe_subscriptions
+Run the included SQL file "civicrm_stripe.sql" to handle the DB-related needs.
 
 ------------
\ No newline at end of file
diff --git a/source/civicrm_stripe.sql b/source/civicrm_stripe.sql
index 3406c3fd..b47b7235 100755
--- a/source/civicrm_stripe.sql
+++ b/source/civicrm_stripe.sql
@@ -31,5 +31,6 @@ CREATE TABLE IF NOT EXISTS `civicrm_stripe_subscriptions` (
   `customer_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
   `invoice_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
   `end_time` int(11) NOT NULL DEFAULT '0',
+  `is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
   KEY `end_time` (`end_time`)
-) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
diff --git a/source/civicrm_templates/CRM/Core/BillingBlock.tpl b/source/civicrm_templates/CRM/Core/BillingBlock.tpl
index 96a007b2..77dd3cc1 100644
--- a/source/civicrm_templates/CRM/Core/BillingBlock.tpl
+++ b/source/civicrm_templates/CRM/Core/BillingBlock.tpl
@@ -57,7 +57,11 @@
 		function stripeResponseHandler(status, response) {		  
 		  if (response.error) {
         	// show the errors on the form
-			cj("form.stripe-payment-form").prepend('<div class="messages crm-error">'
+        	if(cj(".messages.crm-error.stripe-message").length > 0) {
+        		cj(".messages.crm-error.stripe-message").slideUp();
+        		cj(".messages.crm-error.stripe-message").remove();
+        	}
+			cj("form.stripe-payment-form").prepend('<div class="messages crm-error stripe-message">'
 				+'<strong>Payment Error Response:</strong>'
 	   			  +'<ul id="errorList">'
 	         		+'<li>Error: ' + response.error.message + '</li>'
diff --git a/source/extern/civicrm_stripe_cron_live.php b/source/extern/civicrm_stripe_cron_live.php
index 4208094e..719b4fb3 100644
--- a/source/extern/civicrm_stripe_cron_live.php
+++ b/source/extern/civicrm_stripe_cron_live.php
@@ -19,6 +19,7 @@ $query = "
   SELECT  customer_id, invoice_id 
   FROM    civicrm_stripe_subscriptions 
   WHERE   end_time <= '$time' 
+  AND		is_live = '1'
 ";
 
 $end_date = date("Y-m-d H:i:s");
@@ -32,4 +33,4 @@ while($end_recur_query->fetch()) {
     //Delete the Stripe Subscription from our cron watch list.
     CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_subscriptions WHERE invoice_id = '$end_recur_query->invoice_id'");
   }
-}
+}
\ No newline at end of file
diff --git a/source/extern/civicrm_stripe_cron_test.php b/source/extern/civicrm_stripe_cron_test.php
index 4318bb55..852a41d9 100644
--- a/source/extern/civicrm_stripe_cron_test.php
+++ b/source/extern/civicrm_stripe_cron_test.php
@@ -19,6 +19,7 @@ $query = "
   SELECT  customer_id, invoice_id 
   FROM    civicrm_stripe_subscriptions 
   WHERE   end_time <= '$time' 
+  AND	  is_live = '0'
 ";
 
 $end_date = date("Y-m-d H:i:s");
@@ -32,4 +33,4 @@ while($end_recur_query->fetch()) {
     //Delete the Stripe Subscription from our cron watch list.
     CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_subscriptions WHERE invoice_id = '$end_recur_query->invoice_id'");
   }
-}
+}
\ No newline at end of file
-- 
GitLab