/* global Cleave */ var CARD_NUMBER_SELECTOR = '.cardNumber' var CARD_EXPIRATION_SELECTOR = '.cardExpiry' var CARD_CODE_SELECTOR = '.cardCode' //= require harpiya/frontend/coupon_manager Harpiya.ready(function ($) { Harpiya.onPayment = function () { if ($('#checkout_form_payment').length) { if ($('#existing_cards').length) { $('#payment-methods').hide() $('.existing-cc-radio').click(function () { $(this).prop('checked', true) $('#use_existing_card_no').prop('checked', false) $('#use_existing_card_yes').prop('checked', true) $('#payment-methods').hide() Harpiya.enableSave() }) $('#use_existing_card_no').click(function () { $('#payment-methods').show() $('.existing-cc-radio').prop('checked', false) $('#use_existing_card_yes').prop('checked', false) Harpiya.enableSave() }) } if ($(CARD_NUMBER_SELECTOR).length > 0 && $(CARD_EXPIRATION_SELECTOR).length > 0 && $(CARD_CODE_SELECTOR).length > 0) { var cardCodeCleave; var updateCardCodeCleave = function (length) { if (cardCodeCleave) cardCodeCleave.destroy() cardCodeCleave = new Cleave(CARD_CODE_SELECTOR, { numericOnly: true, blocks: [length] }) } updateCardCodeCleave(3) /* eslint-disable no-new */ new Cleave(CARD_NUMBER_SELECTOR, { creditCard: true, onCreditCardTypeChanged: function (type) { $('.ccType').val(type) if (type === 'amex') { updateCardCodeCleave(4) } else { updateCardCodeCleave(3) } } }) /* eslint-disable no-new */ new Cleave(CARD_EXPIRATION_SELECTOR, { date: true, datePattern: ['m', 'Y'] }) } $('input[type="radio"][name="order[payments_attributes][][payment_method_id]"]').click(function () { $('#payment-methods').hide() $('.payment-sources').hide() Harpiya.enableSave() if ($('#payment_method_' + this.value).find('fieldset').children().length !== 0) { if (this.closest('label').dataset.type === 'card') { if ($('#existing_cards').length) { $('.existing-cc-radio').first().prop('checked', true); $('#use_existing_card_no').prop('checked', false) $('#use_existing_card_yes').prop('checked', true) $('#existing_cards').show(); $('#payment-methods').hide(); $('.payment-sources').show() } } else { $('.existing-cc-radio').prop('checked', false); $('#use_existing_card_no').prop('checked', false); $('#existing_cards').hide(); $('#payment-methods').show(); $('.payment-sources').show() } } $('#payment-methods li').hide() if (this.checked) { $('#payment_method_' + this.value).show() } }) $(document).on('click', '#cvv_link', function (event) { var windowName = 'cvv_info' var windowOptions = 'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,scrollbars=1' window.open($(this).attr('href'), windowName, windowOptions) event.preventDefault() }) $('input[type="radio"]:checked').click() $('#checkout_form_payment').submit(function (event) { var input = { couponCodeField: $('#order_coupon_code'), couponStatus: $('#coupon_status') } if ($.trim(input.couponCodeField.val()).length > 0) { // eslint-disable-next-line no-undef if (new CouponManager(input).applyCoupon()) { return true } else { Harpiya.enableSave() event.preventDefault() return false } } }) } } Harpiya.onPayment() })