PaymentCommon.vue
6 days ago
PaymentOnSite.vue
1 month ago
PaymentPayPal.vue
6 days ago
PaymentSquare.vue
6 days ago
PaymentStripe.vue
6 days ago
PaymentWc.vue
1 month ago
PaymentPayPal.vue
220 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(async (r) => { |
| 87 | const body = await r.json().catch(() => null) |
| 88 | |
| 89 | if (!r.ok) { |
| 90 | return Promise.reject(body || { message: 'HTTP ' + r.status }) |
| 91 | } |
| 92 | |
| 93 | return body |
| 94 | }) |
| 95 | .then((response) => { |
| 96 | transactionReference = response.data.transactionReference |
| 97 | return response.data.paymentID |
| 98 | }) |
| 99 | .catch((error) => { |
| 100 | payPalError(error) |
| 101 | return Promise.reject(error) |
| 102 | }) |
| 103 | }, |
| 104 | |
| 105 | onApprove: function (data) { |
| 106 | store.commit('setLoading', true) |
| 107 | |
| 108 | useCreateBooking( |
| 109 | store, |
| 110 | useBookingData( |
| 111 | store, |
| 112 | null, |
| 113 | false, |
| 114 | { |
| 115 | transactionReference: transactionReference, |
| 116 | PaymentId: data.orderID, |
| 117 | PayerId: data.payerID, |
| 118 | }, |
| 119 | null, |
| 120 | ), |
| 121 | successBooking, |
| 122 | (error) => { |
| 123 | useCreateBookingError(store, error.response.data, () => { |
| 124 | emits('payment-error', getErrorMessage()) |
| 125 | }) |
| 126 | }, |
| 127 | ) |
| 128 | }, |
| 129 | |
| 130 | onCancel: function () { |
| 131 | store.commit('setLoading', false) |
| 132 | }, |
| 133 | |
| 134 | onError: function (error) { |
| 135 | payPalError(error) |
| 136 | }, |
| 137 | }) |
| 138 | } catch (e) { |
| 139 | setTimeout(payPalPaymentInit, 100) |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | if (btn && btn.render) { |
| 144 | btn.render(selector).catch(function (e) {}) |
| 145 | } else { |
| 146 | setTimeout(payPalPaymentInit, 100) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function payPalError(error) { |
| 151 | if (error && typeof error === 'object' && !(error instanceof Error)) { |
| 152 | errorBooking(error) |
| 153 | return |
| 154 | } |
| 155 | |
| 156 | let errorString = error.toString() |
| 157 | |
| 158 | try { |
| 159 | let response = JSON.parse( |
| 160 | errorString.substring(errorString.indexOf('{'), errorString.lastIndexOf('}') + 1), |
| 161 | ) |
| 162 | |
| 163 | if (typeof response === 'object' && response.hasOwnProperty('data')) { |
| 164 | errorBooking(response) |
| 165 | } else if (typeof response === 'object' && response.hasOwnProperty('message')) { |
| 166 | usePaymentError(store, function () { |
| 167 | emits('payment-error', response.message) |
| 168 | }) |
| 169 | } else { |
| 170 | usePaymentError(store, function () { |
| 171 | emits('payment-error', errorString) |
| 172 | }) |
| 173 | } |
| 174 | } catch (e) { |
| 175 | usePaymentError(store, function () { |
| 176 | emits('payment-error', errorString) |
| 177 | }) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | function successBooking(response) { |
| 182 | useCreateBookingSuccess(store, response, function () { |
| 183 | nextStep() |
| 184 | }) |
| 185 | } |
| 186 | |
| 187 | // * Components Emits |
| 188 | const emits = defineEmits(['payment-error']) |
| 189 | |
| 190 | function errorBooking(error) { |
| 191 | useCreateBookingError(store, error, () => { |
| 192 | emits('payment-error', getErrorMessage()) |
| 193 | }) |
| 194 | } |
| 195 | |
| 196 | onMounted(() => { |
| 197 | payPalPaymentInit() |
| 198 | }) |
| 199 | </script> |
| 200 | |
| 201 | <script> |
| 202 | export default { |
| 203 | name: 'PaymentPayPal', |
| 204 | } |
| 205 | </script> |
| 206 | |
| 207 | <style lang="scss"> |
| 208 | .amelia-v2-booking { |
| 209 | #amelia-container { |
| 210 | .am-fs__payment_payPal { |
| 211 | height: 56px; |
| 212 | .paypal-button { |
| 213 | width: 160px; |
| 214 | border-radius: 9px; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | </style> |
| 220 |