commerce_gc_client-8.x-1.9/commerce_gc_client.install
commerce_gc_client.install
<?php
/**
* @file
* Install, update, and uninstall functions for the module.
*/
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\commerce_product\Entity\ProductType;
use Drupal\commerce_product\Entity\ProductVariationType;
/**
* Implements hook_install().
*/
function commerce_gc_client_install() {
$settings = \Drupal::configFactory()->getEditable('commerce_gc_client.settings');
$currencies = $settings->get('currency_schemes');
$db = \Drupal::database();
$commerce_default_currencies = $db->select('commerce_store_field_data', 'c')->fields('c', ['default_currency'])->execute()->fetchCol();
foreach ($commerce_default_currencies as $commerce_currency_code) {
if (isset($currencies[$commerce_currency_code])) {
$currencies[$commerce_currency_code]['enabled'] = TRUE;
}
}
$settings->set('currency_schemes', $currencies)->save();
if (!ProductType::load('product_gocardless')) {
$productType = ProductType::create(
[
'id' => 'product_gocardless',
'label' => 'GoCardless Product',
'status' => 1,
'description' => 'A product type for using GoCardless.',
'variationType' => 'product_variation_gocardless',
]
);
$productType->save();
commerce_product_add_body_field($productType);
}
if (!ProductVariationType::load('product_variation_gocardless')) {
$variation_type = ProductVariationType::create(
[
'id' => 'product_variation_gocardless',
'label' => 'GoCardless Product Variation',
'status' => TRUE,
'orderItemType' => 'default',
'generateTitle' => TRUE,
]
);
$variation_type->save();
}
}
/**
* Implements hook_uninstall().
*/
function commerce_gc_client_uninstall() {
$datetime = DateFormat::load('gocardless');
if (!is_null($datetime)) {
$datetime->delete();
}
$fields = [
[
'field_name' => 'gocardless_payment_int',
'entity_type' => 'commerce_product',
'bundle' => 'product',
],
[
'field_name' => 'gocardless_shipping_cost',
'entity_type' => 'commerce_product',
'bundle' => 'product_gocardless',
],
[
'field_name' => 'commerce_price',
'entity_type' => 'commerce_product',
'bundle' => 'product_gocardless',
],
];
foreach ($fields as $field) {
if ($field['field_name'] == 'commerce_price') {
continue;
}
}
}
/**
* Implements hook_schema().
*/
function commerce_gc_client_schema() {
$schema['commerce_gc_client'] = [
'description' => 'Stores data on GoCardless orders',
'fields' => [
'gcid' => [
'description' => 'Commerce GoCardess Client ID',
'type' => 'serial',
'not null' => TRUE,
],
'order_id' => [
'description' => 'The commerce_order.order_id',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'gc_mandate_id' => [
'description' => 'The GoCardless mandate ID',
'type' => 'varchar',
'length' => '64',
'not null' => TRUE,
],
'gc_mandate_status' => [
'description' => 'The GoCardless mandate status',
'type' => 'varchar',
'length' => '32',
'not null' => FALSE,
],
'gc_mandate_scheme' => [
'description' => 'The GoCardless mandate scheme',
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
'created' => [
'description' => 'Unix timestamp when the order was created',
'type' => 'int',
'not null' => FALSE,
],
'data' => [
'description' => 'A serialised array of additional data',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
],
'sandbox' => [
'description' => 'Sandbox or live mode',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 1,
],
],
'primary key' => ['gcid'],
'indexes' => [
'gc_mandate_id' => ['gc_mandate_id'],
'order_id' => ['order_id'],
],
];
$schema['commerce_gc_client_item'] = [
'description' => 'Stores data on GoCardless line items',
'fields' => [
'gcid' => [
'description' => 'The commerce_gc_client.gcid',
'type' => 'int',
'not null' => TRUE,
],
'item_id' => [
'description' => 'The commerce_order_item.order_item_id',
'type' => 'int',
'not null' => TRUE,
],
'type' => [
'description' => 'Subscription or One-off payments',
'type' => 'varchar',
'length' => '1',
'not null' => TRUE,
],
'gc_subscription_id' => [
'description' => 'The GoCardless subscription ID',
'type' => 'varchar',
'length' => '32',
'not null' => FALSE,
],
'gc_subscription_status' => [
'description' => 'The GoCardless subscription status',
'type' => 'varchar',
'length' => '16',
'not null' => FALSE,
],
'next_payment' => [
'description' => 'Timestamp for the next payment',
'type' => 'int',
'not null' => FALSE,
],
],
'primary key' => ['item_id'],
'indexes' => [
'gcid' => ['gcid'],
],
];
$schema['commerce_gc_client_item_schedule'] = [
'description' => 'Schedules table for use with GoCardless scheduled...',
'fields' => [
'item_id' => [
'description' => 'The commerce_gc_client_item.item_id',
'type' => 'int',
'not null' => TRUE,
],
'sid' => [
'description' => 'Schedule ID',
'type' => 'serial',
'not null' => TRUE,
],
'type' => [
'description' => 'The type of scheduled event',
'type' => 'varchar',
'length' => '16',
'not null' => TRUE,
],
'timestamp' => [
'description' => 'The timestamp for the scheduled event',
'type' => 'int',
'not null' => TRUE,
],
'date' => [
'description' => 'The date of the scheduled event as a string',
'type' => 'varchar',
'length' => '32',
'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' => [
'gcid' => ['item_id'],
],
];
$schema['commerce_gc_client_variation'] = [
'description' => 'GoCardless data for Commerce product variations',
'fields' => [
'variation_id' => [
'description' => 'The commerce_product_variation.variation_id',
'type' => 'int',
'not null' => TRUE,
],
'gc_use' => [
'description' => 'If product variation uses GoCardless settings',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
],
'data' => [
'description' => 'Additional data for GoCardless recurrence rules',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
],
],
'primary key' => ['variation_id'],
];
return $schema;
}
/**
* Add currency definition for United States.
*/
function commerce_gc_client_update_8101() {
$settings = \Drupal::configFactory()->getEditable('commerce_gc_client.settings');
$currencies = $settings->get('currency_schemes');
$currencies['USD'] = [
'scheme' => 'ach',
'countries' => [
'US' => [],
],
];
$settings->set('currency_schemes', $currencies)->save();
}
/**
* Register new service and controller classes.
*/
function commerce_gc_client_update_8102() {
return t('Caches have been cleared to register the new service and controller classes provided by the module.');
}
/**
* Provide essential updated site details to Seamless-CMS.
*/
function commerce_gc_client_update_8201() {
$base_url = \Drupal::request()->getSchemeAndHttpHost();
$partner = \Drupal::service('commerce_gc_client.gocardless_partner');
$response = false;
foreach(['sandbox', 'live'] as $mode) {
$result = $partner->api([
'mode' => $mode,
'endpoint' => 'client',
'action' => 'update',
'client_domain' => parse_url($base_url, PHP_URL_HOST),
'client_domain_scheme' => parse_url($base_url, PHP_URL_SCHEME),
'module' => 'Commerce 2.x',
'site_email' => \Drupal::config('system.site')->get('mail'),
]);
if (isset($result->response)) {
$response = t($result->response);
}
}
if ($response) {
return $response;
}
}
/**
* Add 'countries' configuration to GoCardless payment gateway(s).
*/
function commerce_gc_client_update_8202() {
$gateways = \Drupal::entityTypeManager()
->getStorage('commerce_payment_gateway')->loadByProperties([
'plugin' => 'gocardless_client',
]);
foreach($gateways as $gateway) {
$configuration = $gateway->getPluginConfiguration();
$configuration['countries'] = FALSE;
$gateway->setPluginConfiguration($configuration)->save();
}
}
/**
* Clear caches to register new functionality.
*/
function commerce_gc_client_update_8203() {
return t('Caches have been cleared to register new functionality for the module.');
}
/**
* Clear the org_id_* items from the module's settings configuration.
*/
function commerce_gc_client_update_9001() {
$settings = \Drupal::configFactory()->getEditable('commerce_gc_client.settings');
$settings->clear('org_id_sandbox');
$settings->clear('org_id_live');
$settings->save();
}
/**
* Update Seamless-CMS with module version number and other essential data.
*/
function commerce_gc_client_update_9101() {
// First remove the "cookie_created" item from the session to force
// re-authenticatication with the partner site.
$session = \Drupal::request()->getSession();
$session->remove('commerce_gc_client_cookie_created');
$scheme = \Drupal::request()->getScheme();
$http_host = \Drupal::request()->getHttpHost();
$partner = \Drupal::service('commerce_gc_client.gocardless_partner');
$settings = \Drupal::configFactory()
->get('commerce_gc_client.settings')->get();
$gateways = \Drupal::entityTypeManager()
->getStorage('commerce_payment_gateway')
->loadByProperties(['plugin' => 'gocardless_client']);
foreach ($gateways as $gateway) {
$partner->setGateway($gateway->id());
$response = false;
foreach(['sandbox', 'live'] as $mode) {
$result = $partner->api([
'endpoint' => 'client',
'action' => 'update',
'user_name' => $settings['partner_user_' . $mode],
'client_domain' => $http_host,
'client_domain_scheme' => $scheme,
'module' => 'Commerce 2.1.x',
'site_email' => \Drupal::config('system.site')->get('mail'),
]);
if (isset($result->response)) {
$response = t($result->response);
}
}
}
if ($response) {
return $response;
}
}
