Skip to content
Snippets Groups Projects
Commit e106cfab authored by Joshua Walker's avatar Joshua Walker Committed by GitHub
Browse files

Merge pull request #150 from aghstrategies/multi-customer-list-4.7

 Errors when donors repeat across multiple stripe accounts (for 4.7)
parents 9d78a704 fde6da11
No related branches found
No related tags found
No related merge requests found
......@@ -310,11 +310,12 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Prepare escaped query params.
$query_params = array(
1 => array($email, 'String'),
2 => array($this->_paymentProcessor['id'], 'Integer'),
);
$customer_query = CRM_Core_DAO::singleValueQuery("SELECT id
FROM civicrm_stripe_customers
WHERE email = %1 AND is_live = '{$this->_islive}'", $query_params);
WHERE email = %1 AND is_live = '{$this->_islive}' AND processor_id = %2", $query_params);
/****
* If for some reason you cannot use Stripe.js and you are aware of PCI Compliance issues,
......@@ -368,10 +369,11 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$query_params = array(
1 => array($email, 'String'),
2 => array($stripe_customer->id, 'String'),
3 => array($this->_paymentProcessor['id'], 'Integer'),
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers
(email, id, is_live) VALUES (%1, %2, '{$this->_islive}')", $query_params);
(email, id, is_live, processor_id) VALUES (%1, %2, '{$this->_islive}', %3)", $query_params);
}
else {
CRM_Core_Error::fatal(ts('There was an error saving new customer within Stripe. Is Stripe down?'));
......@@ -421,17 +423,19 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Delete whatever we have for this customer.
$query_params = array(
1 => array($email, 'String'),
2 => array($this->_paymentProcessor['id'], 'Integer'),
);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_customers
WHERE email = %1 AND is_live = '{$this->_islive}'", $query_params);
WHERE email = %1 AND is_live = '{$this->_islive}' AND processor_id = %2", $query_params);
// Create new record for this customer.
$query_params = array(
1 => array($email, 'String'),
2 => array($stripe_customer->id, 'String'),
3 => array($this->_paymentProcessor['id'], 'Integer'),
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers (email, id, is_live)
VALUES (%1, %2, '{$this->_islive}')", $query_params);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers (email, id, is_live, processor_id)
VALUES (%1, %2, '{$this->_islive}, %3')", $query_params);
}
else {
// Customer was found in civicrm_stripe database, but unable to be
......@@ -531,11 +535,12 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Prepare escaped query params.
$query_params = array(
1 => array($plan_id, 'String'),
2 => array($this->_paymentProcessor['id'], 'Integer'),
);
$stripe_plan_query = CRM_Core_DAO::singleValueQuery("SELECT plan_id
FROM civicrm_stripe_plans
WHERE plan_id = %1 AND is_live = '{$this->_islive}'", $query_params);
WHERE plan_id = %1 AND is_live = '{$this->_islive}' AND processor_id = %2", $query_params);
if (!isset($stripe_plan_query)) {
$formatted_amount = '$' . number_format(($amount / 100), 2);
......@@ -560,9 +565,10 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Prepare escaped query params.
$query_params = array(
1 => array($plan_id, 'String'),
2 => array($this->_paymentProcessor['id'], 'Integer'),
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_plans (plan_id, is_live)
VALUES (%1, '{$this->_islive}')", $query_params);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_plans (plan_id, is_live, processor_id)
VALUES (%1, '{$this->_islive}', %2)", $query_params);
}
// If a contact/customer has an existing active recurring
......@@ -576,7 +582,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// subscription first.
$subscriptions = $stripe_customer->offsetGet('subscriptions');
$data = $subscriptions->offsetGet('data');
if(!empty($data)) {
$status = $data[0]->offsetGet('status');
......@@ -594,11 +600,12 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Prepare escaped query params.
$query_params = array(
1 => array($stripe_customer->id, 'String'),
2 => array($this->_paymentProcessor['id'], 'Integer'),
);
$existing_subscription_query = CRM_Core_DAO::singleValueQuery("SELECT invoice_id
FROM civicrm_stripe_subscriptions
WHERE customer_id = %1 AND is_live = '{$this->_islive}'", $query_params);
WHERE customer_id = %1 AND is_live = '{$this->_islive}', and processor_id = %2", $query_params);
if (!empty($existing_subscription_query)) {
// Cancel existing Recurring Contribution in CiviCRM.
......@@ -607,6 +614,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Prepare escaped query params.
$query_params = array(
1 => array($existing_subscription_query, 'String'),
2 => array($this->_paymentProcessor['id'], 'Integer'),
);
CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution_recur
......@@ -626,21 +634,22 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$query_params = array(
1 => array($stripe_customer->id, 'String'),
2 => array($invoice_id, 'String'),
2 => array($this->_paymentProcessor['id'], 'Integer'),
);
// Insert the new Stripe Subscription info.
// Set end_time to NULL if installments are ongoing indefinitely
if (empty($installments)) {
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions
(customer_id, invoice_id, is_live)
VALUES (%1, %2, '{$this->_islive}')", $query_params);
(customer_id, invoice_id, is_live, processor_id)
VALUES (%1, %2, '{$this->_islive}', %3)", $query_params);
}
else {
// Add the end time to the query params.
$query_params[3] = array($end_time, 'Integer');
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions
(customer_id, invoice_id, end_time, is_live)
VALUES (%1, %2, %3, '{$this->_islive}')", $query_params);
(customer_id, invoice_id, end_time, is_live, processor_id)
VALUES (%1, %2, %3, '{$this->_islive}', %3)", $query_params);
}
return $params;
......
......@@ -51,4 +51,45 @@ class CRM_Stripe_Upgrader extends CRM_Stripe_Upgrader_Base {
}
return TRUE;
}
/**
* Add processor_id column to civicrm_stripe_customers table.
*
* @return TRUE on success
* @throws Exception
*/
public function upgrade_4_6_01() {
$procIdCheck = mysql_query("SHOW COLUMNS FROM `civicrm_stripe_customers` LIKE 'processor_id'");
if (mysql_num_rows($procIdCheck)) {
$this->ctx->log->info('Skipped civicrm_stripe update 4601. Column processor_id already present on civicrm_stripe_customers and civicrm_stripe_plans table.');
}
else {
$this->ctx->log->info('Applying civicrm_stripe update 4601. Adding processor_id to civicrm_stripe_customers and civicrm_stripe_plans table.');
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_stripe_customers ADD COLUMN `processor_id` int(10) DEFAULT NULL COMMENT "ID from civicrm_payment_processor"');
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_stripe_plans ADD COLUMN `processor_id` int(10) DEFAULT NULL COMMENT "ID from civicrm_payment_processor"');
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_stripe_subscriptions ADD COLUMN `processor_id` int(10) DEFAULT NULL COMMENT "ID from civicrm_payment_processor"');
try {
// Set processor ID if there's only one.
$processorCount = civicrm_api3('PaymentProcessorType', 'get', array(
'name' => "Stripe",
'api.PaymentProcessor.getcount' => array('is_test' => 0),
));
foreach ($processorCount['values'] as $processorType) {
if (!empty($processorType['api.PaymentProcessor.get']['id'])) {
$p = array(
1 => array($processorType['api.PaymentProcessor.get']['id'], 'Integer'),
);
CRM_Core_DAO::executeQuery('UPDATE civicrm_stripe_customers SET processor_id = %1 where processor_id IS NULL', $p);
CRM_Core_DAO::executeQuery('UPDATE civicrm_stripe_plans SET processor_id = %1 where processor_id IS NULL', $p);
CRM_Core_DAO::executeQuery('UPDATE civicrm_stripe_subscriptions SET processor_id = %1 where processor_id IS NULL', $p);
}
}
}
catch (CiviCRM_API3_Exception $e) {
return TRUE;
}
}
return TRUE;
}
}
......@@ -32,6 +32,7 @@ function stripe_civicrm_install() {
`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',
`processor_id` int(10) DEFAULT NULL COMMENT 'ID from civicrm_payment_processor',
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
......@@ -40,6 +41,7 @@ function stripe_civicrm_install() {
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',
`processor_id` int(10) DEFAULT NULL COMMENT 'ID from civicrm_payment_processor',
UNIQUE KEY `plan_id` (`plan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
......@@ -50,6 +52,7 @@ function stripe_civicrm_install() {
`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',
`processor_id` int(10) DEFAULT NULL COMMENT 'ID from civicrm_payment_processor',
KEY `end_time` (`end_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment