@olympio/payment-gateway 0.1.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 +118 -0
- package/dist/index.cjs +1071 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +351 -0
- package/dist/index.d.ts +351 -0
- package/dist/index.js +1019 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# @org/payment-gateway
|
|
2
|
+
|
|
3
|
+
Camada de **infraestrutura** para gateways de pagamento (Asaas, Stripe e DOM Pagamentos), com
|
|
4
|
+
contrato comum, capabilities declaradas, escape hatch tipado, webhooks e erros
|
|
5
|
+
normalizados.
|
|
6
|
+
|
|
7
|
+
A lib **não contém regra de negócio** — ela só fala com o gateway e normaliza
|
|
8
|
+
dados/eventos/erros. A orquestração e as regras de cada cliente ficam na sua
|
|
9
|
+
aplicação. Em termos hexagonais: a lib é a *porta* + os *adapters de saída*; o
|
|
10
|
+
core de aplicação é seu.
|
|
11
|
+
|
|
12
|
+
## Instalação
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @org/payment-gateway
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Uso
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createGateway, Capability, Money } from '@org/payment-gateway';
|
|
22
|
+
|
|
23
|
+
const gateway = createGateway({ provider: 'dom', apiKey: process.env.DOM_KEY! });
|
|
24
|
+
// ou: createGateway({ provider: 'asaas', apiKey: process.env.ASAAS_KEY! });
|
|
25
|
+
// ou: createGateway({ provider: 'stripe', apiKey: process.env.STRIPE_KEY! });
|
|
26
|
+
|
|
27
|
+
const customer = await gateway.customers.create({
|
|
28
|
+
name: 'Maria Silva',
|
|
29
|
+
email: 'maria@exemplo.com',
|
|
30
|
+
taxId: '12345678900', // CPF/CNPJ
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const charge = await gateway.charges.create({
|
|
34
|
+
amount: Money.fromDecimal(199.9, 'BRL'),
|
|
35
|
+
paymentMethod: 'pix',
|
|
36
|
+
customerId: customer.id,
|
|
37
|
+
idempotencyKey: 'pedido-42',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
console.log(charge.status, charge.amount.cents); // 'pending' 19990
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`Money` é sempre em **unidades mínimas** (centavos). Use `Money.fromDecimal` /
|
|
44
|
+
`Money.fromCents` e `toDecimal()` — os adapters cuidam da conversão automática para o formato esperado por cada API (ex: DOM e Asaas em Reais, Stripe em centavos).
|
|
45
|
+
|
|
46
|
+
## Capabilities
|
|
47
|
+
|
|
48
|
+
Cada gateway declara o que suporta. Cheque antes de chamar; chamar algo não
|
|
49
|
+
suportado lança `UnsupportedCapabilityError`.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
if (gateway.supports(Capability.PIX)) {
|
|
53
|
+
// ...
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
| Capability | Asaas | Stripe | DOM |
|
|
58
|
+
| --------------- | :---: | :----: | :-: |
|
|
59
|
+
| `CARD` | ✅ | ✅ | ✅ |
|
|
60
|
+
| `PIX` | ✅ | ❌ | ✅ |
|
|
61
|
+
| `BOLETO` | ✅ | ❌ | ✅ |
|
|
62
|
+
| `REFUND` | ✅ | ✅ | ✅ |
|
|
63
|
+
| `SUBSCRIPTIONS` | ✅ | ✅ | ❌ |
|
|
64
|
+
|
|
65
|
+
## Escape hatch
|
|
66
|
+
|
|
67
|
+
Para recursos exclusivos que o contrato comum não cobre, `raw()` devolve o
|
|
68
|
+
client nativo, **tipado** por adapter.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { StripeGateway } from '@org/payment-gateway';
|
|
72
|
+
|
|
73
|
+
const stripe = new StripeGateway({ apiKey: process.env.STRIPE_KEY! });
|
|
74
|
+
const balance = await stripe.raw().balance.retrieve(); // client `Stripe` nativo
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Webhooks
|
|
78
|
+
|
|
79
|
+
`verifyAndParse` verifica a assinatura e devolve um `WebhookEvent` normalizado.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const event = gateway.webhooks.verifyAndParse({
|
|
83
|
+
payload: rawRequestBody,
|
|
84
|
+
headers: req.headers,
|
|
85
|
+
secret: process.env.WEBHOOK_SECRET!,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
switch (event.type) {
|
|
89
|
+
case 'charge.paid':
|
|
90
|
+
// event.data é um Charge normalizado
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Configuração por gateway
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
// DOM Pagamentos
|
|
99
|
+
createGateway({
|
|
100
|
+
provider: 'dom',
|
|
101
|
+
apiKey,
|
|
102
|
+
baseUrl, // Opcional: use URL de homologação em dev
|
|
103
|
+
webhookToken
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Stripe
|
|
107
|
+
createGateway({ provider: 'stripe', apiKey, webhookSecret });
|
|
108
|
+
|
|
109
|
+
// Asaas
|
|
110
|
+
createGateway({ provider: 'asaas', apiKey, baseUrl, webhookToken });
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Desenvolvimento
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
npm test # roda todos os testes unitários e de contrato
|
|
117
|
+
npm run build # gera os bundles ESM e CJS
|
|
118
|
+
```
|