_customer.js
4 months ago
_events.js
1 year ago
_razorpay_connect.js
1 month ago
_stripe_connect.js
1 year ago
main.js
3 weeks ago
_razorpay_connect.js
120 lines
| 1 | /* |
| 2 | * Copyright (c) 2024 LatePoint LLC. All rights reserved. |
| 3 | */ |
| 4 | |
| 5 | class LatepointRazorpayConnectFront { |
| 6 | |
| 7 | constructor() { |
| 8 | this.ready(); |
| 9 | } |
| 10 | |
| 11 | ready() { |
| 12 | jQuery(document).ready(() => { |
| 13 | |
| 14 | // BOOKING FORM — init payment method |
| 15 | jQuery('body').on('latepoint:initPaymentMethod', '.latepoint-booking-form-element', (e, data) => { |
| 16 | if (data.payment_method === 'razorpay_checkout') { |
| 17 | latepoint_add_action(data.callbacks_list, async () => { |
| 18 | return await this.createRazorpayOrder(jQuery(e.currentTarget)); |
| 19 | }); |
| 20 | } |
| 21 | }); |
| 22 | |
| 23 | // INVOICE PAYMENT FORM — init payment method |
| 24 | jQuery('body').on('latepoint:initOrderPaymentMethod', '.latepoint-transaction-payment-form', (e, data) => { |
| 25 | if (data.payment_processor === 'razorpay_connect' && data.payment_method === 'razorpay_checkout') { |
| 26 | latepoint_add_action(data.callbacks_list, async () => { |
| 27 | return await this.createRazorpayOrderForTransaction(jQuery(e.currentTarget)); |
| 28 | }); |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | async createRazorpayOrder($booking_form_element) { |
| 36 | let formData = latepoint_create_form_data( |
| 37 | $booking_form_element.find('.latepoint-form'), |
| 38 | latepoint_helper.razorpay_connect_route_create_order, |
| 39 | { booking_form_page_url: window.location.href } |
| 40 | ); |
| 41 | |
| 42 | let response = await jQuery.ajax({ |
| 43 | type: 'post', |
| 44 | dataType: 'json', |
| 45 | processData: false, |
| 46 | contentType: false, |
| 47 | url: latepoint_timestamped_ajaxurl(), |
| 48 | data: formData |
| 49 | }); |
| 50 | |
| 51 | if (response.status !== 'success') { |
| 52 | alert(response.message); |
| 53 | throw new Error(response.message); |
| 54 | } |
| 55 | |
| 56 | if (response.amount > 0) { |
| 57 | let options = Object.assign({}, response.options, { |
| 58 | handler: (rzpResponse) => { |
| 59 | $booking_form_element.find('input[name="cart[payment_token]"]').val(rzpResponse.razorpay_payment_id); |
| 60 | latepoint_trigger_next_btn($booking_form_element); |
| 61 | }, |
| 62 | modal: { |
| 63 | ondismiss: () => { |
| 64 | $booking_form_element.find('.latepoint-prev-btn').trigger('click'); |
| 65 | } |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | let rzp = new Razorpay(options); |
| 70 | rzp.open(); |
| 71 | } else { |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | async createRazorpayOrderForTransaction($transaction_form) { |
| 77 | let formData = latepoint_create_form_data( |
| 78 | $transaction_form, |
| 79 | latepoint_helper.razorpay_connect_route_create_order_for_transaction |
| 80 | ); |
| 81 | |
| 82 | let response = await jQuery.ajax({ |
| 83 | type: 'post', |
| 84 | dataType: 'json', |
| 85 | processData: false, |
| 86 | contentType: false, |
| 87 | url: latepoint_timestamped_ajaxurl(), |
| 88 | data: formData |
| 89 | }); |
| 90 | |
| 91 | if (response.status !== 'success') { |
| 92 | alert(response.message); |
| 93 | throw new Error(response.message); |
| 94 | } |
| 95 | |
| 96 | if (response.amount > 0) { |
| 97 | let options = Object.assign({}, response.options, { |
| 98 | handler: async (rzpResponse) => { |
| 99 | $transaction_form.find('input[name="payment_token"]').val(rzpResponse.razorpay_payment_id); |
| 100 | return await $transaction_form.trigger('submit'); |
| 101 | }, |
| 102 | modal: { |
| 103 | ondismiss: () => { |
| 104 | let $access_key = $transaction_form.find('input[name="key"]').val(); |
| 105 | show_summary_before_payment($access_key); |
| 106 | } |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | let rzp = new Razorpay(options); |
| 111 | rzp.open(); |
| 112 | } else { |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | if (latepoint_helper.is_razorpay_connect_enabled) window.latepointRazorpayConnectFront = new LatepointRazorpayConnectFront(); |
| 120 |