-
Notifications
You must be signed in to change notification settings - Fork 69
/
index.js
344 lines (305 loc) · 9.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* global jQuery */
import { __, _n, sprintf } from '@wordpress/i18n';
import { dateI18n } from '@wordpress/date';
import ReactDOM from 'react-dom';
import { dispatch } from '@wordpress/data';
import moment from 'moment';
import { createInterpolateElement } from '@wordpress/element';
/**
* Internal dependencies
*/
import { getConfig } from 'utils/order';
import RefundConfirmationModal from './refund-confirm-modal';
import CancelConfirmationModal from './cancel-confirm-modal';
import InlineNotice from 'components/inline-notice';
import { formatExplicitCurrency } from 'utils/currency';
import { reasons } from 'wcpay/disputes/strings';
import { getDetailsURL } from 'wcpay/components/details-link';
import {
isAwaitingResponse,
isInquiry,
isUnderReview,
} from 'wcpay/disputes/utils';
import { useCharge } from 'wcpay/data';
import wcpayTracks from 'tracks';
import './style.scss';
jQuery( function ( $ ) {
const disableManualRefunds = getConfig( 'disableManualRefunds' ) ?? false;
const manualRefundsTip = getConfig( 'manualRefundsTip' ) ?? '';
const chargeId = getConfig( 'chargeId' );
maybeShowDisputeNotice();
$( '#woocommerce-order-items' ).on(
'click',
'button.refund-items',
function () {
const $manualRefundButton = $( '.do-manual-refund' );
if ( disableManualRefunds ) {
$manualRefundButton.hide();
} else {
// Adjust the messaging on the manual refund button.
$manualRefundButton
.attr( {
// Tips are readable through $.data(), but jQuery.tipTip use the title attribute to generate
// the tooltip.
title: manualRefundsTip,
} )
// Regenerate the tipTip tooltip.
.tipTip();
}
}
);
$( 'select#order_status' ).on( 'change', function () {
const originalStatus = $( 'input#original_post_status' ).val();
const canRefund = getConfig( 'canRefund' );
const refundAmount = getConfig( 'refundAmount' );
if (
this.value === 'wc-refunded' &&
originalStatus !== 'wc-refunded'
) {
renderRefundConfirmationModal(
originalStatus,
canRefund,
refundAmount
);
} else if (
this.value === 'wc-cancelled' &&
originalStatus !== 'wc-cancelled'
) {
if ( ! canRefund || refundAmount <= 0 ) {
return;
}
renderModal(
<CancelConfirmationModal
originalOrderStatus={ originalStatus }
/>
);
}
} );
function renderRefundConfirmationModal(
originalStatus,
canRefund,
refundAmount
) {
if ( ! canRefund ) {
dispatch( 'core/notices' ).createErrorNotice(
__( 'Order cannot be refunded', 'woocommerce-payments' )
);
return;
}
if ( refundAmount <= 0 ) {
dispatch( 'core/notices' ).createErrorNotice(
__( 'Invalid Refund Amount', 'woocommerce-payments' )
);
return;
}
renderModal(
<RefundConfirmationModal
orderStatus={ originalStatus }
refundAmount={ refundAmount }
formattedRefundAmount={ getConfig( 'formattedRefundAmount' ) }
refundedAmount={ getConfig( 'refundedAmount' ) }
/>
);
}
function renderModal( modalToRender ) {
const container = document.createElement( 'div' );
container.id = 'wcpay-orderstatus-confirm-container';
document.body.appendChild( container );
ReactDOM.render( modalToRender, container );
}
function maybeShowDisputeNotice() {
const container = document.querySelector(
'#wcpay-order-payment-details-container'
);
// If the container doesn't exist (WC < 7.9), or the charge ID isn't present, don't render the notice.
if ( ! container || ! chargeId ) {
return;
}
ReactDOM.render( <DisputeNotice chargeId={ chargeId } />, container );
}
} );
const DisputeNotice = ( { chargeId } ) => {
const { data: charge } = useCharge( chargeId );
if ( ! charge?.dispute ) {
return null;
}
const { dispute } = charge;
let urgency = 'warning';
let actions;
// Refunds are only allowed if the dispute is an inquiry or if it's won.
const isRefundable =
isInquiry( dispute ) || [ 'won' ].includes( dispute.status );
const shouldDisableRefund = ! isRefundable;
let disableRefund = false;
let refundDisabledNotice = '';
if ( shouldDisableRefund ) {
const refundButton = document.querySelector( 'button.refund-items' );
if ( refundButton ) {
disableRefund = true;
// Disable the refund button.
refundButton.disabled = true;
const disputeDetailsLink = getDetailsURL( dispute.id, 'disputes' );
let tooltipText = '';
if ( isAwaitingResponse( dispute.status ) ) {
refundDisabledNotice = __(
'Refunds and order editing are disabled during disputes.',
'woocommerce-payments'
);
tooltipText = refundDisabledNotice;
} else if ( isUnderReview( dispute.status ) ) {
refundDisabledNotice = createInterpolateElement(
__(
// eslint-disable-next-line max-len
'This order has an active payment dispute. Refunds and order editing are disabled during this time. <a>View details</a>',
'woocommerce-payments'
),
{
// eslint-disable-next-line jsx-a11y/anchor-has-content
a: <a href={ disputeDetailsLink } />,
}
);
tooltipText = __(
'Refunds and order editing are disabled during an active dispute.',
'woocommerce-payments'
);
} else if ( dispute.status === 'lost' ) {
refundDisabledNotice = createInterpolateElement(
__(
'Refunds and order editing have been disabled as a result of a lost dispute. <a>View details</a>',
'woocommerce-payments'
),
{
// eslint-disable-next-line jsx-a11y/anchor-has-content
a: <a href={ disputeDetailsLink } />,
}
);
tooltipText = __(
'Refunds and order editing have been disabled as a result of a lost dispute.',
'woocommerce-payments'
);
}
// Change refund tooltip's text copy.
jQuery( refundButton )
.parent()
.find( '.woocommerce-help-tip' )
.attr( {
// jQuery.tipTip uses the title attribute to generate the tooltip.
title: tooltipText,
'aria-label': tooltipText,
} )
// Regenerate the tipTip tooltip.
.tipTip();
}
}
let showWarning = false;
let warningText = '';
if (
dispute.evidence_details?.due_by &&
// Only show the notice if the dispute is awaiting a response.
isAwaitingResponse( dispute.status )
) {
const now = moment();
const dueBy = moment.unix( dispute.evidence_details?.due_by );
const countdownDays = Math.floor( dueBy.diff( now, 'days', true ) );
// If the dispute is due in the past, we don't want to show the notice.
if ( now.isBefore( dueBy ) ) {
showWarning = true;
const titleStrings = {
// Translators: %1$s is the formatted dispute amount, %2$s is the dispute reason, %3$s is the due date.
dispute_default: __(
// eslint-disable-next-line max-len
'This order has been disputed in the amount of %1$s. The customer provided the following reason: %2$s. Please respond to this dispute before %3$s.',
'woocommerce-payments'
),
// Translators: %1$s is the formatted dispute amount, %2$s is the dispute reason, %3$s is the due date.
inquiry_default: __(
// eslint-disable-next-line max-len
'The card network involved in this order has opened an inquiry into the transaction with the following reason: %2$s. Please respond to this inquiry before %3$s, just like you would for a formal dispute.',
'woocommerce-payments'
),
// Translators: %1$s is the formatted dispute amount, %2$s is the dispute reason, %3$s is the due date.
dispute_urgent: __(
'Please resolve the dispute on this order for %1$s labeled "%2$s" by %3$s.',
'woocommerce-payments'
),
// Translators: %1$s is the formatted dispute amount, %2$s is the dispute reason, %3$s is the due date.
inquiry_urgent: __(
'Please resolve the inquiry on this order for %1$s labeled "%2$s" by %3$s.',
'woocommerce-payments'
),
};
const amountFormatted = formatExplicitCurrency(
dispute.amount,
dispute.currency
);
let buttonLabel = __( 'Respond now', 'woocommerce-payments' );
let suffix = '';
let titleText = isInquiry( dispute )
? titleStrings.inquiry_default
: titleStrings.dispute_default;
// If the dispute is due within 7 days, use different wording.
if ( countdownDays < 7 ) {
titleText = isInquiry( dispute )
? titleStrings.inquiry_urgent
: titleStrings.dispute_urgent;
suffix = sprintf(
// Translators: %s is the number of days left to respond to the dispute.
_n(
'(%s day left)',
'(%s days left)',
countdownDays,
'woocommerce-payments'
),
countdownDays
);
}
const title = sprintf(
titleText,
amountFormatted,
reasons[ dispute.reason ].display,
dateI18n( 'M j, Y', dueBy.local().toISOString() )
);
// If the dispute is due within 72 hours, we want to highlight it as urgent/red.
if ( countdownDays < 3 ) {
urgency = 'error';
}
if ( countdownDays < 1 ) {
buttonLabel = __( 'Respond today', 'woocommerce-payments' );
suffix = __( '(Last day today)', 'woocommerce-payments' );
}
actions = [
{
label: buttonLabel,
variant: 'secondary',
onClick: () => {
wcpayTracks.recordEvent(
wcpayTracks.events
.ORDER_DISPUTE_NOTICE_BUTTON_CLICK,
{
due_by_days: parseInt( countdownDays, 10 ),
}
);
window.location = getDetailsURL(
dispute.id,
'disputes'
);
},
},
];
warningText = `${ title } ${ suffix }`;
}
}
if ( ! showWarning && ! disableRefund ) {
return null;
}
return (
<InlineNotice
status={ urgency }
isDismissible={ false }
actions={ actions }
>
{ showWarning && <strong>{ warningText }</strong> }
{ disableRefund && <div>{ refundDisabledNotice }</div> }
</InlineNotice>
);
};