-
Notifications
You must be signed in to change notification settings - Fork 2
/
SnipWireConfig.php
1032 lines (881 loc) · 43.1 KB
/
SnipWireConfig.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace ProcessWire;
/**
* SnipWireConfig - Config file for SnipWire.
* (This file is part of the SnipWire package)
*
* Licensed under MPL 2.0 (see LICENSE file provided with this package)
* Copyright 2023 by Martin Gartner
*
* ProcessWire 3.x, Copyright Ryan Cramer
* https://processwire.com
*/
wire('classLoader')->addNamespace('SnipWire\Helpers', __DIR__ . '/helpers');
use SnipWire\Helpers\CurrencyFormat;
use SnipWire\Helpers\Taxes;
class SnipWireConfig extends ModuleConfig
{
/** @var array $availableCreditCards Available credit-card types */
protected $availableCreditCards = [
'visa',
'mastercard',
'maestro',
'amex',
'dinersclub',
'discover',
'jcb',
'cardbleue',
'dankort',
'cartasi',
'postepay',
];
/** @var string $snipWireRootUrl The root URL to ProcessSnipWire page */
protected $snipWireRootUrl = '';
/**
* Construct/initialize
*
* @throws WireException
*/
public function __construct()
{
parent::__construct();
$this->snipWireRootUrl = rtrim($this->wire('pages')->get('template=admin, name=snipwire, status<' . Page::statusTrash)->url, '/') . '/';
}
/**
* Returns an array of credit card labels, indexed by card name
*
* @return array
*/
public static function getCreditCardLabels()
{
return [
'visa' => __('Visa'),
'mastercard' => __('Mastercard'),
'maestro' => __('Maestro'),
'amex' => __('American Express'),
'dinersclub' => __('Diners Club'),
'discover' => __('Discover'),
'jcb' => __('JCB'),
'cardbleue' => __('Carte Bleue'),
'dankort' => __('Dankort'),
'cartasi' => __('CartaSi'),
'postepay' => __('Postepay'),
];
}
/**
* Returns an array of taxes provider labels, indexed by provider
*
* @return array
*
*/
public static function getTaxesProviderLabels()
{
return [
'snipcart' => __('Snipcart'),
'integrated' => __('Integrated (SnipWire)'),
];
}
/**
* Default config
* (overriding the method from parent class)
*
* @return array of 'fieldName' => 'default value'
*/
public function getDefaults()
{
return [
'api_key' => '',
'api_key_test' => '',
'api_key_secret' => '',
'api_key_secret_test' => '',
'snipcart_environment' => 0,
'single_page_shop' => 0,
'single_page_shop_page' => 1,
'credit_cards' => ['visa', 'mastercard', 'maestro'],
'currencies' => ['eur'],
'show_cart_automatically' => 1,
'shipping_same_as_billing' => 1,
'show_continue_shopping' => 1,
'split_firstname_and_lastname' => 1,
'cart_custom_fields_enabled' => 1,
'snipcart_debug' => 1,
'taxes_provider' => 'integrated',
'taxes' => Taxes::getDefaultTaxesConfig(true), // JSON
'taxes_included' => 1,
'shipping_taxes_type' => Taxes::shippingTaxesHighestRate,
'include_snipcart_css' => 1,
'snipcart_css_path' => 'https://cdn.snipcart.com/themes/2.0/base/snipcart.min.css',
'snipcart_css_integrity' => '',
'snipcart_js_path' => 'https://cdn.snipcart.com/scripts/2.0/snipcart.js',
'snipcart_js_integrity' => '',
'include_jquery' => 1,
'jquery_js_path' => 'https://code.jquery.com/jquery-3.3.1.min.js',
'jquery_js_integrity' => 'sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=',
'excluded_templates' => [],
'cart_image_width' => 65,
'cart_image_height' => 65,
'cart_image_cropping' => 1,
'cart_image_quality' => 70,
'cart_image_hidpi' => 1,
'cart_image_hidpiQuality' => 50,
'webhooks_endpoint' => '/webhooks/snipcart',
'product_templates' => [],
'data_item_name_field' => 'title',
'data_item_categories_field' => '',
'currency_param' => 'currency',
'snipwire_debug' => false,
];
}
/**
* Return an InputfieldWrapper of Inputfields necessary to configure this module
*
* Values will be populated to the Inputfields automatically. However, you may also retrieve
* any of the values from $this->[property]; as needed.
*
* @return InputfieldWrapper
* @throws WireException
*/
public function getInputfields()
{
$modules = $this->wire('modules');
$config = $this->wire('config');
$snipwire = $this->wire('snipwire');
$sniprest = $this->wire('sniprest');
$defaults = $this->getDefaults();
$inputfields = parent::getInputfields();
// If something went wrong during installation process (e.g. required modules are missing) return early!
if (!$snipwire || !$sniprest) return $inputfields;
$modules->get('JqueryUI')->use('vex');
$this->_includeAssets();
//
// ---- Additional steps ----
//
$redirectUrl = urlencode($_SERVER['REQUEST_URI']);
$steps = [];
$steps[] = [
'type' => 'link',
'name' => 'snipcart_account',
'url' => 'https://app.snipcart.com',
'target' => '_blank',
'prompt' => $this->_('Create a Snipcart account'),
'description' => $this->_('Create or login to a Snipcart account.'),
];
$steps[] = [
'type' => 'link',
'name' => 'snipcart_api_keys',
'url' => 'https://app.snipcart.com/dashboard/account/credentials',
'target' => '_blank',
'prompt' => $this->_('Get your Snipcart API keys'),
'description' => $this->_('To get your public JavaScript - and secret REST API keys, head to the Account > API Keys section. There you’ll find your public API keys and also need to create your secret API keys for live and test environment.'),
];
$steps[] = [
'type' => 'check',
'name' => 'product_package',
'url' => $this->snipWireRootUrl . 'install-product-package/?ret=' . $redirectUrl,
'prompt' => $this->_('Install Snipcart products package'),
'description' => $this->_('Contains product templates, files, fields and some demo pages required to build a Snipcart product catalogue. This additional step is needed to prevent unintended deletion of your Snipcart products catalogue when main module is uninstalled. These resources need to be removed manually!'),
/*
'followup' => [
'url' => $this->snipWireRootUrl . 'uninstall-product-package/?ret=' . $redirectUrl,
'prompt' => $this->_('Uninstall package'),
'icon' => 'times-circle',
],
*/
];
$steps[] = [
'type' => 'link',
'name' => 'snipcart_domains',
'url' => 'https://app.snipcart.com/dashboard/account/domains',
'target' => '_blank',
'prompt' => $this->_('Snipcart domains setup'),
'description' => $this->_('Tell Snipcart where it can crawl your products. Go to Store Configuration > Domains & URLs and set your default domain name as well as additional allowed domains and sub-domains.'),
];
$stepsCounter = 0;
$doneCounter = 0;
foreach ($steps as $step) {
if ($step['type'] == 'check') $stepsCounter++;
}
if ($stepsCounter) {
// Check which steps are already done and add flag
$snipwireConfig = $modules->getConfig('SnipWire');
for ($i = 0; $i < count($steps); $i++) {
$steps[$i]['done'] = (isset($snipwireConfig[$steps[$i]['name']]) && $snipwireConfig[$steps[$i]['name']]) ? true : false;
if ($steps[$i]['done']) $doneCounter++;
}
/** @var InputfieldMarkup $f */
$f = $modules->get('InputfieldMarkup');
$f->attr('name', '_next_steps');
$f->icon = 'cog';
$f->label = $this->_('Additional steps');
$f->value = '<ul class="uk-list uk-list-divider">';
foreach ($steps as $step) {
$f->value .= $this->renderStep($step);
}
$f->value .= '</ul>';
$f->collapsed = $stepsCounter == $doneCounter ? Inputfield::collapsedYes : Inputfield::collapsedNo;
$inputfields->add($f);
}
//
// ---- Snipcart API configuration ----
//
/** @var InputfieldFieldset $fsAPI */
$fsAPI = $modules->get('InputfieldFieldset');
$fsAPI->icon = 'plug';
$fsAPI->label = $this->_('Snipcart API Configuration');
$fsAPI->set('themeOffset', true);
/** @var InputfieldRadios $f */
$f = $modules->get('InputfieldRadios');
$f->attr('name', 'snipcart_environment');
$f->label = $this->_('Snipcart Environment');
$f->description = $this->_('Snipcart offers two separate isolated environments to allow a secure staging without affecting the live environment.');
$f->notes = $this->_('Changes the environment API key when including the Snipcart JS file in templates.');
$f->optionColumns = 1;
$f->addOption(0, 'TEST mode');
$f->addOption(1, 'LIVE mode');
$f->columnWidth = 50;
$fsAPI->add($f);
/** @var InputfieldButton $btn */
$btn = $modules->get('InputfieldButton');
$btn->attr('id', 'rest_test');
$btn->attr('href', $this->snipWireRootUrl . 'test-snipcart-rest-connection/?ret=' . $redirectUrl);
$btn->text = $this->_('Connection Test');
$btn->icon = 'plug';
$btn->setSecondary(true);
$btn->set('small', true);
$connectionTestMarkup = $btn->render();
/** @var InputfieldMarkup $f */
$f = $modules->get('InputfieldMarkup');
$f->label = $this->_('Snipcart REST API Connection Test');
$f->description = $this->_('SnipWire will send a test request to the Snipcart REST API. You can check if your secret API key for the selected environment is correct.');
$f->notes = $this->_('You first need to enter valid Secret API keys in the corresponding fields.');
$f->value = $connectionTestMarkup;
$f->columnWidth = 50;
$fsAPI->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'api_key');
$f->label = $this->_('Snipcart Public API Key');
$f->description = $this->_('The public API key is used to access the public Snipcart `JavaScript API`.');
$f->notes = $this->_('This key can be shared without security issues.');
$f->required = true;
$f->columnWidth = 50;
$f->requiredIf = 'snipcart_environment=1';
$f->showIf = 'snipcart_environment=1';
$fsAPI->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'api_key_secret');
$f->label = $this->_('Snipcart Secret API Key');
$f->description = $this->_('The secret API key is used to access your Snipcart account via `REST API`.');
$f->notes = $this->_('This key should never be visible to anyone!');
$f->required = true;
$f->columnWidth = 50;
$f->requiredIf = 'snipcart_environment=1';
$f->showIf = 'snipcart_environment=1';
$fsAPI->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'api_key_test');
$f->label = $this->_('Snipcart Public API Key (Test)');
$f->description = $this->_('The public API key is used to access the public Snipcart `JavaScript API`.');
$f->notes = $this->_('This key can be shared without security issues.');
$f->required = true;
$f->columnWidth = 50;
$f->requiredIf = 'snipcart_environment=0';
$f->showIf = 'snipcart_environment=0';
$fsAPI->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'api_key_secret_test');
$f->label = $this->_('Snipcart Secret API Key (Test)');
$f->description = $this->_('The secret API key is used to access your Snipcart account via `REST API`.');
$f->notes = $this->_('This key should never be visible to anyone!');
$f->required = true;
$f->columnWidth = 50;
$f->requiredIf = 'snipcart_environment=0';
$f->showIf = 'snipcart_environment=0';
$fsAPI->add($f);
/** @var InputfieldAsmSelect $f */
$f = $modules->get('InputfieldAsmSelect');
$f->attr('name', 'credit_cards');
$f->label = 'Accepted Credit Cards';
$f->description = $this->_('Specify which credit cards you want to accept during checkout.');
$creditcardLabels = self::getCreditCardLabels();
foreach ($this->availableCreditCards as $card) {
$cardlabel = isset($creditcardLabels[$card]) ? $creditcardLabels[$card] : $card;
$f->addOption($card, $cardlabel);
}
$f->required = true;
$fsAPI->add($f);
/** @var InputfieldAsmSelect $f */
$f = $modules->get('InputfieldAsmSelect');
$f->attr('name', 'currencies');
$f->label = $this->_('Set Currencies');
$f->description = $this->_('Selected currency(s) will be used in your shop catalogue and in the Snipcart shopping-cart system during checkout.');
$f->description .= ' ' . $this->_('As SnipWire fetches the available currency-list directly from Snipcart Dashboard, you will need to first setup the desired currency format(s) in your [Snipcart Dashboard > Regional Settings](https://app.snipcart.com/dashboard/settings/regional).');
$f->description .= ' ' . $this->_('Selecting a currency will also create a corresponding currency specific price input field which needs to be added to your product templates manually.');
$f->notes = $this->_('Selecting more than one currency will enable Snipcart\'s multiple currencies payments feature.');
$f->notes .= ' ' . $this->_('The first currency in the list will be the default one used in your product catalogue, Snipcart shopping-cart and SnipWire dashboard.');
$supportedCurrencies = CurrencyFormat::getSupportedCurrencies();
$currencies = [];
if (!$currencies = $sniprest->getSettings('currencies', WireCache::expireNever, true)) {
$currencies[] = CurrencyFormat::getDefaultCurrencyDefinition();
}
foreach ($currencies as $currency) {
$currencyName = $currency['currency'];
$currencyLabel = isset($supportedCurrencies[$currency['currency']])
? strtoupper($currency['currency']) . ' : ' . $supportedCurrencies[$currency['currency']]
: strtoupper($currency['currency']);
$f->addOption($currencyName, $currencyLabel);
}
$f->required = true;
$fsAPI->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'show_cart_automatically');
$f->label = $this->_('Show Shopping Cart Automatically');
$f->label2 = $this->_('Show cart automatically');
$f->description = $this->_('If you want to prevent the cart from showing up everytime a product is added, you can disable it.');
$f->columnWidth = 50;
$fsAPI->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'shipping_same_as_billing');
$f->label = $this->_('Use Billing Address for Shipping');
$f->label2 = $this->_('Use billing address for shipping preselected');
$f->description = $this->_('Whether the `Use this address for shipping` option on the billing address tab is pre-selected or not.');
$f->columnWidth = 50;
$fsAPI->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'show_continue_shopping');
$f->label = $this->_('Continue shopping Button');
$f->label2 = $this->_('Show the `Continue shopping` button');
$f->description = $this->_('Use this setting if you want to show the `Continue shopping` button.');
$f->description .= ' ' . $this->_('This button will appear just beside the `Close cart` button.');
$f->columnWidth = 50;
$fsAPI->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'split_firstname_and_lastname');
$f->label = $this->_('Split First Name and Last Name');
$f->label2 = $this->_('Split the First name and Last name');
$f->description = $this->_('Use this setting to split the First name and Last name in billing address and shipping address forms.');
$f->columnWidth = 50;
$fsAPI->add($f);
$customCartFieldsPage = $this->wire('pages')->findOne('name=custom-cart-fields, template=snipcart-cart, include=hidden');
if ($customCartFieldsPage->editable()) {
$customCartFieldsPageEditUrl = $customCartFieldsPage->editUrl;
}
if ($customCartFieldsPageEditUrl) {
/** @var InputfieldButton $btn */
$btn = $modules->get('InputfieldButton');
$btn->attr('href', $customCartFieldsPageEditUrl);
$btn->addClass('pw-modal');
$btn->text = $this->_('Custom Cart Fields');
$btn->icon = 'cog';
$btn->setSecondary(true);
$btn->set('small', true);
/** @var InputfieldMarkup $f */
$f = $modules->get('InputfieldMarkup');
$f->label = $this->_('Custom Cart Fields Configuration');
$f->description = $this->_('Those fields will be automatically added to your checkout process as new tab/step called `Order infos`.');
$f->value = $btn->render();
$f->columnWidth = 50;
$fsAPI->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'cart_custom_fields_enabled');
$f->label = $this->_('Enable/Disable Custom Cart Fields');
$f->label2 = $this->_('Custom cart fields enabled');
$f->description = $this->_('Use this setting to select whether custom cart fields should be enabled for checkout process.');
$f->columnWidth = 50;
$fsAPI->add($f);
}
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'snipcart_debug');
$f->label = $this->_('Snipcart JavaScript Debug Mode');
$f->label2 = $this->_('Enable Snipcart JavaScript debug mode');
$f->description = $this->_('This will allow you to see JavaScript errors on your site, failing requests and logs from the services you use in your browsers developer console.');
$f->notes = $this->_('All logs from the Snipcart script will be prefixed with `Snipcart:`');
$f->columnWidth = 100;
$fsAPI->add($f);
$inputfields->add($fsAPI);
//
// ---- Taxes configuration ----
//
/** @var InputfieldFieldset $fsAPI */
$fsTaxes = $modules->get('InputfieldFieldset');
$fsTaxes->icon = 'percent';
$fsTaxes->label = $this->_('Taxes Configuration');
$fsTaxes->set('themeOffset', true);
/** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'taxes_provider');
$f->label = $this->_('Taxes Provider');
$f->description = $this->_('Select the taxes provider which should be used.');
$f->required = true;
$f->columnWidth = 50;
$f->addOptions(self::getTaxesProviderLabels());
$fsTaxes->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'taxes_included');
$f->label = $this->_('Taxes Included in Prices');
$f->label2 = $this->_('Taxes are included in product prices');
$f->description = $this->_('Use this setting if the taxes are included in your product prices.');
$f->columnWidth = 50;
$fsTaxes->add($f);
$languageStrings = [
'tax_name' => $this->_('Tax name'),
'number_for_invoice' => $this->_('Number for Invoice'),
'rate' => $this->_('Rate'),
'applies_on_shipping' => $this->_('Shipping'),
'tax_name_ph' => $this->_('e.g. 20% VAT'),
'number_for_invoice_ph' => $this->_('Opt. tax number'),
'rate_ph' => $this->_('e.g. 0.20'),
'sort_drag_drop' => $this->_('Sort by dragging and dropping'),
'remove_tax_setting' => $this->_('Remove tax setting'),
'add_tax_setting' => $this->_('Add tax setting'),
'js' => [
'confirm_delete' => $this->_('Are you sure you want to delete this element?'),
],
];
$config->js('languageStrings', $languageStrings['js']);
$taxesRepeaterMarkup =
'<table id="TaxesRepeater">' .
'<thead>' .
'<tr>' .
'<th></th>' .
'<th>' . $languageStrings['tax_name'] . '</th>' .
'<th>' . $languageStrings['number_for_invoice'] . '</th>' .
'<th>' . $languageStrings['rate'] . '</td>' .
'<th>' . $languageStrings['applies_on_shipping'] . '</td>' .
'<th></th>' .
'</tr>' .
'</thead>' .
'<tfoot>' .
'<tr>' .
'<td colspan="6">' .
'<a class="RepeaterAddItem" data-repeater-create>' . $languageStrings['add_tax_setting'] . '</a>' .
'</td>' .
'</tr>' .
'</tfoot>' .
'<tbody data-repeater-list="taxesgroup">' .
'<tr data-repeater-item>' .
'<td class="col-action">' . // @todo make drag&drop accessible!
'<span role="button" aria-label="' . $languageStrings['sort_drag_drop'] . '" class="RepeaterSortableIndicator">' . wireIconMarkup('arrows') . '</span>' .
'</td>' .
'<td class="col-data">' .
'<input type="text" aria-label="' . $languageStrings['tax_name'] . '" class="uk-input InputfieldMaxWidth" name="name" placeholder="' . $languageStrings['tax_name_ph'] . '">' .
'</td>' .
'<td class="col-data">' .
'<input type="text" aria-label="' . $languageStrings['number_for_invoice'] . '" class="uk-input InputfieldMaxWidth" name="numberForInvoice" placeholder="' . $languageStrings['number_for_invoice_ph'] . '">' .
'</td>' .
'<td class="col-data">' .
'<input type="text" aria-label="' . $languageStrings['rate'] . '" class="uk-input InputfieldMaxWidth" name="rate" pattern="[-+]?[0-9]*[.]?[0-9]+" placeholder="' . $languageStrings['rate_ph'] . '">' .
'</td>' .
'<td class="col-data">' .
'<label class="inline-checkbox">' .
'<input type="checkbox" aria-label="' . $languageStrings['applies_on_shipping'] . '" class="uk-checkbox" name="appliesOnShipping" value="1">' .
'</label>' .
'</td>' .
'<td class="col-action">' .
'<button type="button" class="RepeaterRemoveItem" title="' . $languageStrings['remove_tax_setting'] . '" data-repeater-delete>' . wireIconMarkup('trash-o') . '</button>' .
'</td>' .
'</tr>' .
'</tbody>' .
'</table>';
$notes =
'<p class="notes">' .
$this->_('The first (none shipping) set in the list will be used as default tax when creating new products.') .
' ' . $this->_('The first set in the list with a "Shipping" marker will be used as default shipping tax when "Shipping Taxes Handling" is set to "Apply a fixed tax rate".') .
'</p>';
// This text field (HTML input hidden by CSS) will be filled with jSON formatted taxes array provided by jquery.repeater.
// onLoad jquery.repeater will use the stored array to pre-build the repeater fields.
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('id+name', 'taxes');
$f->attr('class', 'hiddenTaxesInput');
$f->label = $this->_('Taxes Configuration');
$f->description = $this->_('If activated, SnipWire acts as a taxes provider for Snipcart.');
$f->description .= ' ' . $this->_('You first need to configure the taxes provider webhook in [Snipcart Dashboard > Taxes](https://app.snipcart.com/dashboard/taxes).');
$f->description .= ' ' . $this->_('After that, define the tax rates here to be used by the Snipcart shop-system.');
$f->maxlength = 1024 * 32;
$f->showIf = 'taxes_provider=integrated';
$f->requiredIf = 'taxes_provider=integrated';
$f->appendMarkup = $taxesRepeaterMarkup . $notes;
$fsTaxes->add($f);
/** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'shipping_taxes_type');
$f->addClass('InputfieldMaxWidth');
$f->label = $this->_('Shipping Taxes Handling');
$f->description = $this->_('Select how shipping taxes should be calculated and applied.');
$f->required = true;
$f->columnWidth = 100;
$f->showIf = 'taxes_provider=integrated';
$f->requiredIf = 'taxes_provider=integrated';
$f->addOptions([
Taxes::shippingTaxesNone => $this->_('No shipping taxes'),
Taxes::shippingTaxesFixedRate => $this->_('Apply a fixed tax rate'),
Taxes::shippingTaxesHighestRate => $this->_('Apply predominant tax rate'),
Taxes::shippingTaxesSplittedRate => $this->_('Proportionally split and apply tax rates'),
]);
$fsTaxes->add($f);
$inputfields->add($fsTaxes);
//
// ---- Markup configuration ----
//
/** @var InputfieldFieldset $fsMarkup */
$fsMarkup = $modules->get('InputfieldFieldset');
$fsMarkup->icon = 'html5';
$fsMarkup->label = $this->_('Markup Output Configuration');
$fsMarkup->set('themeOffset', true);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'include_snipcart_css');
$f->label = $this->_('Include Snipcart CSS');
$f->label2 = $this->_('Include Snipcart CSS');
$f->description = $this->_('Whether SnipWire should add the defined Snipcart stylesheet from the field below to your output or not.');
$f->description .= ' ' . $this->_('To change the Cart theme, you can also include your own stylesheet using one of your preferred methods.');
$f->description .= ' ' . $this->_('Check the [Snipcart Theme Repository](https://github.com/snipcart/snipcart-theme) on GitHub for more info.');
$fsMarkup->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'snipcart_css_path');
$f->label = $this->_('Path to Snipcart CSS File');
$f->required = true;
$f->columnWidth = 60;
$f->requiredIf = 'include_snipcart_css=1';
$f->showIf = 'include_snipcart_css=1';
$fsMarkup->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'snipcart_css_integrity');
$f->label = $this->_('Snipcart CSS File Integrity Hash');
$f->columnWidth = 40;
$f->showIf = 'include_snipcart_css=1';
$fsMarkup->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'snipcart_js_path');
$f->label = $this->_('Path to Snipcart JS File');
$f->required = true;
$f->columnWidth = 60;
$fsMarkup->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'snipcart_js_integrity');
$f->label = $this->_('Snipcart JS File Integrity Hash');
$f->columnWidth = 40;
$fsMarkup->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'include_jquery');
$f->label = $this->_('Include jQuery');
$f->label2 = $this->_('Include jQuery');
$f->description = $this->_('Whether SnipWire should add the jQuery library to your output or not.');
$f->description .= ' ' . $this->_('If jQuery is already included in your template, you should not include it twice, so you can uncheck this option.');
$f->notes = $this->_('Snipcart uses [jQuery](https://jquery.com/), so you need to make sure it is included in your output!');
$fsMarkup->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'jquery_js_path');
$f->label = $this->_('Path to jQuery JS File');
$f->required = true;
$f->columnWidth = 60;
$f->requiredIf = 'include_jquery=1';
$f->showIf = 'include_jquery=1';
$fsMarkup->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'jquery_js_integrity');
$f->label = $this->_('jQuery JS File Integrity Hash');
$f->columnWidth = 40;
$f->showIf = 'include_jquery=1';
$fsMarkup->add($f);
/** @var InputfieldAsmSelect $f */
$f = $modules->get('InputfieldAsmSelect');
$f->attr('name', 'excluded_templates');
$f->label = 'Exclude Templates from Snipcart Integration';
$f->description = $this->_('The selected templates will be excluded from Snipcart scripts (JS) and styles (CSS) integration.');
$f->notes = $this->_('Leave empty for no limitation. Please note: system templates (admin, user, language, ...) are always excluded!');
foreach ($this->_getTemplates() as $t) {
$name = $t->name;
$label = !empty($t->label) ? $t->label . ' [' . $name . ']' : $name;
$f->addOption($name, $label);
}
$fsMarkup->add($f);
$inputfields->add($fsMarkup);
//
// ---- Cart image configuration ----
//
/** @var InputfieldFieldset $fsCartImage */
$fsCartImage = $modules->get('InputfieldFieldset');
$fsCartImage->icon = 'picture-o';
$fsCartImage->label = $this->_('Cart Thumbnail Sizing');
$fsCartImage->description = $this->_('Snipcart uses the first image from preinstalled `snipcart_item_image` PageField as cart thumbnail.');
$fsCartImage->description .= ' ' . $this->_('The following settings will define how the cart thumbnail variant is sized/cropped to the specified dimensions.');
$fsCartImage->description .= ' ' . $this->_('Please refer to the [ProcessWire Docs](https://processwire.com/api/ref/pageimage/size/) how the size/crop parameters behave.');
$fsCartImage->set('themeOffset', true);
/** @var InputfieldInteger $f */
$f = $modules->get('InputfieldInteger');
$f->attr('name', 'cart_image_width');
$f->label = $this->_('Width in px');
$f->required = true;
$f->columnWidth = 33;
$fsCartImage->add($f);
/** @var InputfieldInteger $f */
$f = $modules->get('InputfieldInteger');
$f->attr('name', 'cart_image_height');
$f->label = $this->_('Height in px');
$f->required = true;
$f->columnWidth = 33;
$fsCartImage->add($f);
/** @var InputfieldInteger $f */
$f = $modules->get('InputfieldInteger');
$f->attr('name', 'cart_image_quality');
$f->label = $this->_('Quality in %');
$f->required = true;
$f->columnWidth = 34;
$fsCartImage->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'cart_image_hidpi');
$f->label = $this->_('Use HiDPI/retina pixel doubling?');
$f->label2 = $this->_('Use HiDPI');
$f->columnWidth = 33;
$fsCartImage->add($f);
/** @var InputfieldInteger $f */
$f = $modules->get('InputfieldInteger');
$f->attr('name', 'cart_image_hidpiQuality');
$f->label = $this->_('HiDPI Quality in %');
$f->required = true;
$f->columnWidth = 33;
$fsCartImage->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'cart_image_cropping');
$f->label = $this->_('Crop');
$f->label2 = $this->_('Crop thumbnail');
$f->columnWidth = 34;
$fsCartImage->add($f);
$inputfields->add($fsCartImage);
//
// ---- SnipWire API configuration ----
//
/** @var InputfieldFieldset $fsSnipWire */
$fsSnipWire = $modules->get('InputfieldFieldset');
$fsSnipWire->icon = 'plug';
$fsSnipWire->label = $this->_('SnipWire Configuration');
$fsSnipWire->set('themeOffset', true);
$httpRootUrl = rtrim($config->urls->httpRoot, '/');
$webhooksEndpoint = isset($snipwireConfig['webhooks_endpoint'])
? $snipwireConfig['webhooks_endpoint'] // from db
: $this->webhooks_endpoint; // from getDefaults
$webhooksEndpointFullUrl = $httpRootUrl . $webhooksEndpoint;
$webhooksEndpointUrlMarkup =
'<div class="input-group">' .
'<input type="text" class="uk-input" id="webhooks_endpoint_url" aria-label="' . $this->_('Absolute URL for SnipWire webhooks endpoint') . '" value="' . $webhooksEndpointFullUrl . '" disabled="disabled">' .
'<div class="input-group-append">' .
'<button id="webhooks_endpoint_url_copy" class="ui-button ui-widget ui-corner-all ui-state-default" type="button">' .
'<span class="ui-button-text" title="' . $this->_('Copy absolute URL to clipboard') . '">' . wireIconMarkup('copy') . '</span>' .
'</button>' .
'</div>' .
'</div>';
// Set httpRoot as JavaScript property
$config->js('SnipWire', [
'httpRoot' => $httpRootUrl,
]);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('id+name', 'webhooks_endpoint');
$f->label = $this->_('SnipWire Webhooks Endpoint');
$f->maxlength = 128;
$f->required = true;
$f->description = $this->_('To allow Snipcart to send webhooks POST requests to SnipWire, you must define the endpoint where your webhooks will be reachable.');
$f->description .= ' ' . $this->_('After that, enter the absolute URL from the second field below in your Snipcart Dashboard under [Account > Webhooks section](https://app.snipcart.com/dashboard/webhooks).');
$f->notes = $this->_('The endpoint you provide must be relative to your site root with leading slash, e.g. /webhooks/snipcart.');
$f->notes .= ' ' . $this->_('Please note that the webhooks path is only a virtual path and shouldn\'t point to an existing page!');
$f->appendMarkup = $webhooksEndpointUrlMarkup;
$f->pattern = '^\/(?!.*\/\/)([a-zA-Z-\/]+)$';
$fsSnipWire->add($f);
/** @var InputfieldAsmSelect $f */
$f = $modules->get('InputfieldAsmSelect');
$f->attr('name', 'product_templates');
$f->label = 'SnipWire Product Templates';
$f->description = $this->_('The selected templates will be enabled as SnipWire product templates. This means the required Snipcart scripts (JS) and styles (CSS) will be added automatically.');
foreach ($this->_getTemplates() as $t) {
$name = $t->name;
$label = !empty($t->label) ? $t->label . ' [' . $name . ']' : $name;
$f->addOption($name, $label);
}
$fsSnipWire->add($f);
/** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'data_item_name_field');
$f->label = $this->_('Select Field for Snipcart Product Name');
$f->notes = $this->_('Allowed field types: `FieldtypeText(Language)`, `FieldtypePageTitle(Language)`');
$f->required = true;
$f->columnWidth = 50;
$allowedFieldTypes = [
'FieldtypeText',
'FieldtypeTextLanguage',
'FieldtypePageTitle',
'FieldtypePageTitleLanguage',
];
$excludeFieldNames = [
'snipcart_item_id',
'snipcart_item_price_',
];
$productTemplateFields = $this->_getFields($allowedFieldTypes, $excludeFieldNames);
foreach ($productTemplateFields as $ptField) {
$f->addOption($ptField->name, $ptField->name . ' (' . $ptField->type . ')');
}
$fsSnipWire->add($f);
/** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'data_item_categories_field');
$f->label = $this->_('Select Field for Snipcart Categories');
$f->notes = $this->_('Allowed field types: `FieldtypePage`');
$f->columnWidth = 50;
$allowedFieldTypes = [
'FieldtypePage',
];
$productTemplateFields = $this->_getFields($allowedFieldTypes);
$f->addOption('', $this->_('-- Categories disabled --'));
foreach ($productTemplateFields as $ptField) {
$f->addOption($ptField->name, $ptField->name . ' (' . $ptField->type . ')');
}
$fsSnipWire->add($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'currency_param');
$f->label = $this->_('Currency Parameter Name');
$f->description = $this->_('Set the name of the GET, POST and SESSION parameter to be used for setting cart and catalogue currency.');
$f->notes = $this->_('Can be used to switch the currency (used in templates and markup output) via form submit or session variable.');
$fsSnipWire->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'single_page_shop');
$f->label = $this->_('Single-Page Shop');
$f->label2 = $this->_('This Snipcart shop runs on a single-page website');
$f->description = $this->_('For single-page shops, the `data-item-url` field of each product will be filled with the full URL to the selected page.');
$f->notes = $this->_('This tells the Snipcart crawler where to find your products to validate an order\'s integrity.');
$f->columnWidth = 100;
$fsSnipWire->add($f);
/** @var InputfieldPageListSelect $f */
$f = $modules->get('InputfieldPageListSelect');
$f->attr('name', 'single_page_shop_page');
$f->label = $this->_('Select Your Single-Page Shop Page');
$f->required = true; // needs to be set when using requiredIf
$f->requiredIf = 'single_page_shop=1';
$f->showIf = 'single_page_shop=1';
$fsSnipWire->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'snipwire_debug');
$f->label = $this->_('SnipWire Debug Mode');
$f->label2 = $this->_('Enable SnipWire debug mode');
$f->description = $this->_('This will enable SnipWires extended messages, warnings and errors logging into the ProcessWire log system.');
$f->columnWidth = 100;
$fsSnipWire->add($f);
$inputfields->add($fsSnipWire);
return $inputfields;
}
/**
* Render an additional setup step.
*
* @param array $step
* @return string
*/
protected function renderStep(array $step)
{
$out = '';
$target = isset($step['target']) ? ' target="' . $step['target'] . '"' : '';
$out .= '<li>';
// Render as checklist
if ($step['type'] == 'check') {
if ($step['done']) {
$out .= $step['prompt'] . ' ';
$out .= '<span style="color: green;">';
$out .= wireIconMarkup('check-circle') . ' ';
$out .= $this->_('Done');
$out .= '</span>';
// Is there a followup?
if (isset($step['followup'])) {
$fup = $step['followup'];
$out .= ' -- <a' . $target . ' href="' . $fup['url'] . '">';
if (isset($fup['icon'])) $out .= wireIconMarkup($fup['icon']) . ' ';
$out .= $fup['prompt'];
$out .= '</a>';
}
} else {
$out .= '<a' . $target . ' href="' . $step['url'] . '">' . $step['prompt'] . '</a>';
}
// Render as link
} else {
$out .= '<a' . $target . ' href="' . $step['url'] . '">' . $step['prompt'] . '</a>';
}
if (isset($step['description'])) $out .= '<br><span class="detail">' . $step['description'] . '</span>';
$out .= '</li>';
return $out;
}
/**
* Get all templates except system templates (name => label)
*
* @return WireArray $templates
* @throws WireException
*/
private function _getTemplates()
{
$templates = new WireArray();
foreach ($this->wire('templates') as $t) {
// System templates + cart template excluded
if (!($t->flags & Template::flagSystem) && $t->name != 'snipcart-cart') {
$templates->add($t);
}
}
return $templates;
}
/**
* Get a selection of fields suitable for SnipWire product templates.
*
* @param array $allowedFieldTypes An array of allowed field types to be returned [optional]
* @param array $excludeFieldNames An array of field names to be excluded from result [optional]
* @return WireArray $fields
* @throws WireException
*/
private function _getFields($allowedFieldTypes = [], $excludeFieldNames = [])
{