You are here

function commerce_square_update_7001 in Commerce Square Connect 7

Migrate configuration to new format.

File

./commerce_square.install, line 56
Commerce Square install file.

Code

function commerce_square_update_7001() {
  $conditions = array(
    'event' => 'commerce_payment_methods',
    'plugin' => 'reaction rule',
    'active' => TRUE,
    'owner' => 'rules',
  );
  $rules = entity_load('rules_config', FALSE, $conditions);

  /** @var RulesReactionRule $rule */
  foreach ($rules as $rule) {
    foreach ($rule as $child) {
      if ($child instanceof RulesAction) {
        if (!empty($child->settings) && !empty($child->settings['payment_method'])) {
          $payment_method = $child->settings['payment_method'];

          // Payment methods without settings are a string with method ID.
          if (!is_array($payment_method) || empty($payment_method['settings'])) {
            continue;
          }
          if ($payment_method['method_id'] == 'commerce_square') {
            $payment_method_settings = $payment_method['settings'];
            $square_settings = array(
              'app_name' => $payment_method_settings['app_name'],
              'app_secret' => $payment_method_settings['app_secret'],
              'test_app_id' => $payment_method_settings['test_app_id'],
              'test_access_token' => $payment_method_settings['test_access_token'],
              'live_app_id' => $payment_method_settings['live_app_id'],
              'live_access_token' => $payment_method_settings['live_access_token'],
              'live_access_token_expiry' => $payment_method_settings['live_access_token_expiry'],
            );
            variable_set('commerce_square_settings', $square_settings);
            foreach (array_keys($square_settings) as $key_to_remove) {
              unset($payment_method_settings[$key_to_remove]);
            }
            unset($payment_method_settings['test']);
            unset($payment_method_settings['live']);
            $child->settings['payment_method']['settings'] = $payment_method_settings;
            $child
              ->save();
          }
        }
      }
    }
  }
}