|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | define([ |
7 | | - 'Magento_Captcha/js/view/checkout/defaultCaptcha', |
8 | | - 'Magento_Captcha/js/model/captchaList', |
9 | | - 'Magento_SalesRule/js/action/set-coupon-code', |
10 | | - 'Magento_SalesRule/js/action/cancel-coupon', |
11 | | - 'Magento_Checkout/js/model/quote', |
12 | | - 'ko' |
13 | | - ], |
14 | | - function (defaultCaptcha, captchaList, setCouponCodeAction, cancelCouponAction, quote, ko) { |
15 | | - 'use strict'; |
| 7 | + 'Magento_Captcha/js/view/checkout/defaultCaptcha', |
| 8 | + 'Magento_Captcha/js/model/captchaList', |
| 9 | + 'Magento_SalesRule/js/action/set-coupon-code', |
| 10 | + 'Magento_SalesRule/js/action/cancel-coupon', |
| 11 | + 'Magento_Checkout/js/model/quote', |
| 12 | + 'ko' |
| 13 | +], function (defaultCaptcha, captchaList, setCouponCodeAction, cancelCouponAction, quote, ko) { |
| 14 | + 'use strict'; |
16 | 15 |
|
17 | | - var totals = quote.getTotals(), |
18 | | - couponCode = ko.observable(null), |
19 | | - isApplied; |
| 16 | + return defaultCaptcha.extend({ |
| 17 | + /** @inheritdoc */ |
| 18 | + initialize: function () { |
| 19 | + var self = this, |
| 20 | + currentCaptcha, |
| 21 | + totals = quote.getTotals(), |
| 22 | + couponCode = ko.observable(null), |
| 23 | + couponCodeValue, |
| 24 | + isApplied; |
20 | 25 |
|
21 | | - if (totals()) { |
22 | | - couponCode(totals()['coupon_code']); |
23 | | - } |
24 | | - //Captcha can only be required for adding a coupon so we need to know if one was added already. |
25 | | - isApplied = ko.observable(couponCode() != null); |
| 26 | + if (totals()) { |
| 27 | + couponCode(totals()['coupon_code']); |
| 28 | + } |
26 | 29 |
|
27 | | - return defaultCaptcha.extend({ |
28 | | - /** @inheritdoc */ |
29 | | - initialize: function () { |
30 | | - var self = this, |
31 | | - currentCaptcha; |
| 30 | + // Captcha can only be required for adding a coupon so we need to know if one was added already. |
| 31 | + couponCodeValue = couponCode(); |
| 32 | + isApplied = ko.observable(typeof couponCodeValue === 'string' && couponCodeValue.length > 0); |
32 | 33 |
|
33 | | - this._super(); |
34 | | - //Getting coupon captcha model. |
35 | | - currentCaptcha = captchaList.getCaptchaByFormId(this.formId); |
| 34 | + this._super(); |
| 35 | + //Getting coupon captcha model. |
| 36 | + currentCaptcha = captchaList.getCaptchaByFormId(this.formId); |
36 | 37 |
|
37 | | - if (currentCaptcha != null) { |
38 | | - if (!isApplied()) { |
39 | | - //Show captcha if we don't have a coupon applied. |
40 | | - currentCaptcha.setIsVisible(true); |
41 | | - } |
42 | | - this.setCurrentCaptcha(currentCaptcha); |
43 | | - //Add captcha code to coupon-apply request. |
44 | | - setCouponCodeAction.registerDataModifier(function (headers) { |
45 | | - if (self.isRequired()) { |
46 | | - headers['X-Captcha'] = self.captchaValue()(); |
47 | | - } |
48 | | - }); |
49 | | - //Refresh captcha after failed request. |
50 | | - setCouponCodeAction.registerFailCallback(function () { |
51 | | - if (self.isRequired()) { |
52 | | - self.refresh(); |
53 | | - } |
54 | | - }); |
55 | | - //Hide captcha when a coupon has been applied. |
56 | | - setCouponCodeAction.registerSuccessCallback(function () { |
57 | | - self.setIsVisible(false); |
58 | | - }); |
59 | | - //Show captcha again if it was canceled. |
60 | | - cancelCouponAction.registerSuccessCallback(function () { |
61 | | - if (self.isRequired()) { |
62 | | - self.setIsVisible(true); |
63 | | - } |
64 | | - }); |
| 38 | + if (currentCaptcha != null) { |
| 39 | + if (!isApplied()) { |
| 40 | + //Show captcha if we don't have a coupon applied. |
| 41 | + currentCaptcha.setIsVisible(true); |
65 | 42 | } |
| 43 | + this.setCurrentCaptcha(currentCaptcha); |
| 44 | + //Add captcha code to coupon-apply request. |
| 45 | + setCouponCodeAction.registerDataModifier(function (headers) { |
| 46 | + if (self.isRequired()) { |
| 47 | + headers['X-Captcha'] = self.captchaValue()(); |
| 48 | + } |
| 49 | + }); |
| 50 | + //Refresh captcha after failed request. |
| 51 | + setCouponCodeAction.registerFailCallback(function () { |
| 52 | + if (self.isRequired()) { |
| 53 | + self.refresh(); |
| 54 | + } |
| 55 | + }); |
| 56 | + //Hide captcha when a coupon has been applied. |
| 57 | + setCouponCodeAction.registerSuccessCallback(function () { |
| 58 | + self.setIsVisible(false); |
| 59 | + }); |
| 60 | + //Show captcha again if it was canceled. |
| 61 | + cancelCouponAction.registerSuccessCallback(function () { |
| 62 | + if (self.isRequired()) { |
| 63 | + self.setIsVisible(true); |
| 64 | + } |
| 65 | + }); |
66 | 66 | } |
67 | | - }); |
| 67 | + } |
68 | 68 | }); |
| 69 | +}); |
0 commit comments