| 1 |
<?php declare( strict_types=1 ); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Services; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class PostepayMigrationService |
| 7 |
* |
| 8 |
* Handles the one-time migration of PostePay payment method settings |
| 9 |
* from the legacy single PostePay to the new Mastercard/Visa variants. |
| 10 |
* |
| 11 |
* @package MultiSafepay\WooCommerce\Services |
| 12 |
*/ |
| 13 |
class PostepayMigrationService { |
| 14 |
|
| 15 |
/** |
| 16 |
* Option name that marks the migration as complete |
| 17 |
*/ |
| 18 |
private const MIGRATION_COMPLETE_OPTION = 'woocommerce_multisafepay_postepay_migration_done'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Legacy PostePay settings option |
| 22 |
*/ |
| 23 |
private const LEGACY_POSTEPAY_OPTION = 'woocommerce_multisafepay_postepay_settings'; |
| 24 |
|
| 25 |
/** |
| 26 |
* New PostePay Mastercard settings option |
| 27 |
*/ |
| 28 |
private const MASTERCARD_POSTEPAY_OPTION = 'woocommerce_multisafepay_postepay_mastercard_settings'; |
| 29 |
|
| 30 |
/** |
| 31 |
* New PostePay Visa settings option |
| 32 |
*/ |
| 33 |
private const VISA_POSTEPAY_OPTION = 'woocommerce_multisafepay_postepay_visa_settings'; |
| 34 |
|
| 35 |
/** |
| 36 |
* New PostePay - Mastercard title pattern |
| 37 |
*/ |
| 38 |
private const POSTEPAY_MASTERCARD_TITLE_PATTERN = 'PostePay - Mastercard'; |
| 39 |
|
| 40 |
/** |
| 41 |
* New PostePay - Visa title pattern |
| 42 |
*/ |
| 43 |
private const POSTEPAY_VISA_TITLE_PATTERN = 'PostePay - Visa'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Performs the complete migration process if not already done |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function postepay_migration(): void { |
| 51 |
// Check if migration is already complete |
| 52 |
if ( $this->is_migration_complete() ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Only proceed if credit cards are not grouped |
| 57 |
if ( get_option( 'multisafepay_group_credit_cards', false ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Get the legacy PostePay settings |
| 62 |
$legacy_settings = get_option( self::LEGACY_POSTEPAY_OPTION, false ); |
| 63 |
if ( ! is_array( $legacy_settings ) ) { |
| 64 |
// No legacy settings exist, just mark as complete |
| 65 |
$this->mark_migration_complete(); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Check for the special case: legacy is enabled, but variants exist and are disabled |
| 70 |
if ( $this->should_enable_existing_disabled_variants( $legacy_settings ) ) { |
| 71 |
$this->enable_existing_disabled_variants_and_disable_legacy( $legacy_settings ); |
| 72 |
$this->mark_migration_complete(); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// Perform the migration |
| 77 |
$this->migrate_postepay_variants( $legacy_settings ); |
| 78 |
|
| 79 |
// Mark migration as complete |
| 80 |
$this->mark_migration_complete(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Checks if the migration is already complete |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
*/ |
| 88 |
private function is_migration_complete(): bool { |
| 89 |
return (bool) get_option( self::MIGRATION_COMPLETE_OPTION, false ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Migrates settings for both PostePay variants |
| 94 |
* |
| 95 |
* @param array $legacy_settings |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
private function migrate_postepay_variants( array $legacy_settings ): void { |
| 99 |
// Create settings for Mastercard variant |
| 100 |
$this->create_variant_settings( |
| 101 |
self::MASTERCARD_POSTEPAY_OPTION, |
| 102 |
$legacy_settings, |
| 103 |
self::POSTEPAY_MASTERCARD_TITLE_PATTERN |
| 104 |
); |
| 105 |
|
| 106 |
// Create settings for Visa variant |
| 107 |
$this->create_variant_settings( |
| 108 |
self::VISA_POSTEPAY_OPTION, |
| 109 |
$legacy_settings, |
| 110 |
self::POSTEPAY_VISA_TITLE_PATTERN |
| 111 |
); |
| 112 |
|
| 113 |
// Disable the legacy PostePay method |
| 114 |
$legacy_settings['enabled'] = 'no'; |
| 115 |
update_option( self::LEGACY_POSTEPAY_OPTION, $legacy_settings ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Creates settings for a PostePay variant |
| 120 |
* |
| 121 |
* @param string $option_name |
| 122 |
* @param array $base_settings |
| 123 |
* @param string $title |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
private function create_variant_settings( string $option_name, array $base_settings, string $title ): void { |
| 127 |
$existing_settings = get_option( $option_name, false ); |
| 128 |
|
| 129 |
if ( ! $existing_settings ) { |
| 130 |
// Create new settings if they don't exist |
| 131 |
$variant_settings = $base_settings; |
| 132 |
$variant_settings['title'] = $title; |
| 133 |
update_option( $option_name, $variant_settings ); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if ( ! is_array( $existing_settings ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! isset( $existing_settings['enabled'] ) ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if ( 'no' !== $existing_settings['enabled'] ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! isset( $base_settings['enabled'] ) ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
if ( 'yes' !== $base_settings['enabled'] ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
// Enable existing disabled settings if legacy is enabled |
| 158 |
$existing_settings['enabled'] = 'yes'; |
| 159 |
update_option( $option_name, $existing_settings ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Marks the migration process as complete |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
private function mark_migration_complete(): void { |
| 168 |
update_option( self::MIGRATION_COMPLETE_OPTION, true ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Checks if the legacy PostePay settings should enable existing disabled variants |
| 173 |
* |
| 174 |
* @param array $legacy_settings |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
private function should_enable_existing_disabled_variants( array $legacy_settings ): bool { |
| 178 |
return 'yes' === $legacy_settings['enabled'] && |
| 179 |
( get_option( self::MASTERCARD_POSTEPAY_OPTION, false ) || get_option( self::VISA_POSTEPAY_OPTION, false ) ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Enables existing disabled variants and disables the legacy PostePay method |
| 184 |
* |
| 185 |
* @param array $legacy_settings |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
private function enable_existing_disabled_variants_and_disable_legacy( array $legacy_settings ): void { |
| 189 |
// Enable existing disabled variants |
| 190 |
$this->create_variant_settings( |
| 191 |
self::MASTERCARD_POSTEPAY_OPTION, |
| 192 |
$legacy_settings, |
| 193 |
self::POSTEPAY_MASTERCARD_TITLE_PATTERN |
| 194 |
); |
| 195 |
|
| 196 |
$this->create_variant_settings( |
| 197 |
self::VISA_POSTEPAY_OPTION, |
| 198 |
$legacy_settings, |
| 199 |
self::POSTEPAY_VISA_TITLE_PATTERN |
| 200 |
); |
| 201 |
|
| 202 |
// Disable the legacy PostePay method |
| 203 |
$legacy_settings['enabled'] = 'no'; |
| 204 |
update_option( self::LEGACY_POSTEPAY_OPTION, $legacy_settings ); |
| 205 |
} |
| 206 |
} |
| 207 |
|