-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathwhop.test.ts
More file actions
203 lines (186 loc) · 6.25 KB
/
Copy pathwhop.test.ts
File metadata and controls
203 lines (186 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { PAYKIT_ERROR_CODES } from "paykitjs";
import { describe, expect, it, vi } from "vitest";
import { createWhopProvider } from "../whop-provider";
type WhopClientMock = {
checkoutConfigurations: { create: ReturnType<typeof vi.fn> };
memberships: {
cancel: ReturnType<typeof vi.fn>;
list: ReturnType<typeof vi.fn>;
resume: ReturnType<typeof vi.fn>;
};
webhooks: { unwrap: ReturnType<typeof vi.fn> };
payments: { list: ReturnType<typeof vi.fn> };
members: { list: ReturnType<typeof vi.fn> };
};
function createClient(overrides: Partial<WhopClientMock> = {}): WhopClientMock {
const base: WhopClientMock = {
checkoutConfigurations: { create: vi.fn() },
memberships: { cancel: vi.fn(), list: vi.fn(), resume: vi.fn() },
webhooks: { unwrap: vi.fn() },
payments: { list: vi.fn() },
members: { list: vi.fn() },
};
return {
...base,
...overrides,
checkoutConfigurations: {
...base.checkoutConfigurations,
...overrides.checkoutConfigurations,
},
memberships: {
...base.memberships,
...overrides.memberships,
},
webhooks: {
...base.webhooks,
...overrides.webhooks,
},
payments: {
...base.payments,
...overrides.payments,
},
members: {
...base.members,
...overrides.members,
},
};
}
describe("providers/whop", () => {
it("creates a checkout session and returns a payment URL", async () => {
const createCheckout = vi
.fn()
.mockResolvedValue({ id: "checkout_123", purchase_url: "https://whop.com/p/123" });
const client = createClient({ checkoutConfigurations: { create: createCheckout } });
const provider = createWhopProvider(client as never, {
apiKey: "whop_test_123",
companyId: "biz_123",
});
const result = await provider.createSubscriptionCheckout({
cancelUrl: "https://example.com/cancel",
metadata: { ref: "abc" },
providerCustomerId: "cus_123",
providerProduct: { productId: "plan_123" },
successUrl: "https://example.com/success",
});
expect(createCheckout).toHaveBeenCalledWith({
plan_id: "plan_123",
metadata: { ref: "abc" },
redirect_url: "https://example.com/success",
});
expect(result).toEqual({
paymentUrl: "https://whop.com/p/123",
providerCheckoutSessionId: "checkout_123",
});
});
it("throws when a checkout session does not include a purchase URL", async () => {
const createCheckout = vi.fn().mockResolvedValue({ id: "checkout_123" });
const client = createClient({ checkoutConfigurations: { create: createCheckout } });
const provider = createWhopProvider(client as never, {
apiKey: "whop_test_123",
companyId: "biz_123",
});
await expect(
provider.createSubscriptionCheckout({
providerCustomerId: "cus_123",
providerProduct: { productId: "plan_123" },
successUrl: "https://example.com/success",
}),
).rejects.toMatchObject({
code: PAYKIT_ERROR_CODES.PROVIDER_SESSION_INVALID.code,
});
});
it("normalizes subscription events from membership webhooks", async () => {
const unwrap = vi.fn().mockReturnValue({
type: "membership.cancel_at_period_end_changed",
data: {
id: "sub_123",
user: { id: "user_123" },
product: { id: "prod_123" },
cancel_at_period_end: true,
canceled_at: "2024-01-10T00:00:00.000Z",
renewal_period_start: "2024-01-01T00:00:00.000Z",
renewal_period_end: "2024-02-01T00:00:00.000Z",
status: "active",
},
});
const client = createClient({ webhooks: { unwrap } });
const provider = createWhopProvider(client as never, {
apiKey: "whop_test_123",
companyId: "biz_123",
});
const [event] = await provider.handleWebhook({
body: "{}",
headers: { "webhook-id": "evt_123" },
});
expect(event.name).toBe("subscription.updated");
expect(event.payload.providerEventId).toBe("evt_123");
expect(event.payload.providerCustomerId).toBe("user_123");
expect(event.payload.subscription).toMatchObject({
cancelAtPeriodEnd: true,
providerProduct: { productId: "prod_123" },
providerSubscriptionId: "sub_123",
providerSubscriptionScheduleId: null,
status: "active",
});
expect(event.payload.subscription?.canceledAt).toEqual(new Date("2024-01-10T00:00:00.000Z"));
expect(event.payload.subscription?.currentPeriodStartAt).toEqual(
new Date("2024-01-01T00:00:00.000Z"),
);
expect(event.payload.subscription?.currentPeriodEndAt).toEqual(
new Date("2024-02-01T00:00:00.000Z"),
);
expect(event.payload.subscription?.endedAt).toBeNull();
});
it("normalizes checkout events from payment webhooks", async () => {
const unwrap = vi.fn().mockReturnValue({
type: "payment.created",
data: {
id: "pay_123",
status: "paid",
user: { id: "user_123" },
membership: { id: "sub_456" },
metadata: { seats: 2, note: "VIP" },
},
});
const client = createClient({ webhooks: { unwrap } });
const provider = createWhopProvider(client as never, {
apiKey: "whop_test_123",
companyId: "biz_123",
});
const [event] = await provider.handleWebhook({
body: "{}",
headers: { "webhook-id": "evt_456" },
});
expect(event).toEqual({
name: "checkout.completed",
payload: {
checkoutSessionId: "pay_123",
mode: "subscription",
paymentStatus: "paid",
providerCustomerId: "user_123",
providerEventId: "evt_456",
providerSubscriptionId: "sub_456",
status: "paid",
metadata: { seats: "2", note: "VIP" },
},
});
});
it("throws a clear error when webhook signatures are invalid", async () => {
const unwrap = vi.fn().mockImplementation(() => {
throw new Error("invalid");
});
const client = createClient({ webhooks: { unwrap } });
const provider = createWhopProvider(client as never, {
apiKey: "whop_test_123",
companyId: "biz_123",
});
await expect(
provider.handleWebhook({
body: "{}",
headers: { "webhook-id": "evt_789" },
}),
).rejects.toMatchObject({
code: PAYKIT_ERROR_CODES.PROVIDER_SIGNATURE_MISSING.code,
});
});
});