@odla-ai/chapter 0.31.2 → 0.31.3

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.
@@ -1459,50 +1459,7 @@ var handleSchedule = async (req, url, env, ctx) => {
1459
1459
  };
1460
1460
 
1461
1461
  // src/payments.ts
1462
- function parseSigHeader(header) {
1463
- const parts = {};
1464
- for (const p of header.split(",")) {
1465
- const [k, v] = p.split("=", 2);
1466
- if (k && v !== void 0) parts[k] = v;
1467
- }
1468
- return { t: parts.t, v1: parts.v1 };
1469
- }
1470
- function toHex(buf) {
1471
- return [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join("");
1472
- }
1473
- function timingSafeEqual(a, b) {
1474
- if (a.length !== b.length) return false;
1475
- let diff = 0;
1476
- for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
1477
- return diff === 0;
1478
- }
1479
- async function verifyStripeSignature(payload, header, secret, opts = {}) {
1480
- const { t, v1 } = parseSigHeader(header);
1481
- if (!t || !v1) return false;
1482
- const ts = Number(t);
1483
- if (!Number.isFinite(ts)) return false;
1484
- const nowSec = (opts.now ?? Date.now()) / 1e3;
1485
- const tolerance = opts.toleranceSec ?? 300;
1486
- if (Math.abs(nowSec - ts) > tolerance) return false;
1487
- const enc = new TextEncoder();
1488
- const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
1489
- const mac = await crypto.subtle.sign("HMAC", key, enc.encode(`${t}.${payload}`));
1490
- return timingSafeEqual(toHex(mac), v1);
1491
- }
1492
- function stripeForm(params) {
1493
- const out = new URLSearchParams();
1494
- for (const [k, v] of Object.entries(params)) {
1495
- if (v === void 0 || v === null) continue;
1496
- if (typeof v === "object") {
1497
- for (const [k2, v2] of Object.entries(v)) {
1498
- if (v2 !== void 0 && v2 !== null) out.append(`${k}[${k2}]`, String(v2));
1499
- }
1500
- } else {
1501
- out.append(k, String(v));
1502
- }
1503
- }
1504
- return out.toString();
1505
- }
1462
+ import { stripeForm, verifyStripeSignature } from "@odla-ai/stripe";
1506
1463
  function webhookMutationId(eventId) {
1507
1464
  return `stripe:${eventId}`;
1508
1465
  }
@@ -1549,18 +1506,15 @@ function canceledPatch() {
1549
1506
  }
1550
1507
 
1551
1508
  // src/payments-stripe.ts
1509
+ import { stripeRequest, verifyStripeSignature as verifyStripeSignature2 } from "@odla-ai/stripe";
1552
1510
  async function stripeCall(sk, method, path, params, idempotencyKey) {
1553
- const qs = method === "GET" && params ? `?${stripeForm(params)}` : "";
1554
- const headers = { authorization: `Bearer ${sk}` };
1555
- if (idempotencyKey) headers["idempotency-key"] = idempotencyKey;
1556
- const init = { method, headers };
1557
- if (method === "POST" && params) {
1558
- headers["content-type"] = "application/x-www-form-urlencoded";
1559
- init.body = stripeForm(params);
1560
- }
1561
- const res = await fetch(`https://api.stripe.com${path}${qs}`, init);
1562
- const body = await res.json().catch(() => ({}));
1563
- return { ok: res.ok, status: res.status, body };
1511
+ return stripeRequest(
1512
+ { secretKey: sk },
1513
+ method,
1514
+ path,
1515
+ params,
1516
+ idempotencyKey ? { idempotencyKey } : {}
1517
+ );
1564
1518
  }
1565
1519
  function fail(op, r) {
1566
1520
  const err = new Error(`stripe ${op} failed: ${r.status}`);
@@ -1623,7 +1577,7 @@ function createStripeProvider(config) {
1623
1577
  return { customerId, subscriptionId: String(sub.body.id), clientSecret };
1624
1578
  },
1625
1579
  async ingestWebhook(rawBody, sigHeader) {
1626
- if (!webhookSecret || !await verifyStripeSignature(rawBody, sigHeader, webhookSecret)) {
1580
+ if (!webhookSecret || !await verifyStripeSignature2(rawBody, sigHeader, webhookSecret)) {
1627
1581
  return { ok: false, reason: "bad_signature" };
1628
1582
  }
1629
1583
  const event = JSON.parse(rawBody);