mppx 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Challenge.d.ts +18 -18
- package/dist/Challenge.d.ts.map +1 -1
- package/dist/Challenge.js +8 -8
- package/dist/Challenge.js.map +1 -1
- package/dist/Errors.d.ts +58 -8
- package/dist/Errors.d.ts.map +1 -1
- package/dist/Errors.js +51 -9
- package/dist/Errors.js.map +1 -1
- package/dist/Method.d.ts +154 -0
- package/dist/Method.d.ts.map +1 -0
- package/dist/Method.js +81 -0
- package/dist/Method.js.map +1 -0
- package/dist/PaymentRequest.d.ts +5 -5
- package/dist/PaymentRequest.d.ts.map +1 -1
- package/dist/PaymentRequest.js +11 -6
- package/dist/PaymentRequest.js.map +1 -1
- package/dist/cli.js +67 -18
- package/dist/cli.js.map +1 -1
- package/dist/client/Methods.d.ts +2 -2
- package/dist/client/Methods.d.ts.map +1 -1
- package/dist/client/Methods.js +2 -2
- package/dist/client/Methods.js.map +1 -1
- package/dist/client/Mppx.d.ts +7 -7
- package/dist/client/Mppx.d.ts.map +1 -1
- package/dist/client/Mppx.js +3 -3
- package/dist/client/Mppx.js.map +1 -1
- package/dist/client/internal/Fetch.d.ts +10 -10
- package/dist/client/internal/Fetch.d.ts.map +1 -1
- package/dist/client/internal/Fetch.js +2 -2
- package/dist/client/internal/Fetch.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp-sdk/client/McpClient.d.ts +6 -6
- package/dist/mcp-sdk/client/McpClient.d.ts.map +1 -1
- package/dist/mcp-sdk/client/McpClient.js +4 -4
- package/dist/mcp-sdk/client/McpClient.js.map +1 -1
- package/dist/middlewares/elysia.d.ts +1 -1
- package/dist/middlewares/express.d.ts +1 -1
- package/dist/middlewares/hono.d.ts +1 -1
- package/dist/middlewares/internal/mppx.d.ts +7 -7
- package/dist/middlewares/internal/mppx.d.ts.map +1 -1
- package/dist/middlewares/internal/mppx.js +5 -5
- package/dist/middlewares/internal/mppx.js.map +1 -1
- package/dist/middlewares/nextjs.d.ts +1 -1
- package/dist/proxy/Service.js +2 -2
- package/dist/proxy/Service.js.map +1 -1
- package/dist/server/Methods.d.ts +2 -2
- package/dist/server/Methods.d.ts.map +1 -1
- package/dist/server/Methods.js +2 -2
- package/dist/server/Methods.js.map +1 -1
- package/dist/server/Mppx.d.ts +17 -17
- package/dist/server/Mppx.d.ts.map +1 -1
- package/dist/server/Mppx.js +9 -9
- package/dist/server/Mppx.js.map +1 -1
- package/dist/stripe/{Intents.d.ts → Methods.d.ts} +22 -22
- package/dist/stripe/Methods.d.ts.map +1 -0
- package/dist/stripe/Methods.js +42 -0
- package/dist/stripe/Methods.js.map +1 -0
- package/dist/stripe/client/Charge.d.ts +40 -27
- package/dist/stripe/client/Charge.d.ts.map +1 -1
- package/dist/stripe/client/Charge.js +15 -7
- package/dist/stripe/client/Charge.js.map +1 -1
- package/dist/stripe/client/{MethodIntents.d.ts → Methods.d.ts} +24 -23
- package/dist/stripe/client/Methods.d.ts.map +1 -0
- package/dist/stripe/client/{MethodIntents.js → Methods.js} +3 -3
- package/dist/stripe/client/Methods.js.map +1 -0
- package/dist/stripe/client/index.d.ts +1 -1
- package/dist/stripe/client/index.d.ts.map +1 -1
- package/dist/stripe/client/index.js +1 -1
- package/dist/stripe/client/index.js.map +1 -1
- package/dist/stripe/index.d.ts +1 -1
- package/dist/stripe/index.d.ts.map +1 -1
- package/dist/stripe/index.js +1 -1
- package/dist/stripe/index.js.map +1 -1
- package/dist/stripe/internal/types.d.ts +25 -0
- package/dist/stripe/internal/types.d.ts.map +1 -0
- package/dist/stripe/internal/types.js +2 -0
- package/dist/stripe/internal/types.js.map +1 -0
- package/dist/stripe/server/Charge.d.ts +47 -28
- package/dist/stripe/server/Charge.d.ts.map +1 -1
- package/dist/stripe/server/Charge.js +90 -32
- package/dist/stripe/server/Charge.js.map +1 -1
- package/dist/stripe/server/{MethodIntents.d.ts → Methods.d.ts} +24 -23
- package/dist/stripe/server/Methods.d.ts.map +1 -0
- package/dist/stripe/server/{MethodIntents.js → Methods.js} +3 -3
- package/dist/stripe/server/Methods.js.map +1 -0
- package/dist/stripe/server/index.d.ts +1 -1
- package/dist/stripe/server/index.d.ts.map +1 -1
- package/dist/stripe/server/index.js +1 -1
- package/dist/stripe/server/index.js.map +1 -1
- package/dist/tempo/{Intents.d.ts → Methods.d.ts} +72 -69
- package/dist/tempo/Methods.d.ts.map +1 -0
- package/dist/tempo/Methods.js +118 -0
- package/dist/tempo/Methods.js.map +1 -0
- package/dist/tempo/client/ChannelOps.d.ts +1 -1
- package/dist/tempo/client/ChannelOps.js +1 -1
- package/dist/tempo/client/Charge.d.ts +25 -25
- package/dist/tempo/client/Charge.d.ts.map +1 -1
- package/dist/tempo/client/Charge.js +3 -3
- package/dist/tempo/client/Charge.js.map +1 -1
- package/dist/tempo/client/{MethodIntents.d.ts → Methods.d.ts} +74 -70
- package/dist/tempo/client/Methods.d.ts.map +1 -0
- package/dist/tempo/client/{MethodIntents.js → Methods.js} +3 -3
- package/dist/tempo/client/Methods.js.map +1 -0
- package/dist/tempo/client/Session.d.ts +49 -45
- package/dist/tempo/client/Session.d.ts.map +1 -1
- package/dist/tempo/client/Session.js +4 -4
- package/dist/tempo/client/Session.js.map +1 -1
- package/dist/tempo/client/SessionManager.d.ts +1 -1
- package/dist/tempo/client/SessionManager.js +1 -1
- package/dist/tempo/client/index.d.ts +1 -1
- package/dist/tempo/client/index.d.ts.map +1 -1
- package/dist/tempo/client/index.js +1 -1
- package/dist/tempo/client/index.js.map +1 -1
- package/dist/tempo/index.d.ts +1 -1
- package/dist/tempo/index.d.ts.map +1 -1
- package/dist/tempo/index.js +1 -1
- package/dist/tempo/index.js.map +1 -1
- package/dist/tempo/server/Charge.d.ts +27 -27
- package/dist/tempo/server/Charge.d.ts.map +1 -1
- package/dist/tempo/server/Charge.js +3 -3
- package/dist/tempo/server/Charge.js.map +1 -1
- package/dist/tempo/server/{MethodIntents.d.ts → Methods.d.ts} +73 -69
- package/dist/tempo/server/Methods.d.ts.map +1 -0
- package/dist/tempo/server/{MethodIntents.js → Methods.js} +4 -4
- package/dist/tempo/server/Methods.js.map +1 -0
- package/dist/tempo/server/Session.d.ts +51 -47
- package/dist/tempo/server/Session.d.ts.map +1 -1
- package/dist/tempo/server/Session.js +4 -4
- package/dist/tempo/server/Session.js.map +1 -1
- package/dist/tempo/server/index.d.ts +6 -0
- package/dist/tempo/server/index.d.ts.map +1 -0
- package/dist/tempo/server/index.js +6 -0
- package/dist/tempo/server/index.js.map +1 -0
- package/package.json +2 -1
- package/src/Challenge.test-d.ts +3 -3
- package/src/Challenge.test.ts +7 -7
- package/src/Challenge.ts +34 -34
- package/src/Errors.test.ts +75 -21
- package/src/Errors.ts +74 -9
- package/src/Method.test.ts +76 -0
- package/src/Method.ts +228 -0
- package/src/PaymentRequest.test.ts +5 -5
- package/src/PaymentRequest.ts +15 -10
- package/src/cli.test.ts +12 -22
- package/src/cli.ts +74 -21
- package/src/client/Methods.ts +2 -2
- package/src/client/Mppx.test-d.ts +6 -6
- package/src/client/Mppx.test.ts +26 -22
- package/src/client/Mppx.ts +10 -10
- package/src/client/Transport.test.ts +6 -6
- package/src/client/internal/Fetch.ts +21 -24
- package/src/index.ts +1 -2
- package/src/mcp-sdk/client/McpClient.test.ts +1 -1
- package/src/mcp-sdk/client/McpClient.ts +11 -13
- package/src/middlewares/elysia.ts +1 -1
- package/src/middlewares/express.ts +1 -1
- package/src/middlewares/hono.ts +1 -1
- package/src/middlewares/internal/mppx.ts +10 -10
- package/src/middlewares/nextjs.ts +1 -1
- package/src/proxy/Service.ts +2 -2
- package/src/server/Methods.ts +2 -2
- package/src/server/Mppx.test-d.ts +27 -29
- package/src/server/Mppx.test.ts +23 -19
- package/src/server/Mppx.ts +43 -43
- package/src/server/Transport.test.ts +8 -8
- package/src/stripe/{Intents.test.ts → Methods.test.ts} +12 -12
- package/src/stripe/Methods.ts +45 -0
- package/src/stripe/client/Charge.test.ts +189 -0
- package/src/stripe/client/Charge.ts +29 -16
- package/src/stripe/client/{MethodIntents.ts → Methods.ts} +2 -2
- package/src/stripe/client/index.ts +1 -1
- package/src/stripe/index.ts +1 -1
- package/src/stripe/internal/types.ts +22 -0
- package/src/stripe/server/Charge.test.ts +241 -0
- package/src/stripe/server/Charge.ts +124 -38
- package/src/stripe/server/{MethodIntents.ts → Methods.ts} +2 -2
- package/src/stripe/server/index.ts +1 -1
- package/src/tempo/{Intents.test.ts → Methods.test.ts} +15 -15
- package/src/tempo/{Intents.ts → Methods.ts} +77 -22
- package/src/tempo/client/ChannelOps.ts +1 -1
- package/src/tempo/client/Charge.ts +3 -3
- package/src/tempo/client/{MethodIntents.ts → Methods.ts} +2 -2
- package/src/tempo/client/Session.ts +4 -4
- package/src/tempo/client/SessionManager.ts +1 -1
- package/src/tempo/client/index.ts +1 -1
- package/src/tempo/index.ts +1 -1
- package/src/tempo/server/Charge.ts +4 -7
- package/src/tempo/server/{MethodIntents.ts → Methods.ts} +3 -3
- package/src/tempo/server/Session.test.ts +4 -7
- package/src/tempo/server/Session.ts +6 -6
- package/src/tempo/server/index.ts +1 -1
- package/dist/Intent.d.ts +0 -101
- package/dist/Intent.d.ts.map +0 -1
- package/dist/Intent.js +0 -83
- package/dist/Intent.js.map +0 -1
- package/dist/MethodIntent.d.ts +0 -225
- package/dist/MethodIntent.d.ts.map +0 -1
- package/dist/MethodIntent.js +0 -156
- package/dist/MethodIntent.js.map +0 -1
- package/dist/stripe/Intents.d.ts.map +0 -1
- package/dist/stripe/Intents.js +0 -27
- package/dist/stripe/Intents.js.map +0 -1
- package/dist/stripe/client/MethodIntents.d.ts.map +0 -1
- package/dist/stripe/client/MethodIntents.js.map +0 -1
- package/dist/stripe/server/MethodIntents.d.ts.map +0 -1
- package/dist/stripe/server/MethodIntents.js.map +0 -1
- package/dist/tempo/Intents.d.ts.map +0 -1
- package/dist/tempo/Intents.js +0 -81
- package/dist/tempo/Intents.js.map +0 -1
- package/dist/tempo/client/MethodIntents.d.ts.map +0 -1
- package/dist/tempo/client/MethodIntents.js.map +0 -1
- package/dist/tempo/server/MethodIntents.d.ts.map +0 -1
- package/dist/tempo/server/MethodIntents.js.map +0 -1
- package/src/Intent.test.ts +0 -180
- package/src/Intent.ts +0 -109
- package/src/MethodIntent.test.ts +0 -303
- package/src/MethodIntent.ts +0 -388
- package/src/stripe/Intents.ts +0 -27
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { Challenge, Credential } from 'mppx'
|
|
2
|
+
import { Mppx, stripe } from 'mppx/server'
|
|
3
|
+
import { afterEach, describe, expect, test, vi } from 'vitest'
|
|
4
|
+
import * as Http from '~test/Http.js'
|
|
5
|
+
import type { StripeClient } from '../internal/types.js'
|
|
6
|
+
|
|
7
|
+
const realm = 'api.example.com'
|
|
8
|
+
const secretKey = 'test-secret-key'
|
|
9
|
+
|
|
10
|
+
let httpServer: Awaited<ReturnType<typeof Http.createServer>> | undefined
|
|
11
|
+
|
|
12
|
+
afterEach(() => httpServer?.close())
|
|
13
|
+
|
|
14
|
+
function createMockStripeClient(
|
|
15
|
+
overrides?: Partial<{ status: string; id: string; throws: boolean }>,
|
|
16
|
+
): { client: StripeClient; create: ReturnType<typeof vi.fn> } {
|
|
17
|
+
const { status = 'succeeded', id = 'pi_mock_123', throws = false } = overrides ?? {}
|
|
18
|
+
const create = vi.fn(async () => {
|
|
19
|
+
if (throws) throw new Error('Stripe API error')
|
|
20
|
+
return { id, status }
|
|
21
|
+
})
|
|
22
|
+
return {
|
|
23
|
+
client: { paymentIntents: { create } },
|
|
24
|
+
create,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('stripe.charge with client', () => {
|
|
29
|
+
test('default: verifies payment via client.paymentIntents.create', async () => {
|
|
30
|
+
const { client, create } = createMockStripeClient()
|
|
31
|
+
|
|
32
|
+
const server = Mppx.create({
|
|
33
|
+
methods: [
|
|
34
|
+
stripe.charge({
|
|
35
|
+
client,
|
|
36
|
+
networkId: 'internal',
|
|
37
|
+
paymentMethodTypes: ['card'],
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
realm,
|
|
41
|
+
secretKey,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
httpServer = await Http.createServer(async (req, res) => {
|
|
45
|
+
const result = await Mppx.toNodeListener(
|
|
46
|
+
server.charge({ amount: '1', currency: 'usd', decimals: 2 }),
|
|
47
|
+
)(req, res)
|
|
48
|
+
if (result.status === 402) return
|
|
49
|
+
res.end('OK')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const response = await fetch(httpServer.url)
|
|
53
|
+
expect(response.status).toBe(402)
|
|
54
|
+
|
|
55
|
+
const challenge = Challenge.fromResponse(response)
|
|
56
|
+
const credential = Credential.from({
|
|
57
|
+
challenge,
|
|
58
|
+
payload: { spt: 'spt_test_token' },
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const paidResponse = await fetch(httpServer.url, {
|
|
62
|
+
headers: { Authorization: Credential.serialize(credential) },
|
|
63
|
+
})
|
|
64
|
+
expect(paidResponse.status).toBe(200)
|
|
65
|
+
expect(create).toHaveBeenCalledOnce()
|
|
66
|
+
|
|
67
|
+
const [params, options] = create.mock.calls[0]!
|
|
68
|
+
expect(params).toMatchObject({
|
|
69
|
+
amount: 100,
|
|
70
|
+
confirm: true,
|
|
71
|
+
currency: 'usd',
|
|
72
|
+
payment_method: 'spt_test_token',
|
|
73
|
+
})
|
|
74
|
+
expect(params.automatic_payment_methods).toMatchObject({
|
|
75
|
+
allow_redirects: 'never',
|
|
76
|
+
enabled: true,
|
|
77
|
+
})
|
|
78
|
+
expect(options.idempotencyKey).toMatch(/^mppx_/)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('behavior: includes metadata in client call', async () => {
|
|
82
|
+
const { client, create } = createMockStripeClient()
|
|
83
|
+
|
|
84
|
+
const server = Mppx.create({
|
|
85
|
+
methods: [
|
|
86
|
+
stripe.charge({
|
|
87
|
+
client,
|
|
88
|
+
metadata: { plan: 'pro' },
|
|
89
|
+
networkId: 'internal',
|
|
90
|
+
paymentMethodTypes: ['card'],
|
|
91
|
+
}),
|
|
92
|
+
],
|
|
93
|
+
realm,
|
|
94
|
+
secretKey,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
httpServer = await Http.createServer(async (req, res) => {
|
|
98
|
+
const result = await Mppx.toNodeListener(
|
|
99
|
+
server.charge({ amount: '1', currency: 'usd', decimals: 2 }),
|
|
100
|
+
)(req, res)
|
|
101
|
+
if (result.status === 402) return
|
|
102
|
+
res.end('OK')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const response = await fetch(httpServer.url)
|
|
106
|
+
const challenge = Challenge.fromResponse(response)
|
|
107
|
+
const credential = Credential.from({
|
|
108
|
+
challenge,
|
|
109
|
+
payload: { spt: 'spt_test_token' },
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
await fetch(httpServer.url, {
|
|
113
|
+
headers: { Authorization: Credential.serialize(credential) },
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
const [params] = create.mock.calls[0]!
|
|
117
|
+
expect(params.metadata).toMatchObject({ plan: 'pro' })
|
|
118
|
+
expect(params.metadata.mpp_is_mpp).toBe('true')
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('behavior: rejects when client throws', async () => {
|
|
122
|
+
const { client } = createMockStripeClient({ throws: true })
|
|
123
|
+
|
|
124
|
+
const server = Mppx.create({
|
|
125
|
+
methods: [
|
|
126
|
+
stripe.charge({
|
|
127
|
+
client,
|
|
128
|
+
networkId: 'internal',
|
|
129
|
+
paymentMethodTypes: ['card'],
|
|
130
|
+
}),
|
|
131
|
+
],
|
|
132
|
+
realm,
|
|
133
|
+
secretKey,
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
httpServer = await Http.createServer(async (req, res) => {
|
|
137
|
+
const result = await Mppx.toNodeListener(
|
|
138
|
+
server.charge({ amount: '1', currency: 'usd', decimals: 2 }),
|
|
139
|
+
)(req, res)
|
|
140
|
+
if (result.status === 402) return
|
|
141
|
+
res.end('OK')
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const response = await fetch(httpServer.url)
|
|
145
|
+
const challenge = Challenge.fromResponse(response)
|
|
146
|
+
const credential = Credential.from({
|
|
147
|
+
challenge,
|
|
148
|
+
payload: { spt: 'spt_test_token' },
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
const paidResponse = await fetch(httpServer.url, {
|
|
152
|
+
headers: { Authorization: Credential.serialize(credential) },
|
|
153
|
+
})
|
|
154
|
+
expect(paidResponse.status).toBe(402)
|
|
155
|
+
const body = (await paidResponse.json()) as { detail: string }
|
|
156
|
+
expect(body.detail).toContain('Stripe PaymentIntent failed')
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('behavior: rejects requires_action status', async () => {
|
|
160
|
+
const { client } = createMockStripeClient({ status: 'requires_action' })
|
|
161
|
+
|
|
162
|
+
const server = Mppx.create({
|
|
163
|
+
methods: [
|
|
164
|
+
stripe.charge({
|
|
165
|
+
client,
|
|
166
|
+
networkId: 'internal',
|
|
167
|
+
paymentMethodTypes: ['card'],
|
|
168
|
+
}),
|
|
169
|
+
],
|
|
170
|
+
realm,
|
|
171
|
+
secretKey,
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
httpServer = await Http.createServer(async (req, res) => {
|
|
175
|
+
const result = await Mppx.toNodeListener(
|
|
176
|
+
server.charge({ amount: '1', currency: 'usd', decimals: 2 }),
|
|
177
|
+
)(req, res)
|
|
178
|
+
if (result.status === 402) return
|
|
179
|
+
res.end('OK')
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
const response = await fetch(httpServer.url)
|
|
183
|
+
const challenge = Challenge.fromResponse(response)
|
|
184
|
+
const credential = Credential.from({
|
|
185
|
+
challenge,
|
|
186
|
+
payload: { spt: 'spt_test_token' },
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
const paidResponse = await fetch(httpServer.url, {
|
|
190
|
+
headers: { Authorization: Credential.serialize(credential) },
|
|
191
|
+
})
|
|
192
|
+
expect(paidResponse.status).toBe(402)
|
|
193
|
+
const body = (await paidResponse.json()) as { detail: string }
|
|
194
|
+
expect(body.detail).toContain('requires action')
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
test('behavior: receipt contains mock reference', async () => {
|
|
198
|
+
const { client } = createMockStripeClient({ id: 'pi_custom_ref' })
|
|
199
|
+
|
|
200
|
+
const server = Mppx.create({
|
|
201
|
+
methods: [
|
|
202
|
+
stripe.charge({
|
|
203
|
+
client,
|
|
204
|
+
networkId: 'internal',
|
|
205
|
+
paymentMethodTypes: ['card'],
|
|
206
|
+
}),
|
|
207
|
+
],
|
|
208
|
+
realm,
|
|
209
|
+
secretKey,
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
const handle = server.charge({ amount: '1', currency: 'usd', decimals: 2 })
|
|
213
|
+
|
|
214
|
+
const firstResult = await handle(new Request('https://example.com'))
|
|
215
|
+
expect(firstResult.status).toBe(402)
|
|
216
|
+
if (firstResult.status !== 402) throw new Error()
|
|
217
|
+
|
|
218
|
+
const challenge = Challenge.fromResponse(firstResult.challenge)
|
|
219
|
+
const credential = Credential.from({
|
|
220
|
+
challenge,
|
|
221
|
+
payload: { spt: 'spt_test_token' },
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
const result = await handle(
|
|
225
|
+
new Request('https://example.com', {
|
|
226
|
+
headers: { Authorization: Credential.serialize(credential) },
|
|
227
|
+
}),
|
|
228
|
+
)
|
|
229
|
+
expect(result.status).toBe(200)
|
|
230
|
+
if (result.status !== 200) throw new Error()
|
|
231
|
+
|
|
232
|
+
const wrapped = result.withReceipt(Response.json({ ok: true }))
|
|
233
|
+
const receiptHeader = wrapped.headers.get('Payment-Receipt')
|
|
234
|
+
expect(receiptHeader).toBeTruthy()
|
|
235
|
+
|
|
236
|
+
const decoded = JSON.parse(
|
|
237
|
+
Buffer.from(receiptHeader!.replace('Payment ', ''), 'base64url').toString(),
|
|
238
|
+
) as { reference: string }
|
|
239
|
+
expect(decoded.reference).toBe('pi_custom_ref')
|
|
240
|
+
})
|
|
241
|
+
})
|
|
@@ -1,22 +1,37 @@
|
|
|
1
|
+
import type * as Credential from '../../Credential.js'
|
|
1
2
|
import {
|
|
2
3
|
PaymentActionRequiredError,
|
|
3
4
|
PaymentExpiredError,
|
|
4
5
|
VerificationFailedError,
|
|
5
6
|
} from '../../Errors.js'
|
|
6
|
-
import type { LooseOmit } from '../../internal/types.js'
|
|
7
|
-
import * as
|
|
8
|
-
import
|
|
7
|
+
import type { LooseOmit, OneOf } from '../../internal/types.js'
|
|
8
|
+
import * as Method from '../../Method.js'
|
|
9
|
+
import type { StripeClient } from '../internal/types.js'
|
|
10
|
+
import * as Methods from '../Methods.js'
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Creates a Stripe charge method intent for usage on the server.
|
|
12
14
|
*
|
|
13
15
|
* Verifies payment by creating a Stripe PaymentIntent with the provided SPT.
|
|
14
16
|
*
|
|
17
|
+
* Accepts either a `client` (a pre-configured Stripe SDK instance) or a raw
|
|
18
|
+
* `secretKey`. Using `client` is recommended—it lets you configure retries,
|
|
19
|
+
* API version, and other options on the Stripe instance you control.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import Stripe from 'stripe'
|
|
24
|
+
* import { stripe } from 'mppx/server'
|
|
25
|
+
*
|
|
26
|
+
* const stripeClient = new Stripe(process.env.STRIPE_SECRET_KEY!)
|
|
27
|
+
* const charge = stripe.charge({ client: stripeClient, networkId: 'internal', paymentMethodTypes: ['card'] })
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
15
30
|
* @example
|
|
16
31
|
* ```ts
|
|
17
32
|
* import { stripe } from 'mppx/server'
|
|
18
33
|
*
|
|
19
|
-
* const charge = stripe.charge({ secretKey: 'sk_...' })
|
|
34
|
+
* const charge = stripe.charge({ secretKey: 'sk_...', networkId: 'internal', paymentMethodTypes: ['card'] })
|
|
20
35
|
* ```
|
|
21
36
|
*/
|
|
22
37
|
export function charge<const parameters extends charge.Parameters>(parameters: parameters) {
|
|
@@ -29,11 +44,13 @@ export function charge<const parameters extends charge.Parameters>(parameters: p
|
|
|
29
44
|
metadata,
|
|
30
45
|
networkId,
|
|
31
46
|
paymentMethodTypes,
|
|
32
|
-
secretKey,
|
|
33
47
|
} = parameters
|
|
34
48
|
|
|
49
|
+
const client = 'client' in parameters ? parameters.client : undefined
|
|
50
|
+
const secretKey = 'secretKey' in parameters ? parameters.secretKey : undefined
|
|
51
|
+
|
|
35
52
|
type Defaults = charge.DeriveDefaults<parameters>
|
|
36
|
-
return
|
|
53
|
+
return Method.toServer<typeof Methods.charge, Defaults>(Methods.charge, {
|
|
37
54
|
defaults: {
|
|
38
55
|
amount,
|
|
39
56
|
currency,
|
|
@@ -52,41 +69,25 @@ export function charge<const parameters extends charge.Parameters>(parameters: p
|
|
|
52
69
|
if (request.expires && new Date(request.expires) < new Date())
|
|
53
70
|
throw new PaymentExpiredError({ expires: request.expires })
|
|
54
71
|
|
|
55
|
-
const parsed =
|
|
72
|
+
const parsed = Methods.charge.schema.credential.payload.safeParse(credential.payload)
|
|
56
73
|
if (!parsed.success) throw new Error('Invalid credential payload: missing or malformed spt')
|
|
57
74
|
const { spt, externalId: credentialExternalId } = parsed.data as {
|
|
58
75
|
spt: string
|
|
59
76
|
externalId?: string
|
|
60
77
|
}
|
|
61
78
|
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
currency: request.currency as string,
|
|
65
|
-
shared_payment_granted_token: spt,
|
|
66
|
-
confirm: 'true',
|
|
67
|
-
'automatic_payment_methods[enabled]': 'true',
|
|
68
|
-
'automatic_payment_methods[allow_redirects]': 'never',
|
|
69
|
-
})
|
|
70
|
-
const resolvedMetadata = request.methodDetails?.metadata as Record<string, string> | undefined
|
|
71
|
-
if (resolvedMetadata) {
|
|
72
|
-
for (const [key, value] of Object.entries(resolvedMetadata)) {
|
|
73
|
-
body.set(`metadata[${key}]`, value)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
79
|
+
const userMetadata = request.methodDetails?.metadata as Record<string, string> | undefined
|
|
80
|
+
const resolvedMetadata = { ...buildAnalytics({ credential }), ...userMetadata }
|
|
76
81
|
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (!response.ok) throw new VerificationFailedError({ reason: 'Stripe PaymentIntent failed' })
|
|
88
|
-
|
|
89
|
-
const pi = (await response.json()) as { id: string; status: string }
|
|
82
|
+
const pi = client
|
|
83
|
+
? await createWithClient({ client, challenge, request, spt, metadata: resolvedMetadata })
|
|
84
|
+
: await createWithSecretKey({
|
|
85
|
+
secretKey: secretKey!,
|
|
86
|
+
challenge,
|
|
87
|
+
request,
|
|
88
|
+
spt,
|
|
89
|
+
metadata: resolvedMetadata,
|
|
90
|
+
})
|
|
90
91
|
|
|
91
92
|
if (pi.status === 'requires_action') {
|
|
92
93
|
throw new PaymentActionRequiredError({ reason: 'Stripe PaymentIntent requires action' })
|
|
@@ -105,17 +106,102 @@ export function charge<const parameters extends charge.Parameters>(parameters: p
|
|
|
105
106
|
}
|
|
106
107
|
|
|
107
108
|
export declare namespace charge {
|
|
108
|
-
type Defaults = LooseOmit<
|
|
109
|
+
type Defaults = LooseOmit<Method.RequestDefaults<typeof Methods.charge>, 'recipient'>
|
|
109
110
|
|
|
110
111
|
type Parameters = {
|
|
111
|
-
/** Stripe secret API key. */
|
|
112
|
-
secretKey: string
|
|
113
112
|
/** Optional metadata to include in SPT creation requests. */
|
|
114
113
|
metadata?: Record<string, string> | undefined
|
|
115
|
-
} & Defaults
|
|
114
|
+
} & Defaults &
|
|
115
|
+
OneOf<
|
|
116
|
+
| {
|
|
117
|
+
/** Pre-configured Stripe SDK instance. Any object matching the duck-typed `StripeClient` shape works. */
|
|
118
|
+
client: StripeClient
|
|
119
|
+
}
|
|
120
|
+
| {
|
|
121
|
+
/** Stripe secret API key. */
|
|
122
|
+
secretKey: string
|
|
123
|
+
}
|
|
124
|
+
>
|
|
116
125
|
|
|
117
126
|
type DeriveDefaults<parameters extends Parameters> = Pick<
|
|
118
127
|
parameters,
|
|
119
128
|
Extract<keyof parameters, keyof Defaults>
|
|
120
129
|
> & { decimals: number }
|
|
121
130
|
}
|
|
131
|
+
|
|
132
|
+
/** Creates a PaymentIntent using the Stripe SDK client. */
|
|
133
|
+
async function createWithClient(parameters: {
|
|
134
|
+
client: StripeClient
|
|
135
|
+
challenge: { id: string }
|
|
136
|
+
metadata: Record<string, string>
|
|
137
|
+
request: { amount: unknown; currency: unknown }
|
|
138
|
+
spt: string
|
|
139
|
+
}): Promise<{ id: string; status: string }> {
|
|
140
|
+
const { client, challenge, metadata, request, spt } = parameters
|
|
141
|
+
try {
|
|
142
|
+
const result = await client.paymentIntents.create(
|
|
143
|
+
{
|
|
144
|
+
amount: Number(request.amount),
|
|
145
|
+
automatic_payment_methods: { allow_redirects: 'never', enabled: true },
|
|
146
|
+
confirm: true,
|
|
147
|
+
currency: request.currency as string,
|
|
148
|
+
metadata,
|
|
149
|
+
payment_method: spt,
|
|
150
|
+
},
|
|
151
|
+
{ idempotencyKey: `mppx_${challenge.id}_${spt}` },
|
|
152
|
+
)
|
|
153
|
+
return { id: result.id, status: result.status }
|
|
154
|
+
} catch {
|
|
155
|
+
throw new VerificationFailedError({ reason: 'Stripe PaymentIntent failed' })
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Creates a PaymentIntent using a raw secret key and fetch. */
|
|
160
|
+
async function createWithSecretKey(parameters: {
|
|
161
|
+
secretKey: string
|
|
162
|
+
challenge: { id: string }
|
|
163
|
+
metadata: Record<string, string>
|
|
164
|
+
request: { amount: unknown; currency: unknown }
|
|
165
|
+
spt: string
|
|
166
|
+
}): Promise<{ id: string; status: string }> {
|
|
167
|
+
const { secretKey, challenge, metadata, request, spt } = parameters
|
|
168
|
+
|
|
169
|
+
const body = new URLSearchParams({
|
|
170
|
+
amount: request.amount as string,
|
|
171
|
+
'automatic_payment_methods[allow_redirects]': 'never',
|
|
172
|
+
'automatic_payment_methods[enabled]': 'true',
|
|
173
|
+
confirm: 'true',
|
|
174
|
+
currency: request.currency as string,
|
|
175
|
+
shared_payment_granted_token: spt,
|
|
176
|
+
})
|
|
177
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
178
|
+
body.set(`metadata[${key}]`, value)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const response = await fetch('https://api.stripe.com/v1/payment_intents', {
|
|
182
|
+
method: 'POST',
|
|
183
|
+
headers: {
|
|
184
|
+
Authorization: `Basic ${btoa(`${secretKey}:`)}`,
|
|
185
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
186
|
+
'Idempotency-Key': `mppx_${challenge.id}_${spt}`,
|
|
187
|
+
},
|
|
188
|
+
body,
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
if (!response.ok) throw new VerificationFailedError({ reason: 'Stripe PaymentIntent failed' })
|
|
192
|
+
return (await response.json()) as { id: string; status: string }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** @internal */
|
|
196
|
+
function buildAnalytics(parameters: { credential: Credential.Credential }): Record<string, string> {
|
|
197
|
+
const { credential } = parameters
|
|
198
|
+
const { challenge } = credential
|
|
199
|
+
return {
|
|
200
|
+
mpp_version: '1',
|
|
201
|
+
mpp_is_mpp: 'true',
|
|
202
|
+
mpp_intent: challenge.intent,
|
|
203
|
+
mpp_challenge_id: challenge.id,
|
|
204
|
+
mpp_server_id: challenge.realm,
|
|
205
|
+
...(credential.source ? { mpp_client_id: credential.source } : {}),
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { charge as charge_ } from './Charge.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Creates a Stripe `charge` method
|
|
4
|
+
* Creates a Stripe `charge` method for usage on the server.
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```ts
|
|
@@ -19,6 +19,6 @@ export function stripe<const parameters extends stripe.Parameters>(parameters: p
|
|
|
19
19
|
export namespace stripe {
|
|
20
20
|
export type Parameters = charge_.Parameters
|
|
21
21
|
|
|
22
|
-
/** Creates a Stripe `charge` method
|
|
22
|
+
/** Creates a Stripe `charge` method for SPT-based payments. */
|
|
23
23
|
export const charge = charge_
|
|
24
24
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { charge } from './Charge.js'
|
|
2
|
-
export { stripe } from './
|
|
2
|
+
export { stripe } from './Methods.js'
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Methods } from 'mppx/tempo'
|
|
2
2
|
import { describe, expect, expectTypeOf, test } from 'vitest'
|
|
3
3
|
|
|
4
4
|
describe('charge', () => {
|
|
5
|
-
test('has correct name and
|
|
6
|
-
expect(
|
|
7
|
-
expect(
|
|
5
|
+
test('has correct name and intent', () => {
|
|
6
|
+
expect(Methods.charge.intent).toBe('charge')
|
|
7
|
+
expect(Methods.charge.name).toBe('tempo')
|
|
8
8
|
})
|
|
9
9
|
|
|
10
|
-
test('types:
|
|
11
|
-
expectTypeOf(
|
|
10
|
+
test('types: intent is literal', () => {
|
|
11
|
+
expectTypeOf(Methods.charge.intent).toEqualTypeOf<'charge'>()
|
|
12
12
|
})
|
|
13
13
|
|
|
14
|
-
test('types:
|
|
15
|
-
expectTypeOf(
|
|
14
|
+
test('types: name is literal', () => {
|
|
15
|
+
expectTypeOf(Methods.charge.name).toEqualTypeOf<'tempo'>()
|
|
16
16
|
})
|
|
17
17
|
|
|
18
18
|
test('schema: validates valid request', () => {
|
|
19
|
-
const result =
|
|
19
|
+
const result = Methods.charge.schema.request.safeParse({
|
|
20
20
|
amount: '1',
|
|
21
21
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
22
22
|
decimals: 6,
|
|
@@ -27,7 +27,7 @@ describe('charge', () => {
|
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
test('schema: validates request with methodDetails', () => {
|
|
30
|
-
const result =
|
|
30
|
+
const result = Methods.charge.schema.request.safeParse({
|
|
31
31
|
amount: '1',
|
|
32
32
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
33
33
|
decimals: 6,
|
|
@@ -40,7 +40,7 @@ describe('charge', () => {
|
|
|
40
40
|
})
|
|
41
41
|
|
|
42
42
|
test('schema: validates request with memo', () => {
|
|
43
|
-
const result =
|
|
43
|
+
const result = Methods.charge.schema.request.safeParse({
|
|
44
44
|
amount: '1',
|
|
45
45
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
46
46
|
decimals: 6,
|
|
@@ -52,14 +52,14 @@ describe('charge', () => {
|
|
|
52
52
|
})
|
|
53
53
|
|
|
54
54
|
test('schema: rejects invalid request', () => {
|
|
55
|
-
const result =
|
|
55
|
+
const result = Methods.charge.schema.request.safeParse({
|
|
56
56
|
amount: '1',
|
|
57
57
|
})
|
|
58
58
|
expect(result.success).toBe(false)
|
|
59
59
|
})
|
|
60
60
|
|
|
61
61
|
test('schema: validates transaction payload', () => {
|
|
62
|
-
const result =
|
|
62
|
+
const result = Methods.charge.schema.credential.payload.safeParse({
|
|
63
63
|
signature: '0x76f90100000000000000000000000000000000000000000000000000000000000000000000',
|
|
64
64
|
type: 'transaction',
|
|
65
65
|
})
|
|
@@ -67,7 +67,7 @@ describe('charge', () => {
|
|
|
67
67
|
})
|
|
68
68
|
|
|
69
69
|
test('schema: validates hash payload', () => {
|
|
70
|
-
const result =
|
|
70
|
+
const result = Methods.charge.schema.credential.payload.safeParse({
|
|
71
71
|
hash: '0x1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890',
|
|
72
72
|
type: 'hash',
|
|
73
73
|
})
|
|
@@ -75,7 +75,7 @@ describe('charge', () => {
|
|
|
75
75
|
})
|
|
76
76
|
|
|
77
77
|
test('schema: rejects invalid payload type', () => {
|
|
78
|
-
const result =
|
|
78
|
+
const result = Methods.charge.schema.credential.payload.safeParse({
|
|
79
79
|
signature: '0x...',
|
|
80
80
|
type: 'keyAuthorization',
|
|
81
81
|
})
|