From 4183aac6ec9a3e9873dcf950ffcbb233a70a07a0 Mon Sep 17 00:00:00 2001 From: Dhruv Gupta Date: Wed, 18 Nov 2026 12:50:22 +0530 Subject: [PATCH 1/3] Request NO_SHIPPING from PayPal for digital-only orders Co-authored-by: Cursor --- .../checkout-buttons/paypal/paypal.test.tsx | 49 ++++++++++++++++++- .../checkout-buttons/paypal/paypal.tsx | 10 +++- 2 files changed, 55 insertions(+), 4 deletions(-) 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..b853e87a0 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 @@ -25,6 +25,7 @@ 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'; @@ -66,9 +67,17 @@ const PAYPAL_ORDER_ID = 'paypal-order-1'; let form: UseFormReturn | undefined; -function renderPayPalButton({ enableTips = true, tipAmount = 0 } = {}) { +function renderPayPalButton({ + enableTips = true, + tipAmount = 0, + draftOrderOverrides, +}: { + enableTips?: boolean; + tipAmount?: number; + draftOrderOverrides?: Parameters[0]; +} = {}) { const session = buildCheckoutSession({ enableTips }); - const draftOrder = buildDraftOrder(); + const draftOrder = buildDraftOrder(draftOrderOverrides); mockGodaddyApi({ session, draftOrder }); const queryClient = createTestQueryClient(); @@ -255,4 +264,40 @@ describe('PayPalCheckoutButton', () => { expect(tipMinorUnitsInOrder(createdOrders[0])).toBe(0); expect(confirmInput()?.tipAmount).toBeUndefined(); }); + + it('requests NO_SHIPPING and omits shipping for a digital-only order', async () => { + // A digital-only order never collects a shipping address, so the stub + // (country-code-only) shipping object use-build-payment-request.ts builds + // must be dropped here, same as the pickup case already was — otherwise + // PayPal rejects it with POSTAL_CODE_REQUIRED for an address we never + // asked the buyer for. + renderPayPalButton({ + draftOrderOverrides: { + lineItems: [ + { + id: 'line-item-1', + name: 'Digital Game', + type: DeliveryMethods.DIGITAL, + fulfillmentMode: DeliveryMethods.DIGITAL, + }, + ], + }, + }); + const createdOrders: Array> = []; + + await act(async () => { + await getPayPalButtonsProps().createOrder?.( + {}, + payPalActions(createdOrders) + ); + }); + + const purchaseUnit = ( + createdOrders[0].purchase_units as Array> + )[0]; + expect(purchaseUnit.shipping).toBeUndefined(); + expect(createdOrders[0].application_context).toMatchObject({ + shipping_preference: 'NO_SHIPPING', + }); + }); }); 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..bbef81a93 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 @@ -14,6 +14,7 @@ import { } from '@/components/checkout/payment/utils/use-confirm-checkout'; import { useFlushCheckoutSync } from '@/components/checkout/payment/utils/use-flush-checkout-sync'; import { useIsPaymentDisabled } from '@/components/checkout/payment/utils/use-is-payment-disabled'; +import { isDigitalOnlyOrder } from '@/components/checkout/utils/fulfillment'; import { Skeleton } from '@/components/ui/skeleton'; import { useGoDaddyContext } from '@/godaddy-provider'; import { GraphQLErrorWithCodes } from '@/lib/graphql-with-errors'; @@ -68,18 +69,23 @@ function PayPalButtonsWrapper() { const request = buildPaymentRequestsFromOrder( latestOrder ?? undefined ).payPalRequest; + + const suppressShipping = + isPickup || isDigitalOnlyOrder(latestOrder?.lineItems); const order = { ...request, purchase_units: request.purchase_units ? [ { ...request.purchase_units[0], - ...(isPickup ? { shipping: undefined } : {}), // Remove shipping if pickup + ...(suppressShipping ? { shipping: undefined } : {}), }, ] : undefined, application_context: { - shipping_preference: isPickup ? 'NO_SHIPPING' : 'SET_PROVIDED_ADDRESS', + shipping_preference: suppressShipping + ? 'NO_SHIPPING' + : 'SET_PROVIDED_ADDRESS', }, }; return await actions.order.create(order); From d1bb402804dbc0868f7209caa5f368b9b9a23f7c Mon Sep 17 00:00:00 2001 From: Dhruv Gupta Date: Wed, 18 Nov 2026 12:53:15 +0530 Subject: [PATCH 2/3] Add changeset for PayPal digital-only NO_SHIPPING fix Co-authored-by: Cursor --- .changeset/paypal-digital-no-shipping.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/paypal-digital-no-shipping.md diff --git a/.changeset/paypal-digital-no-shipping.md b/.changeset/paypal-digital-no-shipping.md new file mode 100644 index 000000000..92d7ede36 --- /dev/null +++ b/.changeset/paypal-digital-no-shipping.md @@ -0,0 +1,5 @@ +--- +'@godaddy/react': patch +--- + +Request NO_SHIPPING from PayPal for digital-only orders so the country-code-only shipping stub is omitted and PayPal no longer rejects checkout with POSTAL_CODE_REQUIRED. From 1aa287464345ef0169c5c975f03fa726303a060e Mon Sep 17 00:00:00 2001 From: Dhruv Gupta Date: Tue, 24 Nov 2026 00:04:18 +0530 Subject: [PATCH 3/3] fix: Send NO_SHIPPING to PayPal unless shipping to a collected address Omit PayPal shipping when the order has no street address and decide the shipping preference from the delivery method read after the checkout flush, covering pickup, digital and purchase-mode orders. Co-authored-by: Cursor --- .changeset/paypal-digital-no-shipping.md | 2 +- .../payment-request-resolution.test.tsx | 2 +- .../checkout-buttons/paypal/paypal.test.tsx | 172 ++++++++++++++---- .../checkout-buttons/paypal/paypal.tsx | 18 +- .../utils/use-build-payment-request.ts | 32 ++-- 5 files changed, 169 insertions(+), 57 deletions(-) diff --git a/.changeset/paypal-digital-no-shipping.md b/.changeset/paypal-digital-no-shipping.md index 92d7ede36..305f3be08 100644 --- a/.changeset/paypal-digital-no-shipping.md +++ b/.changeset/paypal-digital-no-shipping.md @@ -2,4 +2,4 @@ '@godaddy/react': patch --- -Request NO_SHIPPING from PayPal for digital-only orders so the country-code-only shipping stub is omitted and PayPal no longer rejects checkout with POSTAL_CODE_REQUIRED. +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 b853e87a0..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,6 +21,30 @@ 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, @@ -32,6 +56,7 @@ import { GoDaddyProvider } from '@/godaddy-provider'; import { buildCheckoutSession, buildDraftOrder, + buildLineItem, createTestQueryClient, getOperations, mockGodaddyApi, @@ -70,20 +95,24 @@ let form: UseFormReturn | undefined; 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 }); + 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; @@ -152,6 +181,7 @@ describe('PayPalCheckoutButton', () => { setupCheckoutTestGlobals(); form = undefined; payPalButtonsProps = undefined; + flushResult.forceNullLatestOrder = false; }); afterEach(() => { @@ -265,39 +295,119 @@ describe('PayPalCheckoutButton', () => { expect(confirmInput()?.tipAmount).toBeUndefined(); }); - it('requests NO_SHIPPING and omits shipping for a digital-only order', async () => { - // A digital-only order never collects a shipping address, so the stub - // (country-code-only) shipping object use-build-payment-request.ts builds - // must be dropped here, same as the pickup case already was — otherwise - // PayPal rejects it with POSTAL_CODE_REQUIRED for an address we never - // asked the buyer for. - renderPayPalButton({ - draftOrderOverrides: { - lineItems: [ - { - id: 'line-item-1', - name: 'Digital Game', - type: DeliveryMethods.DIGITAL, - fulfillmentMode: DeliveryMethods.DIGITAL, - }, - ], - }, + 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', + }), + ]); }); - const createdOrders: Array> = []; - await act(async () => { - await getPayPalButtonsProps().createOrder?.( - {}, - payPalActions(createdOrders) - ); + 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', + }); }); - const purchaseUnit = ( - createdOrders[0].purchase_units as Array> - )[0]; - expect(purchaseUnit.shipping).toBeUndefined(); - expect(createdOrders[0].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 bbef81a93..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 @@ -14,7 +14,6 @@ import { } from '@/components/checkout/payment/utils/use-confirm-checkout'; import { useFlushCheckoutSync } from '@/components/checkout/payment/utils/use-flush-checkout-sync'; import { useIsPaymentDisabled } from '@/components/checkout/payment/utils/use-is-payment-disabled'; -import { isDigitalOnlyOrder } from '@/components/checkout/utils/fulfillment'; import { Skeleton } from '@/components/ui/skeleton'; import { useGoDaddyContext } from '@/godaddy-provider'; import { GraphQLErrorWithCodes } from '@/lib/graphql-with-errors'; @@ -27,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. @@ -70,22 +67,25 @@ function PayPalButtonsWrapper() { latestOrder ?? undefined ).payPalRequest; - const suppressShipping = - isPickup || isDigitalOnlyOrder(latestOrder?.lineItems); + // 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], - ...(suppressShipping ? { shipping: undefined } : {}), + ...(isShipping ? {} : { shipping: undefined }), }, ] : undefined, application_context: { - shipping_preference: suppressShipping - ? '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, }, ],