Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 51 additions & 35 deletions apps/storefront/src/lib/checkoutContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
buildStoreCheckoutPayload,
CHECKOUT_CONTEXT_NAMESPACE,
withDigitalCheckoutAddress,
withDigitalStoreApiAddress,
} from "./checkoutContext.ts";
import { buildStripePaymentData, toStripeBillingDetails } from "./stripePaymentData.ts";
import { validateCheckoutForm } from "./validation.ts";
Expand Down Expand Up @@ -104,7 +105,7 @@ test("checkout payload preserves language, order notes, and a different shipping
assert.equal(payload.customer_password, "correct-horse-battery-staple");
});

test("digital checkout supplies a country-valid Store API and Stripe fallback address", () => {
test("digital checkout supplies a country-agnostic Store API and gateway address", () => {
const billing = {
firstName: "Ada",
lastName: "Lovelace",
Expand All @@ -126,48 +127,63 @@ test("digital checkout supplies a country-valid Store API and Stripe fallback ad
);
const stripeBilling = toStripeBillingDetails(paymentBilling);

assert.equal(payload.billing_address.address_1, "Dostawa cyfrowa 1");
assert.equal(payload.billing_address.city, "Warszawa");
assert.equal(payload.billing_address.state, "MZ");
assert.equal(payload.billing_address.postcode, "00-001");
assert.equal(payload.billing_address.address_1, "Digital delivery");
assert.equal(payload.billing_address.city, "Digital order");
assert.equal(payload.billing_address.state, "");
assert.equal(payload.billing_address.postcode, "");
assert.equal(payload.billing_address.country, "PL");
assert.deepEqual(payload.shipping_address, {
...payload.billing_address,
email: undefined,
});
assert.equal(payload.extensions?.[CHECKOUT_CONTEXT_NAMESPACE].digital_order, true);
assert.equal(stripePaymentData.get("billing_address_1"), "Dostawa cyfrowa 1");
assert.equal(stripePaymentData.get("billing_city"), "Warszawa");
assert.equal(stripePaymentData.get("billing_state"), "MZ");
assert.equal(stripePaymentData.get("billing_postcode"), "00-001");
assert.equal(stripeBilling.address.line1, "Dostawa cyfrowa 1");
assert.equal(stripeBilling.address.city, "Warszawa");
assert.equal(stripeBilling.address.state, "MZ");
assert.equal(stripeBilling.address.postal_code, "00-001");
assert.equal(stripePaymentData.get("billing_address_1"), "Digital delivery");
assert.equal(stripePaymentData.get("billing_city"), "Digital order");
assert.equal(stripePaymentData.get("billing_state"), "");
assert.equal(stripePaymentData.get("billing_postcode"), "");
assert.equal(stripeBilling.address.line1, "Digital delivery");
assert.equal(stripeBilling.address.city, "Digital order");
assert.equal(stripeBilling.address.state, undefined);
assert.equal(stripeBilling.address.postal_code, undefined);
});

test("digital checkout uses valid state and postcode formats for supported countries", () => {
const countries = [
{ countryCode: "DE", state: "DE-BE", postcode: "10115" },
{ countryCode: "FR", state: "75", postcode: "75001" },
{ countryCode: "GB", state: "London", postcode: "SW1A 1AA" },
{ countryCode: "NL", state: "NH", postcode: "1011 AA" },
{ countryCode: "US", state: "CA", postcode: "94105" },
];

for (const country of countries) {
const address = withDigitalCheckoutAddress({
firstName: "Ada",
lastName: "Lovelace",
addressLine1: "",
city: "",
postcode: "",
countryCode: country.countryCode,
email: "ada@example.com",
phone: "+48 123 456 789",
});
assert.equal(address.state, country.state);
assert.equal(address.postcode, country.postcode);
test("digital checkout avoids country-specific state and postcode validation", () => {
for (let first = 65; first <= 90; first += 1) {
for (let second = 65; second <= 90; second += 1) {
const countryCode = String.fromCharCode(first, second);
const address = withDigitalCheckoutAddress({
firstName: "Ada",
lastName: "Lovelace",
addressLine1: "Old physical address",
city: "Old physical city",
state: "Old physical state",
postcode: "00000",
countryCode,
email: "ada@example.com",
phone: "+48 123 456 789",
});
const storeApiAddress = withDigitalStoreApiAddress({
first_name: "Ada",
last_name: "Lovelace",
address_1: "Old physical address",
city: "Old physical city",
state: "Old physical state",
postcode: "00000",
country: countryCode,
email: "ada@example.com",
phone: "+48 123 456 789",
});
assert.equal(address.countryCode, countryCode);
assert.equal(address.addressLine1, "Digital delivery");
assert.equal(address.city, "Digital order");
assert.equal(address.state, "");
assert.equal(address.postcode, "");
assert.equal(storeApiAddress.country, countryCode);
assert.equal(storeApiAddress.address_1, "Digital delivery");
assert.equal(storeApiAddress.city, "Digital order");
assert.equal(storeApiAddress.state, "");
assert.equal(storeApiAddress.postcode, "");
}
}
});

Expand Down
43 changes: 11 additions & 32 deletions apps/storefront/src/lib/checkoutContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,50 +72,29 @@ function toStoreApiAddress(
};
}

const DIGITAL_ADDRESS_FALLBACKS: Record<string, {
addressLine1: string;
city: string;
state: string;
postcode: string;
}> = {
DE: { addressLine1: "Digital delivery 1", city: "Berlin", state: "DE-BE", postcode: "10115" },
FR: { addressLine1: "1 Livraison numerique", city: "Paris", state: "75", postcode: "75001" },
GB: { addressLine1: "1 Digital Delivery", city: "London", state: "London", postcode: "SW1A 1AA" },
NL: { addressLine1: "Digital delivery 1", city: "Amsterdam", state: "NH", postcode: "1011 AA" },
PL: { addressLine1: "Dostawa cyfrowa 1", city: "Warszawa", state: "MZ", postcode: "00-001" },
US: { addressLine1: "1 Digital Delivery", city: "San Francisco", state: "CA", postcode: "94105" },
};

function digitalAddressFallback(countryCode: string) {
return DIGITAL_ADDRESS_FALLBACKS[countryCode.trim().toUpperCase()] ?? {
addressLine1: "Digital delivery 1",
city: "Digital order",
state: "Digital order",
postcode: "00000",
};
}
const DIGITAL_CHECKOUT_ADDRESS = {
addressLine1: "Digital delivery",
city: "Digital order",
state: "",
postcode: "",
} as const;

export function withDigitalCheckoutAddress(
details: CheckoutBillingDetails,
): CheckoutBillingDetails {
const fallback = digitalAddressFallback(details.countryCode);
return {
...details,
addressLine1: details.addressLine1.trim() || fallback.addressLine1,
city: details.city.trim() || fallback.city,
state: details.state?.trim() || fallback.state,
postcode: details.postcode.trim() || fallback.postcode,
...DIGITAL_CHECKOUT_ADDRESS,
};
}

export function withDigitalStoreApiAddress(address: StoreApiAddress): StoreApiAddress {
const fallback = digitalAddressFallback(address.country);
return {
...address,
address_1: address.address_1.trim() || fallback.addressLine1,
city: address.city.trim() || fallback.city,
state: address.state?.trim() || fallback.state,
postcode: address.postcode.trim() || fallback.postcode,
address_1: DIGITAL_CHECKOUT_ADDRESS.addressLine1,
city: DIGITAL_CHECKOUT_ADDRESS.city,
state: DIGITAL_CHECKOUT_ADDRESS.state,
postcode: DIGITAL_CHECKOUT_ADDRESS.postcode,
};
}

Expand Down
Loading