@mathrunet/masamune_cloudflare_purchase_stripe 3.2.0

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.
Files changed (91) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/LICENSE +21 -0
  3. package/README.md +174 -0
  4. package/dist/functions.d.ts +44 -0
  5. package/dist/functions.js +47 -0
  6. package/dist/functions.js.map +1 -0
  7. package/dist/index.d.ts +25 -0
  8. package/dist/index.js +42 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/lib/meter/d1_usage_event_store.d.ts +128 -0
  11. package/dist/lib/meter/d1_usage_event_store.js +78 -0
  12. package/dist/lib/meter/d1_usage_event_store.js.map +1 -0
  13. package/dist/lib/meter/flush_meter.d.ts +19 -0
  14. package/dist/lib/meter/flush_meter.js +38 -0
  15. package/dist/lib/meter/flush_meter.js.map +1 -0
  16. package/dist/lib/meter/interface.d.ts +109 -0
  17. package/dist/lib/meter/interface.js +3 -0
  18. package/dist/lib/meter/interface.js.map +1 -0
  19. package/dist/lib/meter/kv_usage_buffer.d.ts +56 -0
  20. package/dist/lib/meter/kv_usage_buffer.js +50 -0
  21. package/dist/lib/meter/kv_usage_buffer.js.map +1 -0
  22. package/dist/lib/meter/record_usage.d.ts +29 -0
  23. package/dist/lib/meter/record_usage.js +72 -0
  24. package/dist/lib/meter/record_usage.js.map +1 -0
  25. package/dist/lib/meter/stripe_meter_client.d.ts +76 -0
  26. package/dist/lib/meter/stripe_meter_client.js +74 -0
  27. package/dist/lib/meter/stripe_meter_client.js.map +1 -0
  28. package/dist/lib/options.d.ts +179 -0
  29. package/dist/lib/options.js +132 -0
  30. package/dist/lib/options.js.map +1 -0
  31. package/dist/lib/purchase/d1_purchase_store.d.ts +78 -0
  32. package/dist/lib/purchase/d1_purchase_store.js +194 -0
  33. package/dist/lib/purchase/d1_purchase_store.js.map +1 -0
  34. package/dist/lib/purchase/helpers.d.ts +36 -0
  35. package/dist/lib/purchase/helpers.js +64 -0
  36. package/dist/lib/purchase/helpers.js.map +1 -0
  37. package/dist/lib/purchase/interface.d.ts +182 -0
  38. package/dist/lib/purchase/interface.js +27 -0
  39. package/dist/lib/purchase/interface.js.map +1 -0
  40. package/dist/lib/purchase/sync_payment.d.ts +15 -0
  41. package/dist/lib/purchase/sync_payment.js +79 -0
  42. package/dist/lib/purchase/sync_payment.js.map +1 -0
  43. package/dist/lib/stripe_client.d.ts +55 -0
  44. package/dist/lib/stripe_client.js +50 -0
  45. package/dist/lib/stripe_client.js.map +1 -0
  46. package/dist/meter.d.ts +21 -0
  47. package/dist/meter.js +38 -0
  48. package/dist/meter.js.map +1 -0
  49. package/dist/purchase.d.ts +58 -0
  50. package/dist/purchase.js +48 -0
  51. package/dist/purchase.js.map +1 -0
  52. package/dist/workers/stripe.d.ts +1 -0
  53. package/dist/workers/stripe.js +861 -0
  54. package/dist/workers/stripe.js.map +1 -0
  55. package/dist/workers/stripe_webhook.d.ts +1 -0
  56. package/dist/workers/stripe_webhook.js +384 -0
  57. package/dist/workers/stripe_webhook.js.map +1 -0
  58. package/dist/workers/stripe_webhook_connect.d.ts +1 -0
  59. package/dist/workers/stripe_webhook_connect.js +97 -0
  60. package/dist/workers/stripe_webhook_connect.js.map +1 -0
  61. package/dist/workers/stripe_webhook_secure.d.ts +1 -0
  62. package/dist/workers/stripe_webhook_secure.js +82 -0
  63. package/dist/workers/stripe_webhook_secure.js.map +1 -0
  64. package/jest.config.json +19 -0
  65. package/package.json +56 -0
  66. package/src/functions.ts +48 -0
  67. package/src/index.ts +25 -0
  68. package/src/lib/meter/d1_usage_event_store.ts +176 -0
  69. package/src/lib/meter/flush_meter.ts +45 -0
  70. package/src/lib/meter/interface.ts +118 -0
  71. package/src/lib/meter/kv_usage_buffer.ts +79 -0
  72. package/src/lib/meter/record_usage.ts +87 -0
  73. package/src/lib/meter/stripe_meter_client.ts +117 -0
  74. package/src/lib/options.ts +299 -0
  75. package/src/lib/purchase/d1_purchase_store.ts +268 -0
  76. package/src/lib/purchase/helpers.ts +77 -0
  77. package/src/lib/purchase/interface.ts +215 -0
  78. package/src/lib/purchase/sync_payment.ts +95 -0
  79. package/src/lib/stripe_client.ts +76 -0
  80. package/src/meter.ts +21 -0
  81. package/src/purchase.ts +66 -0
  82. package/src/workers/stripe.ts +881 -0
  83. package/src/workers/stripe_webhook.ts +414 -0
  84. package/src/workers/stripe_webhook_connect.ts +107 -0
  85. package/src/workers/stripe_webhook_secure.ts +91 -0
  86. package/test/helpers/mem_store.ts +97 -0
  87. package/test/meter.test.ts +217 -0
  88. package/test/purchase_store.test.ts +154 -0
  89. package/test/stripe.test.ts +146 -0
  90. package/test/stripe_webhook.test.ts +174 -0
  91. package/tsconfig.json +14 -0
@@ -0,0 +1,76 @@
1
+ import Stripe from "stripe";
2
+
3
+ /**
4
+ * Stripe API version used across this package (parity with
5
+ * `@mathrunet/masamune_firebase_purchase_stripe`).
6
+ *
7
+ * このパッケージ全体で使用するStripe APIバージョン
8
+ * (`@mathrunet/masamune_firebase_purchase_stripe`と同一)。
9
+ */
10
+ export const STRIPE_API_VERSION = "2025-02-24.acacia";
11
+
12
+ /**
13
+ * Options for [createStripeClient].
14
+ *
15
+ * [createStripeClient]のオプション。
16
+ */
17
+ export interface CreateStripeClientOptions {
18
+ /**
19
+ * Stripe secret key.
20
+ *
21
+ * Stripeのシークレットキー。
22
+ */
23
+ secretKey: string;
24
+
25
+ /**
26
+ * Fetch implementation. Defaults to the global `fetch`. Inject for tests.
27
+ *
28
+ * fetch実装。デフォルトはグローバル`fetch`。テスト時に注入します。
29
+ */
30
+ fetch?: typeof fetch | undefined;
31
+ }
32
+
33
+ /**
34
+ * Create a Stripe SDK client configured for Cloudflare Workers.
35
+ *
36
+ * Uses the fetch-based HTTP client so the SDK works without Node.js APIs.
37
+ *
38
+ * Cloudflare Workers用に構成したStripe SDKクライアントを作成します。
39
+ *
40
+ * fetchベースのHTTPクライアントを使うためNode.js APIなしで動作します。
41
+ */
42
+ export function createStripeClient(options: CreateStripeClientOptions): Stripe {
43
+ // 空キーだとSDKコンストラクタが例外を投げ、Webhook署名検証のような
44
+ // APIキー不要の処理まで実行できなくなるためプレースホルダを渡す。
45
+ // 実際のAPI呼び出しはStripe側の認証エラーとして返る。
46
+ return new Stripe(options.secretKey || "sk_not_configured", {
47
+ apiVersion: STRIPE_API_VERSION,
48
+ httpClient: Stripe.createFetchHttpClient(options.fetch),
49
+ });
50
+ }
51
+
52
+ /**
53
+ * Verify a Stripe webhook signature and construct the event.
54
+ *
55
+ * Uses `constructEventAsync` with the SubtleCrypto provider because the
56
+ * synchronous variant is not available on Cloudflare Workers.
57
+ *
58
+ * StripeのWebhook署名を検証しイベントを構築します。
59
+ *
60
+ * 同期版はCloudflare Workersで使用できないため、SubtleCryptoプロバイダ付きの
61
+ * `constructEventAsync`を使用します。
62
+ */
63
+ export async function constructStripeEvent(options: {
64
+ client: Stripe,
65
+ payload: string,
66
+ signature: string,
67
+ secret: string,
68
+ }): Promise<Stripe.Event> {
69
+ return options.client.webhooks.constructEventAsync(
70
+ options.payload,
71
+ options.signature,
72
+ options.secret,
73
+ undefined,
74
+ Stripe.createSubtleCryptoProvider(),
75
+ );
76
+ }
package/src/meter.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright (c) 2026 mathru. All rights reserved.
3
+ *
4
+ * Standalone entry point exposing only the Stripe Meter (metered billing)
5
+ * library. Import from `@mathrunet/masamune_cloudflare_purchase_stripe/dist/meter`
6
+ * to keep Workers bundles free of the full Masamune re-exports.
7
+ *
8
+ * Stripe Meter(従量課金)ライブラリのみを公開する単独エントリポイント。
9
+ * Masamune全体の再エクスポートをWorkersバンドルに含めたくない場合は
10
+ * `@mathrunet/masamune_cloudflare_purchase_stripe/dist/meter`からインポートしてください。
11
+ *
12
+ * [mathru.net]: https://mathru.net
13
+ * [YouTube]: https://www.youtube.com/c/mathrunetchannel
14
+ */
15
+ export * from "./lib/stripe_client";
16
+ export * from "./lib/meter/interface";
17
+ export * from "./lib/meter/stripe_meter_client";
18
+ export * from "./lib/meter/flush_meter";
19
+ export * from "./lib/meter/record_usage";
20
+ export * from "./lib/meter/d1_usage_event_store";
21
+ export * from "./lib/meter/kv_usage_buffer";
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Copyright (c) 2026 mathru. All rights reserved.
3
+ *
4
+ * Standalone entry point exposing the Stripe purchase library (mode actions,
5
+ * webhooks, stores) without the full Masamune re-exports. Import from
6
+ * `@mathrunet/masamune_cloudflare_purchase_stripe/dist/purchase` to keep
7
+ * Workers bundles small.
8
+ *
9
+ * Stripe購入ライブラリ(modeアクション、Webhook、ストア)のみを公開する単独
10
+ * エントリポイント。Masamune全体の再エクスポートをWorkersバンドルに含めたく
11
+ * ない場合は`@mathrunet/masamune_cloudflare_purchase_stripe/dist/purchase`から
12
+ * インポートしてください。
13
+ *
14
+ * [mathru.net]: https://mathru.net
15
+ * [YouTube]: https://www.youtube.com/c/mathrunetchannel
16
+ */
17
+ import { Hono } from "hono";
18
+ import { StripePurchaseWorkersOptions } from "./lib/options";
19
+
20
+ export * from "./lib/options";
21
+ export * from "./lib/stripe_client";
22
+ export * from "./lib/purchase/interface";
23
+ export * from "./lib/purchase/d1_purchase_store";
24
+ export * from "./lib/purchase/helpers";
25
+ export * from "./lib/purchase/sync_payment";
26
+
27
+ /**
28
+ * Builder signature of the purchase workers (`stripe` / `stripeWebhook` /
29
+ * `stripeWebhookConnect` / `stripeWebhookSecure`).
30
+ *
31
+ * 購入ワーカー(`stripe` / `stripeWebhook` / `stripeWebhookConnect` /
32
+ * `stripeWebhookSecure`)のビルダーシグネチャ。
33
+ */
34
+ export type StripeWorkerBuilder = (
35
+ hono: Hono,
36
+ options: StripePurchaseWorkersOptions,
37
+ data: { [key: string]: any },
38
+ ) => Hono;
39
+
40
+ /**
41
+ * Build the mode-based Stripe action handler onto a Hono app.
42
+ *
43
+ * mode分岐のStripeアクションハンドラをHonoアプリへ構築します。
44
+ */
45
+ export const buildStripe: StripeWorkerBuilder = require("./workers/stripe");
46
+
47
+ /**
48
+ * Build the main Stripe webhook handler onto a Hono app.
49
+ *
50
+ * StripeメインWebhookハンドラをHonoアプリへ構築します。
51
+ */
52
+ export const buildStripeWebhook: StripeWorkerBuilder = require("./workers/stripe_webhook");
53
+
54
+ /**
55
+ * Build the Stripe Connect webhook handler onto a Hono app.
56
+ *
57
+ * Stripe Connect用WebhookハンドラをHonoアプリへ構築します。
58
+ */
59
+ export const buildStripeWebhookConnect: StripeWorkerBuilder = require("./workers/stripe_webhook_connect");
60
+
61
+ /**
62
+ * Build the 3D Secure redirect webhook handler onto a Hono app.
63
+ *
64
+ * 3DセキュアリダイレクトWebhookハンドラをHonoアプリへ構築します。
65
+ */
66
+ export const buildStripeWebhookSecure: StripeWorkerBuilder = require("./workers/stripe_webhook_secure");