Skip to content
Snippets Groups Projects
Commit fc821c96 authored by mattwire's avatar mattwire
Browse files

Add upgrader for 5.2 and deprecate civicrm_stripe_plans/civicrm_stripe_subscriptions tables

parent 260deb03
Branches
Tags
1 merge request!11Support Cancellation and major refactor of backend / webhook processing
......@@ -383,4 +383,10 @@ class CRM_Stripe_Upgrader extends CRM_Stripe_Upgrader_Base {
return TRUE;
}
public function upgrade_5020() {
$this->ctx->log->info('Applying Stripe update 5020. Migrate civicrm_stripe_subscriptions data to recurring contributions.');
civicrm_api3('StripeSubscription', 'updatetransactionids', []);
return TRUE;
}
}
<?php
/**
* Stripe Customer API
* Stripe Subscription API
*
*/
/**
* StripeCustomer.Get API specification
* StripeSubscription.Get API specification
*
* @param array $spec description of fields supported by this API call
* @return void
......@@ -28,6 +28,8 @@ function _civicrm_api3_stripe_subscription_get_spec(&$spec) {
}
/**
* @deprecated This StripeSubscription.get is deprecated as of 5.2 as we now using recurring contribution instead of civicrm_stripe_subscriptions
*
* StripeSubscription.Get API
* This api will get entries from the civicrm_stripe_subscriptions table
*
......@@ -63,6 +65,7 @@ function civicrm_api3_stripe_subscription_get($params) {
}
}
$query = "SELECT * FROM civicrm_stripe_subscriptions ";
if (count($where)) {
$whereClause = implode(' AND ', $where);
......@@ -83,3 +86,29 @@ function civicrm_api3_stripe_subscription_get($params) {
}
return civicrm_api3_create_success($results);
}
function civicrm_api3_stripe_subscription_updatetransactionids() {
if (!CRM_Core_DAO::checkTableExists('civicrm_stripe_subscriptions')) {
throw new CiviCRM_API3_Exception('Table civicrm_stripe_subscriptions is not used in Stripe >=5.2 and does not exist on your install. This API will be removed in a future release.');
}
$sql = "SELECT subscription_id, contribution_recur_id FROM civicrm_stripe_subscriptions";
$dao = CRM_Core_DAO::executeQuery($sql);
$counts = [
'success' => 0,
'failed' => 0
];
while ($dao->fetch()) {
if (!empty($dao->subscription_id) && !empty($dao->contribution_recur_id)) {
try {
civicrm_api3('ContributionRecur', 'create', ['id' => $dao->contribution_recur_id, 'trxn_id' => $dao->subscription_id]);
$counts['success']++;
}
catch (Exception $e) {
Civi::log()->debug('Error updating trxn_id for recur: ' . $dao->contribution_recur_id . ' trxn_id: ' . $dao->subscription_id);
$counts['failed']++;
}
}
}
return civicrm_api3_create_success($counts);
}
......@@ -8,22 +8,3 @@
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
DROP TABLE IF EXISTS civicrm_stripe_plans;
DROP TABLE IF EXISTS civicrm_stripe_subscriptions;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment