PaymentCommon.vue
1 month ago
PaymentOnSite.vue
1 month ago
PaymentPayPal.vue
1 month ago
PaymentSquare.vue
1 month ago
PaymentStripe.vue
1 month ago
PaymentWc.vue
1 month ago
PaymentPayPal.vue
205 lines
| 1 | <template> |
| 2 | <div class="am-fs__payment_payPal" :style="cssVars"></div> |
| 3 | </template> |
| 4 | |
| 5 | <script setup> |
| 6 | import { settings, ajaxUrl } from '../../../../../plugins/settings.js' |
| 7 | |
| 8 | // * Import from Vue |
| 9 | import { inject, computed, onMounted } from 'vue' |
| 10 | |
| 11 | // * Import from Vuex |
| 12 | import { useStore } from 'vuex' |
| 13 | |
| 14 | // * Composables |
| 15 | import { |
| 16 | usePaymentError, |
| 17 | useBookingData, |
| 18 | useCreateBooking, |
| 19 | useCreateBookingSuccess, |
| 20 | useCreateBookingError, |
| 21 | getErrorMessage, |
| 22 | } from '../../../../../assets/js/public/booking.js' |
| 23 | |
| 24 | const store = useStore() |
| 25 | |
| 26 | // * Labels |
| 27 | const amLabels = inject('amLabels') |
| 28 | |
| 29 | // * Step Functions |
| 30 | const { nextStep } = inject('changingStepsFunctions', { |
| 31 | nextStep: () => {}, |
| 32 | }) |
| 33 | |
| 34 | // * Colors |
| 35 | // let amColors = inject('amColors') |
| 36 | // |
| 37 | let cssVars = computed(() => { |
| 38 | return {} |
| 39 | }) |
| 40 | |
| 41 | // * Payment Part |
| 42 | let transactionReference = null |
| 43 | |
| 44 | const shortcodeData = inject('shortcodeData') |
| 45 | |
| 46 | function payPalPaymentInit() { |
| 47 | const selector = '#am-paypal-element-' + shortcodeData.value.counter |
| 48 | const el = document.getElementById('am-paypal-element-' + shortcodeData.value.counter) |
| 49 | |
| 50 | if ( |
| 51 | typeof window.paypal === 'undefined' || |
| 52 | typeof window.paypal.Buttons !== 'function' || |
| 53 | !window.paypal.FUNDING || |
| 54 | !el |
| 55 | ) { |
| 56 | setTimeout(payPalPaymentInit, 100) |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | let btn |
| 61 | try { |
| 62 | btn = window.paypal.Buttons({ |
| 63 | fundingSource: window.paypal.FUNDING.PAYPAL, |
| 64 | style: { |
| 65 | color: 'gold', |
| 66 | shape: 'rect', |
| 67 | tagline: false, |
| 68 | height: 40, |
| 69 | }, |
| 70 | |
| 71 | onClick: function (data, actions) { |
| 72 | if (store.getters['coupon/getCouponValidated']) { |
| 73 | actions.resolve() |
| 74 | } else { |
| 75 | emits('payment-error', amLabels.coupon_mandatory) |
| 76 | actions.reject() |
| 77 | } |
| 78 | }, |
| 79 | |
| 80 | createOrder: function () { |
| 81 | return fetch(ajaxUrl + '/payment/payPal', { |
| 82 | method: 'POST', |
| 83 | headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }, |
| 84 | body: JSON.stringify(useBookingData(store, null, true, {}, null)['data']), |
| 85 | }) |
| 86 | .then((r) => { |
| 87 | if (!r.ok) return Promise.reject(new Error('HTTP ' + r.status)) |
| 88 | return r.json() |
| 89 | }) |
| 90 | .then((response) => { |
| 91 | transactionReference = response.data.transactionReference |
| 92 | return response.data.paymentID |
| 93 | }) |
| 94 | .catch((error) => { |
| 95 | payPalError(error) |
| 96 | }) |
| 97 | }, |
| 98 | |
| 99 | onApprove: function (data) { |
| 100 | store.commit('setLoading', true) |
| 101 | |
| 102 | useCreateBooking( |
| 103 | store, |
| 104 | useBookingData( |
| 105 | store, |
| 106 | null, |
| 107 | false, |
| 108 | { |
| 109 | transactionReference: transactionReference, |
| 110 | PaymentId: data.orderID, |
| 111 | PayerId: data.payerID, |
| 112 | }, |
| 113 | null, |
| 114 | ), |
| 115 | successBooking, |
| 116 | (error) => { |
| 117 | useCreateBookingError(store, error.response.data, () => { |
| 118 | emits('payment-error', getErrorMessage()) |
| 119 | }) |
| 120 | }, |
| 121 | ) |
| 122 | }, |
| 123 | |
| 124 | onCancel: function () { |
| 125 | store.commit('setLoading', false) |
| 126 | }, |
| 127 | |
| 128 | onError: function (error) { |
| 129 | payPalError(error) |
| 130 | }, |
| 131 | }) |
| 132 | } catch (e) { |
| 133 | setTimeout(payPalPaymentInit, 100) |
| 134 | return |
| 135 | } |
| 136 | |
| 137 | if (btn && btn.render) { |
| 138 | btn.render(selector).catch(function (e) {}) |
| 139 | } else { |
| 140 | setTimeout(payPalPaymentInit, 100) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | function payPalError(error) { |
| 145 | let errorString = error.toString() |
| 146 | |
| 147 | let response = JSON.parse( |
| 148 | JSON.stringify( |
| 149 | JSON.parse(errorString.substring(errorString.indexOf('{'), errorString.lastIndexOf('}') + 1)), |
| 150 | ), |
| 151 | ) |
| 152 | |
| 153 | if (typeof response === 'object' && response.hasOwnProperty('data')) { |
| 154 | errorBooking(response) |
| 155 | } else if (typeof response === 'object' && response.hasOwnProperty('message')) { |
| 156 | usePaymentError(store, function () { |
| 157 | emits('payment-error', response.message) |
| 158 | }) |
| 159 | } else { |
| 160 | usePaymentError(store, function () { |
| 161 | emits('payment-error', errorString) |
| 162 | }) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | function successBooking(response) { |
| 167 | useCreateBookingSuccess(store, response, function () { |
| 168 | nextStep() |
| 169 | }) |
| 170 | } |
| 171 | |
| 172 | // * Components Emits |
| 173 | const emits = defineEmits(['payment-error']) |
| 174 | |
| 175 | function errorBooking(error) { |
| 176 | useCreateBookingError(store, error, () => { |
| 177 | emits('payment-error', getErrorMessage()) |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | onMounted(() => { |
| 182 | payPalPaymentInit() |
| 183 | }) |
| 184 | </script> |
| 185 | |
| 186 | <script> |
| 187 | export default { |
| 188 | name: 'PaymentPayPal', |
| 189 | } |
| 190 | </script> |
| 191 | |
| 192 | <style lang="scss"> |
| 193 | .amelia-v2-booking { |
| 194 | #amelia-container { |
| 195 | .am-fs__payment_payPal { |
| 196 | height: 56px; |
| 197 | .paypal-button { |
| 198 | width: 160px; |
| 199 | border-radius: 9px; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | </style> |
| 205 |