@fayz-ai/plugin-payments 0.1.1 → 0.9.0-next.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.
- package/README.md +43 -0
- package/package.json +6 -7
- package/dist/chunk-RMA57VFG.cjs +0 -84
- package/dist/chunk-RMA57VFG.cjs.map +0 -1
- package/dist/index.cjs +0 -20
- package/dist/index.cjs.map +0 -1
- package/dist/public/index.cjs +0 -114
- package/dist/public/index.cjs.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @fayz-ai/plugin-payments
|
|
2
|
+
|
|
3
|
+
> A Pix charge provider (and website payment surface) for Fayz apps and storefronts.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@fayz-ai/plugin-payments)
|
|
6
|
+
[](https://github.com/FayaLabs/fayz-sdk/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
**Status:** early / beta — pre-1.0. Ships a working Pix **mock** provider plus React hooks and a public payment surface; the real gateway (Supabase / MercadoPago) is deferred and not yet implemented — apps run on the mock until a gateway is wired. APIs may change before 1.0.
|
|
9
|
+
|
|
10
|
+
Taking money is the one thing every commerce build cannot fake, and in Brazil that means Pix. `plugin-payments` gives Fayz apps a single `PaymentProvider` abstraction for Pix charges — create a charge, get a QR / copy-paste code, poll its status — behind the same safe mock/real resolver used across the SDK. Build and demo the whole checkout flow today on the mock; swap in a real gateway later without touching the UI.
|
|
11
|
+
|
|
12
|
+
## What's inside
|
|
13
|
+
- **Payment provider** — `createSafePaymentProvider()` resolves a real gateway when configured, else `createMockPaymentProvider()`; `PaymentProvider` / `PixCharge` / `CreateChargeInput` / `ChargeStatus` types re-exported from `@fayz-ai/core`
|
|
14
|
+
- **Working mock** — generates a Pix BR code, auto-pays after a configurable delay (default 6s) and expires after a window (default 5min), so status polling is realistic
|
|
15
|
+
- **Public payment surface** (`@fayz-ai/plugin-payments/public`) — `createPublicPaymentPlugin(options)` returns `{ manifest, Provider, paymentProvider }`, plus `usePixCharge`, `useChargeStatus`, and `PaymentProviderContext` / `usePaymentContext`
|
|
16
|
+
- **Deferred real provider** — `createSupabasePaymentProvider()` is a stub that throws until the Supabase/MercadoPago gateway lands
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
```bash
|
|
20
|
+
npm install @fayz-ai/plugin-payments
|
|
21
|
+
```
|
|
22
|
+
Runtime dep: `@fayz-ai/core`. The `./public` subpath additionally needs `react` / `react-dom`.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
```ts
|
|
26
|
+
// Headless: a charge provider anywhere
|
|
27
|
+
import { createSafePaymentProvider } from '@fayz-ai/plugin-payments'
|
|
28
|
+
|
|
29
|
+
const payments = createSafePaymentProvider()
|
|
30
|
+
const charge = await payments.createCharge({ amount: 49.9, currency: 'BRL', orderId: 'abc' })
|
|
31
|
+
// charge.pixCopyPaste, charge.pixQrCode, then poll payments.getChargeStatus(charge.chargeId)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
// Website surface: the payment plugin bundle
|
|
36
|
+
import { createPublicPaymentPlugin } from '@fayz-ai/plugin-payments/public'
|
|
37
|
+
|
|
38
|
+
const paymentPlugin = createPublicPaymentPlugin({ currency: 'BRL' })
|
|
39
|
+
// wrap your checkout tree in <paymentPlugin.Provider> and use usePixCharge()/useChargeStatus()
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Part of the Fayz SDK
|
|
43
|
+
The money seam. Feeds the storefront/checkout surfaces and pairs with `@fayz-ai/plugin-orders` and `@fayz-ai/plugin-shop`; composes alongside `@fayz-ai/plugin-blog` in bespoke website builds.
|
package/package.json
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fayz-ai/plugin-payments",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0-next.0",
|
|
4
4
|
"description": "[experimental] Fayz SDK — payments plugin (Pix charge provider + website surface)",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
7
8
|
"module": "./dist/index.js",
|
|
8
9
|
"types": "./dist/index.d.ts",
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
12
|
"source": "./src/index.ts",
|
|
12
13
|
"types": "./dist/index.d.ts",
|
|
13
|
-
"import": "./dist/index.js"
|
|
14
|
-
"require": "./dist/index.cjs"
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
15
|
},
|
|
16
16
|
"./public": {
|
|
17
17
|
"source": "./src/public/index.ts",
|
|
18
18
|
"types": "./dist/public/index.d.ts",
|
|
19
|
-
"import": "./dist/public/index.js"
|
|
20
|
-
"require": "./dist/public/index.cjs"
|
|
19
|
+
"import": "./dist/public/index.js"
|
|
21
20
|
}
|
|
22
21
|
},
|
|
23
22
|
"files": [
|
|
@@ -29,7 +28,7 @@
|
|
|
29
28
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
30
29
|
},
|
|
31
30
|
"dependencies": {
|
|
32
|
-
"@fayz-ai/core": "^0.
|
|
31
|
+
"@fayz-ai/core": "^0.9.0-next.0"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"@types/react": "^18.3.0",
|
package/dist/chunk-RMA57VFG.cjs
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var core = require('@fayz-ai/core');
|
|
4
|
-
|
|
5
|
-
// src/index.ts
|
|
6
|
-
|
|
7
|
-
// src/data/mock.ts
|
|
8
|
-
var seq = 0;
|
|
9
|
-
function buildBrCode(amount, chargeId) {
|
|
10
|
-
const amt = amount.toFixed(2);
|
|
11
|
-
return `00020126850014br.gov.bcb.pix2563qrcode.fayz.dev/pix/v2/${chargeId}520400005303986540${amt.length}${amt}5802BR5913HEMPDENT6009SAO PAULO62070503***6304FZ${seq % 90 + 10}`;
|
|
12
|
-
}
|
|
13
|
-
function createMockPaymentProvider(options) {
|
|
14
|
-
const autoPayAfterMs = options?.autoPayAfterMs ?? 6e3;
|
|
15
|
-
const expiresAfterMs = options?.expiresAfterMs ?? 5 * 6e4;
|
|
16
|
-
const charges = /* @__PURE__ */ new Map();
|
|
17
|
-
function statusOf(rec) {
|
|
18
|
-
if (rec.paid) return "paid";
|
|
19
|
-
const now = Date.now();
|
|
20
|
-
if (now >= rec.autoPayAt) return "paid";
|
|
21
|
-
if (now >= rec.expiresAt) return "expired";
|
|
22
|
-
return "pending";
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
async createCharge(input) {
|
|
26
|
-
const now = Date.now();
|
|
27
|
-
const chargeId = `mockpix-${++seq}-${input.orderId ?? "na"}`;
|
|
28
|
-
const brCode = buildBrCode(input.amount, chargeId);
|
|
29
|
-
const charge = {
|
|
30
|
-
chargeId,
|
|
31
|
-
status: "pending",
|
|
32
|
-
amount: input.amount,
|
|
33
|
-
currency: input.currency,
|
|
34
|
-
pixQrCode: brCode,
|
|
35
|
-
pixCopyPaste: brCode,
|
|
36
|
-
expiresAt: new Date(now + expiresAfterMs).toISOString()
|
|
37
|
-
};
|
|
38
|
-
charges.set(chargeId, {
|
|
39
|
-
charge,
|
|
40
|
-
createdAt: now,
|
|
41
|
-
autoPayAt: now + autoPayAfterMs,
|
|
42
|
-
expiresAt: now + expiresAfterMs,
|
|
43
|
-
paid: false
|
|
44
|
-
});
|
|
45
|
-
return charge;
|
|
46
|
-
},
|
|
47
|
-
async getChargeStatus(chargeId) {
|
|
48
|
-
const rec = charges.get(chargeId);
|
|
49
|
-
if (!rec) return "failed";
|
|
50
|
-
return statusOf(rec);
|
|
51
|
-
},
|
|
52
|
-
markPaid(chargeId) {
|
|
53
|
-
const rec = charges.get(chargeId);
|
|
54
|
-
if (rec) rec.paid = true;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// src/data/supabase.ts
|
|
60
|
-
function createSupabasePaymentProvider() {
|
|
61
|
-
const notImplemented = () => {
|
|
62
|
-
throw new Error(
|
|
63
|
-
"[plugin-payments] Supabase/MercadoPago provider not implemented yet \u2014 deferred. Run on the mock provider (no gateway configured) for now."
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
return {
|
|
67
|
-
createCharge: (_input) => notImplemented(),
|
|
68
|
-
getChargeStatus: (_chargeId) => notImplemented()
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// src/index.ts
|
|
73
|
-
function createSafePaymentProvider(mockOptions) {
|
|
74
|
-
return core.createSafeDataProvider(
|
|
75
|
-
() => createSupabasePaymentProvider(),
|
|
76
|
-
() => createMockPaymentProvider(mockOptions)
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
exports.createMockPaymentProvider = createMockPaymentProvider;
|
|
81
|
-
exports.createSafePaymentProvider = createSafePaymentProvider;
|
|
82
|
-
exports.createSupabasePaymentProvider = createSupabasePaymentProvider;
|
|
83
|
-
//# sourceMappingURL=chunk-RMA57VFG.cjs.map
|
|
84
|
-
//# sourceMappingURL=chunk-RMA57VFG.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/data/mock.ts","../src/data/supabase.ts","../src/index.ts"],"names":["createSafeDataProvider"],"mappings":";;;;;;;AAuBA,IAAI,GAAA,GAAM,CAAA;AAGV,SAAS,WAAA,CAAY,QAAgB,QAAA,EAA0B;AAC7D,EAAA,MAAM,GAAA,GAAM,MAAA,CAAO,OAAA,CAAQ,CAAC,CAAA;AAE5B,EAAA,OACE,CAAA,uDAAA,EAA0D,QAAQ,CAAA,kBAAA,EAC7C,GAAA,CAAI,MAAM,GAAG,GAAG,CAAA,gDAAA,EAAoD,GAAA,GAAM,EAAA,GAAK,EAAG,CAAA,CAAA;AAE3G;AAEO,SAAS,0BAA0B,OAAA,EAAmD;AAC3F,EAAA,MAAM,cAAA,G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;AACD,MAAA,OAAO,MAAA;AAAA,IACT,CAAA;AAAA,IAEA,MAAM,gBAAgB,QAAA,EAAyC;AAC7D,MAAA,MAAM,GAAA,GAAM,OAAA,CAAQ,GAAA,CAAI,QAAQ,CAAA;AAChC,MAAA,IAAI,CAAC,KAAK,OAAO,QAAA;AACjB,MAAA,OAAO,SAAS,GAAG,CAAA;AAAA,IACrB,CAAA;AAAA,IAEA,SAAS,QAAA,EAAwB;AAC/B,MAAA,MAAM,GAAA,GAAM,OAAA,CAAQ,GAAA,CAAI,QAAQ,CAAA;AAChC,MAAA,IAAI,GAAA,MAAS,IAAA,GAAO,IAAA;AAAA,IACtB;AAAA,GACF;AACF;;;ACvEO,SAAS,6BAAA,GAAiD;AAC/D,EAAA,MAAM,iBAAiB,MAAa;AAClC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF,CAAA;AACA,EAAA,OAAO;AAAA,IACL,YAAA,EAAc,CAAC,MAAA,KAAkD,cAAA,EAAe;AAAA,IAChF,eAAA,EAAiB,CAAC,SAAA,KAA6C,cAAA;AAAe,GAChF;AACF;;;ACVO,SAAS,0BAA0B,WAAA,EAAmD;AAC3F,EAAA,OAAOA,2BAAA;AAAA,IACL,MAAM,6BAAA,EAA8B;AAAA,IACpC,MAAM,0BAA0B,WAAW;AAAA,GAC7C;AACF","file":"chunk-RMA57VFG.cjs","sourcesContent":["import type { PaymentProvider, CreateChargeInput, PixCharge, ChargeStatus } from '@fayz-ai/core'\n\n/** Mock provider adds a demo-only `markPaid` shortcut on top of the contract. */\nexport interface MockPaymentProvider extends PaymentProvider {\n /** Force a charge to 'paid' immediately (demo \"Já paguei\" button). */\n markPaid(chargeId: string): void\n}\n\nexport interface MockPaymentOptions {\n /** Auto-confirm a pending charge after this many ms (simulates the gateway webhook). Default 6000. */\n autoPayAfterMs?: number\n /** Charge validity window in ms. Default 5 min. */\n expiresAfterMs?: number\n}\n\ninterface ChargeRecord {\n charge: PixCharge\n createdAt: number\n autoPayAt: number\n expiresAt: number\n paid: boolean\n}\n\nlet seq = 0\n\n/** Build a realistic-looking (non-functional) Pix BR Code \"copia e cola\". */\nfunction buildBrCode(amount: number, chargeId: string): string {\n const amt = amount.toFixed(2)\n // Simplified EMV-ish layout — enough to look/scan like a Pix payload in the POC.\n return (\n `00020126850014br.gov.bcb.pix2563qrcode.fayz.dev/pix/v2/${chargeId}` +\n `520400005303986540${amt.length}${amt}5802BR5913HEMPDENT6009SAO PAULO62070503***6304FZ${(seq % 90 + 10)}`\n )\n}\n\nexport function createMockPaymentProvider(options?: MockPaymentOptions): MockPaymentProvider {\n const autoPayAfterMs = options?.autoPayAfterMs ?? 6000\n const expiresAfterMs = options?.expiresAfterMs ?? 5 * 60_000\n const charges = new Map<string, ChargeRecord>()\n\n function statusOf(rec: ChargeRecord): ChargeStatus {\n if (rec.paid) return 'paid'\n const now = Date.now()\n if (now >= rec.autoPayAt) return 'paid'\n if (now >= rec.expiresAt) return 'expired'\n return 'pending'\n }\n\n return {\n async createCharge(input: CreateChargeInput): Promise<PixCharge> {\n const now = Date.now()\n const chargeId = `mockpix-${++seq}-${input.orderId ?? 'na'}`\n const brCode = buildBrCode(input.amount, chargeId)\n const charge: PixCharge = {\n chargeId,\n status: 'pending',\n amount: input.amount,\n currency: input.currency,\n pixQrCode: brCode,\n pixCopyPaste: brCode,\n expiresAt: new Date(now + expiresAfterMs).toISOString(),\n }\n charges.set(chargeId, {\n charge,\n createdAt: now,\n autoPayAt: now + autoPayAfterMs,\n expiresAt: now + expiresAfterMs,\n paid: false,\n })\n return charge\n },\n\n async getChargeStatus(chargeId: string): Promise<ChargeStatus> {\n const rec = charges.get(chargeId)\n if (!rec) return 'failed'\n return statusOf(rec)\n },\n\n markPaid(chargeId: string): void {\n const rec = charges.get(chargeId)\n if (rec) rec.paid = true\n },\n }\n}\n","import type { PaymentProvider, CreateChargeInput, PixCharge, ChargeStatus } from '@fayz-ai/core'\n\n// ---------------------------------------------------------------------------\n// Supabase/gateway-backed payment provider — STUB (deferred).\n//\n// Later: a real MercadoPago (or other PSP) Pix integration via a Supabase edge\n// function (credentials server-side; actions create_charge | get_status |\n// webhook), mirroring the plugbank-sync edge function. Swapping this in is a\n// pure provider change — the booking widget and hooks are untouched because they\n// depend only on the core PaymentProvider interface.\n// ---------------------------------------------------------------------------\n\nexport function createSupabasePaymentProvider(): PaymentProvider {\n const notImplemented = (): never => {\n throw new Error(\n '[plugin-payments] Supabase/MercadoPago provider not implemented yet — deferred. ' +\n 'Run on the mock provider (no gateway configured) for now.',\n )\n }\n return {\n createCharge: (_input: CreateChargeInput): Promise<PixCharge> => notImplemented(),\n getChargeStatus: (_chargeId: string): Promise<ChargeStatus> => notImplemented(),\n }\n}\n","import { createSafeDataProvider } from '@fayz-ai/core'\nimport type { PaymentProvider } from '@fayz-ai/core'\nimport { createMockPaymentProvider, type MockPaymentOptions } from './data/mock'\nimport { createSupabasePaymentProvider } from './data/supabase'\n\n// ---------------------------------------------------------------------------\n// @fayz-ai/plugin-payments — gateway-agnostic charge provider (Pix today).\n// Bookkeeping (invoices/movements/reconciliation) stays in plugin-financial;\n// this plugin OWNS money-in initiation (create a charge → payable artifacts →\n// status). Mirrors createSafeFinancialProvider.\n// ---------------------------------------------------------------------------\n\n/** Supabase/gateway when configured, else the mock provider. */\nexport function createSafePaymentProvider(mockOptions?: MockPaymentOptions): PaymentProvider {\n return createSafeDataProvider(\n () => createSupabasePaymentProvider(),\n () => createMockPaymentProvider(mockOptions),\n )\n}\n\nexport { createMockPaymentProvider, createSupabasePaymentProvider } from './data'\nexport type { MockPaymentProvider, MockPaymentOptions } from './data'\nexport type {\n PaymentProvider, PaymentMethod, ChargeStatus, CreateChargeInput, PixCharge,\n} from '@fayz-ai/core'\n"]}
|
package/dist/index.cjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkRMA57VFG_cjs = require('./chunk-RMA57VFG.cjs');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "createMockPaymentProvider", {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: function () { return chunkRMA57VFG_cjs.createMockPaymentProvider; }
|
|
10
|
-
});
|
|
11
|
-
Object.defineProperty(exports, "createSafePaymentProvider", {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: function () { return chunkRMA57VFG_cjs.createSafePaymentProvider; }
|
|
14
|
-
});
|
|
15
|
-
Object.defineProperty(exports, "createSupabasePaymentProvider", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function () { return chunkRMA57VFG_cjs.createSupabasePaymentProvider; }
|
|
18
|
-
});
|
|
19
|
-
//# sourceMappingURL=index.cjs.map
|
|
20
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
|
package/dist/public/index.cjs
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkRMA57VFG_cjs = require('../chunk-RMA57VFG.cjs');
|
|
4
|
-
var react = require('react');
|
|
5
|
-
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
-
|
|
7
|
-
var PaymentContext = react.createContext(null);
|
|
8
|
-
function PaymentProviderContext({ value, children }) {
|
|
9
|
-
return /* @__PURE__ */ jsxRuntime.jsx(PaymentContext.Provider, { value, children });
|
|
10
|
-
}
|
|
11
|
-
function usePaymentContext() {
|
|
12
|
-
const ctx = react.useContext(PaymentContext);
|
|
13
|
-
if (!ctx) {
|
|
14
|
-
throw new Error("[plugin-payments] usePaymentContext must be used within the payments Provider.");
|
|
15
|
-
}
|
|
16
|
-
return ctx;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// src/public/createPublicPaymentPlugin.ts
|
|
20
|
-
function createPublicPaymentPlugin(options) {
|
|
21
|
-
const paymentProvider = options?.paymentProvider ?? chunkRMA57VFG_cjs.createMockPaymentProvider(options?.mock);
|
|
22
|
-
const value = { provider: paymentProvider };
|
|
23
|
-
const Provider = ({ children }) => react.createElement(PaymentProviderContext, { value, children });
|
|
24
|
-
Provider.displayName = "PaymentPluginProvider";
|
|
25
|
-
const manifest = {
|
|
26
|
-
id: "payments",
|
|
27
|
-
name: "Pagamentos",
|
|
28
|
-
icon: "CreditCard",
|
|
29
|
-
version: "0.1.0",
|
|
30
|
-
scope: options?.scope ?? "universal",
|
|
31
|
-
scaffolds: ["website", "landing_page"],
|
|
32
|
-
defaultEnabled: true,
|
|
33
|
-
dependencies: [],
|
|
34
|
-
navigation: [],
|
|
35
|
-
routes: [],
|
|
36
|
-
widgets: []
|
|
37
|
-
};
|
|
38
|
-
return { manifest, Provider, paymentProvider };
|
|
39
|
-
}
|
|
40
|
-
function usePixCharge() {
|
|
41
|
-
const { provider } = usePaymentContext();
|
|
42
|
-
const [charge, setCharge] = react.useState(null);
|
|
43
|
-
const [creating, setCreating] = react.useState(false);
|
|
44
|
-
const [error, setError] = react.useState(null);
|
|
45
|
-
const create = react.useCallback(
|
|
46
|
-
async (input) => {
|
|
47
|
-
setCreating(true);
|
|
48
|
-
setError(null);
|
|
49
|
-
try {
|
|
50
|
-
const c = await provider.createCharge(input);
|
|
51
|
-
setCharge(c);
|
|
52
|
-
return c;
|
|
53
|
-
} catch (err) {
|
|
54
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
55
|
-
return null;
|
|
56
|
-
} finally {
|
|
57
|
-
setCreating(false);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
[provider]
|
|
61
|
-
);
|
|
62
|
-
const reset = react.useCallback(() => {
|
|
63
|
-
setCharge(null);
|
|
64
|
-
setError(null);
|
|
65
|
-
}, []);
|
|
66
|
-
return { charge, creating, error, create, reset };
|
|
67
|
-
}
|
|
68
|
-
function useChargeStatus(chargeId, intervalMs = 2500) {
|
|
69
|
-
const { provider } = usePaymentContext();
|
|
70
|
-
const [status, setStatus] = react.useState(null);
|
|
71
|
-
const timer = react.useRef(null);
|
|
72
|
-
react.useEffect(() => {
|
|
73
|
-
let active = true;
|
|
74
|
-
setStatus(null);
|
|
75
|
-
if (!chargeId) return;
|
|
76
|
-
const clear = () => {
|
|
77
|
-
if (timer.current) {
|
|
78
|
-
clearInterval(timer.current);
|
|
79
|
-
timer.current = null;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
const tick = () => {
|
|
83
|
-
provider.getChargeStatus(chargeId).then((s) => {
|
|
84
|
-
if (!active) return;
|
|
85
|
-
setStatus(s);
|
|
86
|
-
if (s === "paid" || s === "expired" || s === "failed") clear();
|
|
87
|
-
}).catch(() => {
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
tick();
|
|
91
|
-
timer.current = setInterval(tick, intervalMs);
|
|
92
|
-
return () => {
|
|
93
|
-
active = false;
|
|
94
|
-
clear();
|
|
95
|
-
};
|
|
96
|
-
}, [provider, chargeId, intervalMs]);
|
|
97
|
-
return status;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
Object.defineProperty(exports, "createMockPaymentProvider", {
|
|
101
|
-
enumerable: true,
|
|
102
|
-
get: function () { return chunkRMA57VFG_cjs.createMockPaymentProvider; }
|
|
103
|
-
});
|
|
104
|
-
Object.defineProperty(exports, "createSafePaymentProvider", {
|
|
105
|
-
enumerable: true,
|
|
106
|
-
get: function () { return chunkRMA57VFG_cjs.createSafePaymentProvider; }
|
|
107
|
-
});
|
|
108
|
-
exports.PaymentProviderContext = PaymentProviderContext;
|
|
109
|
-
exports.createPublicPaymentPlugin = createPublicPaymentPlugin;
|
|
110
|
-
exports.useChargeStatus = useChargeStatus;
|
|
111
|
-
exports.usePaymentContext = usePaymentContext;
|
|
112
|
-
exports.usePixCharge = usePixCharge;
|
|
113
|
-
//# sourceMappingURL=index.cjs.map
|
|
114
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/public/context.tsx","../../src/public/createPublicPaymentPlugin.ts","../../src/public/hooks.ts"],"names":["createContext","jsx","useContext","createMockPaymentProvider","createElement","useState","useCallback","useRef","useEffect"],"mappings":";;;;;;AAOA,IAAM,cAAA,GAAiBA,oBAA0C,IAAI,CAAA;AAE9D,SAAS,sBAAA,CAAuB,EAAE,KAAA,EAAO,QAAA,EAAS,EAAwD;AAC/G,EAAA,uBAAOC,cAAA,CAAC,cAAA,CAAe,QAAA,EAAf,EAAwB,OAAe,QAAA,EAAS,CAAA;AAC1D;AAEO,SAAS,iBAAA,GAAyC;AACvD,EAAA,MAAM,GAAA,GAAMC,iBAAW,cAAc,CAAA;AACrC,EAAA,IAAI,CAAC,GAAA,EAAK;AACR,IAAA,MAAM,IAAI,MAAM,gFAAgF,CAAA;AAAA,EAClG;AACA,EAAA,OAAO,GAAA;AACT;;;ACWO,SAAS,0BAA0B,OAAA,EAAqD;AAM7F,EAAA,MAAM,eAAA,GAAkB,OAAA,EAAS,eAAA,IAAmBC,2CAAA,CAA0B,SAAS,IAAI,CAAA;AAE3F,EAAA,MAAM,KAAA,GAA6B,EAAE,QAAA,EAAU,eAAA,EAAgB;AAC/D,EAAA,MAAM,QAAA,GAAwC,CAAC,EAAE,QAAA,EAAS,KACxDC,oBAAc,sBAAA,EAAwB,EAAE,KAAA,EAAO,QAAA,EAAU,CAAA;AAC3D,EAAA,QAAA,CAAS,WAAA,GAAc,uBAAA;AAEvB,EAAA,MAAM,QAAA,GAA2B;AAAA,IAC/B,EAAA,EAAI,UAAA;AAAA,IACJ,IAAA,EAAM,YAAA;AAAA,IACN,IAAA,EAAM,YAAA;AAAA,IACN,OAAA,EAAS,OAAA;AAAA,IACT,KAAA,EAAO,SAAS,KAAA,IAAS,WAAA;AAAA,IACzB,SAAA,EAAW,CAAC,SAAA,EAAW,cAAc,CAAA;AAAA,IACrC,cAAA,EAAgB,IAAA;AAAA,IAChB,cAAc,EAAC;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,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["import { createContext, useContext, type ReactNode } from 'react'\nimport type { PaymentProvider } from '@fayz-ai/core'\n\nexport interface PaymentContextValue {\n provider: PaymentProvider\n}\n\nconst PaymentContext = createContext<PaymentContextValue | null>(null)\n\nexport function PaymentProviderContext({ value, children }: { value: PaymentContextValue; children: ReactNode }) {\n return <PaymentContext.Provider value={value}>{children}</PaymentContext.Provider>\n}\n\nexport function usePaymentContext(): PaymentContextValue {\n const ctx = useContext(PaymentContext)\n if (!ctx) {\n throw new Error('[plugin-payments] usePaymentContext must be used within the payments Provider.')\n }\n return ctx\n}\n","import { createElement, type FC, type ReactNode } from 'react'\nimport type { PluginManifest, PluginScope, PaymentProvider } from '@fayz-ai/core'\nimport { createMockPaymentProvider, type MockPaymentOptions } from '../data/mock'\nimport { PaymentProviderContext, type PaymentContextValue } from './context'\n\n// ---------------------------------------------------------------------------\n// @fayz-ai/plugin-payments/public — website payment surface.\n//\n// Returns a { manifest, Provider, paymentProvider } bundle mirroring the other\n// website plugins. The host injects `paymentProvider` into the booking plugin;\n// the Provider/hooks are for a future standalone checkout page. No routes — a\n// payment is a service, not a screen.\n// ---------------------------------------------------------------------------\n\nexport interface PublicPaymentOptions {\n /** Currency for charges (informational; charges carry their own). */\n currency?: string\n /** Inject a custom provider (real gateway). Overrides the safe resolver. */\n paymentProvider?: PaymentProvider\n /** Tuning for the mock provider (auto-pay/expiry) when no real provider is set. */\n mock?: MockPaymentOptions\n scope?: PluginScope\n}\n\nexport interface PublicPaymentPlugin {\n manifest: PluginManifest\n Provider: FC<{ children: ReactNode }>\n paymentProvider: PaymentProvider\n}\n\nexport function createPublicPaymentPlugin(options?: PublicPaymentOptions): PublicPaymentPlugin {\n // Default is the MOCK provider even when a Supabase client is configured:\n // the Supabase payment path is still a stub (real gateway = MercadoPago edge\n // fn, deferred), so a real provider must be injected explicitly via\n // `paymentProvider`. Don't route through createSafePaymentProvider here or\n // any app with a configured Supabase client would hit the throwing stub.\n const paymentProvider = options?.paymentProvider ?? createMockPaymentProvider(options?.mock)\n\n const value: PaymentContextValue = { provider: paymentProvider }\n const Provider: FC<{ children: ReactNode }> = ({ children }) =>\n createElement(PaymentProviderContext, { value, children })\n Provider.displayName = 'PaymentPluginProvider'\n\n const manifest: PluginManifest = {\n id: 'payments',\n name: 'Pagamentos',\n icon: 'CreditCard',\n version: '0.1.0',\n scope: options?.scope ?? 'universal',\n scaffolds: ['website', 'landing_page'],\n defaultEnabled: true,\n dependencies: [],\n navigation: [],\n routes: [],\n widgets: [],\n }\n\n return { manifest, Provider, paymentProvider }\n}\n","import { useCallback, useEffect, useRef, useState } from 'react'\nimport type { CreateChargeInput, PixCharge, ChargeStatus } from '@fayz-ai/core'\nimport { usePaymentContext } from './context'\n\nexport interface UsePixChargeResult {\n charge: PixCharge | null\n creating: boolean\n error: Error | null\n create: (input: CreateChargeInput) => Promise<PixCharge | null>\n reset: () => void\n}\n\n/**\n * Create a Pix charge on demand. For a standalone checkout page; the booking\n * widget uses its own injected provider directly (agenda depends only on core).\n */\nexport function usePixCharge(): UsePixChargeResult {\n const { provider } = usePaymentContext()\n const [charge, setCharge] = useState<PixCharge | null>(null)\n const [creating, setCreating] = useState(false)\n const [error, setError] = useState<Error | null>(null)\n\n const create = useCallback(\n async (input: CreateChargeInput) => {\n setCreating(true)\n setError(null)\n try {\n const c = await provider.createCharge(input)\n setCharge(c)\n return c\n } catch (err) {\n setError(err instanceof Error ? err : new Error(String(err)))\n return null\n } finally {\n setCreating(false)\n }\n },\n [provider],\n )\n\n const reset = useCallback(() => {\n setCharge(null)\n setError(null)\n }, [])\n\n return { charge, creating, error, create, reset }\n}\n\n/**\n * Poll a charge's status until it settles. Cleans the interval on unmount,\n * chargeId change, and terminal states.\n */\nexport function useChargeStatus(chargeId: string | null, intervalMs = 2500): ChargeStatus | null {\n const { provider } = usePaymentContext()\n const [status, setStatus] = useState<ChargeStatus | null>(null)\n const timer = useRef<ReturnType<typeof setInterval> | null>(null)\n\n useEffect(() => {\n let active = true\n setStatus(null)\n if (!chargeId) return\n\n const clear = () => {\n if (timer.current) { clearInterval(timer.current); timer.current = null }\n }\n const tick = () => {\n provider\n .getChargeStatus(chargeId)\n .then((s) => {\n if (!active) return\n setStatus(s)\n if (s === 'paid' || s === 'expired' || s === 'failed') clear()\n })\n .catch(() => { /* keep polling on transient errors */ })\n }\n tick()\n timer.current = setInterval(tick, intervalMs)\n return () => { active = false; clear() }\n }, [provider, chargeId, intervalMs])\n\n return status\n}\n"]}
|