| 1 | import * as Provider from "@/Provider"; |
| 2 | import * as Stripe from "@/Stripe"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { |
| 5 | GetIssuingCardholders, |
| 6 | GetIssuingCardholder, |
| 7 | } from "@distilled.cloud/stripe/stripe"; |
| 8 | import { expect } from "alchemy-test"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Result from "effect/Result"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import { isMissingStripeResource } from "@/Stripe/missing.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 16 | |
| 17 | const ISSUING_ENABLED = process.env.STRIPE_TEST_ISSUING === "1"; |
| 18 | |
| 19 | const logLevel = Effect.provideService( |
| 20 | MinimumLogLevel, |
| 21 | process.env.DEBUG ? "Debug" : "Info", |
| 22 | ); |
| 23 | |
| 24 | const isMissing = isMissingStripeResource; |
| 25 | |
| 26 | const waitUntilInactive = (id: string) => |
| 27 | GetIssuingCardholder({ cardholder: id }).pipe( |
| 28 | Effect.map((cardholder) => |
| 29 | cardholder.status === "inactive" |
| 30 | ? ("inactive" as const) |
| 31 | : ("active" as const), |
| 32 | ), |
| 33 | Effect.catchIf(isMissing, () => Effect.succeed("inactive" as const)), |
| 34 | Effect.repeat({ |
| 35 | schedule: Schedule.spaced("1 second"), |
| 36 | until: (status) => status === "inactive", |
| 37 | times: 10, |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | const billing = { |
| 42 | address: { |
| 43 | line1: "123 Main Street", |
| 44 | city: "San Francisco", |
| 45 | state: "CA", |
| 46 | postalCode: "94111", |
| 47 | country: "US", |
| 48 | }, |
| 49 | } as const; |
| 50 | |
| 51 | test.provider( |
| 52 | "issuing cardholders entitlement probe", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const result = yield* GetIssuingCardholders({ limit: 1 }).pipe( |
| 58 | Effect.result, |
| 59 | ); |
| 60 | |
| 61 | if (Result.isSuccess(result)) { |
| 62 | expect(Array.isArray(result.success.data)).toBe(true); |
| 63 | } else { |
| 64 | expect(result.failure._tag).not.toEqual("UnknownStripeError"); |
| 65 | expect(["InvalidRequestError", "Forbidden", "Unauthorized"]).toContain( |
| 66 | result.failure._tag, |
| 67 | ); |
| 68 | if (result.failure._tag === "InvalidRequestError") { |
| 69 | expect(result.failure.message).toContain("not set up to use Issuing"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | yield* stack.destroy(); |
| 74 | }).pipe(logLevel), |
| 75 | { |
| 76 | tags: ["provider:stripe", "provider:stripe:issuingcardholder", "live"], |
| 77 | timeout: 60_000, |
| 78 | }, |
| 79 | ); |
| 80 | |
| 81 | test.provider.skipIf(!ISSUING_ENABLED)( |
| 82 | "create, update, and deactivate an issuing cardholder", |
| 83 | (stack) => |
| 84 | Effect.gen(function* () { |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const created = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | return yield* Stripe.IssuingCardholder("CatalogHolder", { |
| 90 | name: "Alice Example", |
| 91 | type: "individual", |
| 92 | email: "alchemy.issuing.holder@example.com", |
| 93 | phoneNumber: "+15555550100", |
| 94 | billing, |
| 95 | individual: { firstName: "Alice", lastName: "Example" }, |
| 96 | preferredLocales: ["en"], |
| 97 | metadata: { team: "ops" }, |
| 98 | }); |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | expect(created.id).toMatch(/^ich_/); |
| 103 | expect(created.name).toEqual("Alice Example"); |
| 104 | expect(created.type).toEqual("individual"); |
| 105 | expect(created.email).toEqual("alchemy.issuing.holder@example.com"); |
| 106 | expect(created.phoneNumber).toEqual("+15555550100"); |
| 107 | expect(created.status).toEqual("active"); |
| 108 | expect(created.firstName).toEqual("Alice"); |
| 109 | expect(created.lastName).toEqual("Example"); |
| 110 | expect(created.preferredLocales).toEqual(["en"]); |
| 111 | expect(created.billing.address.city).toEqual("San Francisco"); |
| 112 | expect(created.billing.address.postalCode).toEqual("94111"); |
| 113 | expect(created.metadata).toMatchObject({ team: "ops" }); |
| 114 | expect(created.created).toEqual(expect.any(Number)); |
| 115 | expect(created.livemode).toEqual(false); |
| 116 | |
| 117 | const fetched = yield* GetIssuingCardholder({ |
| 118 | cardholder: created.id, |
| 119 | }); |
| 120 | expect(fetched.id).toEqual(created.id); |
| 121 | expect(fetched.name).toEqual("Alice Example"); |
| 122 | expect(fetched.email).toEqual("alchemy.issuing.holder@example.com"); |
| 123 | expect(fetched.phone_number).toEqual("+15555550100"); |
| 124 | expect(fetched.status).toEqual("active"); |
| 125 | expect(fetched.metadata?.team).toEqual("ops"); |
| 126 | expect( |
| 127 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stack], |
| 128 | ).toBeDefined(); |
| 129 | expect( |
| 130 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stage], |
| 131 | ).toBeDefined(); |
| 132 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 133 | |
| 134 | const updated = yield* stack.deploy( |
| 135 | Effect.gen(function* () { |
| 136 | return yield* Stripe.IssuingCardholder("CatalogHolder", { |
| 137 | name: "Alice Example", |
| 138 | type: "individual", |
| 139 | email: "alchemy.issuing.holder.updated@example.com", |
| 140 | phoneNumber: "+15555550199", |
| 141 | billing: { |
| 142 | address: { |
| 143 | ...billing.address, |
| 144 | line1: "456 Market Street", |
| 145 | }, |
| 146 | }, |
| 147 | individual: { firstName: "Alice", lastName: "Example" }, |
| 148 | preferredLocales: ["en", "es"], |
| 149 | spendingControls: { |
| 150 | spendingLimits: [{ amount: 10000, interval: "monthly" }], |
| 151 | spendingLimitsCurrency: "usd", |
| 152 | }, |
| 153 | metadata: { team: "finance", role: "lead" }, |
| 154 | }); |
| 155 | }), |
| 156 | ); |
| 157 | |
| 158 | expect(updated.id).toEqual(created.id); |
| 159 | expect(updated.email).toEqual( |
| 160 | "alchemy.issuing.holder.updated@example.com", |
| 161 | ); |
| 162 | expect(updated.phoneNumber).toEqual("+15555550199"); |
| 163 | expect(updated.billing.address.line1).toEqual("456 Market Street"); |
| 164 | expect(updated.preferredLocales).toEqual(["en", "es"]); |
| 165 | expect(updated.spendingControls?.spendingLimitsCurrency).toEqual("usd"); |
| 166 | expect(updated.spendingControls?.spendingLimits).toEqual([ |
| 167 | { amount: 10000, interval: "monthly" }, |
| 168 | ]); |
| 169 | expect(updated.metadata).toEqual({ team: "finance", role: "lead" }); |
| 170 | expect(updated.status).toEqual("active"); |
| 171 | |
| 172 | const refetched = yield* GetIssuingCardholder({ |
| 173 | cardholder: updated.id, |
| 174 | }); |
| 175 | expect(refetched.email).toEqual( |
| 176 | "alchemy.issuing.holder.updated@example.com", |
| 177 | ); |
| 178 | expect(refetched.phone_number).toEqual("+15555550199"); |
| 179 | expect(refetched.billing.address.line1).toEqual("456 Market Street"); |
| 180 | expect(refetched.preferred_locales).toEqual(["en", "es"]); |
| 181 | expect(refetched.metadata?.team).toEqual("finance"); |
| 182 | expect(refetched.metadata?.role).toEqual("lead"); |
| 183 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 184 | |
| 185 | yield* stack.destroy(); |
| 186 | |
| 187 | const inactive = yield* waitUntilInactive(created.id); |
| 188 | expect(inactive).toEqual("inactive"); |
| 189 | const deactivated = yield* GetIssuingCardholder({ |
| 190 | cardholder: created.id, |
| 191 | }); |
| 192 | expect(deactivated.status).toEqual("inactive"); |
| 193 | }).pipe(logLevel), |
| 194 | { |
| 195 | tags: ["provider:stripe", "provider:stripe:issuingcardholder", "live"], |
| 196 | timeout: 120_000, |
| 197 | }, |
| 198 | ); |
| 199 | |
| 200 | test.provider.skipIf(!ISSUING_ENABLED)( |
| 201 | "list enumerates the deployed issuing cardholder", |
| 202 | (stack) => |
| 203 | Effect.gen(function* () { |
| 204 | yield* stack.destroy(); |
| 205 | |
| 206 | const deployed = yield* stack.deploy( |
| 207 | Effect.gen(function* () { |
| 208 | return yield* Stripe.IssuingCardholder("ListHolder", { |
| 209 | name: "List Example", |
| 210 | type: "individual", |
| 211 | billing, |
| 212 | metadata: { kind: "list" }, |
| 213 | }); |
| 214 | }), |
| 215 | ); |
| 216 | |
| 217 | const provider = yield* Provider.findProvider(Stripe.IssuingCardholder); |
| 218 | const all = yield* provider.list(); |
| 219 | const found = all.find((cardholder) => cardholder.id === deployed.id); |
| 220 | expect(found).toBeDefined(); |
| 221 | expect(found?.name).toEqual(deployed.name); |
| 222 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 223 | |
| 224 | yield* stack.destroy(); |
| 225 | |
| 226 | const inactive = yield* waitUntilInactive(deployed.id); |
| 227 | expect(inactive).toEqual("inactive"); |
| 228 | |
| 229 | const after = yield* provider.list(); |
| 230 | expect( |
| 231 | after.find((cardholder) => cardholder.id === deployed.id), |
| 232 | ).toBeUndefined(); |
| 233 | }).pipe(logLevel), |
| 234 | { |
| 235 | tags: ["provider:stripe", "provider:stripe:issuingcardholder", "live"], |
| 236 | timeout: 120_000, |
| 237 | }, |
| 238 | ); |