| 1 | import * as Provider from "@/Provider"; |
| 2 | import * as Stripe from "@/Stripe"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { |
| 5 | GetBillingPortalConfiguration, |
| 6 | GetBillingPortalConfigurations, |
| 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 Schedule from "effect/Schedule"; |
| 12 | import { isMissingStripeResource } from "@/Stripe/missing.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 15 | |
| 16 | // Stripe cannot deactivate the account default, and the API cannot choose a |
| 17 | // different default. Require account setup before creating disposable fixtures. |
| 18 | const requireDefaultConfiguration = Effect.gen(function* () { |
| 19 | const configurations = yield* GetBillingPortalConfigurations({ |
| 20 | is_default: true, |
| 21 | limit: 1, |
| 22 | }); |
| 23 | if (configurations.data.length === 0) { |
| 24 | return yield* Effect.fail( |
| 25 | new Error( |
| 26 | "Configure the default customer portal in the Stripe Dashboard before running BillingPortalConfiguration lifecycle tests.", |
| 27 | ), |
| 28 | ); |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | const logLevel = Effect.provideService( |
| 33 | MinimumLogLevel, |
| 34 | process.env.DEBUG ? "Debug" : "Info", |
| 35 | ); |
| 36 | |
| 37 | const isMissing = isMissingStripeResource; |
| 38 | |
| 39 | const waitUntilDeactivated = (id: string) => |
| 40 | GetBillingPortalConfiguration({ configuration: id }).pipe( |
| 41 | Effect.map((configuration) => |
| 42 | configuration.active ? ("active" as const) : ("inactive" as const), |
| 43 | ), |
| 44 | Effect.catchIf(isMissing, () => Effect.succeed("inactive" as const)), |
| 45 | Effect.repeat({ |
| 46 | schedule: Schedule.spaced("1 second"), |
| 47 | until: (status) => status === "inactive", |
| 48 | times: 10, |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | test.provider( |
| 53 | // Use a separate stack from the legacy test, which could own the account |
| 54 | // default and cannot destroy it. Leave that default and its state intact. |
| 55 | "create, update, and deactivate a non-default billing portal configuration", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* requireDefaultConfiguration; |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* Stripe.BillingPortalConfiguration("CustomerPortal", { |
| 64 | name: "Alchemy Customer Portal", |
| 65 | defaultReturnUrl: "https://example.com/account", |
| 66 | businessProfile: { |
| 67 | headline: "Manage your billing", |
| 68 | privacyPolicyUrl: "https://example.com/privacy", |
| 69 | }, |
| 70 | features: { |
| 71 | invoiceHistory: { enabled: true }, |
| 72 | customerUpdate: { |
| 73 | enabled: true, |
| 74 | allowedUpdates: ["email", "address"], |
| 75 | }, |
| 76 | }, |
| 77 | metadata: { env: "test" }, |
| 78 | }); |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | expect(created.id).toMatch(/^bpc_/); |
| 83 | expect(created.isDefault).toEqual(false); |
| 84 | expect(created.name).toEqual("Alchemy Customer Portal"); |
| 85 | expect(created.active).toEqual(true); |
| 86 | expect(created.defaultReturnUrl).toEqual("https://example.com/account"); |
| 87 | expect(created.businessProfile.headline).toEqual("Manage your billing"); |
| 88 | expect(created.businessProfile.privacyPolicyUrl).toEqual( |
| 89 | "https://example.com/privacy", |
| 90 | ); |
| 91 | expect(created.features.invoiceHistory.enabled).toEqual(true); |
| 92 | expect(created.features.customerUpdate.enabled).toEqual(true); |
| 93 | expect(created.features.customerUpdate.allowedUpdates).toEqual([ |
| 94 | "email", |
| 95 | "address", |
| 96 | ]); |
| 97 | expect(created.metadata).toMatchObject({ env: "test" }); |
| 98 | expect(created.livemode).toEqual(false); |
| 99 | expect(created.created).toEqual(expect.any(Number)); |
| 100 | |
| 101 | const fetched = yield* GetBillingPortalConfiguration({ |
| 102 | configuration: created.id, |
| 103 | }); |
| 104 | expect(fetched.id).toEqual(created.id); |
| 105 | expect(fetched.is_default).toEqual(false); |
| 106 | expect(fetched.name).toEqual("Alchemy Customer Portal"); |
| 107 | expect(fetched.active).toEqual(true); |
| 108 | expect(fetched.default_return_url).toEqual("https://example.com/account"); |
| 109 | expect(fetched.features.invoice_history.enabled).toEqual(true); |
| 110 | expect(fetched.features.customer_update.enabled).toEqual(true); |
| 111 | expect(fetched.metadata?.env).toEqual("test"); |
| 112 | expect( |
| 113 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stack], |
| 114 | ).toBeDefined(); |
| 115 | expect( |
| 116 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stage], |
| 117 | ).toBeDefined(); |
| 118 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 119 | |
| 120 | const updated = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* Stripe.BillingPortalConfiguration("CustomerPortal", { |
| 123 | name: "Alchemy Customer Portal Updated", |
| 124 | defaultReturnUrl: "https://example.com/billing", |
| 125 | businessProfile: { |
| 126 | headline: "Updated headline", |
| 127 | privacyPolicyUrl: "https://example.com/privacy", |
| 128 | termsOfServiceUrl: "https://example.com/terms", |
| 129 | }, |
| 130 | features: { |
| 131 | invoiceHistory: { enabled: true }, |
| 132 | customerUpdate: { |
| 133 | enabled: true, |
| 134 | allowedUpdates: ["email", "name"], |
| 135 | }, |
| 136 | paymentMethodUpdate: { enabled: true }, |
| 137 | }, |
| 138 | metadata: { env: "test", revision: "2" }, |
| 139 | }); |
| 140 | }), |
| 141 | ); |
| 142 | |
| 143 | expect(updated.id).toEqual(created.id); |
| 144 | expect(updated.isDefault).toEqual(false); |
| 145 | expect(updated.name).toEqual("Alchemy Customer Portal Updated"); |
| 146 | expect(updated.active).toEqual(true); |
| 147 | expect(updated.defaultReturnUrl).toEqual("https://example.com/billing"); |
| 148 | expect(updated.businessProfile.headline).toEqual("Updated headline"); |
| 149 | expect(updated.businessProfile.termsOfServiceUrl).toEqual( |
| 150 | "https://example.com/terms", |
| 151 | ); |
| 152 | expect(updated.features.customerUpdate.allowedUpdates).toEqual([ |
| 153 | "email", |
| 154 | "name", |
| 155 | ]); |
| 156 | expect(updated.features.paymentMethodUpdate.enabled).toEqual(true); |
| 157 | expect(updated.metadata).toEqual({ env: "test", revision: "2" }); |
| 158 | |
| 159 | const refetched = yield* GetBillingPortalConfiguration({ |
| 160 | configuration: updated.id, |
| 161 | }); |
| 162 | expect(refetched.name).toEqual("Alchemy Customer Portal Updated"); |
| 163 | expect(refetched.default_return_url).toEqual( |
| 164 | "https://example.com/billing", |
| 165 | ); |
| 166 | expect(refetched.features.payment_method_update.enabled).toEqual(true); |
| 167 | expect(refetched.metadata?.env).toEqual("test"); |
| 168 | expect(refetched.metadata?.revision).toEqual("2"); |
| 169 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 170 | |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | const deactivated = yield* waitUntilDeactivated(created.id); |
| 174 | expect(deactivated).toEqual("inactive"); |
| 175 | }).pipe(logLevel), |
| 176 | { |
| 177 | tags: [ |
| 178 | "provider:stripe", |
| 179 | "provider:stripe:billingportalconfiguration", |
| 180 | "live", |
| 181 | ], |
| 182 | timeout: 120_000, |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | test.provider( |
| 187 | "list enumerates the deployed billing portal configuration", |
| 188 | (stack) => |
| 189 | Effect.gen(function* () { |
| 190 | yield* requireDefaultConfiguration; |
| 191 | yield* stack.destroy(); |
| 192 | |
| 193 | const deployed = yield* stack.deploy( |
| 194 | Effect.gen(function* () { |
| 195 | return yield* Stripe.BillingPortalConfiguration("ListPortal", { |
| 196 | name: "Alchemy List Portal", |
| 197 | features: { |
| 198 | invoiceHistory: { enabled: true }, |
| 199 | }, |
| 200 | metadata: { kind: "list" }, |
| 201 | }); |
| 202 | }), |
| 203 | ); |
| 204 | |
| 205 | expect(deployed.isDefault).toEqual(false); |
| 206 | |
| 207 | const provider = yield* Provider.findProvider( |
| 208 | Stripe.BillingPortalConfiguration, |
| 209 | ); |
| 210 | const all = yield* provider.list(); |
| 211 | const found = all.find( |
| 212 | (configuration) => configuration.id === deployed.id, |
| 213 | ); |
| 214 | expect(found).toBeDefined(); |
| 215 | expect(found?.name).toEqual(deployed.name); |
| 216 | expect(found?.features.invoiceHistory.enabled).toEqual(true); |
| 217 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 218 | |
| 219 | yield* stack.destroy(); |
| 220 | |
| 221 | const deactivated = yield* waitUntilDeactivated(deployed.id); |
| 222 | expect(deactivated).toEqual("inactive"); |
| 223 | }).pipe(logLevel), |
| 224 | { |
| 225 | tags: [ |
| 226 | "provider:stripe", |
| 227 | "provider:stripe:billingportalconfiguration", |
| 228 | "live", |
| 229 | ], |
| 230 | timeout: 120_000, |
| 231 | }, |
| 232 | ); |