|
| 1 | +import braintree from 'braintree-web'; |
| 2 | +import braintreePayments from './payments'; |
| 3 | +import JSONEditor from 'jsoneditor'; |
| 4 | +import 'jsoneditor/dist/jsoneditor.css'; |
| 5 | + |
| 6 | +let jsClientToken = document.querySelector('.js-client-token'); |
| 7 | +let clientToken = jsClientToken.dataset.clientToken; |
| 8 | +let submitButtonOne = document.querySelector('#submit-button-one'); |
| 9 | +let apmAmount = document.getElementById('apm-amount'); |
| 10 | +let jsPaypalClientId = document.querySelector('.js-paypal-client-id'); |
| 11 | +let paypalClientId = jsPaypalClientId.dataset.paypalClientId; |
| 12 | +let settings = JSON.parse(document.getElementById('customer-settings').dataset.settings); |
| 13 | +let deviceData; |
| 14 | + |
| 15 | +let paypalOrderJsonEditor = document.getElementById('paypal-order-json-editor'); |
| 16 | +let paypalOrderJson = { |
| 17 | + flow: 'checkout', |
| 18 | + currency: 'GBP', |
| 19 | + amount: '123,45', |
| 20 | + intent: 'authorize' |
| 21 | +}; |
| 22 | + |
| 23 | +let customerJsonEditor = new JSONEditor( |
| 24 | + paypalOrderJsonEditor, |
| 25 | + { |
| 26 | + limitDragging: true, |
| 27 | + name: 'CustomerInfo', |
| 28 | + modes: ['code'], |
| 29 | + mainMenuBar: true, |
| 30 | + navigationBar: false, |
| 31 | + statusBar: false, |
| 32 | + search: false, |
| 33 | + history: false |
| 34 | + }, |
| 35 | + paypalOrderJson |
| 36 | +); |
| 37 | +braintreePayments.animatePaymentForm(); |
| 38 | + |
| 39 | +submitButtonOne.addEventListener('click', function () { |
| 40 | + apmAmount = apmAmount.value; |
| 41 | + let destroy = document.getElementById('apm-creation-destroyable'); |
| 42 | + destroy.parentNode.removeChild(destroy); |
| 43 | + braintree.client.create({ |
| 44 | + authorization: clientToken |
| 45 | + }, function (clientErr, clientInstance) { |
| 46 | + if (clientErr) { |
| 47 | + console.error('Error creating client:', clientErr); |
| 48 | + return; |
| 49 | + } |
| 50 | + braintree.dataCollector.create({ |
| 51 | + client: clientInstance, |
| 52 | + kount: true, |
| 53 | + paypal: true |
| 54 | + }, function (err, dataCollectorInstance) { |
| 55 | + deviceData = JSON.parse(dataCollectorInstance.deviceData); |
| 56 | + }); |
| 57 | + braintree.paypalCheckout.create({ |
| 58 | + client: clientInstance |
| 59 | + }, function (paypalCheckoutErr, paypalCheckoutInstance) { |
| 60 | + if (paypalCheckoutErr) { |
| 61 | + console.error('Error creating PayPal Checkout:', paypalCheckoutErr); |
| 62 | + return; |
| 63 | + } |
| 64 | + paypalCheckoutInstance.loadPayPalSDK({ |
| 65 | + 'buyer-country': 'GB', |
| 66 | + components: 'buttons,messages', |
| 67 | + currency: 'GBP', |
| 68 | + intent: 'authorize', |
| 69 | + dataAttributes: { |
| 70 | + amount: apmAmount |
| 71 | + } |
| 72 | + }, function () { |
| 73 | + let apmAmountObject = {'amount': apmAmount}; |
| 74 | + let paypalOrderObject = customerJsonEditor.get(); |
| 75 | + let finalPayPalObject = {...apmAmountObject, ...paypalOrderObject} |
| 76 | + let button = paypal.Buttons({ |
| 77 | + style: { |
| 78 | + "layout": "vertical", |
| 79 | + "color": "gold", |
| 80 | + "shape": "pill", |
| 81 | + "label": "pay" |
| 82 | + }, |
| 83 | + fundingSource: paypal.FUNDING.PAYPAL, |
| 84 | + createOrder: function () { |
| 85 | + return paypalCheckoutInstance.createPayment(finalPayPalObject); |
| 86 | + }, |
| 87 | + onApprove: function (data) { |
| 88 | + return paypalCheckoutInstance.tokenizePayment(data, function (err, payload) { |
| 89 | + let destroy = document.getElementById('apm-buttons-destroyable'); |
| 90 | + destroy.parentNode.removeChild(destroy); |
| 91 | + braintreePayments.sendServerPayLoad(payload,deviceData); |
| 92 | + }); |
| 93 | + }, |
| 94 | + |
| 95 | + onCancel: function (data) { |
| 96 | + console.log('PayPal payment cancelled', JSON.stringify(data, 0, 2)); |
| 97 | + }, |
| 98 | + |
| 99 | + onError: function (err) { |
| 100 | + console.error('PayPal error', err); |
| 101 | + } |
| 102 | + |
| 103 | + }); |
| 104 | + if (!button.isEligible()) { |
| 105 | + console.log('Not eligible'); |
| 106 | + } |
| 107 | + button.render('#apm-container'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments