diff --git a/.changeset/paypal-digital-no-shipping.md b/.changeset/paypal-digital-no-shipping.md new file mode 100644 index 000000000..305f3be08 --- /dev/null +++ b/.changeset/paypal-digital-no-shipping.md @@ -0,0 +1,5 @@ +--- +'@godaddy/react': patch +--- + +Request NO_SHIPPING from PayPal unless the buyer is shipping to a collected address. Digital, purchase-mode and pickup orders no longer send a country-code-only shipping stub, which PayPal rejected with POSTAL_CODE_REQUIRED. diff --git a/packages/react/src/components/checkout/payment/checkout-buttons/paypal/payment-request-resolution.test.tsx b/packages/react/src/components/checkout/payment/checkout-buttons/paypal/payment-request-resolution.test.tsx index 46a4a6d22..23261443b 100644 --- a/packages/react/src/components/checkout/payment/checkout-buttons/paypal/payment-request-resolution.test.tsx +++ b/packages/react/src/components/checkout/payment/checkout-buttons/paypal/payment-request-resolution.test.tsx @@ -153,7 +153,7 @@ it('creates and confirms PayPal with the request from the flushed latest order', billing: { name: { full_name: 'Latest Buyer' } }, }, ], - application_context: { shipping_preference: 'SET_PROVIDED_ADDRESS' }, + application_context: { shipping_preference: 'NO_SHIPPING' }, }); expect(mocks.createOrder.mock.invocationCallOrder[0]).toBeGreaterThan( mocks.flush.mock.invocationCallOrder[0] diff --git a/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.test.tsx b/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.test.tsx index 0759a8db7..df34723bc 100644 --- a/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.test.tsx +++ b/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.test.tsx @@ -21,16 +21,42 @@ vi.mock('@paypal/react-paypal-js', () => ({ }, })); +// The flush can settle without an order (e.g. the refetch fails), leaving the +// payment request to fall back to the cached draft order. +const flushResult = vi.hoisted(() => ({ forceNullLatestOrder: false })); +vi.mock( + '@/components/checkout/payment/utils/use-flush-checkout-sync', + async importOriginal => { + const actual = + await importOriginal< + typeof import('@/components/checkout/payment/utils/use-flush-checkout-sync') + >(); + return { + useFlushCheckoutSync: () => { + const flush = actual.useFlushCheckoutSync(); + return async (options?: Parameters[0]) => { + const result = await flush(options); + return flushResult.forceNullLatestOrder + ? { ...result, latestOrder: null } + : result; + }; + }, + }; + } +); + import { type CheckoutFormData, checkoutContext, } from '@/components/checkout/checkout'; +import { DeliveryMethods } from '@/components/checkout/delivery/delivery-methods'; import { DraftOrderSyncProvider } from '@/components/checkout/order/draft-order-sync-provider'; import { PayPalCheckoutButton } from '@/components/checkout/payment/checkout-buttons/paypal/paypal'; import { GoDaddyProvider } from '@/godaddy-provider'; import { buildCheckoutSession, buildDraftOrder, + buildLineItem, createTestQueryClient, getOperations, mockGodaddyApi, @@ -66,15 +92,27 @@ const PAYPAL_ORDER_ID = 'paypal-order-1'; let form: UseFormReturn | undefined; -function renderPayPalButton({ enableTips = true, tipAmount = 0 } = {}) { - const session = buildCheckoutSession({ enableTips }); - const draftOrder = buildDraftOrder(); +function renderPayPalButton({ + enableTips = true, + tipAmount = 0, + deliveryMethod, + sessionOverrides, + draftOrderOverrides, +}: { + enableTips?: boolean; + tipAmount?: number; + deliveryMethod?: DeliveryMethods; + sessionOverrides?: Parameters[0]; + draftOrderOverrides?: Parameters[0]; +} = {}) { + const session = buildCheckoutSession({ enableTips, ...sessionOverrides }); + const draftOrder = buildDraftOrder(draftOrderOverrides); mockGodaddyApi({ session, draftOrder }); const queryClient = createTestQueryClient(); function Harness() { const methods = useForm({ - defaultValues: { tipAmount } as CheckoutFormData, + defaultValues: { tipAmount, deliveryMethod } as CheckoutFormData, }); form = methods; @@ -143,6 +181,7 @@ describe('PayPalCheckoutButton', () => { setupCheckoutTestGlobals(); form = undefined; payPalButtonsProps = undefined; + flushResult.forceNullLatestOrder = false; }); afterEach(() => { @@ -255,4 +294,120 @@ describe('PayPalCheckoutButton', () => { expect(tipMinorUnitsInOrder(createdOrders[0])).toBe(0); expect(confirmInput()?.tipAmount).toBeUndefined(); }); + + describe('shipping preference', () => { + // An order that never collected a shipping address must not send PayPal a + // country-code-only stub: PayPal rejects it with POSTAL_CODE_REQUIRED. + const noShippingAddress = { shipping: { address: null } }; + + async function createPayPalOrder() { + const createdOrders: Array> = []; + await act(async () => { + await getPayPalButtonsProps().createOrder?.( + {}, + payPalActions(createdOrders) + ); + }); + const order = createdOrders[0]; + const purchaseUnit = ( + order.purchase_units as Array> + )[0]; + return { order, purchaseUnit }; + } + + function digitalLineItem() { + return { + ...buildLineItem(), + name: 'Digital Game', + type: DeliveryMethods.DIGITAL, + fulfillmentMode: DeliveryMethods.DIGITAL, + }; + } + + it('requests NO_SHIPPING for a digital-only order', async () => { + renderPayPalButton({ + deliveryMethod: DeliveryMethods.DIGITAL, + draftOrderOverrides: { + ...noShippingAddress, + lineItems: [digitalLineItem()], + }, + }); + + const { order, purchaseUnit } = await createPayPalOrder(); + + expect(purchaseUnit.shipping).toBeUndefined(); + expect(order.application_context).toMatchObject({ + shipping_preference: 'NO_SHIPPING', + }); + expect(purchaseUnit.items).toEqual([ + expect.objectContaining({ + name: 'Digital Game', + unit_amount: expect.objectContaining({ value: '25.00' }), + quantity: '1', + }), + ]); + }); + + it('requests NO_SHIPPING for a digital-only order when the flush returns no order', async () => { + flushResult.forceNullLatestOrder = true; + renderPayPalButton({ + deliveryMethod: DeliveryMethods.DIGITAL, + draftOrderOverrides: { + ...noShippingAddress, + lineItems: [digitalLineItem()], + }, + }); + + const { order, purchaseUnit } = await createPayPalOrder(); + + expect(purchaseUnit.shipping).toBeUndefined(); + expect(order.application_context).toMatchObject({ + shipping_preference: 'NO_SHIPPING', + }); + }); + + it('requests NO_SHIPPING in purchase mode', async () => { + // Shipping and pickup both disabled: a physical SKU is paid for without + // collecting any address. + renderPayPalButton({ + deliveryMethod: DeliveryMethods.PURCHASE, + sessionOverrides: { enableShipping: false, enableLocalPickup: false }, + draftOrderOverrides: noShippingAddress, + }); + + const { order, purchaseUnit } = await createPayPalOrder(); + + expect(purchaseUnit.shipping).toBeUndefined(); + expect(order.application_context).toMatchObject({ + shipping_preference: 'NO_SHIPPING', + }); + }); + + it('requests NO_SHIPPING for pickup even when the order kept a shipping address', async () => { + renderPayPalButton({ deliveryMethod: DeliveryMethods.PICKUP }); + + const { order, purchaseUnit } = await createPayPalOrder(); + + expect(purchaseUnit.shipping).toBeUndefined(); + expect(order.application_context).toMatchObject({ + shipping_preference: 'NO_SHIPPING', + }); + }); + + it('sends the collected address for a shipped order', async () => { + renderPayPalButton({ deliveryMethod: DeliveryMethods.SHIP }); + + const { order, purchaseUnit } = await createPayPalOrder(); + + expect(purchaseUnit.shipping).toMatchObject({ + address: expect.objectContaining({ + address_line_1: expect.any(String), + postal_code: expect.any(String), + }), + }); + expect(order.application_context).toMatchObject({ + shipping_preference: 'SET_PROVIDED_ADDRESS', + }); + }); + }); }); diff --git a/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.tsx b/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.tsx index 6eaac8384..c14d61eed 100644 --- a/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.tsx +++ b/packages/react/src/components/checkout/payment/checkout-buttons/paypal/paypal.tsx @@ -26,8 +26,6 @@ function PayPalButtonsWrapper() { const confirmCheckout = useConfirmCheckout(); const flushCheckoutSync = useFlushCheckoutSync(); const [isPaypalDisabled, setIsPaypalDisabled] = useState(false); - const deliveryMethod = form.watch('deliveryMethod'); - const isPickup = deliveryMethod === DeliveryMethods.PICKUP; const [{ isResolved, isPending }] = usePayPalScriptReducer(); // PayPal's popup can stay open while the tip changes underneath, so confirm // sends the tip `createOrder` submitted rather than the current form value. @@ -68,18 +66,26 @@ function PayPalButtonsWrapper() { const request = buildPaymentRequestsFromOrder( latestOrder ?? undefined ).payPalRequest; + + // Read after the flush so the method matches the order PayPal is sent. + // Pickup, digital and purchase mode never collect a shipping address. + const isShipping = + form.getValues('deliveryMethod') === DeliveryMethods.SHIP && + Boolean(request.purchase_units[0]?.shipping); const order = { ...request, purchase_units: request.purchase_units ? [ { ...request.purchase_units[0], - ...(isPickup ? { shipping: undefined } : {}), // Remove shipping if pickup + ...(isShipping ? {} : { shipping: undefined }), }, ] : undefined, application_context: { - shipping_preference: isPickup ? 'NO_SHIPPING' : 'SET_PROVIDED_ADDRESS', + shipping_preference: isShipping + ? 'SET_PROVIDED_ADDRESS' + : 'NO_SHIPPING', }, }; return await actions.order.create(order); diff --git a/packages/react/src/components/checkout/payment/utils/use-build-payment-request.ts b/packages/react/src/components/checkout/payment/utils/use-build-payment-request.ts index abacf2dbd..007680444 100644 --- a/packages/react/src/components/checkout/payment/utils/use-build-payment-request.ts +++ b/packages/react/src/components/checkout/payment/utils/use-build-payment-request.ts @@ -240,20 +240,22 @@ export function buildPaymentRequests({ const totalWithTipMinorUnits = totalMinorUnits + tipAmount; const countryCode = session?.shipping?.originAddress?.countryCode || 'US'; - const shippingAddress = { - name: { - full_name: - `${order?.shipping?.firstName || ''} ${order?.shipping?.lastName || ''}`.trim(), - }, - address: { - address_line_1: order?.shipping?.address?.addressLine1 || undefined, - address_line_2: order?.shipping?.address?.addressLine2 || undefined, - admin_area_2: order?.shipping?.address?.adminArea2 || undefined, - admin_area_1: order?.shipping?.address?.adminArea1 || undefined, - postal_code: order?.shipping?.address?.postalCode || undefined, - country_code: order?.shipping?.address?.countryCode || countryCode, - }, - }; + const shippingAddress = order?.shipping?.address?.addressLine1 + ? { + name: { + full_name: + `${order.shipping.firstName || ''} ${order.shipping.lastName || ''}`.trim(), + }, + address: { + address_line_1: order.shipping.address.addressLine1, + address_line_2: order.shipping.address.addressLine2 || undefined, + admin_area_2: order.shipping.address.adminArea2 || undefined, + admin_area_1: order.shipping.address.adminArea1 || undefined, + postal_code: order.shipping.address.postalCode || undefined, + country_code: order.shipping.address.countryCode || countryCode, + }, + } + : undefined; const billingAddress = { name: { full_name: @@ -568,7 +570,7 @@ export function buildPaymentRequests({ ] : [] ), - shipping: shippingAddress, + ...(shippingAddress ? { shipping: shippingAddress } : {}), billing: billingAddress, }, ],