PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / assets / src / js / frontend / profile / order-refund.js

order-refund.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at assets/src/js/frontend/profile/order-refund.js

205 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import SweetAlert from 'sweetalert2';
2 import * as lpToastify from '../../lpToastify';
3 import * as lpUtils from '../../utils.js';
4
5 /**
6 * Order Refund Script
7 *
8 * Handle refund action on profile orders list.
9 *
10 * @since 4.3.5
11 * @version 1.0.0
12 */
13 export class OrderRefund {
14 constructor() {
15 this.isRequesting = false;
16 }
17
18 static selectors = {
19 actionRefund: '.lp-refund-order-action',
20 };
21
22 init() {
23 this.events();
24 }
25
26 events() {
27 if ( OrderRefund._loadedEvents ) {
28 return;
29 }
30
31 OrderRefund._loadedEvents = this;
32
33 lpUtils.eventHandlers( 'click', [
34 {
35 selector: OrderRefund.selectors.actionRefund,
36 class: this,
37 callBack: this.handleRefundClick.name,
38 },
39 ] );
40 }
41
42 getAjaxHandle() {
43 const ajaxHandle = window.lpAJAXG;
44 if ( ! ajaxHandle || typeof ajaxHandle.fetchAJAX !== 'function' ) {
45 return null;
46 }
47
48 return ajaxHandle;
49 }
50
51 setActionLoadingState( actionLink, isLoading ) {
52 if ( ! actionLink ) {
53 return;
54 }
55
56 if ( isLoading ) {
57 actionLink.dataset.refundSubmitting = 'yes';
58 } else {
59 delete actionLink.dataset.refundSubmitting;
60 }
61
62 lpUtils.lpSetLoadingEl( actionLink, isLoading ? 1 : 0 );
63 }
64
65 getActionData( actionLink ) {
66 const reasonMin = parseInt( actionLink.dataset.reasonMin || '10', 10 );
67
68 return {
69 orderId: parseInt( actionLink.dataset.orderId || '0', 10 ),
70 requireReason: actionLink.dataset.requireReason === 'yes',
71 reasonMin: Number.isNaN( reasonMin ) ? 10 : reasonMin,
72 reasonPrompt: actionLink.dataset.reasonPrompt || '',
73 reasonPlaceholder: actionLink.dataset.reasonPlaceholder || '',
74 reasonRequired: actionLink.dataset.reasonRequired || '',
75 confirmTitle: actionLink.dataset.confirmTitle || '',
76 confirmText: actionLink.dataset.confirmText || '',
77 confirmButton: actionLink.dataset.confirmButton || '',
78 cancelButton: actionLink.dataset.cancelButton || '',
79 };
80 }
81
82 openReasonModal( data ) {
83 return SweetAlert.fire( {
84 title: data.reasonPrompt,
85 input: 'textarea',
86 inputPlaceholder: data.reasonPlaceholder,
87 inputAutoTrim: true,
88 showCancelButton: true,
89 confirmButtonText: data.confirmButton,
90 cancelButtonText: data.cancelButton,
91 inputValidator: ( value ) => {
92 const reason = ( value || '' ).trim();
93 if ( ! reason.length ) {
94 return data.reasonRequired;
95 }
96
97 return undefined;
98 },
99 } );
100 }
101
102 openConfirmModal( data ) {
103 return SweetAlert.fire( {
104 icon: 'warning',
105 title: data.confirmTitle,
106 text: data.confirmText,
107 showCancelButton: true,
108 confirmButtonText: data.confirmButton,
109 cancelButtonText: data.cancelButton,
110 } );
111 }
112
113 sendRefundRequest( actionLink, data, reason = '' ) {
114 const ajaxHandle = this.getAjaxHandle();
115 if ( ! ajaxHandle ) {
116 lpToastify.show( 'Refund action is unavailable right now.', 'error' );
117 return;
118 }
119
120 if ( ! data.orderId ) {
121 lpToastify.show( 'Invalid order.', 'error' );
122 return;
123 }
124
125 this.isRequesting = true;
126 this.setActionLoadingState( actionLink, true );
127
128 const dataSend = {
129 action: 'request_refund_order',
130 order_id: data.orderId,
131 reason,
132 };
133
134 ajaxHandle.fetchAJAX( dataSend, {
135 success: ( response ) => {
136 const { status, message, data } = response;
137
138 if ( status !== 'success' ) {
139 throw new Error( message );
140 }
141
142 lpToastify.show( message, 'success' );
143 setTimeout( () => {
144 window.location.reload();
145 }, 1200 );
146 },
147 error: ( error ) => {
148 const message = error?.message || error || 'Refund request failed.';
149 lpToastify.show( message, 'error' );
150 },
151 completed: () => {
152 this.isRequesting = false;
153 this.setActionLoadingState( actionLink, false );
154 },
155 } );
156 }
157
158 async handleRefundClick( args ) {
159 const { e, target } = args;
160 e.preventDefault();
161
162 const actionLink = target.closest( OrderRefund.selectors.actionRefund );
163 if ( ! actionLink ) {
164 return;
165 }
166
167 if (
168 this.isRequesting ||
169 actionLink.dataset.refundSubmitting === 'yes' ||
170 actionLink.classList.contains( 'loading' )
171 ) {
172 return;
173 }
174
175 const actionData = this.getActionData( actionLink );
176 let reason = '';
177
178 if ( actionData.requireReason ) {
179 const reasonResult = await this.openReasonModal( actionData );
180 if ( ! reasonResult.isConfirmed ) {
181 return;
182 }
183
184 reason = ( reasonResult.value || '' ).trim();
185 }
186
187 const confirmResult = await this.openConfirmModal( actionData );
188 if ( ! confirmResult.isConfirmed ) {
189 return;
190 }
191
192 this.sendRefundRequest( actionLink, actionData, reason );
193 }
194 }
195
196 const orderRefund = () => {
197 const orderRefundHandle = new OrderRefund();
198
199 lpUtils.lpOnElementReady( OrderRefund.selectors.actionRefund, () => {
200 orderRefundHandle.init();
201 } );
202 };
203
204 export default orderRefund;
205