uc_gc_client-8.x-1.x-dev/uc_gc_client.install

uc_gc_client.install
<?php

/**
 * @file
 * Installation file for the uc_gc_client module.
 */

use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\uc_order\Entity\OrderStatus;
use Drupal\uc_country\Controller\CountryController;

/**
 * Implements hook_install().
 */
function uc_gc_client_install() {
  $site_email = \Drupal::config('system.site')->get('mail');
  $config = \Drupal::service('config.factory')->getEditable('uc_gc_client.settings');
  $config->set('warnings_email', $site_email)->save();

  $countries = $config->get('countries');
  $countryController = new CountryController();
  $uc_countries = $countryController->countryOptionsCallback();
  foreach ($countries as $country_code => $country) {
    if (isset($uc_countries[$country_code])) {
      $countries[$country_code]['enabled'] = 1;
    }
  }
  $config->set('countries', $countries)->save();

  // Provide 'Interval' attribute and set of options.
  $check = db_select('uc_attributes', 'a')
    ->fields('a')
    ->condition('name', 'Interval', '=')
    ->execute()->fetch();

  if (empty($check)) {
    $insert_att = db_insert('uc_attributes')
      ->fields([
        'name' => 'Interval',
        'label' => 'Payment interval',
        'ordering' => 0,
        'required' => 1,
        'display' => 1,
        'description' => 'How often would you like to pay your direct debit?',
      ])
      ->execute();

    $options = ['Weekly', 'Fortnightly', 'Monthly', 'Yearly'];
    foreach ($options as $option) {
      db_insert('uc_attribute_options')
        ->fields([
          'aid' => $insert_att,
          'name' => $option,
          'cost' => 0,
          'price' => 0,
          'weight' => 0,
          'ordering' => 0,
        ])
        ->execute();
    }
  }
}

/**
 * Implements hook_schema().
 */
