appointments.js
2 days ago
attendees.js
1 month ago
colorManipulation.js
1 month ago
customer.js
1 month ago
date.js
1 month ago
defaultCustomize.js
1 month ago
employee.js
1 week ago
events.js
1 month ago
formFieldsTemplates.js
1 month ago
formatting.js
1 month ago
helper.js
1 week ago
image.js
1 month ago
integrationApple.js
1 month ago
integrationGoogle.js
1 month ago
integrationOutlook.js
1 month ago
integrationRazorpay.js
2 days ago
integrationStripe.js
1 month ago
integrationZoom.js
1 month ago
licence.js
1 month ago
liveAnnouncer.js
1 month ago
phone.js
1 month ago
pricing.js
1 month ago
recurring.js
1 week ago
responsive.js
1 month ago
scrollElements.js
1 month ago
settings.js
1 month ago
translationsElementPlus.js
2 days ago
utilHeaderHeight.js
1 month ago
whiteLabel.js
2 days ago
integrationRazorpay.js
177 lines
| 1 | import httpClient from '@/plugins/axios' |
| 2 | import { |
| 3 | useCreateBookingSuccess, |
| 4 | useCreateBookingError, |
| 5 | getErrorMessage, |
| 6 | } from '@/assets/js/public/booking.js' |
| 7 | |
| 8 | const STATUS_CHECK_FAILED = 'error' |
| 9 | |
| 10 | /** |
| 11 | * Persist deferred-gateway booking ids into sessionStorage before redirect/checkout. |
| 12 | * |
| 13 | * @param {object} response |
| 14 | * @param {{ includeName?: boolean }} [options] |
| 15 | */ |
| 16 | export function storeDeferredGatewayCache(response, options = {}) { |
| 17 | const saved = sessionStorage.getItem('ameliaCacheData') |
| 18 | if (!saved || saved === 'null') { |
| 19 | return |
| 20 | } |
| 21 | |
| 22 | const ameliaCacheData = JSON.parse(saved) |
| 23 | if (!ameliaCacheData) { |
| 24 | return |
| 25 | } |
| 26 | |
| 27 | ameliaCacheData.bookings = response.data.data.bookings |
| 28 | ameliaCacheData.packageCustomer = response.data.data.packageCustomer |
| 29 | |
| 30 | if (options.includeName || response.data.data.name) { |
| 31 | ameliaCacheData.name = response.data.data.name |
| 32 | } |
| 33 | |
| 34 | sessionStorage.setItem('ameliaCacheData', JSON.stringify(ameliaCacheData)) |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param {object} response |
| 39 | */ |
| 40 | export function storeRazorpayCache(response) { |
| 41 | storeDeferredGatewayCache(response, { includeName: true }) |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param {number|string} id |
| 46 | * @param {string} type |
| 47 | * @param {string} token |
| 48 | * @returns {Promise<boolean|'error'>} |
| 49 | */ |
| 50 | async function isRemotelyPendingBooking(id, type, token) { |
| 51 | try { |
| 52 | const response = await httpClient.post('/bookings/delete/remotely/' + id, { |
| 53 | skipEventHandler: true, |
| 54 | type, |
| 55 | token, |
| 56 | statusOnly: true, |
| 57 | }) |
| 58 | |
| 59 | return response?.data?.data?.pending === true |
| 60 | } catch (e) { |
| 61 | console.log(e.message) |
| 62 | return STATUS_CHECK_FAILED |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param {number|string} id |
| 68 | * @param {string} type |
| 69 | * @param {string} token |
| 70 | * @returns {Promise<boolean|'error'>} true when deleted, false when confirmed non-pending, 'error' on failure |
| 71 | */ |
| 72 | async function deleteRemotelyIfPending(id, type, token) { |
| 73 | const pending = await isRemotelyPendingBooking(id, type, token) |
| 74 | if (pending === STATUS_CHECK_FAILED) { |
| 75 | return STATUS_CHECK_FAILED |
| 76 | } |
| 77 | |
| 78 | if (!pending) { |
| 79 | return false |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | await httpClient.post('/bookings/delete/remotely/' + id, { |
| 84 | skipEventHandler: true, |
| 85 | type, |
| 86 | token, |
| 87 | }) |
| 88 | return true |
| 89 | } catch (e) { |
| 90 | console.log(e.message) |
| 91 | return STATUS_CHECK_FAILED |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Delete only bookings that are still pending on the server after Razorpay dismiss. |
| 97 | * Clears session cache only when every booking is confirmed non-pending or deleted. |
| 98 | * |
| 99 | * @returns {Promise<boolean>} |
| 100 | */ |
| 101 | export async function deletePendingRazorpayBookings() { |
| 102 | const savedData = sessionStorage.getItem('ameliaCacheData') |
| 103 | if (!savedData || savedData === 'null') { |
| 104 | return false |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | const data = JSON.parse(savedData) |
| 109 | let deletedPending = false |
| 110 | let requestFailed = false |
| 111 | |
| 112 | if (data.packageCustomer) { |
| 113 | const result = await deleteRemotelyIfPending( |
| 114 | data.packageCustomer.id, |
| 115 | 'package', |
| 116 | data.packageCustomer.token, |
| 117 | ) |
| 118 | if (result === STATUS_CHECK_FAILED) { |
| 119 | requestFailed = true |
| 120 | } else if (result === true) { |
| 121 | deletedPending = true |
| 122 | } |
| 123 | } else if (data.bookings) { |
| 124 | for (const booking of data.bookings) { |
| 125 | const result = await deleteRemotelyIfPending(booking.id, data.type, booking.token) |
| 126 | if (result === STATUS_CHECK_FAILED) { |
| 127 | requestFailed = true |
| 128 | break |
| 129 | } |
| 130 | if (result === true) { |
| 131 | deletedPending = true |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (!requestFailed) { |
| 137 | sessionStorage.removeItem('ameliaCacheData') |
| 138 | } |
| 139 | |
| 140 | return !requestFailed && deletedPending |
| 141 | } catch (e) { |
| 142 | console.log(e.message) |
| 143 | return false |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Finalize Razorpay checkout via notify callback. |
| 149 | * |
| 150 | * @param {object} store |
| 151 | * @param {string} paymentId |
| 152 | * @param {string} signature |
| 153 | * @param {string} orderId |
| 154 | * @param {string} cacheName |
| 155 | * @param {{ onSuccess: Function, onError: Function }} callbacks |
| 156 | */ |
| 157 | export function razorpayVerify(store, paymentId, signature, orderId, cacheName, callbacks) { |
| 158 | httpClient |
| 159 | .post('/payment/razorpay/notify', { |
| 160 | name: cacheName, |
| 161 | paymentId: paymentId, |
| 162 | signature: signature, |
| 163 | orderId: orderId, |
| 164 | }) |
| 165 | .then((response) => { |
| 166 | sessionStorage.removeItem('ameliaCacheData') |
| 167 | useCreateBookingSuccess(store, response, () => { |
| 168 | callbacks.onSuccess(response) |
| 169 | }) |
| 170 | }) |
| 171 | .catch((error) => { |
| 172 | useCreateBookingError(store, error.response ? error.response.data : {}, () => { |
| 173 | callbacks.onError(getErrorMessage()) |
| 174 | }) |
| 175 | }) |
| 176 | } |
| 177 |