diff --git a/.changeset/nk-billing-address-line2.md b/.changeset/nk-billing-address-line2.md new file mode 100644 index 0000000..8fef0f1 --- /dev/null +++ b/.changeset/nk-billing-address-line2.md @@ -0,0 +1,5 @@ +--- +"@ingram-tech/nk-billing": minor +--- + +The customer address carries `line2`. `findOrCreateCustomer` sends `details.address.line2` (it dropped it), and `updateCustomerBillingAddress` takes an optional `line2`. An empty one is not sent. diff --git a/packages/nk-billing/src/customers.test.ts b/packages/nk-billing/src/customers.test.ts index 413c8d4..c2823d4 100644 --- a/packages/nk-billing/src/customers.test.ts +++ b/packages/nk-billing/src/customers.test.ts @@ -1,6 +1,10 @@ import type Stripe from "stripe"; import { describe, expect, it } from "vitest"; -import { findCustomer } from "./customers.js"; +import { + findCustomer, + findOrCreateCustomer, + updateCustomerBillingAddress, +} from "./customers.js"; /** Records the search query; the escaping is the unit under test. */ function recordingStripe() { @@ -44,3 +48,67 @@ describe("findCustomer", () => { }); }); }); + +/** Records the create and update params; their address is the unit under test. */ +function addressStripe() { + const created: Stripe.CustomerCreateParams[] = []; + const updated: Stripe.CustomerUpdateParams[] = []; + const stripe = { + customers: { + search: () => Promise.resolve({ data: [] }), + create: (params: Stripe.CustomerCreateParams) => { + created.push(params); + return Promise.resolve({ id: "cus_1" }); + }, + update: (_id: string, params: Stripe.CustomerUpdateParams) => { + updated.push(params); + return Promise.resolve({ id: "cus_1" }); + }, + }, + }; + // Structural fake of the one surface under test. + return { created, updated, stripe: stripe as unknown as Stripe }; +} + +const brussels = { city: "Ixelles", postal_code: "1050", country: "BE" }; + +describe("the second address line", () => { + it.each([ + ["sends it", "Boîte 3", { line2: "Boîte 3" }], + ["omits an empty one", "", {}], + ["omits a missing one", null, {}], + ])("findOrCreateCustomer %s", async (_label, line2, expected) => { + const { created, stripe } = addressStripe(); + await findOrCreateCustomer( + { metadataKey: "acme_org_id", id: "org_1" }, + { + name: "Acme", + address: { line1: "Avenue Louise 250", line2, ...brussels }, + }, + stripe, + ); + expect(created[0]?.address).toEqual({ + line1: "Avenue Louise 250", + ...expected, + ...brussels, + }); + }); + + it.each([ + ["sends it", "Boîte 3", { line2: "Boîte 3" }], + ["omits an empty one", "", {}], + ["omits a missing one", undefined, {}], + ])("updateCustomerBillingAddress %s", async (_label, line2, expected) => { + const { updated, stripe } = addressStripe(); + await updateCustomerBillingAddress( + "cus_1", + { address: { line1: "Avenue Louise 250", line2, ...brussels } }, + stripe, + ); + expect(updated[0]?.address).toEqual({ + line1: "Avenue Louise 250", + ...expected, + ...brussels, + }); + }); +}); diff --git a/packages/nk-billing/src/customers.ts b/packages/nk-billing/src/customers.ts index f252965..55b5f0d 100644 --- a/packages/nk-billing/src/customers.ts +++ b/packages/nk-billing/src/customers.ts @@ -28,6 +28,7 @@ export interface CustomerDetails { vatNumber?: string | null; address?: { line1?: string | null; + line2?: string | null; city?: string | null; postal_code?: string | null; country?: string | null; @@ -73,6 +74,7 @@ export async function findOrCreateCustomer( if (details.address?.line1 || details.address?.city) { params.address = { line1: details.address.line1 ?? undefined, + line2: details.address.line2 || undefined, city: details.address.city ?? undefined, postal_code: details.address.postal_code ?? undefined, country: details.address.country ?? undefined, @@ -121,6 +123,8 @@ export async function updateCustomerBillingAddress( name?: string | null; address: { line1: string; + /** The second street line; an empty one is not sent. */ + line2?: string | null; city: string; postal_code: string; country: string; @@ -128,8 +132,9 @@ export async function updateCustomerBillingAddress( }, stripe: Stripe = getStripe(), ): Promise { + const { line2, ...address } = opts.address; await stripe.customers.update(customerId, { ...(opts.name ? { name: opts.name } : {}), - address: opts.address, + address: { ...address, ...(line2 ? { line2 } : {}) }, }); }