diff --git a/public/form.js b/public/form.js index 4f7c70f..e64ca42 100644 --- a/public/form.js +++ b/public/form.js @@ -1,6 +1,72 @@ -function isFormValid() { - const form = document.getElementById('mainForm'); - return form.checkValidity(); // Returns true if the form is valid +function isFormValid(form) { + return form.checkValidity(); +} + +function removePayPalElement() { + const element = document.getElementById('paypal-button-container'); + element.innerHTML = ''; + element.style.display = 'none'; +} + +function hideDialog() { + document.getElementById('overlay').style.display = 'none'; + document.getElementById('dialog').style.display = 'none'; + document.getElementById('openDialogButton').style.display = 'block'; +} + +function renderPaypalElement() { + const element = document.getElementById('paypal-button-container'); + element.innerHTML = ''; + element.style.display = 'block'; + + paypal.Buttons({ + style: {color: 'blue', shape: 'pill', label: 'pay', height: 40}, + + // Call your server to set up the transaction + createOrder: function(data, actions) { + return fetch('/demo/checkout/api/paypal/order/create/', { + method: 'post' + }).then(function(res) { + return res.json(); + }).then(function(orderData) { + return orderData.id; + }); + }, + + // Call your server to finalize the transaction + onApprove: function(data, actions) { + return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', { + method: 'post' + }).then(function(res) { + return res.json(); + }).then(function(orderData) { + var errorDetail = Array.isArray(orderData.details) && orderData.details[0]; + + // (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart() + if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') { + return actions.restart(); + } + + // (2) Other non-recoverable errors -> Show a failure message + if (errorDetail) { + var msg = 'Sorry, your transaction could not be processed.'; + if (errorDetail.description) msg += '\n\n' + errorDetail.description; + if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')'; + return alert(msg); // TODO show a prettier message + } + + // (3) Successful transaction -> Show confirmation or thank you + // Grab transaction.status and transaction.id, call up php and save it in db. + // var transaction = orderData.purchase_units[0].payments.captures[0]; + // alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details'); + + // Replace the above to show a success message within this page, e.g. + element.innerHTML = ''; + element.innerHTML = '