|
12 | 12 | <select name="gateway" id="apm-gateway"> |
13 | 13 | <option value="">Select Gateway</option> |
14 | 14 | </select> |
| 15 | + <select id="flow-type" name="flow_type"> |
| 16 | + <option value="pay">Pay</option> |
| 17 | + <option value="pay-with-token">Pay with token</option> |
| 18 | + <option value="tokenize">Tokenize</option> |
| 19 | + <option value="buy-and-tokenize">Buy & Tokenize</option> |
| 20 | + </select> |
15 | 21 | <input type="text" id="invoice-id" name="invoice_id" placeholder="Invoice ID" /> |
16 | 22 | <input type="text" id="customer-id" name="customer_id" placeholder="Customer ID" style="display: none" /> |
17 | 23 | <input type="text" id="customer-token-id" name="customer_token_id" placeholder="Customer Token ID" style="display: none" /> |
|
67 | 73 | gatewayDict[option.value] = option.textContent.trim(); |
68 | 74 | }); |
69 | 75 |
|
| 76 | + const flowTypeEl = document.querySelector('#flow-type'); |
70 | 77 | const invoiceIdEl = document.querySelector('#invoice-id'); |
71 | 78 | const customerIdEl = document.querySelector('#customer-id'); |
72 | 79 | const customerTokenIdEl = document.querySelector('#customer-token-id'); |
73 | 80 |
|
74 | | - let flow = 'authorization'; |
75 | | - |
76 | 81 | let previousId; |
77 | 82 | let previousGateway; |
78 | 83 |
|
|
115 | 120 | return Object.keys(redirect).length ? { redirect } : {}; |
116 | 121 | } |
117 | 122 |
|
118 | | - gatewayEl.addEventListener('change', (e) => { |
119 | | - const value = e.target.value; |
120 | | - const gateway = gatewayDict[value]; |
| 123 | + function syncInputVisibility() { |
| 124 | + const ft = flowTypeEl.value; |
| 125 | + invoiceIdEl.style.display = |
| 126 | + (ft === 'pay' || ft === 'pay-with-token' || ft === 'buy-and-tokenize') ? 'inline-block' : 'none'; |
| 127 | + customerIdEl.style.display = |
| 128 | + (ft === 'tokenize' || ft === 'buy-and-tokenize') ? 'inline-block' : 'none'; |
| 129 | + customerTokenIdEl.style.display = |
| 130 | + (ft === 'pay-with-token' || ft === 'tokenize' || ft === 'buy-and-tokenize') ? 'inline-block' : 'none'; |
| 131 | + } |
121 | 132 |
|
122 | | - switch (gateway) { |
123 | | - case 'Forage Authorization': |
124 | | - flow = 'authorization'; |
125 | | - invoiceIdEl.style.display = 'inline-block'; |
126 | | - customerIdEl.style.display = 'none'; |
127 | | - customerTokenIdEl.style.display = 'inline-block'; |
128 | | - break; |
129 | | - case 'Forage Tokenization': |
130 | | - flow = 'tokenization'; |
131 | | - invoiceIdEl.style.display = 'none'; |
132 | | - customerIdEl.style.display = 'inline-block'; |
133 | | - customerTokenIdEl.style.display = 'inline-block'; |
134 | | - break; |
135 | | - default: |
136 | | - flow = 'authorization'; |
137 | | - invoiceIdEl.style.display = 'inline-block'; |
138 | | - customerIdEl.style.display = 'none'; |
139 | | - customerTokenIdEl.style.display = 'none'; |
140 | | - break; |
141 | | - } |
142 | | - }) |
| 133 | + flowTypeEl.addEventListener('change', syncInputVisibility); |
| 134 | + syncInputVisibility(); |
143 | 135 |
|
144 | 136 | document.querySelector('#initialise-apm').addEventListener('submit', (e) => { |
145 | 137 | e.preventDefault() |
|
150 | 142 |
|
151 | 143 | let apm; |
152 | 144 |
|
153 | | - if (flow === 'authorization') { |
| 145 | + const flowType = flowTypeEl.value; |
| 146 | + const needsInvoice = flowType === 'pay' || flowType === 'pay-with-token' || flowType === 'buy-and-tokenize'; |
| 147 | + const needsCustomer = flowType === 'tokenize' || flowType === 'buy-and-tokenize'; |
| 148 | + const needsCustomToken = flowType === 'pay-with-token' || flowType === 'tokenize' || flowType === 'buy-and-tokenize'; |
| 149 | + const useAuth = flowType !== 'tokenize'; |
| 150 | + const useToken = flowType === 'tokenize'; |
| 151 | + |
| 152 | + if (needsInvoice) { |
154 | 153 | if (!invoiceId) { |
155 | 154 | alert('Please enter an invoice ID') |
156 | 155 | return; |
|
167 | 166 | } |
168 | 167 |
|
169 | 168 | if (previousId === invoiceId) { |
170 | | - const result = confirm("You're using a invoice id that has already been used. Are you sure you want to use it again?") |
| 169 | + const result = confirm("You're using an invoice id that has already been used. Are you sure you want to use it again?") |
171 | 170 | if (!result) { |
172 | 171 | return |
173 | 172 | } |
174 | 173 | } |
175 | 174 |
|
176 | | - |
177 | 175 | previousId = invoiceId; |
178 | 176 | previousGateway = gatewayConfigurationId; |
| 177 | + } |
| 178 | + |
| 179 | + if (needsCustomer && !customerId) { |
| 180 | + alert('Please enter a customer ID') |
| 181 | + return |
| 182 | + } |
| 183 | + |
| 184 | + if (needsCustomToken && !customerTokenId) { |
| 185 | + alert('Please enter a customer token ID') |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + if (useAuth) { |
179 | 190 | apm = client.apm.authorization(el, { |
180 | 191 | gatewayConfigurationId, |
181 | 192 | invoiceId, |
|
189 | 200 | }, |
190 | 201 | ...buildRedirectOptions(), |
191 | 202 | }) |
192 | | - } |
193 | | - |
194 | | - if (flow === 'tokenization') { |
195 | | - if (!customerId) { |
196 | | - alert('Please enter a customer ID') |
197 | | - return |
198 | | - } |
199 | | - if (!customerTokenId) { |
200 | | - alert('Please enter a customer token ID') |
201 | | - return |
202 | | - } |
203 | | - |
| 203 | + } else if (useToken) { |
204 | 204 | apm = client.apm.tokenization(el, { |
205 | 205 | gatewayConfigurationId, |
206 | 206 | customerId, |
|
0 commit comments