-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_scripts.js
1102 lines (1005 loc) · 44.1 KB
/
_scripts.js
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
var ReCharge = {};
ReCharge.Novum = {};
{% include '_components.js' %}
ReCharge.Utils = {
getParamValue: function(param) {
var value = new RegExp("[?&]" + param + "=([^&#]*)", "i").exec(window.location.href);
return value ? value[1] : null;
},
hasCaliforniaProvince: function(charge, address) {
const shipping =
charge?.shipping_address?.province === 'California' && charge.shipping_address?.country === 'United States' ||
address.province === 'California' && address.country === 'United States';
const billing = charge?.billing_address?.province === 'California' && charge.billing_address?.country === 'United States';
return shipping || billing;
},
hasMinimumOrderAmount: function(orders) {
const minimumOrderCount = {{ settings.customer_portal.subscription.cancellation_minimum_order_count }};
return minimumOrderCount === 0 || orders.length >= minimumOrderCount;
},
checkIfStoreAllowsCancellation: async function() {
const cancelBtn = document.querySelector(".js-cancel-sub-btn") || null;
if (!cancelBtn) return;
const { cancel_subscription, cancellation_minimum_order_count } = {{ settings | json }}.customer_portal.subscription;
const subscription = ReCharge.Novum.subscription;
const { orders, charges, retention_strategies } = await ReCharge.Novum.Helpers.fetchCharges();
sessionStorage.setItem('rc_retention_strategies', JSON.stringify(retention_strategies));
if (
(cancel_subscription && this.hasMinimumOrderAmount(orders)) ||
this.hasCaliforniaProvince(charges[0], subscription.address)
) {
cancelBtn.addEventListener("click", cancelSubscriptionFlow);
return;
} else {
ReCharge.Utils.contactStoreWording(
cancelBtn,
ReCharge.Utils.renderCancelSubscriptionLayout(),
`{{ 'cp_cancel_cancel_title' | t }}`
);
}
},
contactStoreWording: function(element, message, title) {
element.addEventListener('click', () => {
ReCharge.Novum.sidebarHeading.innerHTML = title;
ReCharge.Novum.sidebarContent.innerHTML = `<div class="text-center"><br>${message}</div>`;
ReCharge.Novum.toggleSidebar();
});
},
renderContactStoreLayout: function(message) {
return `
<p class="text-center">
{{ 'cp_please_contact_us_at' | t }}
<span class="address-info-msg title-bold">
{{ settings.customer_portal.subscription.cancellation_email_contact }}
</span> {{ 'cp_to_update_prefix' | t }} ${message.toLowerCase()}.
</p>
`;
},
renderCancelSubscriptionLayout: function() {
return `
<span class="title-bold">
{{ 'cp_cancel_message_to_user' | t }}
</span>
{{ 'cp_to_cancel_your_subscription_please_email_us_at' | t }}
<span class="address-info-msg title-bold">
{{ settings.customer_portal.subscription.cancellation_email_contact }}
</span>
{{ 'cp_process_your_cancellation_no_charged_for_this_subscription' | t }}
`;
},
renderNoProductsLayout: function() {
return `
<h2> {{ 'cp_toss_these_in' | t }} </h2>
<div> {{ 'cp_add_product_to_next_package' | t }} </div>
<div class="text-center margin-top-10" id="upsells--loader"> {{ 'cp_no_products_to_show' | t }}</div>
`;
},
renderErrorFetchingProductsLayout: function() {
return `
<h2>{{ 'cp_toss_these_in' | t }}</h2>
<div class="margin-bottom-10">{{ 'cp_add_product_to_next_package' | t }}</div>
<div class="rc-card mb-5 cursor-pointer">
<p class="text-font-14 margin-bottom-20 error-message">{{ 'cp_error_fetching_products' | t }}</p>
<button
class="rc_btn--secondary rc_btn--secondary--color text-uppercase title-bold text-center"
onclick="window.location.reload()">{{ 'cp_try_again' | t }}
</button>
</div>
`;
},
renderChargeProcessSuccessLayout(type, data = {}) {
if (type === 'one') {
ReCharge.Novum.sidebarContent.innerHTML = `
<div>
{{ 'cp_your_order_of' | t }}
<strong class="title-bold"> ${ReCharge.Novum.subscription.product_title.replace('Auto renew', '')} </strong>
${
ReCharge.Novum.subscription.variant_title &&
ReCharge.Novum.subscription.variant_title !== `Default title`
? `<strong class="title-bold"> ${ReCharge.Novum.subscription.variant_title} </strong>`
: ``
}
{{ 'cp_will_be_processed_today' | t }}
</div>
<br>
<button
class="rc_btn text-uppercase title-bold margin-top-10"
onclick="window.location.reload()"
>
{{ 'cp_i_understand_button' | t }}
</button>
`;
} else {
const dateData = ReCharge.Utils.calculateNextChargeDate(
new Date(),
ReCharge.Novum.subscription.order_interval_unit,
Number(ReCharge.Novum.subscription.order_interval_frequency)
);
ReCharge.Novum.sidebarContent.innerHTML = `
<div>
{{ 'cp_your_order_of' | t }}
<strong class="title-bold">${ReCharge.Novum.subscription.product_title.replace('Auto renew', '')}</strong>
${
ReCharge.Novum.subscription.variant_title &&
ReCharge.Novum.subscription.variant_title !== `Default title`
? `<strong class="title-bold"> ${ReCharge.Novum.subscription.variant_title} </strong>`
: ``
}
{{ 'cp_has_been_successfully_processed_to_see_info_browse_to' | t }}
<a href="{{ order_list_url }}" class="title-bold">
{{ 'Purchase_History_Tab' | t }}
</a>.
{{ 'cp_next_order_processed_on' | t }}
</div>
<br>
<h3 class="color-light-green text-center">
${dateData.month} ${dateData.futureDate.getDate()}${dateData.futureDateSuffix}
</h3>
<button
class="rc_btn text-uppercase title-bold margin-top-10"
onclick="ReCharge.Utils.redirectToPurchaseHistoryPage(event)"
>
{{ 'cp_i_understand_button' | t }}
</button>
`;
}
},
redirectToPurchaseHistoryPage(ev) {
ev.preventDefault();
ReCharge.Forms.toggleSubmitButton(ev.target);
window.location.href = "{{ order_list_url }}";
},
calculateNextChargeDate(dateValue, unit, frequency) {
let shipmentDate = new Date(dateValue);
let futureDate = new Date(dateValue);
if (unit === 'day') {
futureDate.setDate(futureDate.getDate() + frequency);
} else if (unit === 'week') {
const newUnit = frequency * 7
futureDate.setDate(futureDate.getDate() + newUnit);
} else {
futureDate.setMonth(futureDate.getMonth() + frequency);
}
const month = futureDate.toLocaleString('default', { month: 'long' });
const shipmentDateSuffix = ReCharge.Novum.Utils.getNumberSuffix(shipmentDate.getDate());
const futureDateSuffix = ReCharge.Novum.Utils.getNumberSuffix(futureDate.getDate());
const formattedDate = ReCharge.Novum.Utils.formatDate(shipmentDate);
return {
formattedDate,
month,
futureDate,
shipmentDate,
shipmentDateSuffix,
futureDateSuffix
}
}
};
ReCharge.Toast = {
addToastListener: function(toaster) {
toaster.addEventListener('animationend', function(e) {
if (e.animationName === 'hide') {
return toaster.removeChild(e.target);
}
});
},
buildToaster: function() {
var toaster = document.createElement('ul');
toaster.className = 'rc_toaster';
ReCharge.Toast.addToastListener(toaster);
return toaster;
},
buildToast: function(type, message) {
type = typeof(type) === 'undefined'
? `{{ 'cp_toast_error' | t }}`
: type;
message = typeof(message) === 'undefined'
? `{{ 'cp_toast_message_failed' | t }}`
: message;
// Build elements
var notice = document.createElement('li'),
category = document.createElement('span'),
content = document.createElement('p');
// Add content
category.innerHTML = type.charAt(0).toUpperCase() + type.slice(1);
content.innerHTML = message;
// Assemble notice
notice.className = 'rc_toast rc_toast--' + type;
notice.appendChild(category).className = 'rc_toast__type';
notice.appendChild(content).className = 'rc_toast__message';
return notice;
},
addToast: function(type, message) {
var notice = ReCharge.Toast.buildToast(type, message);
try {
document.querySelector('.rc_toaster').appendChild(notice);
} catch (e) {
document.querySelector('body')
.appendChild(ReCharge.Toast.buildToaster())
.appendChild(notice);
}
}
};
ReCharge.Helpers = {
toggle: function(id) {
var element = document.getElementById(id);
element.style.display = element.style.display === 'none' ? '' : 'none';
return false;
}
}
ReCharge.Forms = {
prettyError: message => {
message = message.split('_').join(' ');
return message.charAt(0).toUpperCase() + message.slice(1);
},
printError: (form, input, error) => {
const elementSelector = input == 'general' ? 'button[type="submit"]' : `input[name="${input}"]`;
const inputElem = form.querySelector(elementSelector);
const errorMessage = document.createElement('p');
errorMessage.className = 'error-message';
errorMessage.innerText = ReCharge.Forms.prettyError(error);
try {
inputElem.className = inputElem.className += ' error';
inputElem.parentNode.insertBefore(errorMessage, inputElem.nextSibling);
} catch (e) {
console.warn(form, input, error, e);
ReCharge.Toast.addToast(`{{ 'cp_toast_warning' | t }}`, ReCharge.Forms.prettyError(error));
}
},
printAllErrors: (form, errors) => {
Object.keys(errors).forEach(input => {
const input_errors = Array.isArray(errors[input]) ? errors[input] : [errors[input]];
input_errors.forEach(error => {
ReCharge.Forms.printError(form, input, error);
});
});
},
updatePropertyElements: (name, value) => {
document.querySelectorAll(`[data-property="${name}"]`).forEach(elem => elem.innerText = value);
},
updateAllProperties: elements => {
Object.keys(elements).forEach(key => {
const elem = elements[key];
ReCharge.Forms.updatePropertyElements(elem.name, elem.value);
});
},
resetErrors: () => {
document.querySelectorAll('input.error').forEach(elem => {
elem.className = elem.className.replace('error', '');
});
document.querySelectorAll('p.error-message').forEach(elem => {
elem.parentNode.removeChild(elem);
});
},
buildCountries: function(type = 'shipping') {
let countries = JSON.parse(sessionStorage.getItem('rc_shipping_countries'));
if (type === 'billing') {
countries = JSON.parse(sessionStorage.getItem('rc_billing_countries'));
}
if (!countries) {
// If no countries try to get the generic countries to work on all pages
countries = JSON.parse(sessionStorage.getItem('rc_countries'));
}
if ( !countries.length || !document.querySelector('#country')) { return; }
var activeCountry = document.querySelector('#country').getAttribute('data-value'),
options = `<option value="">{{ 'cp_please_select_a_country' | t }}</option>`;
options += countries.map(function(country) {
var selected = (country.name === activeCountry) ? ' selected' : '';
return '<option value="' + country.name + '"' + selected + '>' + country.name + '</option>';
}).join('\n');
document.querySelector('#country').innerHTML = options;
},
showProvinceDropdown: function() {
if (!document.querySelector('#province') || !document.querySelector('#province_selector')) { return; }
document.querySelector('#province').setAttribute('style', 'display: none;');
document.querySelector('#province_selector').setAttribute('style', 'display: inline-block;');
},
hideProvinceDropdown: function() {
if (!document.querySelector('#province') || !document.querySelector('#province_selector')) { return; }
document.querySelector('#province').setAttribute('style', 'display: inline-block;');
document.querySelector('#province_selector').setAttribute('style', 'display: none;');
},
updateProvinceInput: function(elem) {
if (!document.querySelector('#province')) { return; }
document.querySelector('#province').value = elem.value;
},
updateProvinces: function(elem) {
const countries = JSON.parse(sessionStorage.getItem('rc_billing_countries') || sessionStorage.getItem('rc_countries'));
if (!countries.length || !document.querySelector('#province')) { return; }
const country = countries.find(function(country) {
return country.name === elem.value;
});
if (!country || !country.provinces.length) {
window.ReCharge.Forms.hideProvinceDropdown();
return;
}
var provinces = country.provinces,
activeProvince = document.querySelector('#province').value,
options = `<option value="">{{ 'cp_select_province' | t }}</option>`;
options += provinces.map(function(province) {
var selected = (province.name === activeProvince) ? ' selected' : '';
return '<option value="' + province.name + '"' + selected + '>' + province.name + '</option>';
}).join('\n');
document.querySelector('#province_selector').innerHTML = options;
ReCharge.Forms.showProvinceDropdown();
},
toggleSubmitButton: function(elem) {
elem.disabled = !elem.disabled;
let newText = elem.getAttribute('data-text') || `{{ "cp_processing_message" | t }}`;
if (elem.disabled) {
elem.innerHTML = `
<div class="title-bold" style="display: flex; justify-content: center; align-items: center;">
${newText}
<img src="https://rechargeassets-bootstrapheroes-rechargeapps.netdna-ssl.com/static/images/spinner-anim-3.gif?t=1589649332" style="width: 30px; height: 12px;" class="margin-left-10">
</div>
`;
} else {
elem.innerHTML = newText;
}
},
toggleButtonLoading(buttonEl) {
// Will save the previous text and reapply it when toggled back
const isDisabled = buttonEl.disabled;
let originalText = buttonEl.getAttribute('data-original-text');
if (!originalText) {
originalText = buttonEl.innerText;
buttonEl.setAttribute('data-original-text', originalText);
}
if(isDisabled) {
buttonEl.innerHTML = originalText;
// Reset height/width to original values
buttonEl.style.width = '';
buttonEl.style.height = '';
} else {
// Set the width/height so it doesn't change sizes when showing spinner
buttonEl.style.width = buttonEl.offsetWidth;
buttonEl.style.height = buttonEl.offsetHeight;
buttonEl.replaceChildren(ReCharge.Novum.spinner);
}
buttonEl.toggleAttribute('disabled');
},
decodeResponse: function(response) {
if (typeof(response) === 'string') {
return response;
}
return response['error'] || response['errors'];
},
populateAddressData: function(address) {
const requiredAddressData = [ "first_name", "last_name", "address1", "address2", "company", "city", "zip", "phone", "province"];
for (const prop in address) {
requiredAddressData.includes(prop) ? document.querySelector(`#${prop}`).value = address[prop] : '';
}
document.querySelector("#country")
.setAttribute("data-value", address.country);
}
};
ReCharge.Endpoints = {
base: "{{ shopify_proxy_url if proxy_redirect else '' }}/portal/{{ customer.hash }}/",
request_objects: function() {
return attachQueryParams(`${this.base}request_objects`);
},
// Addresses endpoints
list_addresses_url: function() {
return attachQueryParams(`${this.base}addresses`);
},
create_address_url: function() {
return attachQueryParams(`${this.base}addresses/new`);
},
show_address_url: function(id) {
return attachQueryParams(`${this.base}addresses/${id}`);
},
// Subscriptions endpoints
list_subscriptions_url: function() {
return attachQueryParams(`${this.base}subscriptions`);
},
create_subscription_url: function() {
return attachQueryParams(`${this.base}subscriptions/new`);
},
show_subscription_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}`);
},
update_subscription_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}`);
},
// Subscription action endpoints
activate_subscription_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}/activate`);
},
skip_subscription_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}/skip`);
},
unskip_subscription_url: function (id) {
return attachQueryParams(`${this.base}subscriptions/${id}/unskip`);
},
swap_subscription_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}/swap`);
},
subscription_charge_date_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}/set_next_charge_date`);
},
delay_subscription_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}/delay`);
},
update_subscription_payment_method: function(subscriptionId, paymentMethodId) {
return attachQueryParams(`${this.base}subscriptions/${subscriptionId}/move/payment_method/${paymentMethodId}`);
},
update_subscription_address: function(subscriptionId, addressId) {
return attachQueryParams(`${this.base}subscriptions/${subscriptionId}/move/address/${addressId}`);
},
// Subscription cancel endpoints
cancel_subscription_url: function(id) {
return attachQueryParams(`${this.base}subscriptions/${id}/cancel`);
},
retention_strategy_url: function(data) {
return attachQueryParams(`${this.base}subscriptions/${data['id']}/cancel/${data['strategy']}`);
},
// Discount endpoints
apply_discount_to_address_url: function(id) {
return attachQueryParams(`${this.base}addresses/${id}/apply_discount`);
},
remove_discount_from_address_url: function(id) {
return attachQueryParams(`${this.base}addresses/${id}/remove_discount`);
},
// One-time endpoints
cancel_onetime_product: function(id) {
return attachQueryParams(`${this.base}onetimes/${id}/cancel`);
},
// process charge
process_charge: function(id) {
return attachQueryParams(`${this.base}charges/${id}/process`);
},
create_onetime: function() {
return attachQueryParams(`${this.base}onetimes`);
},
update_onetime: function(id) {
return attachQueryParams(`${this.base}onetimes/${id}`);
},
update_next_charge_date: function(url) {
return attachQueryParams(url);
},
onetime_charge_date_url: function(id) {
return attachQueryParams(`${this.base}onetimes/${id}/set_next_charge_date`);
},
update_billing_address: function() {
return attachQueryParams(`${this.base}payment_source/1/address`);
},
create_billing_address: function() {
return attachQueryParams(`${this.base}payment_source/address`);
},
send_shopify_connector_email: function() {
return attachQueryParams(`${this.base}notifications`);
},
shipping: function (id) {
return attachQueryParams(`${this.base}shipping/${id}`);
},
payment_methods: function (id) {
return attachQueryParams(`${this.base}payment_methods/${id}`);
},
customer: function () {
return attachQueryParams(`${this.base}customer`);
},
logout: function () {
return attachQueryParams(`${this.base}logout`);
}
};
ReCharge.Api = {
/** Pending api requests */
pendingRequests: {},
/** Submits a promise, updates the form's loading state, and only allows the request to be submitted once */
submitRequest: async (promise, { key, submitButton, successMessage }) => {
try {
if(!ReCharge.Api.pendingRequests[key]){
ReCharge.Forms.toggleButtonLoading(submitButton);
ReCharge.Api.pendingRequests[key] = promise();
}
const response = await ReCharge.Api.pendingRequests[key];
if(successMessage && ReCharge.Api.pendingRequests[key]){
ReCharge.Toast.addToast(`{{ 'cp_toast_success' | t }}`, successMessage);
}
return response;
} catch (err) {
if(ReCharge.Api.pendingRequests[key]){ // Only show error on first promise
console.error(err);
const errorMessage = err.response?.data?.error || `{{ "cp_something_went_wrong" | t }}`;
ReCharge.Toast.addToast(`{{ 'cp_toast_error' | t }}`, errorMessage);
throw err;
}
} finally {
delete ReCharge.Api.pendingRequests[key];
ReCharge.Forms.toggleButtonLoading(submitButton);
}
},
getPaymentMethods: async () => {
const { data: { payment_methods } } = await axios.get(ReCharge.Endpoints.request_objects() + '&schema={ "payment_methods": {} }');
return payment_methods;
},
getShippingAddresses: async () => {
const { data: { addresses } } = await axios.get(ReCharge.Endpoints.request_objects() + '&schema={ "addresses": {} }');
return addresses;
},
createShippingAddress: async (address) => {
const { data } = await axios.post(ReCharge.Endpoints.list_addresses_url(), address);
return data.address;
},
updateShippingAddress: async (address) => {
const { data } = await axios.post(ReCharge.Endpoints.show_address_url(address.id), address);
return data.address;
},
deleteShippingAddress: async (id) => {
return axios.delete(ReCharge.Endpoints.shipping(id))
},
updateCustomer: async (customer) => {
const { data } = await axios.post(ReCharge.Endpoints.customer(), customer);
return data.customer;
},
updateSubscriptionAddress: async(subscriptionId, addressId) => {
const { data } = await axios.post(ReCharge.Endpoints.update_subscription_address(subscriptionId, addressId));
return data;
},
updateSubscriptionPaymentMethod: async(subscriptionId, paymentMethodId) => {
const { data } = await axios.post(ReCharge.Endpoints.update_subscription_payment_method(subscriptionId, paymentMethodId));
return data;
}
};
ReCharge.Actions = {
get: async function(endpoint, id, schema) {
if (window.locked) { return false; } else { window.locked = true; }
if (typeof(endpoint) === 'undefined' || typeof(id) === 'undefined' || typeof(schema) === 'undefined') {
return false;
}
let url = ReCharge.Endpoints[endpoint](id);
console.log('get', url);
const dataUrl = attachQueryParams(`${ReCharge.Endpoints.request_objects()}&schema=${schema}`);
try {
const response = await axios(dataUrl);
console.log(response.data);
} catch(error) {
console.error(error);
} finally {
delete window.locked;
}
},
post: async function(endpoint, id, data) {
if (window.locked) { return false; } else { window.locked = true; }
if (typeof(endpoint) === 'undefined') { return false; }
var url = ReCharge.Endpoints[endpoint](id, data);
console.log('post', url);
let dataUrl = attachQueryParams(url);
try {
const response = await axios({
url: dataUrl,
method: 'post',
data
});
return response.data;
} catch(error) {
return error;
} finally {
delete window.locked;
}
},
put: async function(endpoint, id, data, productTitle = null) {
if (window.locked) { return false; } else { window.locked = true; }
if (typeof(endpoint) === 'undefined' || typeof(id) === 'undefined') { return false; }
var url = ReCharge.Endpoints[endpoint](id, data);
console.log('put', url);
let dataUrl = attachQueryParams(url);
let translation = `{{ 'cp_product_added' | t }}`;
translation = translation.replace('{cp_placeholder}', productTitle);
let successMessage = productTitle ? translation : `{{ "cp_updated_successfully" | t }}`;
try {
const response = await axios({
url: dataUrl,
method: 'post',
data
});
console.log(response.data);
ReCharge.Toast.addToast(
`{{ 'cp_toast_success' | t }}`, successMessage);
if (data && data.redirect_url) {
window.location.href = attachQueryParams(data.redirect_url);
} else {
window.location.reload();
}
} catch(error) {
console.error(error.response.data.error);
ReCharge.Toast.addToast(`{{ 'cp_toast_error' | t }}`, error.response.data.error);
} finally {
delete window.locked;
}
},
sendRequest: async function(evt) {
evt.preventDefault();
if (window.locked) {
return false;
}
ReCharge.Forms.resetErrors();
const form = evt.target;
let url = form.getAttribute('action');
let submitBtn = evt.target.querySelector('[type="submit"]');
let buttonText = submitBtn.innerText;
ReCharge.Forms.toggleSubmitButton(submitBtn);
let dataUrl = attachQueryParams(url);
let redirectUrl = form.querySelector('[name="redirect_url"]') || null;
try {
const response = await axios({
url: dataUrl,
method: 'post',
data: new FormData(form)
});
console.log(response.data);
ReCharge.Toast.addToast(`{{ 'rc_toast_success' | t }}`, `{{ "cp_updates_saved_successfully" | t }}`);
if (redirectUrl !== null) {
window.location.href = attachQueryParams(redirectUrl.value.split('?')[0]);
} else {
window.location.reload();
}
} catch(errorData) {
ReCharge.Forms.toggleSubmitButton(submitBtn);
submitBtn.innerText = buttonText;
const errors = ReCharge.Forms.decodeResponse(errorData.response.data);
console.error('errors', errors);
if (typeof (errors) === 'object') {
ReCharge.Forms.printAllErrors(evt.target, errors);
ReCharge.Toast.addToast(`{{ 'rc_toast_error' | t }}`, `{{ "cp_fix_form_errors" | t }}`);
} else {
ReCharge.Toast.addToast(`{{ 'rc_toast_error' | t }}`, ReCharge.Forms.prettyError(errors));
}
} finally {
delete window.locked;
}
},
getProducts: async function(limit = 6, productsSchema = null, url = null, requestedActionType = 'add_product') {
const schema = productsSchema || ReCharge.Schemas.products.list(limit, requestedActionType);
let dataUrl = attachQueryParams(`
${ReCharge.Endpoints.request_objects()}&schema=${schema}`
);
ReCharge.Novum.Pagination.limit = limit;
if (url) {
dataUrl = url;
}
try {
const response = await axios(dataUrl);
ReCharge.Novum.products = response.data.products;
if (
response.data.meta &&
response.data.meta.products
) {
ReCharge.Novum.meta = response.data.meta.products;
if (limit === 12) {
ReCharge.Novum.upsellMeta = response.data.meta.products;
ReCharge.Novum.Pagination.limit = 12;
} else if (limit === 6) {
ReCharge.Novum.addMeta = response.data.meta.products;
ReCharge.Novum.Pagination.limit = 6;
}
}
if (
response.data.products &&
!Array.isArray(response.data.products)
) {
ReCharge.Novum.products = [response.data.products];
response.data.products = [response.data.products];
}
return response.data;
} catch(error) {
console.error(error);
return [];
} finally {
delete window.locked;
}
},
//added by falcon at 2021-10-18
getProductsNotInSubscription: async function(limit = 6, productsSchema = null, requestedActionType = 'add_product') {
////
const rcRecommendedSuggestedVitamins = sessionStorage.getItem('rc_recommended_suggested_vitamins') || "";
console.log(" -------- rcRecommendedSuggestedVitamins => ", rcRecommendedSuggestedVitamins);
const recommendedSuggestedProductIds = rcRecommendedSuggestedVitamins.split(',')
.filter(
product_id => {
return product_id != "undefined" && product_id != "";
}
)
.map(
product_id => Number(product_id)
);
console.log(" -------- recommendedSuggestedProductIds => ", recommendedSuggestedProductIds);
////
const schema = productsSchema || ReCharge.Schemas.products.list(null, requestedActionType);
let dataUrl = attachQueryParams(`
${ReCharge.Endpoints.request_objects()}&schema=${schema}`
);
ReCharge.Novum.Pagination.limit = limit;
try {
const response = await axios(dataUrl);
let subscriptionRechargeIdArr = [];
const rc_subscriptions = JSON.parse(sessionStorage.getItem('rc_subscriptions')) || null;
if (rc_subscriptions != null ) {
for(let subscription of rc_subscriptions) {
subscriptionRechargeIdArr.push(subscription.recharge_product_id);
}
}
console.log(" ---------- response.data.products => ", response.data.products)
// const productsNotInSubscription = response.data.products.filter(product =>{
// return subscriptionRechargeIdArr.indexOf(product.id) === -1;
// })
const productsNotInSubscription = response.data.products.filter(product =>{
return subscriptionRechargeIdArr.indexOf(product.id) === -1 && recommendedSuggestedProductIds.indexOf(product.shopify_details.variants[0].shopify_id) > -1;
})
response.data.products = productsNotInSubscription.slice(0, 6);
ReCharge.Novum.products = response.data.products;
if (
response.data.meta &&
response.data.meta.products
) {
ReCharge.Novum.meta = response.data.meta.products;
if (limit === 12) {
ReCharge.Novum.upsellMeta = response.data.meta.products;
ReCharge.Novum.Pagination.limit = 12;
} else if (limit === 6) {
ReCharge.Novum.addMeta = response.data.meta.products;
ReCharge.Novum.Pagination.limit = 6;
}
}
if (
response.data.products &&
!Array.isArray(response.data.products)
) {
ReCharge.Novum.products = [response.data.products];
response.data.products = [response.data.products];
}
response.data.productsCount = productsNotInSubscription.length;
return response.data;
} catch(error) {
console.error(error);
return [];
} finally {
delete window.locked;
}
}
}
ReCharge.Schemas = {
subscriptions: function(id) {
if (id) {
return '{ "subscription": { "id": ' + id + ' } }';
}
return '{ "subscriptions": { "product": {} } }' ;
},
products: {
list(limit = 6, requestedActionType = 'add_product', page = 1) {
let actionType = `, "action_type": "${requestedActionType}"`;
return `{ "products": { "base_source": "store_settings", "limit": ${limit}, "page": ${page} ${actionType} } }`;
},
search(query, limit = 6, page = 1, requestedActionType = "add_product") {
let title = ``;
let excludePrepaids = `, "exclude_prepaids": true`;
let actionType = `, "action_type": "${requestedActionType}"`;
if (query.length > 0) {
title = `"title": "${query}",`;
}
if (requestedActionType != "swap_product") {
excludePrepaids = ``;
}
return `{ "products": { ${title} "base_source": "store_settings", "limit": ${limit}, "page": ${page} ${excludePrepaids} ${actionType} } }`;
},
getProduct(id) {
return `{ "products": { "shopify_product_id": ${id} } }`;
}
},
charges: {
perSubscription(addressId, subId) {
return `{ "charges": { "address_id": "${addressId}", "subscription_id": "${subId}", "status": "QUEUED" }, "orders": { "status": "SUCCESS", "subscription_id": "${subId}" }, "retention_strategies": { "subscription_id": "${subId}", "sort_by":"id-asc" } }`;
},
plusOnetimes(addressId, subId) {
return `{ "charges": { "address_id": "${ addressId }", "subscription_id": "${ subId }", "status": "QUEUED" }, "onetimes": { "product": {} } }`;
}
},
addresses: {
countries: function() {
return `{ "shipping_countries": [], "billing_countries": [] }`;
}
},
discounts: {
list: function(id) {
if (id) {
return `{ "discounts": {"id": ${id} } }`;
}
return `{ "discounts": [] }`;
}
}
};
ReCharge.Subscription = {
list: function() {
return ReCharge.Actions.get('request_objects', null, ReCharge.Schemas.subscriptions());
},
get: function(id) {
return ReCharge.Actions.get('request_objects', null, ReCharge.Schemas.subscriptions(id));
},
create: function(data) {
return ReCharge.Actions.put('create_subscription_url', null, data);
},
update: function(id, data) {
return ReCharge.Actions.put('update_subscription_url', id, data);
},
activate: function(id) {
return ReCharge.Actions.put('activate_subscription_url', id, {});
},
skip: function(id, charge_id, date) {
return ReCharge.Actions.put('skip_subscription_url', id, { charge_id: charge_id, date: date });
},
unskip: function(id, charge_id) {
return ReCharge.Actions.put('unskip_subscription_url', id, { charge_id: charge_id });
},
setChargeDate: function(id, date) {
return ReCharge.Actions.put('subscription_charge_date_url', id, { date: date });
},
delay: function(id, delay) {
return ReCharge.Actions.put('delay_subscription_url', id, { days: delay });
},
swap: function(id, variant_id) {
return ReCharge.Actions.put('swap_subscription_url', id, { shopify_variant_id: variant_id });
},
cancel: function(id, strategy, data) {
if (typeof(data) === 'undefined') { data = {}; }
return ReCharge.Actions.put('retention_strategy_url', { id: id, strategy: strategy }, data);
},
};
ReCharge.Discount = {
apply: function(id, discount_code) {
return ReCharge.Actions.put('apply_discount_to_address_url', id, { 'discount_code': discount_code });
},
remove: function(id, discount_code) {
return ReCharge.Actions.put('remove_discount_from_address_url', id, { 'discount_code': discount_code });
},
calculateDiscountedPrice: function(product, price) {
if (product.subscription_defaults) {
const hasDiscount = product.discount_amount && product.discount_amount !== 0 || false;
if (hasDiscount) {
if (product.discount_type == 'percentage') {
price *= 1 - product.discount_amount / 100;
} else {
price -= product.discount_amount;
}
}
}
return price;
}
};
ReCharge.Drawer = {
setHeader: (header) => {
document.querySelector('#te-modal-heading span').innerHTML = header;
},
open: ({ header, content, onBack } = {}) => {
ReCharge.Drawer.setHeader(header);
if(typeof content === 'string') {
document.getElementById('te-modal-content').innerHTML = content;
} else {
document.getElementById('te-modal-content').replaceChildren(content);
}
// Set focus to the drawer
document.getElementById('te-modal').focus();
// Set the content height to fill the rest of the space based on header height
document.getElementById('te-modal-content').style.height = `calc(100% - ${document.getElementById('te-modal-heading').offsetHeight}px)`;
document.body.classList.add('locked');
document.getElementById('sidebar-underlay').classList.add('visible');
document.getElementById('te-modal').classList.add('visible');
document.querySelectorAll('.close-sidebar').forEach(sidebar => sidebar.addEventListener('click', ReCharge.Drawer.close));
ReCharge.Drawer.toggleBackBtn(false);
// If an onBack is passed, add the event
if(onBack){
document.querySelector('#te-modal .back-btn')?.addEventListener('click', onBack);
}
},
close: () => {
document.querySelector('body').classList.remove('locked');
document.getElementById('sidebar-underlay').classList.remove('visible');
document.getElementById('te-modal').classList.remove('visible');
},
toggleBackBtn: (isShown) => {
const modal = document.getElementById('te-modal');
modal.querySelector('.back-btn').style.visibility = !isShown ? 'hidden' : '';
}
}
// Standalone modal utility that can render an accessible modal anywhere (Only 1 can be open at a time)
ReCharge.Modal = {
open({
title,
content,
cancelBtnText = 'Cancel',
confirmBtnText = 'Confirm',
onConfirm
}) {
// Creating overlay
const modalOverlay = document.createElement('div');
modalOverlay.id = 'rc-modal--overlay'
modalOverlay.classList.add('rc-modal__overlay');
// Creating modal container
const modal = document.createElement('div');
modal.id = 'rc-modal';
modal.classList.add('rc-modal');
modal.innerHTML = `