Skip to content

Commit

Permalink
CIVIEWAY-246 Refactor legacy upgrade functions into Upgrader class
Browse files Browse the repository at this point in the history
  • Loading branch information
agileware-fj committed Jan 25, 2023
1 parent a2c398a commit a0120b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 87 deletions.
35 changes: 35 additions & 0 deletions CRM/eWAYRecurring/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,41 @@
* Collection of upgrade steps.
*/
class CRM_eWAYRecurring_Upgrader extends CRM_Extension_Upgrader_Base {
public function upgrade_6() {
$setting_url = CRM_Utils_System::url('civicrm/admin/paymentProcessor', ['reset' => 1]);
$this->ctx->log->info(E::ts('Version 2.x of the eWay Payment Processor extension uses the new eWay Rapid API. Please go to the <a href="%2">Payment Processor page</a> and update the eWay API credentials with the new API Key and API Password. For more details see the <a href="%1">upgrade notes</a>.', [
1 => 'https://github.com/agileware/au.com.agileware.ewayrecurring/blob/master/UPGRADE.md',
2 => $setting_url,
]));

$this->ctx->log->info('Update Payment Processor labels for Rapid API');
CRM_Core_DAO::executeQuery("UPDATE civicrm_payment_processor_type SET user_name_label = 'API Key', password_label = 'API Password' WHERE name = 'eWay_Recurring'");

$this->ctx->log->info('Create eWAY Contribution Transactions store.');
$query = <<<SQL
CREATE TABLE IF NOT EXISTS `civicrm_eway_contribution_transactions` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Unique EwayContributionTransactions ID',
`contribution_id` INT UNSIGNED COMMENT 'FK to Contact',
`payment_processor_id` INT UNSIGNED COMMENT 'FK to PaymentProcessor',
`access_code` TEXT,
`failed_message` TEXT DEFAULT NULL,
`status` INT UNSIGNED DEFAULT 0,
`tries` INT UNSIGNED DEFAULT 0,
PRIMARY KEY(`id`),
CONSTRAINT FK_civicrm_eway_contribution_transactions_contribution_id FOREIGN KEY(`contribution_id`) REFERENCES `civicrm_contribution` (`id`) ON DELETE CASCADE,
CONSTRAINT FK_civicrm_eway_contribution_transactions_payment_processor_id FOREIGN KEY(`payment_processor_id`) REFERENCES `civicrm_payment_processor` (`id`) ON DELETE CASCADE
);
SQL;
CRM_Core_DAO::executeQuery($query);
return TRUE;
}

public function upgrade_7() {
$this->ctx->log->info('Add email receipt field to eWAY Contribution Transactions store.');
CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_eway_contribution_transactions` ADD `is_email_receipt` TINYINT(1) DEFAULT 1");
return TRUE;
}

public function upgrade_20201() {
$this->ctx->log->info('Applying 2.2.1 update; Fix billing mode for payment processor.');
CRM_Core_DAO::executeQuery("UPDATE civicrm_payment_processor_type SET billing_mode = 4 WHERE name = 'eWay_Recurring'");
Expand Down
87 changes: 0 additions & 87 deletions eWAYRecurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,93 +49,6 @@ function ewayrecurring_civicrm_enable() {
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function ewayrecurring_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
$schemaVersion = intval(CRM_Core_BAO_Extension::getSchemaVersion('au.com.agileware.ewayrecurring'));
$upgrades = [];

if ($op == 'check') {
if ($schemaVersion < 6) {
$setting_url = CRM_Utils_System::url('civicrm/admin/paymentProcessor', ['reset' => 1]);
CRM_Core_Session::setStatus(ts('Version 2.x of the eWay Payment Processor extension uses the new eWay Rapid API. Please go to the <a href="%2">Payment Processor page</a> and update the eWay API credentials with the new API Key and API Password. For more details see the <a href="%1">upgrade notes</a>.', [
1 => 'https://github.com/agileware/au.com.agileware.ewayrecurring/blob/master/UPGRADE.md',
2 => $setting_url,
]), ts('eWay Payment Processor Update'));
}
if ($schemaVersion < 7) {
CRM_Core_Session::setStatus(ts('Please edit and save (without any changes) your existing eWay payment processor after updating.'), ts('eWay Payment Processor Update'));
}
return [$schemaVersion < 20201];
}
elseif ($op == 'enqueue') {
if (NULL == $queue) {
return CRM_Core_Error::fatal('au.com.agileware.ewayrecurring: No Queue supplied for upgrade');
}
if ($schemaVersion < 5) {
$queue->createItem(
new CRM_Queue_Task('_ewayrecurring_upgrade_schema_version', [
5,
],
'Update schema version'
)
);
}
if ($schemaVersion < 6) {
$queue->createItem(
new CRM_Queue_Task('_ewayrecurring_upgrade_schema', [
6,
"UPDATE civicrm_payment_processor_type SET user_name_label = 'API Key', password_label = 'API Password' WHERE name = 'eWay_Recurring'",
],
'Perform Rapid API related changes'
)
);

// add the table if not exist
$queue->createItem(
new CRM_Queue_Task('_ewayrecurring_upgrade_schema', [
6,
"CREATE TABLE IF NOT EXISTS `civicrm_eway_contribution_transactions`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Unique EwayContributionTransactions ID',
`contribution_id` INT UNSIGNED COMMENT 'FK to Contact',
`payment_processor_id` INT UNSIGNED COMMENT 'FK to PaymentProcessor',
`access_code` TEXT,
`failed_message` TEXT DEFAULT NULL,
`status` INT UNSIGNED DEFAULT 0,
`tries` INT UNSIGNED DEFAULT 0,
PRIMARY KEY(`id`),
CONSTRAINT FK_civicrm_eway_contribution_transactions_contribution_id FOREIGN KEY(`contribution_id`) REFERENCES `civicrm_contribution`(`id`) ON DELETE CASCADE,
CONSTRAINT FK_civicrm_eway_contribution_transactions_payment_processor_id FOREIGN KEY(`payment_processor_id`) REFERENCES `civicrm_payment_processor`(`id`) ON DELETE CASCADE
);",
],
'Create the table if not exist.'
)
);
}
if ($schemaVersion < 7) {
// CIVIEWAY-76 remember the send email option
$queue->createItem(
new CRM_Queue_Task('_ewayrecurring_upgrade_schema', [
7,
"ALTER TABLE `civicrm_eway_contribution_transactions` ADD `is_email_receipt` TINYINT(1) DEFAULT 1",
],
'Save the send email option.'
)
);
}

if ($schemaVersion < 20200) {
$queue->createItem(
new CRM_Queue_Task('_ewayrecurring_upgrade_schema', [
20200,
"UPDATE civicrm_payment_processor SET billing_mode = 4 WHERE payment_processor_type_id = (SELECT id FROM civicrm_payment_processor_type WHERE name = 'eWay_Recurring')",
],
'Updating existing processors.'
)
);
}
}
// @TODO Refactor the upgrader function to use the upgrader class
// return _ewayrecurring_civix_civicrm_upgrade($op, $queue);
}

/**
* Implements hook_civicrm_managed().
Expand Down

0 comments on commit a0120b6

Please sign in to comment.