Commit 6ba9a51b authored by drastik's avatar drastik

Add is_live column to civicrm_stripe_plans and civicrm_stripe_customers tables.

Update info to 1.9.2.
Update civix.
Generated Upgrader.php (may not be triggering yet).
Set queries and writes to civicrm_stripe_plans and _customers to use is_live flag.
parent 089d1f0e
......@@ -256,6 +256,15 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
return $params;
}
// Get live/test mode.
switch ($this->_mode) {
case 'test':
$transaction_mode = 0;
break;
case 'live':
$transaction_mode = 1;
}
// Get proper entry URL for returning on error.
if (!(array_key_exists('qfKey', $params))) {
// Probably not called from a civicrm form (e.g. webform) -
......@@ -334,7 +343,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$customer_query = CRM_Core_DAO::singleValueQuery("SELECT id
FROM civicrm_stripe_customers
WHERE email = %1", $query_params);
WHERE email = %1 AND is_live = '$transaction_mode'", $query_params);
/****
* If for some reason you cannot use Stripe.js and you are aware of PCI Compliance issues,
......@@ -391,7 +400,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers
(email, id) VALUES (%1, %2)", $query_params);
(email, id, is_live) VALUES (%1, %2, '$transaction_mode')", $query_params);
}
else {
CRM_Core_Error::fatal(ts('There was an error saving new customer within Stripe. Is Stripe down?'));
......@@ -435,23 +444,23 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// with a Customer within Stripe. (Perhaps deleted using Stripe interface?).
// Store the relationship between CiviCRM's email address for the Contact & Stripe's Customer ID.
if (isset($stripe_customer)) {
if ($this->isErrorReturn($stripe_customer)) {
/*if ($this->isErrorReturn($stripe_customer)) {
return $stripe_customer;
}
}*/
// Delete whatever we have for this customer.
$query_params = array(
1 => array($email, 'String'),
);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_customers
WHERE email = %1", $query_params);
WHERE email = %1 AND is_live = '$transaction_mode'", $query_params);
// Create new record for this customer.
$query_params = array(
1 => array($email, 'String'),
2 => array($stripe_customer->id, 'String'),
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers (email, id)
VALUES (%1, %2)", $query_params);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers (email, id, is_live)
VALUES (%1, %2, '$transaction_mode')", $query_params);
}
else {
// Customer was found in civicrm_stripe database, but unable to be
......@@ -553,7 +562,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$frequency = $params['frequency_unit'];
$installments = $params['installments'];
$frequency_interval = (empty($params['frequency_interval']) ? 1 : $params['frequency_interval']);
$plan_id = "every-{$frequency_interval}-{$frequency}-{$amount}";
$currency = strtolower($params['currencyID']);
$plan_id = "every-{$frequency_interval}-{$frequency}-{$amount}-{$currency}";
// Prepare escaped query params.
$query_params = array(
......@@ -562,7 +572,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$stripe_plan_query = CRM_Core_DAO::singleValueQuery("SELECT plan_id
FROM civicrm_stripe_plans
WHERE plan_id = %1", $query_params);
WHERE plan_id = %1 AND is_live = '$transaction_mode'", $query_params);
if (!isset($stripe_plan_query)) {
$formatted_amount = '$' . number_format(($amount / 100), 2);
......@@ -571,7 +581,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
'amount' => $amount,
'interval' => $frequency,
'name' => "CiviCRM every {$frequency_interval} {$frequency}s {$formatted_amount}",
'currency' => strtolower($params['currencyID']),
'currency' => $currency,
'id' => $plan_id,
'interval_count' => $frequency_interval,
);
......@@ -588,8 +598,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$query_params = array(
1 => array($plan_id, 'String'),
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_plans (plan_id)
VALUES (%1)", $query_params);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_plans (plan_id, is_live)
VALUES (%1, '$transaction_mode')", $query_params);
}
// If a contact/customer has an existing active recurring
......@@ -618,7 +628,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$existing_subscription_query = CRM_Core_DAO::singleValueQuery("SELECT invoice_id
FROM civicrm_stripe_subscriptions
WHERE customer_id = %1", $query_params);
WHERE customer_id = %1 AND is_live = '$transaction_mode'", $query_params);
if (!empty($existing_subscription_query)) {
// Cancel existing Recurring Contribution in CiviCRM.
......
<?php
/**
* Collection of upgrade steps.
*/
class CRM_Stripe_Upgrader extends CRM_Stripe_Upgrader_Base {
// By convention, functions that look like "function upgrade_NNNN()" are
// upgrade tasks. They are executed in order (like Drupal's hook_update_N).
/**
* Add is_live column to civicrm_stripe_plans and civicrm_stripe_customers tables.
*
* @return TRUE on success
* @throws Exception
*/
public function upgrade_4200() {
$live_column_check = mysql_query("SHOW COLUMNS FROM `civicrm_stripe_customers` LIKE 'is_live'");
$live_column_exists = (mysql_num_rows($live_column_check)) ? TRUE : FALSE;
if (!$live_column_exists) {
$this->ctx->log->info('Applying civicrm_stripe update 4200. Adding is_live to civicrm_stripe_plans and civicrm_stripe_customers tables.');
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_stripe_customers ADD COLUMN `is_live` tinyint(4) NOT NULL COMMENT "Whether this is a live or test transaction"');
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_stripe_plans ADD COLUMN `is_live` tinyint(4) NOT NULL COMMENT "Whether this is a live or test transaction"');
}
else {
$this->ctx->log->info('Skipped civicrm_stripe update 4200. Column is_live already present on civicrm_stripe_plans table.');
}
return TRUE;
}
}
<?php
// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
/**
* Base class which provides helpers to execute upgrade logic
*/
class CRM_Stripe_Upgrader_Base {
/**
* @var varies, subclass of ttis
*/
static $instance;
/**
* @var CRM_Queue_TaskContext
*/
protected $ctx;
/**
* @var string, eg 'com.example.myextension'
*/
protected $extensionName;
/**
* @var string, full path to the extension's source tree
*/
protected $extensionDir;
/**
* @var array(revisionNumber) sorted numerically
*/
private $revisions;
/**
* Obtain a reference to the active upgrade handler.
*/
static public function instance() {
if (! self::$instance) {
// FIXME auto-generate
self::$instance = new CRM_Stripe_Upgrader(
'com.drastikbydesign.stripe',
realpath(__DIR__ .'/../../../')
);
}
return self::$instance;
}
/**
* Adapter that lets you add normal (non-static) member functions to the queue.
*
* Note: Each upgrader instance should only be associated with one
* task-context; otherwise, this will be non-reentrant.
*
* @code
* CRM_Stripe_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
* @endcode
*/
static public function _queueAdapter() {
$instance = self::instance();
$args = func_get_args();
$instance->ctx = array_shift($args);
$instance->queue = $instance->ctx->queue;
$method = array_shift($args);
return call_user_func_array(array($instance, $method), $args);
}
public function __construct($extensionName, $extensionDir) {
$this->extensionName = $extensionName;
$this->extensionDir = $extensionDir;
}
// ******** Task helpers ********
/**
* Run a CustomData file.
*
* @param string $relativePath the CustomData XML file path (relative to this extension's dir)
* @return bool
*/
public function executeCustomDataFile($relativePath) {
$xml_file = $this->extensionDir . '/' . $relativePath;
return $this->executeCustomDataFileByAbsPath($xml_file);
}
/**
* Run a CustomData file
*
* @param string $xml_file the CustomData XML file path (absolute path)
*
* @return bool
*/
protected static function executeCustomDataFileByAbsPath($xml_file) {
require_once 'CRM/Utils/Migrate/Import.php';
$import = new CRM_Utils_Migrate_Import();
$import->run($xml_file);
return TRUE;
}
/**
* Run a SQL file.
*
* @param string $relativePath the SQL file path (relative to this extension's dir)
*
* @return bool
*/
public function executeSqlFile($relativePath) {
CRM_Utils_File::sourceSQLFile(
CIVICRM_DSN,
$this->extensionDir . '/' . $relativePath
);
return TRUE;
}
/**
* Run one SQL query.
*
* This is just a wrapper for CRM_Core_DAO::executeSql, but it
* provides syntatic sugar for queueing several tasks that
* run different queries
*/
public function executeSql($query, $params = array()) {
// FIXME verify that we raise an exception on error
CRM_Core_DAO::executeSql($query, $params);
return TRUE;
}
/**
* Syntatic sugar for enqueuing a task which calls a function in this class.
*
* The task is weighted so that it is processed
* as part of the currently-pending revision.
*
* After passing the $funcName, you can also pass parameters that will go to
* the function. Note that all params must be serializable.
*/
public function addTask($title) {
$args = func_get_args();
$title = array_shift($args);
$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
$args,
$title
);
return $this->queue->createItem($task, array('weight' => -1));
}
// ******** Revision-tracking helpers ********
/**
* Determine if there are any pending revisions.
*
* @return bool
*/
public function hasPendingRevisions() {
$revisions = $this->getRevisions();
$currentRevision = $this->getCurrentRevision();
if (empty($revisions)) {
return FALSE;
}
if (empty($currentRevision)) {
return TRUE;
}
return ($currentRevision < max($revisions));
}
/**
* Add any pending revisions to the queue.
*/
public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
$this->queue = $queue;
$currentRevision = $this->getCurrentRevision();
foreach ($this->getRevisions() as $revision) {
if ($revision > $currentRevision) {
$title = ts('Upgrade %1 to revision %2', array(
1 => $this->extensionName,
2 => $revision,
));
// note: don't use addTask() because it sets weight=-1
$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
array('upgrade_' . $revision),
$title
);
$this->queue->createItem($task);
$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
array('setCurrentRevision', $revision),
$title
);
$this->queue->createItem($task);
}
}
}
/**
* Get a list of revisions.
*
* @return array(revisionNumbers) sorted numerically
*/
public function getRevisions() {
if (! is_array($this->revisions)) {
$this->revisions = array();
$clazz = new ReflectionClass(get_class($this));
$methods = $clazz->getMethods();
foreach ($methods as $method) {
if (preg_match('/^upgrade_(.*)/', $method->name, $matches)) {
$this->revisions[] = $matches[1];
}
}
sort($this->revisions, SORT_NUMERIC);
}
return $this->revisions;
}
public function getCurrentRevision() {
// return CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
$key = $this->extensionName . ':version';
return CRM_Core_BAO_Setting::getItem('Extension', $key);
}
public function setCurrentRevision($revision) {
// We call this during hook_civicrm_install, but the underlying SQL
// UPDATE fails because the extension record hasn't been INSERTed yet.
// Instead, track revisions in our own namespace.
// CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision);
$key = $this->extensionName . ':version';
CRM_Core_BAO_Setting::setItem($revision, 'Extension', $key);
return TRUE;
}
// ******** Hook delegates ********
public function onInstall() {
$files = glob($this->extensionDir . '/sql/*_install.sql');
if (is_array($files)) {
foreach ($files as $file) {
CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
}
}
$files = glob($this->extensionDir . '/xml/*_install.xml');
if (is_array($files)) {
foreach ($files as $file) {
$this->executeCustomDataFileByAbsPath($file);
}
}
if (is_callable(array($this, 'install'))) {
$this->install();
}
$revisions = $this->getRevisions();
if (!empty($revisions)) {
$this->setCurrentRevision(max($revisions));
}
}
public function onUninstall() {
if (is_callable(array($this, 'uninstall'))) {
$this->uninstall();
}
$files = glob($this->extensionDir . '/sql/*_uninstall.sql');
if (is_array($files)) {
foreach ($files as $file) {
CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
}
}
$this->setCurrentRevision(NULL);
}
public function onEnable() {
// stub for possible future use
if (is_callable(array($this, 'enable'))) {
$this->enable();
}
}
public function onDisable() {
// stub for possible future use
if (is_callable(array($this, 'disable'))) {
$this->disable();
}
}
public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) {
switch ($op) {
case 'check':
return array($this->hasPendingRevisions());
case 'enqueue':
return $this->enqueuePendingRevisions($queue);
default:
}
}
}
......@@ -11,11 +11,12 @@
<author>Joshua Walker (drastik) - Drastik by Design</author>
<email>admin@drastikbydesign.com</email>
</maintainer>
<releaseDate>2015-01-12</releaseDate>
<version>1.9.1</version>
<releaseDate>2015-09-09</releaseDate>
<version>1.9.2</version>
<compatibility>
<ver>4.4</ver>
<ver>4.5</ver>
<ver>4.6</ver>
</compatibility>
<comments>http://drastikbydesign.com/blog-entry/civicrm-stripe-payment-processor</comments>
<civix>
......
......@@ -3,34 +3,38 @@
// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
/**
* (Delegated) Implementation of hook_civicrm_config
* (Delegated) Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function _stripe_civix_civicrm_config(&$config = NULL) {
static $configured = FALSE;
if ($configured) return;
if ($configured) {
return;
}
$configured = TRUE;
$template =& CRM_Core_Smarty::singleton();
$extRoot = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
$extRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR;
$extDir = $extRoot . 'templates';
if ( is_array( $template->template_dir ) ) {
array_unshift( $template->template_dir, $extDir );
} else {
}
else {
$template->template_dir = array( $extDir, $template->template_dir );
}
$include_path = $extRoot . PATH_SEPARATOR . get_include_path( );
set_include_path( $include_path );
set_include_path($include_path);
}
/**
* (Delegated) Implementation of hook_civicrm_xmlMenu
* (Delegated) Implements hook_civicrm_xmlMenu().
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function _stripe_civix_civicrm_xmlMenu(&$files) {
......@@ -40,7 +44,7 @@ function _stripe_civix_civicrm_xmlMenu(&$files) {
}
/**
* Implementation of hook_civicrm_install
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
......@@ -52,7 +56,7 @@ function _stripe_civix_civicrm_install() {
}
/**
* Implementation of hook_civicrm_uninstall
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
......@@ -64,7 +68,7 @@ function _stripe_civix_civicrm_uninstall() {
}
/**
* (Delegated) Implementation of hook_civicrm_enable
* (Delegated) Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
......@@ -78,7 +82,7 @@ function _stripe_civix_civicrm_enable() {
}
/**
* (Delegated) Implementation of hook_civicrm_disable
* (Delegated) Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
* @return mixed
......@@ -93,7 +97,7 @@ function _stripe_civix_civicrm_disable() {
}
/**
* (Delegated) Implementation of hook_civicrm_upgrade
* (Delegated) Implements hook_civicrm_upgrade().
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
......@@ -115,7 +119,8 @@ function _stripe_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
function _stripe_civix_upgrader() {
if (!file_exists(__DIR__.'/CRM/Stripe/Upgrader.php')) {
return NULL;
} else {
}
else {
return CRM_Stripe_Upgrader_Base::instance();
}
}
......@@ -158,7 +163,7 @@ function _stripe_civix_find_files($dir, $pattern) {
return $result;
}
/**
* (Delegated) Implementation of hook_civicrm_managed
* (Delegated) Implements hook_civicrm_managed().
*
* Find any *.mgd.php files, merge their content, and return.
*
......@@ -178,7 +183,7 @@ function _stripe_civix_civicrm_managed(&$entities) {
}
/**
* (Delegated) Implementation of hook_civicrm_caseTypes
* (Delegated) Implements hook_civicrm_caseTypes().
*
* Find any and return any files matching "xml/case/*.xml"
*
......@@ -206,6 +211,31 @@ function _stripe_civix_civicrm_caseTypes(&$caseTypes) {
}
}
/**
* (Delegated) Implements hook_civicrm_angularModules().
*
* Find any and return any files matching "ang/*.ang.php"
*
* Note: This hook only runs in CiviCRM 4.5+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
*/
function _stripe_civix_civicrm_angularModules(&$angularModules) {
if (!is_dir(__DIR__ . '/ang')) {
return;
}
$files = _stripe_civix_glob(__DIR__ . '/ang/*.ang.php');
foreach ($files as $file) {
$name = preg_replace(':\.ang\.php$:', '', basename($file));
$module = include $file;
if (empty($module['ext'])) {
$module['ext'] = 'com.drastikbydesign.stripe';
}
$angularModules[$name] = $module;
}
}
/**
* Glob wrapper which is guaranteed to return an array.
*
......@@ -224,12 +254,12 @@ function _stripe_civix_glob($pattern) {
}
/**
* Inserts a navigation menu item at a given place in the hierarchy
* Inserts a navigation menu item at a given place in the hierarchy.
*
* $menu - menu hierarchy
* $path - path where insertion should happen (ie. Administer/System Settings)
* $item - menu you need to insert (parent/child attributes will be filled for you)
* $parentId - used internally to recurse in the menu structure
* @param array $menu - menu hierarchy
* @param string $path - path where insertion should happen (ie. Administer/System Settings)
* @param array $item - menu you need to insert (parent/child attributes will be filled for you)
* @param int $parentId - used internally to recurse in the menu structure
*/
function _stripe_civix_insert_navigation_menu(&$menu, $path, $item, $parentId = NULL) {
static $navId;
......@@ -247,7 +277,8 @@ function _stripe_civix_insert_navigation_menu(&$menu, $path, $item, $parentId =
))
);
return true;
} else {
}
else {
// Find an recurse into the next level down
$found = false;
$path = explode('/', $path);
......@@ -263,13 +294,15 @@ function _stripe_civix_insert_navigation_menu(&$menu, $path, $item, $parentId =
}
/**
* (Delegated) Implementation of hook_civicrm_alterSettingsFolders
* (Delegated) Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function _stripe_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
static $configured = FALSE;
if ($configured) return;
if ($configured) {
return;
}
$configured = TRUE;
$settingsDir = __DIR__ . DIRECTORY_SEPARATOR . 'settings';
......
......@@ -31,6 +31,7 @@ function stripe_civicrm_install() {
CREATE TABLE IF NOT EXISTS `civicrm_stripe_customers` (
`email` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
......@@ -38,6 +39,7 @@ function stripe_civicrm_install() {
CRM_Core_DAO::executeQuery("
CREATE TABLE IF NOT EXISTS `civicrm_stripe_plans` (
`plan_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
UNIQUE KEY `plan_id` (`plan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment