@yaks/stripe 0.0.0-stage → 0.2.36

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/testing.js ADDED
@@ -0,0 +1,72 @@
1
+ // What Stripe sends, for a test that stands in for it.
2
+ /**
3
+ * A `Stripe-Signature` header over exactly these bytes: HMAC-SHA256 of
4
+ * `<timestamp>.<raw body>`, keyed by the endpoint's signing secret. The scheme
5
+ * is the same for a platform endpoint and a Connect one; only the secret
6
+ * differs. @yaks/hook's `refusal('stripe', …)` verifies it.
7
+ */ export let signed = async (secret, raw, at)=>{
8
+ let key = await crypto.subtle.importKey('raw', new TextEncoder().encode(secret), {
9
+ name: 'HMAC',
10
+ hash: 'SHA-256'
11
+ }, false, [
12
+ 'sign'
13
+ ]);
14
+ let mac = await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(`${at}.${raw}`));
15
+ let hex = [
16
+ ...new Uint8Array(mac)
17
+ ].map((n)=>n.toString(16).padStart(2, '0')).join('');
18
+ return `t=${at},v1=${hex}`;
19
+ };
20
+ /**
21
+ * A subscription shaped the way ./plans.ts `read` reads one, and no more,
22
+ * tagged with `metadata`: active, paid a month ahead, under ids of its own. A
23
+ * `customer.subscription.*` event carries its subscription whole and is read
24
+ * without asking Stripe back, so this stands in for one Stripe never held.
25
+ * `over` sets the fields a particular delivery turns on, such as a
26
+ * cancellation's `status` and `ended_at`.
27
+ */ export let fakeSub = (metadata, over = {})=>{
28
+ let id = crypto.randomUUID().replaceAll('-', '');
29
+ return {
30
+ id: `sub_probe${id}`,
31
+ customer: `cus_probe${id}`,
32
+ status: 'active',
33
+ metadata,
34
+ current_period_end: Math.floor(Date.now() / 1000) + 30 * 86400,
35
+ ...over
36
+ };
37
+ };
38
+ /**
39
+ * An event about `object`, signed with `secret` as Stripe signs it and posted
40
+ * to `path` through `post`: the door's answer, or a throw where it refused.
41
+ * `on` is the connected account the event is about, which Stripe puts on the
42
+ * envelope rather than in the object; `at` is when Stripe made it, and a
43
+ * redelivery is the same event again, so a test that replays one passes the
44
+ * same moment twice.
45
+ */ export let delivered = async (post, path, secret, type, object, on, at = Math.floor(Date.now() / 1000))=>{
46
+ let raw = JSON.stringify({
47
+ id: `evt_${crypto.randomUUID()}`,
48
+ type,
49
+ created: at,
50
+ // The envelope says the object's mode, which is the sandbox's unless the
51
+ // object says otherwise.
52
+ livemode: object.livemode ?? false,
53
+ ...on ? {
54
+ account: on
55
+ } : {},
56
+ data: {
57
+ object
58
+ }
59
+ });
60
+ let r = await post(path, {
61
+ method: 'POST',
62
+ body: raw,
63
+ headers: {
64
+ 'content-type': 'application/json',
65
+ 'stripe-signature': await signed(secret, raw, at)
66
+ }
67
+ });
68
+ let said = await r.text();
69
+ if (!r.ok) throw new Error(`${path}: ${r.status} ${said}`);
70
+ return said;
71
+ };
72
+ //# sourceMappingURL=testing.js.map
package/testing.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.js","sources":["./testing.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAEvD;;;;;CAKC,GACD,OAAO,IAAI,SAAS,OAClB,QACA,KACA;EAEA,IAAI,MAAM,MAAM,OAAO,MAAM,CAAC,SAAS,CACrC,OACA,IAAI,cAAc,MAAM,CAAC,SACzB;IAAE,MAAM;IAAQ,MAAM;EAAU,GAChC,OACA;IAAC;GAAO;EAEV,IAAI,MAAM,MAAM,OAAO,MAAM,CAAC,IAAI,CAChC,QACA,KACA,IAAI,cAAc,MAAM,CAAC,GAAG,GAAG,CAAC,EAAE,KAAK;EAEzC,IAAI,MAAM;OAAI,IAAI,WAAW;GAAK,CAC/B,GAAG,CAAC,CAAC,IAAM,EAAE,QAAQ,CAAC,IAAI,QAAQ,CAAC,GAAG,MAAM,IAAI,CAAC;EACpD,OAAO,CAAC,EAAE,EAAE,GAAG,IAAI,EAAE,KAAK;AAC5B,EAAC;AAED;;;;;;;CAOC,GACD,OAAO,IAAI,UAAU,CACnB,UACA,OAAgC,CAAC,CAAC;EASlC,IAAI,KAAK,OAAO,UAAU,GAAG,UAAU,CAAC,KAAK;EAC7C,OAAO;IACL,IAAI,CAAC,SAAS,EAAE,IAAI;IACpB,UAAU,CAAC,SAAS,EAAE,IAAI;IAC1B,QAAQ;IACR;IACA,oBAAoB,KAAK,KAAK,CAAC,KAAK,GAAG,KAAK,QAAQ,KAAK;IACzD,GAAG,IAAI;EACT;AACF,EAAC;AAED;;;;;;;CAOC,GACD,OAAO,IAAI,YAAY,OACrB,MACA,MACA,QACA,MACA,QACA,IACA,KAAa,KAAK,KAAK,CAAC,KAAK,GAAG,KAAK,KAAK;EAE1C,IAAI,MAAM,KAAK,SAAS,CAAC;IACvB,IAAI,CAAC,IAAI,EAAE,OAAO,UAAU,IAAI;IAChC;IACA,SAAS;IACT,yEAAyE;IACzE,yBAAyB;IACzB,UAAU,AAAC,OAAkC,QAAQ,IAAI;IACzD,GAAI,KAAK;MAAE,SAAS;IAAG,IAAI,CAAC,CAAC;IAC7B,MAAM;MAAE;IAAO;EACjB;EACA,IAAI,IAAI,MAAM,KAAK,MAAM;IACvB,QAAQ;IACR,MAAM;IACN,SAAS;MACP,gBAAgB;MAChB,oBAAoB,MAAM,OAAO,QAAQ,KAAK;IAChD;EACF;EACA,IAAI,OAAO,MAAM,EAAE,IAAI;EACvB,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,IAAI,MAAM,GAAG,KAAK,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM;EACzD,OAAO;AACT,EAAC"}
package/testing.ts ADDED
@@ -0,0 +1,99 @@
1
+ // What Stripe sends, for a test that stands in for it.
2
+
3
+ /**
4
+ * A `Stripe-Signature` header over exactly these bytes: HMAC-SHA256 of
5
+ * `<timestamp>.<raw body>`, keyed by the endpoint's signing secret. The scheme
6
+ * is the same for a platform endpoint and a Connect one; only the secret
7
+ * differs. @yaks/hook's `refusal('stripe', …)` verifies it.
8
+ */
9
+ export let signed = async (
10
+ secret: string,
11
+ raw: string,
12
+ at: number,
13
+ ): Promise<string> => {
14
+ let key = await crypto.subtle.importKey(
15
+ 'raw',
16
+ new TextEncoder().encode(secret),
17
+ { name: 'HMAC', hash: 'SHA-256' },
18
+ false,
19
+ ['sign'],
20
+ )
21
+ let mac = await crypto.subtle.sign(
22
+ 'HMAC',
23
+ key,
24
+ new TextEncoder().encode(`${at}.${raw}`),
25
+ )
26
+ let hex = [...new Uint8Array(mac)]
27
+ .map((n) => n.toString(16).padStart(2, '0')).join('')
28
+ return `t=${at},v1=${hex}`
29
+ }
30
+
31
+ /**
32
+ * A subscription shaped the way ./plans.ts `read` reads one, and no more,
33
+ * tagged with `metadata`: active, paid a month ahead, under ids of its own. A
34
+ * `customer.subscription.*` event carries its subscription whole and is read
35
+ * without asking Stripe back, so this stands in for one Stripe never held.
36
+ * `over` sets the fields a particular delivery turns on, such as a
37
+ * cancellation's `status` and `ended_at`.
38
+ */
39
+ export let fakeSub = (
40
+ metadata: Record<string, string>,
41
+ over: Record<string, unknown> = {},
42
+ ): {
43
+ [field: string]: unknown
44
+ id: string
45
+ customer: string
46
+ status: string
47
+ metadata: Record<string, string>
48
+ current_period_end: number
49
+ } => {
50
+ let id = crypto.randomUUID().replaceAll('-', '')
51
+ return {
52
+ id: `sub_probe${id}`,
53
+ customer: `cus_probe${id}`,
54
+ status: 'active',
55
+ metadata,
56
+ current_period_end: Math.floor(Date.now() / 1000) + 30 * 86400,
57
+ ...over,
58
+ }
59
+ }
60
+
61
+ /**
62
+ * An event about `object`, signed with `secret` as Stripe signs it and posted
63
+ * to `path` through `post`: the door's answer, or a throw where it refused.
64
+ * `on` is the connected account the event is about, which Stripe puts on the
65
+ * envelope rather than in the object; `at` is when Stripe made it, and a
66
+ * redelivery is the same event again, so a test that replays one passes the
67
+ * same moment twice.
68
+ */
69
+ export let delivered = async (
70
+ post: (path: string, init: RequestInit) => Promise<Response>,
71
+ path: string,
72
+ secret: string,
73
+ type: string,
74
+ object: unknown,
75
+ on?: string,
76
+ at: number = Math.floor(Date.now() / 1000),
77
+ ): Promise<string> => {
78
+ let raw = JSON.stringify({
79
+ id: `evt_${crypto.randomUUID()}`,
80
+ type,
81
+ created: at,
82
+ // The envelope says the object's mode, which is the sandbox's unless the
83
+ // object says otherwise.
84
+ livemode: (object as { livemode?: boolean }).livemode ?? false,
85
+ ...(on ? { account: on } : {}),
86
+ data: { object },
87
+ })
88
+ let r = await post(path, {
89
+ method: 'POST',
90
+ body: raw,
91
+ headers: {
92
+ 'content-type': 'application/json',
93
+ 'stripe-signature': await signed(secret, raw, at),
94
+ },
95
+ })
96
+ let said = await r.text()
97
+ if (!r.ok) throw new Error(`${path}: ${r.status} ${said}`)
98
+ return said
99
+ }