-
Notifications
You must be signed in to change notification settings - Fork 14
/
class-wc-settings-page-komoju.php
406 lines (363 loc) · 14.2 KB
/
class-wc-settings-page-komoju.php
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
/**
*KOMOJU settings.
*
* @class WC_Settings_Page_Komoju
*
* @extends WC_Settings_Page
*
* @version 3.1.5
*
* @author Komoju
*/
require_once dirname(__FILE__) . '/komoju-php/komoju-php/lib/komoju.php';
class WC_Settings_Page_Komoju extends WC_Settings_Page
{
public function __construct()
{
$this->id = 'komoju_settings';
$this->label = __('Komoju', 'komoju-woocommerce');
add_action(
'woocommerce_admin_field_komoju_payment_types',
[$this, 'output_payment_methods']
);
add_action(
'woocommerce_admin_field_komoju_setup_button',
[$this, 'output_setup_button']
);
add_action(
'woocommerce_admin_field_komoju_endpoint',
[$this, 'output_endpoint_field']
);
parent::__construct();
}
// Override from WC_Settings_Page
public function get_sections()
{
$sections = [
'' => __('Payment methods', 'komoju-woocommerce'),
'api_settings' => __('API settings', 'komoju-woocommerce'),
];
return apply_filters('woocommerce_get_sections_' . $this->id, $sections);
}
// Override from WC_Settings_Page
public function get_settings()
{
global $current_section;
$settings = [];
if ('' === $current_section) {
$settings = apply_filters(
'woocommerce_komoju_settings',
include 'includes/account-settings-komoju.php'
);
} elseif ('api_settings' === $current_section) {
$settings = apply_filters(
'woocommerce_komoju_settings',
include 'includes/api-settings-komoju.php'
);
}
return apply_filters('woocommerce_get_settings_' . $this->id, $settings, $current_section);
}
// Override from WC_Settings_Page
public function save()
{
$old_payment_types = get_option('komoju_woocommerce_payment_types');
parent::save();
$new_payment_types = get_option('komoju_woocommerce_payment_types');
if ($old_payment_types != $new_payment_types) {
$this->cache_payment_methods_from_komoju($old_payment_types, $new_payment_types);
}
}
// Override from WC_Settings_Page
public function output()
{
$just_connected = get_option('komoju_woocommerce_just_connected_merchant_name');
if ($just_connected) {
delete_option('komoju_woocommerce_just_connected_merchant_name');
$this->output_connected_notice($just_connected);
}
parent::output();
}
// This shows a flash message (meant for the top of the KOMOJU settings page) for when
// the user returns from the quick setup feature.
public function output_connected_notice($merchant_name)
{
?>
<div id="message" class="updated inline">
<p><strong><?php echo sprintf(__('Successfully connected to KOMOJU account %s.'), $merchant_name); ?></strong></p>
</div>
<?php
}
// Action handler for rendering settings with type = 'komoju_endpoint'
public function output_endpoint_field($setting)
{
$value = isset($setting['value']) ? $setting['value'] : $setting['default'];
$untainted = $value === $setting['default']; ?>
<tr valign="top">
<th class="titledesc" scope="row">
<label for="<?php echo esc_attr($setting['id']); ?>"><?php echo $setting['title']; ?></label>
</th>
<td class="forminp forminp-text komoju-endpoint-field komoju-endpoint-<?php echo esc_attr($setting['id']); ?>">
<input id="<?php echo esc_attr($setting['id']); ?>"
name="<?php echo esc_attr($setting['id']); ?>"
value="<?php echo esc_attr($value); ?>"
data-default="<?php echo esc_attr($setting['default']); ?>"
type="text"
<?php if ($untainted) {
echo 'disabled';
} ?>>
<p class="description">
<?php echo __("Only modify this if you know what you're doing.", 'komoju-woocommerce'); ?>
</p>
<div>
<?php if ($untainted) { ?>
<button
type="button"
class="komoju-endpoint-edit"
data-target="<?php echo esc_attr($setting['id']); ?>"
onclick="komoju_woocommerce_enable_endpoint_field(event)">
<?php echo __('Edit', 'komoju-woocommerce'); ?>
</button>
<?php } ?>
<button
type="button"
class="komoju-endpoint-reset"
data-target="<?php echo esc_attr($setting['id']); ?>"
onclick="komoju_woocommerce_reset_endpoint_field(event)">
<?php echo __('Reset', 'komoju-woocommerce'); ?>
</button>
</div>
<script>
function komoju_woocommerce_enable_endpoint_field(event) {
const button = event.target;
const input = document.getElementById(button.dataset.target);
input.disabled = false;
event.target.remove();
}
function komoju_woocommerce_reset_endpoint_field(event) {
const button = event.target;
const input = document.getElementById(button.dataset.target);
input.value = input.dataset.default;
}
</script>
<style>
.komoju-endpoint-field {
display: flex;
flex-flow: column wrap;
align-items: flex-start;
gap: 4px;
}
.komoju-endpoint-field button {
min-width: 80px;
}
.komoju-endpoint-field p.description {
margin: 0;
}
</style>
</td>
</tr>
<?php
}
// Action handler for rendering settings with type = 'komoju_setup_button'
public function output_setup_button($setting)
{
$nonce = wp_generate_uuid4();
update_option('komoju_woocommerce_nonce', $nonce);
$already_connected = get_option('komoju_woocommerce_secret_key') ? true : false;
$setup_url = KomojuApi::endpoint() . '/plugin/auth?' .
'post_url=' . rawurlencode($this->contracted_url_for_webhooks()) . '&' .
'webhook_url=' . rawurlencode($this->contracted_url_for_webhooks()) . '&' .
'nonce=' . rawurlencode($nonce); ?>
<tr>
<th class="titledesc" scope="row">
<label><?php echo $setting['title']; ?></label>
</th>
<td class="forminp forminp-text komoju-setup-button" style="height: 60px">
<a href="<?php echo esc_attr($setup_url); ?>"
class='komoju-setup <?php echo $already_connected ? 'connected' : ''; ?>'>
<?php
if ($already_connected) {
echo __('Reconnect with KOMOJU', 'komoju-woocommerce');
} else {
echo __('Sign into KOMOJU', 'komoju-woocommerce');
} ?>
</a>
<style>
a.komoju-setup {
text-decoration: none;
background-color: #1880DE;
font-size: 18px;
color: white;
border: none;
border-radius: 8px;
padding: 26px;
margin-bottom: 12px;
}
a.komoju-setup:hover {
background-color: #3590E1;
}
a.komoju-setup.connected {
background-color: white;
color: #172E44;
border: 2px solid #C1CDD8;
}
a.komoju-setup.connected:hover {
background-color: #F0F8FF;
}
</style>
</td>
</tr>
<?php
}
// Action handler for rendering settings with type = 'komoju_payment_types'
public function output_payment_methods($setting)
{
$value = is_array($setting['value']) ? $setting['value'] : [];
$locale = WC_Gateway_Komoju::get_locale_or_fallback();
$all_payment_methods = $this->fetch_all_payment_methods();
if ($all_payment_methods === null) {
?>
<tr style="color: darkred"><td></td><td>
<?php
$secret_key = $this->secret_key();
if ($secret_key && $secret_key !== '') {
echo __('Unable to reach KOMOJU. Is your secret key correct?', 'komoju-woocommerce');
} else {
echo __('Once signed into KOMOJU, you can select payment methods to use as WooCommerce gateways.', 'komoju-woocommerce');
} ?>
</td></tr>
<?php
return;
}
// Show each payment method as a checkbox with an icon?>
<tr>
<th class="titledesc" scope="row">
<label><?php echo $setting['title']; ?></label>
</th>
<td class="forminp forminp-text komoju-payment-methods"
style="display: flex; flex-flow: row wrap; max-width: 800px; margin-bottom: 12px">
<?php
foreach ($all_payment_methods as $slug => $payment_method) {
?>
<label style="display: flex; align-items: center; gap: 5px; margin-bottom: 5px; width: 200px">
<input
type="checkbox"
name="<?php echo esc_attr($setting['id']); ?>[]"
value="<?php echo esc_attr($slug); ?>"
<?php if (in_array($slug, $value)) {
echo 'checked';
} ?>
>
<img
width="38"
height="24"
src="https://komoju.com/payment_methods/<?php echo esc_attr($slug); ?>.svg">
<?php echo $payment_method['name_' . $locale]; ?>
</label>
<?php
} ?>
</td>
</tr>
<?php
}
// Basically, the 'komoju_woocommerce_payment_types' option is just an array of slugs,
// and 'komoju_woocommerce_payment_methods' holds the actual payment method objects
// we get from the KOMOJU API.
//
// This action handler updates the 'komoju_woocommerce_payment_types' option
// to match the 'komoju_woocommerce_payment_types' option.
public function cache_payment_methods_from_komoju($old_payment_types, $payment_types)
{
$all_payment_methods = $this->fetch_all_payment_methods();
if ($all_payment_methods === null) {
return;
}
// Clear gateway settings from removed entries
if ($old_payment_types) {
$to_remove = array_diff($old_payment_types, $payment_types);
foreach ($to_remove as $slug) {
delete_option('woocommerce_komoju_' . $slug . '_settings');
}
}
// Populate komoju_woocommerce_payment_methods option with fresh values from KOMOJU
$payment_methods = [];
foreach ($payment_types as $slug) {
$payment_methods[$slug] = $all_payment_methods[$slug];
}
update_option('komoju_woocommerce_payment_methods', $payment_methods, true);
}
private function url_for_webhooks()
{
// In dev the relative plugin URL will remove the host name, but it
// will appear in production instances
return WC()->api_request_url('WC_Gateway_Komoju');
}
// There is a quirk in our production setup where all HTTP requests containing the
// word "localhost" are rejected. We still want local setup to work, so we use
// "local" as an alias for localhost.
private function contracted_url_for_webhooks()
{
$url = $this->url_for_webhooks();
$url = str_ireplace('http://localhost:', 'http://local:', $url);
$url = str_ireplace('http://127.0.0.1:', 'http://local:', $url);
return $url;
}
// Returns a list of payment methods from KOMOJU.
//
// @return array that looks something like this:
// [
// "konbini" => [
// "currency" => "JPY",
// "name_en" => "Konbini",
// "name_ja" => "コンビニ",
// "name_ko" => "편의점",
// "payment_method_fee" => 190,
// "type_slug" => "konbini"
// ],
// ...
// ]
private function fetch_all_payment_methods()
{
$api = new KomojuApi($this->secret_key());
if (!$api->secretKey || strlen($api->secretKey) === 0) {
return null;
}
try {
$all_payment_methods = $api->paymentMethods();
$methods_by_slug = [];
$wc_currency = get_woocommerce_currency();
foreach ($all_payment_methods as $payment_method) {
$slug = $payment_method['type_slug'];
$pm_currency = $payment_method['currency'];
// If $slug is not set, register
if (!isset($methods_by_slug[$slug])) {
$methods_by_slug[$slug] = $payment_method;
} else {
// If $slug is already registered and
// the payment currency matches the WooCommerce currency then override it
if ($pm_currency === $wc_currency) {
$methods_by_slug[$slug] = $payment_method;
}
}
$methods_by_slug[$slug] = $payment_method;
}
return $methods_by_slug;
} catch (KomojuExceptionBadServer $ex) {
return null;
}
}
private function secret_key()
{
$global_option = get_option('komoju_woocommerce_secret_key');
if (!$global_option) {
// This is for backwards compatibility. We used to have all settings saved under
// a single payment gateway called "Komoju". We've sinced moved to having this
// global settings page, but want to continue supporting old setups.
return WC_Gateway_Komoju::get_legacy_setting('secretKey');
}
return $global_option;
}
}