1import * as Provider from "@/Provider";
2import * as Stripe from "@/Stripe";
3import * as Test from "@/Test/Alchemy";
4import { GetPaymentMethodDomain } from "@distilled.cloud/stripe/stripe";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9import { isMissingStripeResource } from "@/Stripe/missing.ts";
10
11const { test } = Test.make({ providers: Stripe.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18const isMissing = isMissingStripeResource;
19
20const waitUntilDisabled = (id: string) =>
21 GetPaymentMethodDomain({
22 payment_method_domain: id,
23 }).pipe(
24 Effect.map((domain) =>
25 domain.enabled ? ("enabled" as const) : ("disabled" as const),
26 ),
27 Effect.catchIf(isMissing, () => Effect.succeed("disabled" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "disabled",
31 times: 10,
32 }),
33 );
34
35test.provider(
36 "create, update, and deactivate a payment method domain",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const created = yield* stack.deploy(
42 Effect.gen(function* () {
43 return yield* Stripe.PaymentMethodDomain("Checkout", {
44 domainName: "alchemy-pmd-lifecycle.example.com",
45 });
46 }),
47 );
48
49 expect(created.id).toMatch(/^pmd_/);
50 expect(created.domainName).toEqual("alchemy-pmd-lifecycle.example.com");
51 expect(created.enabled).toEqual(true);
52 expect(created.applePay.status).toEqual(expect.any(String));
53 expect(created.googlePay.status).toEqual(expect.any(String));
54 expect(created.link.status).toEqual(expect.any(String));
55 expect(created.paypal.status).toEqual(expect.any(String));
56 expect(created.amazonPay.status).toEqual(expect.any(String));
57 expect(created.klarna.status).toEqual(expect.any(String));
58 expect(created.created).toEqual(expect.any(Number));
59 expect(created.livemode).toEqual(false);
60
61 const fetched = yield* GetPaymentMethodDomain({
62 payment_method_domain: created.id,
63 });
64 expect(fetched.id).toEqual(created.id);
65 expect(fetched.domain_name).toEqual("alchemy-pmd-lifecycle.example.com");
66 expect(fetched.enabled).toEqual(true);
67
68 const updated = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* Stripe.PaymentMethodDomain("Checkout", {
71 domainName: "alchemy-pmd-lifecycle.example.com",
72 enabled: false,
73 });
74 }),
75 );
76
77 expect(updated.id).toEqual(created.id);
78 expect(updated.domainName).toEqual("alchemy-pmd-lifecycle.example.com");
79 expect(updated.enabled).toEqual(false);
80
81 const refetched = yield* GetPaymentMethodDomain({
82 payment_method_domain: updated.id,
83 });
84 expect(refetched.id).toEqual(updated.id);
85 expect(refetched.domain_name).toEqual(
86 "alchemy-pmd-lifecycle.example.com",
87 );
88 expect(refetched.enabled).toEqual(false);
89
90 yield* stack.destroy();
91
92 const disabled = yield* waitUntilDisabled(created.id);
93 expect(disabled).toEqual("disabled");
94 const deactivated = yield* GetPaymentMethodDomain({
95 payment_method_domain: created.id,
96 });
97 expect(deactivated.enabled).toEqual(false);
98 }).pipe(logLevel),
99 {
100 tags: ["provider:stripe", "provider:stripe:paymentmethoddomain", "live"],
101 timeout: 120_000,
102 },
103);
104
105test.provider(
106 "list enumerates the deployed payment method domain",
107 (stack) =>
108 Effect.gen(function* () {
109 yield* stack.destroy();
110
111 const deployed = yield* stack.deploy(
112 Effect.gen(function* () {
113 return yield* Stripe.PaymentMethodDomain("ListCheckout", {
114 domainName: "alchemy-pmd-list.example.com",
115 });
116 }),
117 );
118
119 const provider = yield* Provider.findProvider(Stripe.PaymentMethodDomain);
120 const all = yield* provider.list();
121 const found = all.find((domain) => domain.id === deployed.id);
122 expect(found).toBeDefined();
123 expect(found?.domainName).toEqual(deployed.domainName);
124 expect(found?.enabled).toEqual(true);
125
126 yield* stack.destroy();
127
128 const disabled = yield* waitUntilDisabled(deployed.id);
129 expect(disabled).toEqual("disabled");
130
131 const after = yield* provider.list();
132 expect(after.find((domain) => domain.id === deployed.id)).toBeUndefined();
133 }).pipe(logLevel),
134 {
135 tags: ["provider:stripe", "provider:stripe:paymentmethoddomain", "live"],
136 timeout: 120_000,
137 },
138);