@crediblemark/buayar 0.1.2 → 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 ADDED
@@ -0,0 +1,241 @@
1
+ # 💳 @crediblemark/buayar
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@crediblemark/buayar.svg?style=flat-square&color=amber)](https://www.npmjs.com/package/@crediblemark/buayar)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](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.
package/dist/index.js CHANGED
@@ -52,8 +52,11 @@ var DuitkuProvider = class extends BasePaymentProvider {
52
52
  const { merchantCode, apiKey, sandbox } = config;
53
53
  const url = sandbox ? "https://api-sandbox.duitku.com/api/merchant/createInvoice" : "https://api-prod.duitku.com/api/merchant/createInvoice";
54
54
  const integerAmount = Math.round(amount);
55
- const rawSignature = merchantCode + orderId + integerAmount.toString() + apiKey;
56
- const signature = import_crypto.default.createHash("md5").update(rawSignature).digest("hex");
55
+ const rawPayloadSignature = merchantCode + orderId + integerAmount.toString() + apiKey;
56
+ const payloadSignature = import_crypto.default.createHash("md5").update(rawPayloadSignature).digest("hex");
57
+ const timestamp = Date.now().toString();
58
+ const rawHeaderSignature = merchantCode + timestamp + apiKey;
59
+ const headerSignature = import_crypto.default.createHash("sha256").update(rawHeaderSignature).digest("hex");
57
60
  const payload = {
58
61
  merchantCode,
59
62
  paymentAmount: integerAmount,
@@ -61,7 +64,7 @@ var DuitkuProvider = class extends BasePaymentProvider {
61
64
  productDetails,
62
65
  email: customer.email,
63
66
  phoneNumber: customer.phone || "",
64
- signature,
67
+ signature: payloadSignature,
65
68
  callbackUrl,
66
69
  returnUrl,
67
70
  expiryPeriod: 1440
@@ -71,7 +74,11 @@ var DuitkuProvider = class extends BasePaymentProvider {
71
74
  const response = await fetch(url, {
72
75
  method: "POST",
73
76
  headers: {
74
- "Content-Type": "application/json"
77
+ "Content-Type": "application/json",
78
+ "Accept": "application/json",
79
+ "x-duitku-signature": headerSignature,
80
+ "x-duitku-timestamp": timestamp,
81
+ "x-duitku-merchantcode": merchantCode
75
82
  },
76
83
  body: JSON.stringify(payload)
77
84
  });
package/dist/index.mjs CHANGED
@@ -13,8 +13,11 @@ var DuitkuProvider = class extends BasePaymentProvider {
13
13
  const { merchantCode, apiKey, sandbox } = config;
14
14
  const url = sandbox ? "https://api-sandbox.duitku.com/api/merchant/createInvoice" : "https://api-prod.duitku.com/api/merchant/createInvoice";
15
15
  const integerAmount = Math.round(amount);
16
- const rawSignature = merchantCode + orderId + integerAmount.toString() + apiKey;
17
- const signature = crypto.createHash("md5").update(rawSignature).digest("hex");
16
+ const rawPayloadSignature = merchantCode + orderId + integerAmount.toString() + apiKey;
17
+ const payloadSignature = crypto.createHash("md5").update(rawPayloadSignature).digest("hex");
18
+ const timestamp = Date.now().toString();
19
+ const rawHeaderSignature = merchantCode + timestamp + apiKey;
20
+ const headerSignature = crypto.createHash("sha256").update(rawHeaderSignature).digest("hex");
18
21
  const payload = {
19
22
  merchantCode,
20
23
  paymentAmount: integerAmount,
@@ -22,7 +25,7 @@ var DuitkuProvider = class extends BasePaymentProvider {
22
25
  productDetails,
23
26
  email: customer.email,
24
27
  phoneNumber: customer.phone || "",
25
- signature,
28
+ signature: payloadSignature,
26
29
  callbackUrl,
27
30
  returnUrl,
28
31
  expiryPeriod: 1440
@@ -32,7 +35,11 @@ var DuitkuProvider = class extends BasePaymentProvider {
32
35
  const response = await fetch(url, {
33
36
  method: "POST",
34
37
  headers: {
35
- "Content-Type": "application/json"
38
+ "Content-Type": "application/json",
39
+ "Accept": "application/json",
40
+ "x-duitku-signature": headerSignature,
41
+ "x-duitku-timestamp": timestamp,
42
+ "x-duitku-merchantcode": merchantCode
36
43
  },
37
44
  body: JSON.stringify(payload)
38
45
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crediblemark/buayar",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Unified Payment Gateway SDK for CredBuild platforms and sites",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",