@crediblemark/buayar 0.1.3 → 0.1.4
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 +241 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# 💳 @crediblemark/buayar
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@crediblemark/buayar)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
**`@crediblemark/buayar`** adalah Unified Payment Gateway SDK yang dirancang khusus untuk mempermudah integrasi berbagai gerbang pembayaran (Payment Gateway) pada platform dan situs berbasis **CredBuild**.
|
|
7
|
+
|
|
8
|
+
Dengan SDK ini, Anda cukup menulis kode satu kali menggunakan struktur API yang seragam untuk mengelola pembuatan invoice/transaksi serta verifikasi callback webhook dari berbagai penyedia layanan payment gateway.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 🚀 Fitur Utama
|
|
13
|
+
|
|
14
|
+
- 🔄 **Unified API**: Satu antarmuka (interface) terpadu untuk semua provider payment gateway.
|
|
15
|
+
- 🛡️ **Tipe Data Kuat (TypeScript)**: Dilengkapi dengan deklarasi tipe data lengkap untuk mencegah *runtime error*.
|
|
16
|
+
- 🇮🇩 **Dukungan Lokal**: Siap digunakan untuk transaksi di Indonesia (seperti Duitku).
|
|
17
|
+
- ⚙️ **Modular & Dapat Diperluas**: Memungkinkan penambahan provider baru dengan mewarisi kelas base yang disediakan.
|
|
18
|
+
- 🔒 **Otomatisasi Signature**: Keamanan transaksi terjamin dengan pembuatan *hash* signature (MD5, SHA-256) otomatis secara internal.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 📦 Instalasi
|
|
23
|
+
|
|
24
|
+
Instal package menggunakan manajer paket pilihan Anda:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# Menggunakan Bun (Sangat Direkomendasikan)
|
|
28
|
+
bun add @crediblemark/buayar
|
|
29
|
+
|
|
30
|
+
# Menggunakan NPM
|
|
31
|
+
npm install @crediblemark/buayar
|
|
32
|
+
|
|
33
|
+
# Menggunakan Yarn
|
|
34
|
+
yarn add @crediblemark/buayar
|
|
35
|
+
|
|
36
|
+
# Menggunakan PNPM
|
|
37
|
+
pnpm add @crediblemark/buayar
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 🗺️ Alur Proses Pembayaran
|
|
43
|
+
|
|
44
|
+
```mermaid
|
|
45
|
+
sequenceDiagram
|
|
46
|
+
autonumber
|
|
47
|
+
actor Customer as 👤 Pelanggan
|
|
48
|
+
participant App as 💻 Aplikasi Anda (Backend)
|
|
49
|
+
participant SDK as 💳 @crediblemark/buayar
|
|
50
|
+
participant PG as 🏛️ Payment Gateway (e.g. Duitku)
|
|
51
|
+
|
|
52
|
+
Customer->>App: 1. Checkout Order
|
|
53
|
+
App->>SDK: 2. createInvoice(provider, params, config)
|
|
54
|
+
SDK->>SDK: 3. Kalkulasi & Generate Signature
|
|
55
|
+
SDK->>PG: 4. Inquiry Request (API)
|
|
56
|
+
PG-->>SDK: 5. Kembalikan Payment URL & Ref
|
|
57
|
+
SDK-->>App: 6. Response (InvoiceResponse)
|
|
58
|
+
App-->>Customer: 7. Redirect ke Halaman Pembayaran (Payment URL)
|
|
59
|
+
Customer->>PG: 8. Selesaikan Pembayaran
|
|
60
|
+
PG->>App: 9. Webhook Callback (Notifikasi Pembayaran)
|
|
61
|
+
App->>SDK: 10. verifyCallback(provider, body, config)
|
|
62
|
+
SDK-->>App: 11. Kembalikan Status Terverifikasi (paid/failed)
|
|
63
|
+
App-->>PG: 12. Response OK ke Payment Gateway
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## 🛠️ Integrasi & Penggunaan
|
|
69
|
+
|
|
70
|
+
### 1. Inisialisasi & Membuat Transaksi (Invoice)
|
|
71
|
+
|
|
72
|
+
Berikut adalah contoh pembuatan transaksi menggunakan provider **Duitku**:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { paymentManager } from "@crediblemark/buayar";
|
|
76
|
+
|
|
77
|
+
const providerName = "duitku";
|
|
78
|
+
|
|
79
|
+
// Konfigurasi Kredensial Provider
|
|
80
|
+
const providerConfig = {
|
|
81
|
+
merchantCode: "DXXXX", // Merchant Code dari Duitku
|
|
82
|
+
apiKey: "xxxxxxxxxxxxxxxx", // API Key / Merchant Key Anda
|
|
83
|
+
sandbox: true, // Set true untuk development / testing
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Parameter Invoice / Pembayaran
|
|
87
|
+
const invoiceParams = {
|
|
88
|
+
orderId: "ORDER-100249",
|
|
89
|
+
amount: 150000, // Nominal transaksi (Rupiah)
|
|
90
|
+
productDetails: "Pembelian Template Landing Page Premium",
|
|
91
|
+
customer: {
|
|
92
|
+
name: "Rasyiqi Crediblemark",
|
|
93
|
+
email: "rasyiqi@crediblemark.com",
|
|
94
|
+
phone: "081234567890", // Opsional
|
|
95
|
+
},
|
|
96
|
+
returnUrl: "https://situsbisnis.com/payment/success",
|
|
97
|
+
callbackUrl: "https://api.situsbisnis.com/v1/payment/callback",
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
async function handleCheckout() {
|
|
101
|
+
try {
|
|
102
|
+
const result = await paymentManager.createInvoice(
|
|
103
|
+
providerName,
|
|
104
|
+
invoiceParams,
|
|
105
|
+
providerConfig
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
if (result.success) {
|
|
109
|
+
console.log("Invoice Berhasil Dibuat! 🎉");
|
|
110
|
+
console.log("URL Pembayaran:", result.paymentUrl);
|
|
111
|
+
console.log("Referensi Gateway:", result.reference);
|
|
112
|
+
|
|
113
|
+
// Redirect pelanggan ke paymentUrl
|
|
114
|
+
// response.redirect(result.paymentUrl);
|
|
115
|
+
} else {
|
|
116
|
+
console.error("Gagal membuat invoice:", result.error);
|
|
117
|
+
}
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.error("Terjadi kesalahan:", error);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
handleCheckout();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 2. Verifikasi Callback / Webhook Notifikasi
|
|
127
|
+
|
|
128
|
+
Gunakan fungsi ini di endpoint API callback/webhook untuk memastikan data notifikasi yang dikirim oleh Payment Gateway valid dan tidak dimanipulasi:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { paymentManager } from "@crediblemark/buayar";
|
|
132
|
+
|
|
133
|
+
// Endpoint API Webhook Handler
|
|
134
|
+
async function handlePaymentCallback(req: any, res: any) {
|
|
135
|
+
const providerName = "duitku";
|
|
136
|
+
const callbackPayload = req.body; // Payload POST dari gateway
|
|
137
|
+
|
|
138
|
+
const providerConfig = {
|
|
139
|
+
merchantCode: "DXXXX",
|
|
140
|
+
apiKey: "xxxxxxxxxxxxxxxx",
|
|
141
|
+
sandbox: true,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const verification = await paymentManager.verifyCallback(
|
|
146
|
+
providerName,
|
|
147
|
+
callbackPayload,
|
|
148
|
+
providerConfig
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
if (verification.isValid) {
|
|
152
|
+
console.log(`Signature valid untuk Order: ${verification.orderId}`);
|
|
153
|
+
|
|
154
|
+
if (verification.status === "paid") {
|
|
155
|
+
console.log("Status: PEMBAYARAN SELESAI & LUNAS! ✅");
|
|
156
|
+
// Update database Anda: ubah status order menjadi LUNAS
|
|
157
|
+
} else if (verification.status === "failed") {
|
|
158
|
+
console.log("Status: PEMBAYARAN GAGAL ❌");
|
|
159
|
+
// Update database: ubah status order menjadi GAGAL
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Kirim respon sukses ke Payment Gateway (Duitku meminta respon plaintext OK)
|
|
163
|
+
res.status(200).send("OK");
|
|
164
|
+
} else {
|
|
165
|
+
console.warn("Peringatan: Signature callback tidak valid! Potensi manipulasi data.");
|
|
166
|
+
res.status(400).send("Bad Signature");
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
console.error("Gagal memproses callback:", error);
|
|
170
|
+
res.status(500).send("Internal Server Error");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## 🗂️ Referensi API
|
|
178
|
+
|
|
179
|
+
### `PaymentManager`
|
|
180
|
+
|
|
181
|
+
#### `createInvoice(providerName, params, config)`
|
|
182
|
+
Membuat transaksi baru ke gerbang pembayaran tertentu.
|
|
183
|
+
- **`providerName`**: `string` - Nama provider payment gateway (contoh: `'duitku'`).
|
|
184
|
+
- **`params`**: `CreateInvoiceParams` - Data invoice dan detail pelanggan.
|
|
185
|
+
- **`config`**: `ProviderConfig` - Kredensial merchant dan API key.
|
|
186
|
+
- **Return**: `Promise<InvoiceResponse>`
|
|
187
|
+
|
|
188
|
+
#### `verifyCallback(providerName, body, config)`
|
|
189
|
+
Memvalidasi signature webhook callback dari payment gateway.
|
|
190
|
+
- **`providerName`**: `string` - Nama provider payment gateway (contoh: `'duitku'`).
|
|
191
|
+
- **`body`**: `any` - Payload callback mentah dari request body.
|
|
192
|
+
- **`config`**: `ProviderConfig` - Kredensial merchant dan API key.
|
|
193
|
+
- **Return**: `Promise<VerifyCallbackResult>`
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 📐 Spesifikasi Interface (TypeScript)
|
|
198
|
+
|
|
199
|
+
### `CreateInvoiceParams`
|
|
200
|
+
```typescript
|
|
201
|
+
interface CreateInvoiceParams {
|
|
202
|
+
orderId: string;
|
|
203
|
+
amount: number;
|
|
204
|
+
productDetails: string;
|
|
205
|
+
customer: {
|
|
206
|
+
name: string;
|
|
207
|
+
email: string;
|
|
208
|
+
phone?: string;
|
|
209
|
+
};
|
|
210
|
+
returnUrl: string;
|
|
211
|
+
callbackUrl: string;
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### `InvoiceResponse`
|
|
216
|
+
```typescript
|
|
217
|
+
interface InvoiceResponse {
|
|
218
|
+
success: boolean;
|
|
219
|
+
paymentUrl?: string;
|
|
220
|
+
reference?: string;
|
|
221
|
+
rawResponse: any;
|
|
222
|
+
error?: string;
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### `VerifyCallbackResult`
|
|
227
|
+
```typescript
|
|
228
|
+
interface VerifyCallbackResult {
|
|
229
|
+
isValid: boolean;
|
|
230
|
+
orderId: string;
|
|
231
|
+
amount: number;
|
|
232
|
+
status: "paid" | "pending" | "failed";
|
|
233
|
+
rawPayload: any;
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## 📄 Lisensi
|
|
240
|
+
|
|
241
|
+
Proyek ini dilisensikan di bawah **MIT License**. Hak Cipta © 2026 Rasyiqi Crediblemark.
|