Commit bc1689aa authored by mattwire's avatar mattwire

Use contact_id as reference in civicrm_stripe_customers and don't require an...

Use contact_id as reference in civicrm_stripe_customers and don't require an email address for payment
parent ca29d05d
This diff is collapsed.
<?php
class CRM_Stripe_Customer {
/**
* Find an existing Stripe customer in the CiviCRM database
*
* @param $params
*
* @return null|string
* @throws \CRM_Core_Exception
*/
public static function find($params) {
$requiredParams = ['is_live', 'processor_id'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (find): Missing required parameter: ' . $required);
}
}
if (empty($params['email']) && empty($params['contact_id'])) {
throw new CRM_Core_Exception('Stripe Customer (find): One of email or contact_id is required');
}
$queryParams = [
1 => [$params['contact_id'], 'String'],
2 => [$params['is_live'], 'Boolean'],
3 => [$params['processor_id'], 'Positive'],
];
return CRM_Core_DAO::singleValueQuery("SELECT id
FROM civicrm_stripe_customers
WHERE contact_id = %1 AND is_live = %2 AND processor_id = %3", $queryParams);
}
/**
* Add a new Stripe customer to the CiviCRM database
*
* @param $params
*
* @throws \CRM_Core_Exception
*/
public static function add($params) {
$requiredParams = ['contact_id', 'customer_id', 'is_live', 'processor_id'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (add): Missing required parameter: ' . $required);
}
}
$queryParams = [
1 => [$params['contact_id'], 'String'],
2 => [$params['customer_id'], 'String'],
3 => [$params['is_live'], 'Boolean'],
4 => [$params['processor_id'], 'Integer'],
];
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers
(contact_id, id, is_live, processor_id) VALUES (%1, %2, %3, %3)", $queryParams);
}
public static function create($params, $paymentProcessor) {
$params['processor_id'] = $paymentProcessor->_paymentProcessor['id'];
$requiredParams = ['contact_id', 'card_token', 'is_live', 'processor_id'];
// $optionalParams = ['email'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (create): Missing required parameter: ' . $required);
}
}
$contactDisplayName = civicrm_api3('Contact', 'getvalue', [
'return' => ['display_name'],
'id' => $params['contact_id'],
]);
$sc_create_params = [
'description' => $contactDisplayName . ' (CiviCRM)',
'card' => $params['card_token'],
'email' => CRM_Utils_Array::value('email', $params),
'metadata' => ['civicrm_contact_id' => $params['contact_id']],
];
$stripe_customer = $paymentProcessor->stripeCatchErrors('create_customer', $sc_create_params, $params);
// Store the relationship between CiviCRM's email address for the Contact & Stripe's Customer ID.
if (isset($stripe_customer)) {
if ($paymentProcessor->isErrorReturn($stripe_customer)) {
return $stripe_customer;
}
$params = [
'contact_id' => $params['contact_id'],
'customer_id' => $stripe_customer->id,
'is_live' => $params['is_live'],
'processor_id' => $params['processor_id'],
];
self::add($params);
}
else {
Throw new CRM_Core_Exception(ts('There was an error saving new customer within Stripe.'));
}
return $stripe_customer;
}
/**
* Delete a Stripe customer from the CiviCRM database
*
* @param $params
*
* @throws \CRM_Core_Exception
*/
public static function delete($params) {
$requiredParams = ['contact_id', 'is_live', 'processor_id'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (delete): Missing required parameter: ' . $required);
}
}
$queryParams = [
1 => [$params['contact_id'], 'String'],
2 => [$params['is_live'], 'Boolean'],
3 => [$params['processor_id'], 'Integer'],
];
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_customers
WHERE contact_id = %1 AND is_live = %2 AND processor_id = %3", $queryParams);
}
}
......@@ -10,18 +10,6 @@ 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).
/**
* Standard: run an install sql script
*/
public function install() {
}
/**
* Standard: run an uninstall script
*/
public function uninstall() {
}
/**
* Add is_live column to civicrm_stripe_plans and civicrm_stripe_customers tables.
*
......@@ -379,4 +367,21 @@ class CRM_Stripe_Upgrader extends CRM_Stripe_Upgrader_Base {
}
return TRUE;
}
public function upgrade_5010() {
$this->ctx->log->info('Applying Stripe update 5010. Adding contact_id to civicrm_stripe_customers.');
if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_stripe_customers', 'contact_id', FALSE)) {
CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_stripe_customers`
ADD COLUMN `contact_id` int(10) UNSIGNED DEFAULT NULL COMMENT "FK ID from civicrm_contact"');
CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_stripe_customers`
ADD CONSTRAINT `FK_civicrm_stripe_customers_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE;');
CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_stripe_customers` ADD UNIQUE (contact_id)');
}
$this->ctx->log->info('Applying Stripe update 5010. Getting Contact IDs for civicrm_stripe_customers.');
civicrm_api3('Stripe', 'customercontactids', []);
return TRUE;
}
}
<?php
/**
* Stripe Customer API
*
*/
/**
* Stripe.Customer_Contactids API
* This api will update the civicrm_stripe_customers table and add contact IDs for all known email addresses
*
* @param array $params
* @see civicrm_api3_create_success
*
* @return array
*/
function civicrm_api3_stripe_customercontactids($params) {
$dao = CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_stripe_customers');
$counts = [
'updated' => 0,
'failed' => 0,
];
while ($dao->fetch()) {
try {
$contactId = civicrm_api3('Contact', 'getvalue', [
'return' => "id",
'email' => $dao->email,
]);
CRM_Core_DAO::executeQuery("UPDATE `civicrm_stripe_customers` SET contact_id={$contactId} WHERE email='{$dao->email}'");
$counts['updated']++;
} catch (Exception $e) {
Civi::log()
->debug('Stripe Upgrader: No contact ID found for stripe customer with email: ' . $dao->email);
$counts['failed']++;
}
}
return civicrm_api3_create_success($counts);
}
......@@ -12,15 +12,18 @@
<author>Matthew Wire (MJW Consulting)</author>
<email>mjw@mjwconsult.co.uk</email>
</maintainer>
<releaseDate>2018-09-19</releaseDate>
<version>5.0</version>
<develStage>stable</develStage>
<releaseDate>2018-10-11</releaseDate>
<version>5.1.alpha3</version>
<develStage>beta</develStage>
<compatibility>
<ver>5.0</ver>
<ver>5.3</ver>
</compatibility>
<comments>Original Author: Joshua Walker (drastik) - Drastik by Design.
Jamie Mcclelland (ProgressiveTech) did a lot of the 5.x compatibility work.
</comments>
<requires>
<ext>org.civicrm.paymentlib</ext>
</requires>
<civix>
<namespace>CRM/Stripe</namespace>
</civix>
......
/* Create required tables for Stripe */
CREATE TABLE IF NOT EXISTS `civicrm_stripe_customers` (
`id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`contact_id` int(10) UNSIGNED DEFAULT NULL COMMENT 'FK ID from civicrm_contact',
`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`),
UNIQUE KEY `contact_id` (`contact_id`),
CONSTRAINT `FK_civicrm_stripe_customers_contact_id` FOREIGN KEY (`contact_id`)
REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
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;
CREATE TABLE IF NOT EXISTS `civicrm_stripe_subscriptions` (
`subscription_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`customer_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`contribution_recur_id` INT(10) UNSIGNED NULL DEFAULT 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`), PRIMARY KEY `subscription_id` (`subscription_id`),
CONSTRAINT `FK_civicrm_stripe_contribution_recur_id` FOREIGN KEY (`contribution_recur_id`)
REFERENCES `civicrm_contribution_recur`(`id`) ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/* Remove Stripe tables on uninstall. */
DROP TABLE civicrm_stripe_customers;
DROP TABLE civicrm_stripe_plans;
DROP TABLE civicrm_stripe_subscriptions;
\ No newline at end of file
......@@ -25,41 +25,6 @@ function stripe_civicrm_xmlMenu(&$files) {
* Implementation of hook_civicrm_install().
*/
function stripe_civicrm_install() {
// Create required tables for Stripe.
require_once "CRM/Core/DAO.php";
CRM_Core_DAO::executeQuery("
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',
`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;
");
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',
`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;
");
CRM_Core_DAO::executeQuery("
CREATE TABLE IF NOT EXISTS `civicrm_stripe_subscriptions` (
`subscription_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`customer_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`contribution_recur_id` INT(10) UNSIGNED NULL DEFAULT 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`), PRIMARY KEY `subscription_id` (`subscription_id`),
CONSTRAINT `FK_civicrm_stripe_contribution_recur_id` FOREIGN KEY (`contribution_recur_id`)
REFERENCES `civicrm_contribution_recur`(`id`) ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
_stripe_civix_civicrm_install();
}
......@@ -67,12 +32,6 @@ function stripe_civicrm_install() {
* Implementation of hook_civicrm_uninstall().
*/
function stripe_civicrm_uninstall() {
// Remove Stripe tables on uninstall.
require_once "CRM/Core/DAO.php";
CRM_Core_DAO::executeQuery("DROP TABLE civicrm_stripe_customers");
CRM_Core_DAO::executeQuery("DROP TABLE civicrm_stripe_plans");
CRM_Core_DAO::executeQuery("DROP TABLE civicrm_stripe_subscriptions");
_stripe_civix_civicrm_uninstall();
}
......
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