|
| 1 | +/* eslint-disable no-undef */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +$(document).ready(function () { |
| 6 | + var captureContext = JSON.parse($('#flexTokenResponse').val()).keyId; |
| 7 | + var flex = new Flex(captureContext); // eslint-disable-line no-undef |
| 8 | + var customStyles = { |
| 9 | + input: { |
| 10 | + 'font-family': '-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol"', |
| 11 | + 'font-size': '1em', |
| 12 | + 'line-height': '1.5', |
| 13 | + color: '#495057' |
| 14 | + }, |
| 15 | + ':focus': { |
| 16 | + color: 'blue' |
| 17 | + }, |
| 18 | + ':disabled': { |
| 19 | + cursor: 'not-allowed' |
| 20 | + }, |
| 21 | + valid: { |
| 22 | + color: '#3c763d' |
| 23 | + }, |
| 24 | + invalid: { |
| 25 | + color: '#a94442' |
| 26 | + } |
| 27 | + }; |
| 28 | + var microform = flex.microform({ |
| 29 | + styles: customStyles |
| 30 | + }); |
| 31 | + var number = microform.createField('number'); |
| 32 | + var securityCode = microform.createField('securityCode'); |
| 33 | + securityCode.load('#securityCode-container'); |
| 34 | + number.load('#cardNumber-container'); |
| 35 | + number.on('change', function (data) { |
| 36 | + var cardType = data.card[0].name; |
| 37 | + $('.card-number-wrapper').attr('data-type', cardType); |
| 38 | + $('#cardType').val(cardType); |
| 39 | + }); |
| 40 | + /** |
| 41 | + * * |
| 42 | + * @param {*} token * |
| 43 | + * @returns {*} * |
| 44 | + */ |
| 45 | + function parseJwt(token) { |
| 46 | + var base64Url = token.split('.')[1]; |
| 47 | + var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); |
| 48 | + var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) { // eslint-disable-line no-undef |
| 49 | + return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); |
| 50 | + }).join('')); |
| 51 | + |
| 52 | + return JSON.parse(jsonPayload); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * * |
| 57 | + * @returns {*} * |
| 58 | + */ |
| 59 | + function flexTokenCreation() { |
| 60 | + var expMonth = $('#expirationMonth').val(); |
| 61 | + var expYear = $('#expirationYear').val(); |
| 62 | + |
| 63 | + if (expMonth === '' || expYear === '') { |
| 64 | + return false; |
| 65 | + } |
| 66 | + // Send in optional parameters from other parts of your payment form |
| 67 | + var options = { |
| 68 | + expirationMonth: expMonth.length === 1 ? '0' + expMonth : expMonth, |
| 69 | + expirationYear: expYear |
| 70 | + // cardType: /* ... */ |
| 71 | + }; |
| 72 | + // validation |
| 73 | + // look for field validation errors |
| 74 | + |
| 75 | + // eslint-disable-next-line consistent-return |
| 76 | + microform.createToken(options, function (err, response) { |
| 77 | + // At this point the token may be added to the form |
| 78 | + // as hidden fields and the submission continued |
| 79 | + |
| 80 | + if (err) { |
| 81 | + $('.card-number-wrapper .invalid-feedback').text(err.message).css('display', 'block'); |
| 82 | + return true; |
| 83 | + } |
| 84 | + var decodedJwt = parseJwt(response); |
| 85 | + document.getElementById('cardNumber').valid = true; |
| 86 | + $('#flex-response').val(response); |
| 87 | + $('#cardNumber').val(decodedJwt.data.number); |
| 88 | + |
| 89 | + if ($('.submit-payment').length === 1) { |
| 90 | + $('.submit-payment').trigger('click'); |
| 91 | + } else { |
| 92 | + $('.save-payment').trigger('click'); |
| 93 | + } |
| 94 | + }); |
| 95 | + return true; |
| 96 | + } |
| 97 | + // check for card type function |
| 98 | + /** |
| 99 | + */ |
| 100 | + function assignCorrectCardType() { |
| 101 | + var cardType = $('#cardType').val(); |
| 102 | + if (cardType.charCodeAt(0) !== cardType.toUpperCase().charCodeAt(0)) { |
| 103 | + var correctCardType = ''; |
| 104 | + switch (cardType) { // eslint-disable-line default-case |
| 105 | + case 'visa': |
| 106 | + correctCardType = 'Visa'; |
| 107 | + break; |
| 108 | + case 'mastercard': |
| 109 | + correctCardType = 'Master Card'; |
| 110 | + break; |
| 111 | + case 'amex': |
| 112 | + correctCardType = 'Amex'; |
| 113 | + break; |
| 114 | + case 'discover': |
| 115 | + correctCardType = 'Discover'; |
| 116 | + break; |
| 117 | + case 'diners-club': |
| 118 | + correctCardType = 'DinersClub'; |
| 119 | + break; |
| 120 | + case 'maestro': |
| 121 | + correctCardType = 'Maestro'; |
| 122 | + break; |
| 123 | + case 'jcb': |
| 124 | + correctCardType = 'JCB'; |
| 125 | + break; |
| 126 | + } |
| 127 | + $('#cardType').val(correctCardType); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + $('.payment-summary .edit-button').on('click', function () { |
| 132 | + $('#flex-response').val(''); |
| 133 | + }); |
| 134 | + |
| 135 | + // intercept the form submission and make a tokenize request instead |
| 136 | + $('.submit-payment').on('click', function (event) { |
| 137 | + if ($('.tab-pane.active').find("input[name$='paymentMethod']").val() === 'CREDIT_CARD') { |
| 138 | + if (($('#flex-response').val() === '' || $('#flex-response').val() === undefined) && $('.payment-information').data('is-new-payment')) { |
| 139 | + flexTokenCreation(); |
| 140 | + event.stopImmediatePropagation(); |
| 141 | + } |
| 142 | + } |
| 143 | + }); |
| 144 | + $('.save-payment').on('click', function (event) { |
| 145 | + if (($('#flex-response').val() === '' || $('#flex-response').val() === undefined)) { |
| 146 | + flexTokenCreation(); |
| 147 | + assignCorrectCardType(); |
| 148 | + event.preventDefault(); |
| 149 | + } |
| 150 | + }); |
| 151 | +}); |
0 commit comments