| 1 | import * as Provider from "@/Provider"; |
| 2 | import * as Stripe from "@/Stripe"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { |
| 5 | DeleteAccount, |
| 6 | GetAccountByAccount, |
| 7 | CreateAccount, |
| 8 | } from "@distilled.cloud/stripe/stripe"; |
| 9 | import { expect } from "alchemy-test"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Result from "effect/Result"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import { isMissingStripeResource } from "@/Stripe/missing.ts"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 17 | |
| 18 | const logLevel = Effect.provideService( |
| 19 | MinimumLogLevel, |
| 20 | process.env.DEBUG ? "Debug" : "Info", |
| 21 | ); |
| 22 | |
| 23 | /** Opt-in: the testing Stripe account must be a Connect platform. */ |
| 24 | const CONNECT_ENABLED = process.env.STRIPE_TEST_CONNECT === "1"; |
| 25 | |
| 26 | const isMissing = isMissingStripeResource; |
| 27 | |
| 28 | const waitUntilGone = (id: string) => |
| 29 | GetAccountByAccount({ account: id }).pipe( |
| 30 | Effect.as("found" as const), |
| 31 | Effect.catchIf(isMissing, () => Effect.succeed("gone" as const)), |
| 32 | Effect.repeat({ |
| 33 | schedule: Schedule.spaced("1 second"), |
| 34 | until: (status) => status === "gone", |
| 35 | times: 10, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | const deleteAccount = (account: string) => |
| 40 | DeleteAccount({ account }).pipe(Effect.catchIf(isMissing, () => Effect.void)); |
| 41 | |
| 42 | const probeCreate = () => |
| 43 | CreateAccount({ |
| 44 | type: "express", |
| 45 | country: "US", |
| 46 | email: "alchemy.account.probe@example.com", |
| 47 | }); |
| 48 | |
| 49 | test.provider( |
| 50 | "connect accounts entitlement probe", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | yield* stack.destroy(); |
| 54 | |
| 55 | const probe = yield* probeCreate().pipe(Effect.result); |
| 56 | if (Result.isFailure(probe)) { |
| 57 | expect(probe.failure._tag).not.toEqual("UnknownStripeError"); |
| 58 | expect(probe.failure._tag).toEqual("InvalidRequestError"); |
| 59 | expect(probe.failure.message ?? "").toContain("signed up for Connect"); |
| 60 | yield* stack.destroy(); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | yield* deleteAccount(probe.success.id); |
| 65 | yield* stack.destroy(); |
| 66 | }).pipe(logLevel), |
| 67 | { |
| 68 | tags: ["provider:stripe", "provider:stripe:account", "live"], |
| 69 | timeout: 60_000, |
| 70 | }, |
| 71 | ); |
| 72 | |
| 73 | test.provider.skipIf(!CONNECT_ENABLED)( |
| 74 | "create, update, and delete a connected account", |
| 75 | (stack) => |
| 76 | Effect.gen(function* () { |
| 77 | yield* stack.destroy(); |
| 78 | |
| 79 | const created = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | return yield* Stripe.Account("CatalogAccount", { |
| 82 | type: "express", |
| 83 | country: "US", |
| 84 | email: "alchemy.catalog.account@example.com", |
| 85 | businessType: "company", |
| 86 | defaultCurrency: "usd", |
| 87 | businessProfile: { |
| 88 | name: "Alchemy Catalog Merchant", |
| 89 | url: "https://alchemy.run", |
| 90 | mcc: "5734", |
| 91 | supportEmail: "support@alchemy.run", |
| 92 | }, |
| 93 | capabilities: { |
| 94 | card_payments: { requested: true }, |
| 95 | transfers: { requested: true }, |
| 96 | }, |
| 97 | settings: { |
| 98 | payouts: { schedule: { interval: "manual" } }, |
| 99 | }, |
| 100 | metadata: { tier: "gold" }, |
| 101 | }); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(created.id).toMatch(/^acct_/); |
| 106 | expect(created.type).toEqual("express"); |
| 107 | expect(created.country).toEqual("US"); |
| 108 | expect(created.email).toEqual("alchemy.catalog.account@example.com"); |
| 109 | expect(created.businessType).toEqual("company"); |
| 110 | expect(created.defaultCurrency).toEqual("usd"); |
| 111 | expect(created.businessProfileName).toEqual("Alchemy Catalog Merchant"); |
| 112 | expect(created.businessProfileUrl).toEqual("https://alchemy.run"); |
| 113 | expect(created.metadata).toMatchObject({ tier: "gold" }); |
| 114 | expect(Object.keys(created.capabilities)).toEqual( |
| 115 | expect.arrayContaining(["card_payments", "transfers"]), |
| 116 | ); |
| 117 | |
| 118 | const fetched = yield* GetAccountByAccount({ account: created.id }); |
| 119 | expect(fetched.id).toEqual(created.id); |
| 120 | expect(fetched.email).toEqual(created.email); |
| 121 | expect(fetched.business_profile?.name).toEqual( |
| 122 | "Alchemy Catalog Merchant", |
| 123 | ); |
| 124 | expect(fetched.business_profile?.url).toEqual("https://alchemy.run"); |
| 125 | expect(fetched.settings?.payouts?.schedule?.interval).toEqual("manual"); |
| 126 | expect(fetched.metadata?.tier).toEqual("gold"); |
| 127 | expect( |
| 128 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stack], |
| 129 | ).toBeDefined(); |
| 130 | expect( |
| 131 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stage], |
| 132 | ).toBeDefined(); |
| 133 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 134 | |
| 135 | const updated = yield* stack.deploy( |
| 136 | Effect.gen(function* () { |
| 137 | return yield* Stripe.Account("CatalogAccount", { |
| 138 | type: "express", |
| 139 | country: "US", |
| 140 | email: "alchemy.catalog.account.updated@example.com", |
| 141 | businessType: "company", |
| 142 | defaultCurrency: "usd", |
| 143 | businessProfile: { |
| 144 | name: "Alchemy Catalog Merchant Updated", |
| 145 | url: "https://www.alchemy.run", |
| 146 | mcc: "5734", |
| 147 | supportEmail: "support@alchemy.run", |
| 148 | }, |
| 149 | capabilities: { |
| 150 | card_payments: { requested: true }, |
| 151 | transfers: { requested: true }, |
| 152 | }, |
| 153 | settings: { |
| 154 | payouts: { schedule: { interval: "manual" } }, |
| 155 | }, |
| 156 | metadata: { tier: "platinum", sku: "acct-1" }, |
| 157 | }); |
| 158 | }), |
| 159 | ); |
| 160 | |
| 161 | expect(updated.id).toEqual(created.id); |
| 162 | expect(updated.email).toEqual( |
| 163 | "alchemy.catalog.account.updated@example.com", |
| 164 | ); |
| 165 | expect(updated.businessProfileName).toEqual( |
| 166 | "Alchemy Catalog Merchant Updated", |
| 167 | ); |
| 168 | expect(updated.metadata).toEqual({ tier: "platinum", sku: "acct-1" }); |
| 169 | |
| 170 | const refetched = yield* GetAccountByAccount({ account: updated.id }); |
| 171 | expect(refetched.email).toEqual(updated.email); |
| 172 | expect(refetched.business_profile?.name).toEqual( |
| 173 | "Alchemy Catalog Merchant Updated", |
| 174 | ); |
| 175 | expect(refetched.business_profile?.url).toEqual( |
| 176 | "https://www.alchemy.run", |
| 177 | ); |
| 178 | expect(refetched.metadata?.tier).toEqual("platinum"); |
| 179 | expect(refetched.metadata?.sku).toEqual("acct-1"); |
| 180 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 181 | |
| 182 | yield* stack.destroy(); |
| 183 | |
| 184 | const gone = yield* waitUntilGone(created.id); |
| 185 | expect(gone).toEqual("gone"); |
| 186 | }).pipe(logLevel), |
| 187 | { |
| 188 | tags: ["provider:stripe", "provider:stripe:account", "live"], |
| 189 | timeout: 120_000, |
| 190 | }, |
| 191 | ); |
| 192 | |
| 193 | test.provider.skipIf(!CONNECT_ENABLED)( |
| 194 | "list enumerates the deployed connected account", |
| 195 | (stack) => |
| 196 | Effect.gen(function* () { |
| 197 | yield* stack.destroy(); |
| 198 | |
| 199 | const deployed = yield* stack.deploy( |
| 200 | Effect.gen(function* () { |
| 201 | return yield* Stripe.Account("ListAccount", { |
| 202 | type: "express", |
| 203 | country: "US", |
| 204 | email: "alchemy.list.account@example.com", |
| 205 | metadata: { kind: "list" }, |
| 206 | }); |
| 207 | }), |
| 208 | ); |
| 209 | |
| 210 | const provider = yield* Provider.findProvider(Stripe.Account); |
| 211 | const all = yield* provider.list(); |
| 212 | const found = all.find((account) => account.id === deployed.id); |
| 213 | expect(found).toBeDefined(); |
| 214 | expect(found?.email).toEqual(deployed.email); |
| 215 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 216 | |
| 217 | yield* stack.destroy(); |
| 218 | |
| 219 | const gone = yield* waitUntilGone(deployed.id); |
| 220 | expect(gone).toEqual("gone"); |
| 221 | }).pipe(logLevel), |
| 222 | { |
| 223 | tags: ["provider:stripe", "provider:stripe:account", "live"], |
| 224 | timeout: 120_000, |
| 225 | }, |
| 226 | ); |