-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_currency_resolver.install
188 lines (159 loc) · 4.87 KB
/
commerce_currency_resolver.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @file
* Install file for commerce_currency_resolver.
*/
use CommerceGuys\Addressing\Country\CountryRepository;
use Drupal\commerce_exchanger\Entity\ExchangeRates;
/**
* Implements hook_install().
*/
function commerce_currency_resolver_install() {
// @see commerce_price_install().
if (!\Drupal::isConfigSyncing()) {
// Get default country and their currency.
$default_country = \Drupal::config('system.date')->get('country.default');
$default_country = $default_country ?: 'US';
$country_repository = new CountryRepository();
$country = $country_repository->get($default_country);
$currency_code = $country->getCurrencyCode();
// Set initial default currency by default country.
// User can change it later.
\Drupal::service('config.factory')->getEditable('commerce_currency_resolver.settings')->set('currency_default', $currency_code)->save();
}
}
/**
* Implements hook_uninstall().
*/
function commerce_currency_resolver_uninstall() {
// Remove all stored states.
\Drupal::state()->deleteMultiple([
'commerce_currency_resolver.last_update_time',
]);
}
/**
* Fix issues with plugin discovery and using same plugin ID.
*/
function commerce_currency_resolver_update_8001() {
// Set module weight to 10. We are extending some commerce stuff,
// where this module should be after commerce_promotion.
module_set_weight('commerce_currency_resolver', 10);
}
/**
* Set back weight of the module to 0.
*
* @see https://www.drupal.org/project/commerce_currency_resolver/issues/3082160
*/
function commerce_currency_resolver_update_8002() {
module_set_weight('commerce_currency_resolver', 0);
}
/**
* Enable commerce_exchanger.
*
* @see https://www.drupal.org/project/commerce_currency_resolver/issues/3082160
*/
function commerce_currency_resolver_update_8003() {
\Drupal::service('module_installer')->install([
'commerce_exchanger',
]);
}
/**
* Upgrade path for commerce_exchanger.
*
* @see https://www.drupal.org/project/commerce_currency_resolver/issues/3082267
*/
function commerce_currency_resolver_update_8004() {
// Default currency.
$default_currency = \Drupal::configFactory()
->get('commerce_currency_resolver.settings')
->get('currency_default');
// Get existing settings for exchange rates.
$config = \Drupal::configFactory()
->get('commerce_currency_resolver.currency_conversion');
// Provide some defaults.
$plugin_id = 'manual';
$label = 'Manual';
$cron = 1;
$cross_sync = 1;
$api_key = '';
$auth = '';
$mode = 'live';
if ($config) {
$source = $config->get('source');
$cron = $config->get('cron');
$cross_sync = $config->get('use_cross_sync');
$api_key = $config->get('api_key');
$auth = $config->get('auth');
switch ($source) {
case 'exchange_rate_bluesnap':
$plugin_id = 'bluesnap';
$label = 'BlueSnap';
$default_currency = 'USD';
if (!empty($config->get('bluesnap'))) {
$mode = $config->get('bluesnap')['mode'] === 'production' ? 'live' : 'test';
$auth = [
'username' => $config->get('bluesnap')['username'],
'password' => $config->get('bluesnap')['password'],
];
}
break;
case 'exchange_rate_ecb':
$plugin_id = 'ecb';
$label = 'European Central Bank';
$default_currency = 'EUR';
break;
case 'exchange_rate_fixer_paid';
case 'exchange_rate_fixer';
$plugin_id = 'fixer';
$label = 'Fixer.io';
break;
default:
}
}
$values = [
'id' => $plugin_id,
'label' => $label,
'plugin' => $plugin_id,
];
$values['configuration'] = [
'cron' => $cron,
'use_cross_sync' => $cross_sync,
'demo_amount' => 100,
'base_currency' => $default_currency,
'mode' => $mode,
];
if ($api_key) {
$values['configuration']['api_key'] = $api_key;
}
if ($auth) {
$values['configuration']['auth'] = $auth;
}
$values['configuration']['manual'] = $plugin_id === 'manual';
// Create new exchange rates.
$entity = ExchangeRates::create(
$values
);
$entity->save();
// Set new value to currency resolver.
$settings = \Drupal::configFactory()
->getEditable('commerce_currency_resolver.settings');
$settings->set('currency_exchange_rates', $entity->id());
$settings->save();
// Delete now old configuration.
// commerce_currency_resolver.currency_conversion.
\Drupal::configFactory()
->getEditable('commerce_currency_resolver.currency_conversion')
->delete();
}
/**
* Enable shipping submodule if needed.
*
* @see https://www.drupal.org/project/commerce_currency_resolver/issues/3111998
*/
function commerce_currency_resolver_update_8006() {
if (\Drupal::service('module_handler')->moduleExists('commerce_shipping')) {
\Drupal::service('module_installer')->install([
'commerce_currency_resolver_shipping',
]);
}
}