function uc_gc_client_schema() {
  $schema['uc_gc_client'] = [
    'description' => 'Stores data on GoCardless orders',
    'fields' => [
      'ucid' => [
        'description' => 'Ubercart order ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'ucpid' => [
        'description' => 'Ubercart product ID',
        'type' => 'int',
        'not null' => FALSE,
      ],
      'gcid' => [
        'description' => 'The GoCardless mandate ID',
        'type' => 'varchar',
        'length' => '64',
        'not null' => TRUE,
      ],
      'gcrid' => [
        'description' => 'The GoCardless Redirect flow ID associated with order',
        'type' => 'varchar',
        'length' => '64',
        'not null' => FALSE,
      ],
      'gccid' => [
        'description' => 'The GoCardless Customer ID associated with order',
        'type' => 'varchar',
        'length' => '64',
        'not null' => FALSE,
      ],
      'scheme' => [
        'description' => 'The GoCardless currency scheme',
        'type' => 'varchar',
        'length' => '32',
        'not null' => FALSE,
      ],
      'uid' => [
        'description' => 'Customer’s user ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'next_payment' => [
        'description' => 'Timestamp for the next payment',
        'type' => 'int',
        'not null' => FALSE,
      ],
      'next_payment_uuid' => [
        'description' => 'Unique identifier for the next payment',
        'type' => 'varchar',
        'length' => '128',
        'not null' => FALSE,
      ],
      'type' => [
        'description' => 'Subscription or One-off payment',
        'type' => 'varchar',
        'length' => '1',
        'not null' => TRUE,
        'default' => 'S',
      ],
      'status' => [
        'description' => 'The GoCardless mandate status',
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ],
      'created' => [
        'description' => 'Unix timestamp when the order was created',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'start_date' => [
        'description' => 'Unix timestamp when the first payment is charged',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
      ],
      'updated' => [
        'description' => 'Unix timestamp when the Status was updated',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'sandbox' => [
        'description' => 'Whether or not it is a sandbox transaction',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'unique keys' => [
      'primary index' => ['gcid', 'ucpid'],
    ],
  ];
  $schema['uc_gc_client_products'] = [
    'description' => 'GoCardless data for Ubercart products',
    'fields' => [
      'nid' => [
        'description' => 'The node ID for the product',
        'type' => 'int',
        'not null' => TRUE,
      ],
      'gc_use' => [
        'description' => 'If product uses GoCardless settings',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ],
      'type' => [
        'description' => 'Subscription or pre-authorization',
        'type' => 'varchar',
        'length' => '1',
        'not null' => FALSE,
      ],
      'create_payment' => [
        'description' => 'Create payment immediately upon completion of checkout',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => FALSE,
      ],
      'dom' => [
        'description' => 'One or more days of month that payment is created on',
        'type' => 'varchar',
        'length' => '64',
        'not null' => FALSE,
      ],
      'start_date' => [
        'description' => 'Optional start date for new mandate',
        'type' => 'varchar',
        'length' => '16',
        'not null' => FALSE,
      ],
      'interval_length' => [
        'description' => 'The interval between scheduled payment creations',
        'type' => 'int',
        'not null' => FALSE,
      ],
      'interval_unit' => [
        'description' => 'The interval unit between scheduled payment creations',
        'type' => 'varchar',
        'length' => '32',
        'not null' => FALSE,
      ],
      'price_x' => [
        'description' => 'Subscription multiplier',
        'type' => 'numeric',
        'not null' => FALSE,
        'default' => 1,
        'precision' => '6',
        'scale' => '3',
      ],
    ],
    'primary key' => ['nid'],
  ];
  $schema['uc_gc_client_schedules'] = [
    'description' => 'Schedules table',
    'fields' => [
      'sid' => [
        'description' => 'Schedule ID',
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'ucid' => [
        'description' => 'Order ID',
        'type' => 'int',
        'not null' => TRUE,
      ],
      'ucpid' => [
        'description' => 'Product ID',
        'type' => 'int',
        'not null' => TRUE,
      ],
      'type' => [
        'description' => 'The type of scheduled event',
        'type' => 'varchar',
        'length' => '16',
        'not null' => TRUE,
      ],
      'date' => [
        'description' => 'A formatted date string for the scheduled event',
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
      ],
      'timestamp' => [
        'description' => 'The timestamp for the scheduled event',
        'type' => 'int',
        'not null' => TRUE,
      ],
      'status' => [
        'description' => 'The status of the schedule',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
      ],
      'data' => [
        'description' => 'Available for serialized data by other modules',
        'type' => 'blob',
        'not null' => FALSE,
      ],
      'created' => [
        'description' => 'Timestamp when scheduled event was created',
        'type' => 'int',
        'not null' => FALSE,
      ],
    ],
    'primary key' => ['sid'],
    'indexes' => [
      'timestamp' => ['timestamp'],
      'type' => ['type'],
      'ucid' => ['ucid', 'ucpid'],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function uc_gc_client_uninstall() {
  foreach (['mandate_active', 'mandate_failed'] as $status) {
    $uc_orders = OrderStatus::load($status);
    if (!is_null($uc_orders)) {
      $uc_orders->setLocked(FALSE)->delete();
    }
  }
  // Foreach (['webhook_live', 'webhook_sandbox',] as $entity) {
  // \Drupal::state()->delete('uc_gc_client_' . $entity);
  // }.
  $datetime = DateFormat::load('gocardless');
  if (!is_null($datetime)) {
    $datetime->delete();
  }
}

/**
 * Add country definitions for Denmark, Australia and New Zealand.
 */
function uc_gc_client_update_8101() {
  $config = \Drupal::service('config.factory')->getEditable('uc_gc_client.settings');
  $countries = $config->get('countries');
  $countries_new = [
    'AU' => [
      'region' => 'becs',
      'currency' => 'AUD',
      'sign' => '$',
      'enabled' => '0',
    ],
    'DK' => [
      'region' => 'betalingsservice',
      'currency' => 'DKK',
      'sign' => 'kr',
      'enabled' => '0',
    ],
    'NZ' => [
      'region' => 'becs_nz',
      'currency' => 'NZD',
      'sign' => '$',
      'enabled' => '0',
    ],
  ];
  $config->set('countries', array_merge($countries, $countries_new))->save();
}

/**
 * Add the 'scheme' field in the uc_gc_client table.
 */
function uc_gc_client_update_8102() {
  $scheme = [
    'description' => 'The GoCardless currency scheme',
    'type' => 'varchar',
    'length' => '32',
    'not null' => FALSE,
  ];
  if (!db_field_exists('uc_gc_client', 'scheme')) {
    db_add_field('uc_gc_client', 'scheme', $scheme);
  }
}

/**
 * Add country definition for Canada.
 */
function uc_gc_client_update_8103() {
  $config = \Drupal::service('config.factory')->getEditable('uc_gc_client.settings');
  $countries = $config->get('countries');
  $countries_new = [
    'CA' => [
      'region' => 'pad',
      'currency' => 'CAD',
      'sign' => '$',
      'enabled' => '0',
    ],
  ];
  $config->set('countries', array_merge($countries, $countries_new))->save();
}

/**
 * Add country definition for US.
 */
function uc_gc_client_update_8104() {
  $config = \Drupal::service('config.factory')->getEditable('uc_gc_client.settings');
  $countries = $config->get('countries');
  $countries_new = [
    'US' => [
      'region' => 'ach',
      'currency' => 'USD',
      'sign' => '$',
      'enabled' => '0',
    ],
  ];
  $config->set('countries', array_merge($countries, $countries_new))->save();
}

/**
 * Add the 'next_payment_uuid' field in the uc_gc_client table.
 */
function uc_gc_client_update_8105() {
  if (!db_field_exists('uc_gc_client', 'next_payment_uuid')) {
    $field = [
      'description' => 'Unique identifier for the next payment',
      'type' => 'varchar',
      'length' => '128',
      'not null' => FALSE,
    ];
    db_add_field('uc_gc_client', 'next_payment_uuid', $field);
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc