Skip to content

Commit 1ba3de8

Browse files
committed
Fix edge case when shipping information is added by mistake for digital products
1 parent 7160996 commit 1ba3de8

File tree

2 files changed

+74
-23
lines changed

2 files changed

+74
-23
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fix
3+
4+
Fix edge case when shipping information is added by mistake for digital products.

client/disputes/new-evidence/index.tsx

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,21 @@ export default ( { query }: { query: { id: string } } ) => {
397397
setIsAccordionOpen( currentStep === 0 );
398398
}, [ currentStep ] );
399399

400+
// Clear shipping information when shipping is not needed
401+
useEffect( () => {
402+
if ( ! hasShipping ) {
403+
setShippingCarrier( '' );
404+
setShippingDate( '' );
405+
setShippingTrackingNumber( '' );
406+
setShippingAddress( '' );
407+
// Clear shipping documentation from evidence
408+
setEvidence( ( prev: any ) => ( {
409+
...prev,
410+
shipping_documentation: '',
411+
} ) );
412+
}
413+
}, [ hasShipping ] );
414+
400415
// --- File upload logic ---
401416
const isUploadingEvidence = () =>
402417
Object.values( isUploading ).some( Boolean );
@@ -462,30 +477,62 @@ export default ( { query }: { query: { id: string } } ) => {
462477
: 'wcpay_dispute_save_evidence_clicked'
463478
);
464479

465-
// Only include file keys in the evidence object if they have a non-empty value
480+
// Build base evidence object
481+
const baseEvidence = {
482+
...dispute.evidence,
483+
product_description: productDescription,
484+
receipt: evidence.receipt,
485+
customer_communication: evidence.customer_communication,
486+
customer_signature: evidence.customer_signature,
487+
refund_policy: evidence.refund_policy,
488+
duplicate_charge_documentation:
489+
evidence.duplicate_charge_documentation,
490+
service_documentation: evidence.service_documentation,
491+
cancellation_policy: evidence.cancellation_policy,
492+
access_activity_log: evidence.access_activity_log,
493+
uncategorized_file: evidence.uncategorized_file,
494+
uncategorized_text: coverLetter,
495+
customer_purchase_ip: dispute.order?.ip_address,
496+
};
497+
498+
// Only include shipping information if shipping is needed
499+
if ( hasShipping ) {
500+
baseEvidence.shipping_documentation =
501+
evidence.shipping_documentation;
502+
baseEvidence.shipping_carrier = shippingCarrier;
503+
baseEvidence.shipping_date = shippingDate;
504+
baseEvidence.shipping_tracking_number = shippingTrackingNumber;
505+
baseEvidence.shipping_address = shippingAddress;
506+
} else {
507+
// Clear shipping information when not needed
508+
baseEvidence.shipping_documentation = '';
509+
baseEvidence.shipping_carrier = '';
510+
baseEvidence.shipping_date = '';
511+
baseEvidence.shipping_tracking_number = '';
512+
baseEvidence.shipping_address = '';
513+
}
514+
515+
// Define shipping field keys that need special handling
516+
// These fields must always be sent to Stripe (even when empty) to clear existing data when shipping is not needed
517+
const shippingFieldKeys = [
518+
'shipping_documentation',
519+
'shipping_carrier',
520+
'shipping_date',
521+
'shipping_tracking_number',
522+
'shipping_address',
523+
];
524+
525+
// Filter evidence: include shipping fields even if empty (to clear them),
526+
// but filter out other empty fields
466527
const evidenceToSend = Object.fromEntries(
467-
Object.entries( {
468-
...dispute.evidence,
469-
product_description: productDescription,
470-
receipt: evidence.receipt,
471-
customer_communication: evidence.customer_communication,
472-
customer_signature: evidence.customer_signature,
473-
refund_policy: evidence.refund_policy,
474-
duplicate_charge_documentation:
475-
evidence.duplicate_charge_documentation,
476-
shipping_documentation: evidence.shipping_documentation,
477-
service_documentation: evidence.service_documentation,
478-
cancellation_policy: evidence.cancellation_policy,
479-
access_activity_log: evidence.access_activity_log,
480-
uncategorized_file: evidence.uncategorized_file,
481-
uncategorized_text: coverLetter,
482-
// Add shipping details
483-
shipping_carrier: shippingCarrier,
484-
shipping_date: shippingDate,
485-
shipping_tracking_number: shippingTrackingNumber,
486-
shipping_address: shippingAddress,
487-
customer_purchase_ip: dispute.order?.ip_address,
488-
} ).filter( ( [ , value ] ) => value && value !== '' )
528+
Object.entries( baseEvidence ).filter( ( [ key, value ] ) => {
529+
// Always include shipping fields (even if empty) to ensure they're cleared on Stripe
530+
if ( shippingFieldKeys.includes( key ) ) {
531+
return true;
532+
}
533+
// For non-shipping fields, only include if they have a value
534+
return value && value !== '';
535+
} )
489536
);
490537

491538
// Update metadata with the current productType

0 commit comments

Comments
 (0)