@crediblemark/buayar 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.
@@ -0,0 +1,55 @@
1
+ interface CreateInvoiceParams {
2
+ orderId: string;
3
+ amount: number;
4
+ productDetails: string;
5
+ customer: {
6
+ name: string;
7
+ email: string;
8
+ phone?: string;
9
+ };
10
+ returnUrl: string;
11
+ callbackUrl: string;
12
+ }
13
+ interface InvoiceResponse {
14
+ success: boolean;
15
+ paymentUrl?: string;
16
+ reference?: string;
17
+ rawResponse: any;
18
+ error?: string;
19
+ }
20
+ interface VerifyCallbackResult {
21
+ isValid: boolean;
22
+ orderId: string;
23
+ amount: number;
24
+ status: "paid" | "pending" | "failed";
25
+ rawPayload: any;
26
+ }
27
+ interface ProviderConfig {
28
+ merchantCode: string;
29
+ apiKey: string;
30
+ sandbox: boolean;
31
+ }
32
+
33
+ declare abstract class BasePaymentProvider {
34
+ abstract readonly name: string;
35
+ abstract createInvoice(params: CreateInvoiceParams, config: ProviderConfig): Promise<InvoiceResponse>;
36
+ abstract verifyCallback(body: any, config: ProviderConfig): Promise<VerifyCallbackResult>;
37
+ }
38
+
39
+ declare class DuitkuProvider extends BasePaymentProvider {
40
+ readonly name = "duitku";
41
+ createInvoice(params: CreateInvoiceParams, config: ProviderConfig): Promise<InvoiceResponse>;
42
+ verifyCallback(body: any, config: ProviderConfig): Promise<VerifyCallbackResult>;
43
+ }
44
+
45
+ declare class PaymentManager {
46
+ private providers;
47
+ constructor();
48
+ registerProvider(provider: BasePaymentProvider): void;
49
+ getProvider(name: string): BasePaymentProvider;
50
+ createInvoice(providerName: string, params: CreateInvoiceParams, config: ProviderConfig): Promise<InvoiceResponse>;
51
+ verifyCallback(providerName: string, body: any, config: ProviderConfig): Promise<VerifyCallbackResult>;
52
+ }
53
+ declare const paymentManager: PaymentManager;
54
+
55
+ export { BasePaymentProvider, type CreateInvoiceParams, DuitkuProvider, type InvoiceResponse, PaymentManager, type ProviderConfig, type VerifyCallbackResult, paymentManager };
@@ -0,0 +1,55 @@
1
+ interface CreateInvoiceParams {
2
+ orderId: string;
3
+ amount: number;
4
+ productDetails: string;
5
+ customer: {
6
+ name: string;
7
+ email: string;
8
+ phone?: string;
9
+ };
10
+ returnUrl: string;
11
+ callbackUrl: string;
12
+ }
13
+ interface InvoiceResponse {
14
+ success: boolean;
15
+ paymentUrl?: string;
16
+ reference?: string;
17
+ rawResponse: any;
18
+ error?: string;
19
+ }
20
+ interface VerifyCallbackResult {
21
+ isValid: boolean;
22
+ orderId: string;
23
+ amount: number;
24
+ status: "paid" | "pending" | "failed";
25
+ rawPayload: any;
26
+ }
27
+ interface ProviderConfig {
28
+ merchantCode: string;
29
+ apiKey: string;
30
+ sandbox: boolean;
31
+ }
32
+
33
+ declare abstract class BasePaymentProvider {
34
+ abstract readonly name: string;
35
+ abstract createInvoice(params: CreateInvoiceParams, config: ProviderConfig): Promise<InvoiceResponse>;
36
+ abstract verifyCallback(body: any, config: ProviderConfig): Promise<VerifyCallbackResult>;
37
+ }
38
+
39
+ declare class DuitkuProvider extends BasePaymentProvider {
40
+ readonly name = "duitku";
41
+ createInvoice(params: CreateInvoiceParams, config: ProviderConfig): Promise<InvoiceResponse>;
42
+ verifyCallback(body: any, config: ProviderConfig): Promise<VerifyCallbackResult>;
43
+ }
44
+
45
+ declare class PaymentManager {
46
+ private providers;
47
+ constructor();
48
+ registerProvider(provider: BasePaymentProvider): void;
49
+ getProvider(name: string): BasePaymentProvider;
50
+ createInvoice(providerName: string, params: CreateInvoiceParams, config: ProviderConfig): Promise<InvoiceResponse>;
51
+ verifyCallback(providerName: string, body: any, config: ProviderConfig): Promise<VerifyCallbackResult>;
52
+ }
53
+ declare const paymentManager: PaymentManager;
54
+
55
+ export { BasePaymentProvider, type CreateInvoiceParams, DuitkuProvider, type InvoiceResponse, PaymentManager, type ProviderConfig, type VerifyCallbackResult, paymentManager };
package/dist/index.js ADDED
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ BasePaymentProvider: () => BasePaymentProvider,
34
+ DuitkuProvider: () => DuitkuProvider,
35
+ PaymentManager: () => PaymentManager,
36
+ paymentManager: () => paymentManager
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+
40
+ // src/providers/duitku.ts
41
+ var import_crypto = __toESM(require("crypto"));
42
+
43
+ // src/providers/base.ts
44
+ var BasePaymentProvider = class {
45
+ };
46
+
47
+ // src/providers/duitku.ts
48
+ var DuitkuProvider = class extends BasePaymentProvider {
49
+ name = "duitku";
50
+ async createInvoice(params, config) {
51
+ const { orderId, amount, productDetails, customer, returnUrl, callbackUrl } = params;
52
+ const { merchantCode, apiKey, sandbox } = config;
53
+ const url = sandbox ? "https://sandbox.duitku.com/webapi/api/merchant/v2/inquiry" : "https://passport.duitku.com/webapi/api/merchant/v2/inquiry";
54
+ const rawSignature = merchantCode + orderId + amount.toString() + apiKey;
55
+ const signature = import_crypto.default.createHash("md5").update(rawSignature).digest("hex");
56
+ const payload = {
57
+ merchantCode,
58
+ paymentAmount: amount,
59
+ merchantOrderId: orderId,
60
+ productDetails,
61
+ email: customer.email,
62
+ phoneNumber: customer.phone || "",
63
+ signature,
64
+ callbackUrl,
65
+ returnUrl,
66
+ expiryPeriod: 1440
67
+ // 24 hours expiry
68
+ };
69
+ try {
70
+ const response = await fetch(url, {
71
+ method: "POST",
72
+ headers: {
73
+ "Content-Type": "application/json"
74
+ },
75
+ body: JSON.stringify(payload)
76
+ });
77
+ if (!response.ok) {
78
+ return {
79
+ success: false,
80
+ rawResponse: null,
81
+ error: `HTTP error! Status: ${response.status}`
82
+ };
83
+ }
84
+ const data = await response.json();
85
+ if (data.statusCode === "00") {
86
+ return {
87
+ success: true,
88
+ paymentUrl: data.paymentUrl,
89
+ reference: data.reference,
90
+ rawResponse: data
91
+ };
92
+ } else {
93
+ return {
94
+ success: false,
95
+ rawResponse: data,
96
+ error: data.statusMessage || `Duitku Error: ${data.statusCode}`
97
+ };
98
+ }
99
+ } catch (e) {
100
+ return {
101
+ success: false,
102
+ rawResponse: null,
103
+ error: e.message || "Failed to make inquiry request to Duitku"
104
+ };
105
+ }
106
+ }
107
+ async verifyCallback(body, config) {
108
+ const { apiKey } = config;
109
+ const merchantCode = body.merchantCode || "";
110
+ const amount = body.amount || "";
111
+ const merchantOrderId = body.merchantOrderId || "";
112
+ const signature = body.signature || "";
113
+ const rawSignature = merchantCode + amount + merchantOrderId + apiKey;
114
+ const computedSignature = import_crypto.default.createHash("md5").update(rawSignature).digest("hex");
115
+ const isValid = signature.toLowerCase() === computedSignature.toLowerCase();
116
+ const isPaid = body.resultCode === "00";
117
+ return {
118
+ isValid,
119
+ orderId: merchantOrderId,
120
+ amount: amount ? Number(amount) : 0,
121
+ status: isValid ? isPaid ? "paid" : "failed" : "failed",
122
+ rawPayload: body
123
+ };
124
+ }
125
+ };
126
+
127
+ // src/index.ts
128
+ var PaymentManager = class {
129
+ providers = /* @__PURE__ */ new Map();
130
+ constructor() {
131
+ this.registerProvider(new DuitkuProvider());
132
+ }
133
+ registerProvider(provider) {
134
+ this.providers.set(provider.name.toLowerCase(), provider);
135
+ }
136
+ getProvider(name) {
137
+ const provider = this.providers.get(name.toLowerCase());
138
+ if (!provider) {
139
+ throw new Error(`Payment provider '${name}' is not registered`);
140
+ }
141
+ return provider;
142
+ }
143
+ async createInvoice(providerName, params, config) {
144
+ const provider = this.getProvider(providerName);
145
+ return provider.createInvoice(params, config);
146
+ }
147
+ async verifyCallback(providerName, body, config) {
148
+ const provider = this.getProvider(providerName);
149
+ return provider.verifyCallback(body, config);
150
+ }
151
+ };
152
+ var paymentManager = new PaymentManager();
153
+ // Annotate the CommonJS export names for ESM import in node:
154
+ 0 && (module.exports = {
155
+ BasePaymentProvider,
156
+ DuitkuProvider,
157
+ PaymentManager,
158
+ paymentManager
159
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,119 @@
1
+ // src/providers/duitku.ts
2
+ import crypto from "crypto";
3
+
4
+ // src/providers/base.ts
5
+ var BasePaymentProvider = class {
6
+ };
7
+
8
+ // src/providers/duitku.ts
9
+ var DuitkuProvider = class extends BasePaymentProvider {
10
+ name = "duitku";
11
+ async createInvoice(params, config) {
12
+ const { orderId, amount, productDetails, customer, returnUrl, callbackUrl } = params;
13
+ const { merchantCode, apiKey, sandbox } = config;
14
+ const url = sandbox ? "https://sandbox.duitku.com/webapi/api/merchant/v2/inquiry" : "https://passport.duitku.com/webapi/api/merchant/v2/inquiry";
15
+ const rawSignature = merchantCode + orderId + amount.toString() + apiKey;
16
+ const signature = crypto.createHash("md5").update(rawSignature).digest("hex");
17
+ const payload = {
18
+ merchantCode,
19
+ paymentAmount: amount,
20
+ merchantOrderId: orderId,
21
+ productDetails,
22
+ email: customer.email,
23
+ phoneNumber: customer.phone || "",
24
+ signature,
25
+ callbackUrl,
26
+ returnUrl,
27
+ expiryPeriod: 1440
28
+ // 24 hours expiry
29
+ };
30
+ try {
31
+ const response = await fetch(url, {
32
+ method: "POST",
33
+ headers: {
34
+ "Content-Type": "application/json"
35
+ },
36
+ body: JSON.stringify(payload)
37
+ });
38
+ if (!response.ok) {
39
+ return {
40
+ success: false,
41
+ rawResponse: null,
42
+ error: `HTTP error! Status: ${response.status}`
43
+ };
44
+ }
45
+ const data = await response.json();
46
+ if (data.statusCode === "00") {
47
+ return {
48
+ success: true,
49
+ paymentUrl: data.paymentUrl,
50
+ reference: data.reference,
51
+ rawResponse: data
52
+ };
53
+ } else {
54
+ return {
55
+ success: false,
56
+ rawResponse: data,
57
+ error: data.statusMessage || `Duitku Error: ${data.statusCode}`
58
+ };
59
+ }
60
+ } catch (e) {
61
+ return {
62
+ success: false,
63
+ rawResponse: null,
64
+ error: e.message || "Failed to make inquiry request to Duitku"
65
+ };
66
+ }
67
+ }
68
+ async verifyCallback(body, config) {
69
+ const { apiKey } = config;
70
+ const merchantCode = body.merchantCode || "";
71
+ const amount = body.amount || "";
72
+ const merchantOrderId = body.merchantOrderId || "";
73
+ const signature = body.signature || "";
74
+ const rawSignature = merchantCode + amount + merchantOrderId + apiKey;
75
+ const computedSignature = crypto.createHash("md5").update(rawSignature).digest("hex");
76
+ const isValid = signature.toLowerCase() === computedSignature.toLowerCase();
77
+ const isPaid = body.resultCode === "00";
78
+ return {
79
+ isValid,
80
+ orderId: merchantOrderId,
81
+ amount: amount ? Number(amount) : 0,
82
+ status: isValid ? isPaid ? "paid" : "failed" : "failed",
83
+ rawPayload: body
84
+ };
85
+ }
86
+ };
87
+
88
+ // src/index.ts
89
+ var PaymentManager = class {
90
+ providers = /* @__PURE__ */ new Map();
91
+ constructor() {
92
+ this.registerProvider(new DuitkuProvider());
93
+ }
94
+ registerProvider(provider) {
95
+ this.providers.set(provider.name.toLowerCase(), provider);
96
+ }
97
+ getProvider(name) {
98
+ const provider = this.providers.get(name.toLowerCase());
99
+ if (!provider) {
100
+ throw new Error(`Payment provider '${name}' is not registered`);
101
+ }
102
+ return provider;
103
+ }
104
+ async createInvoice(providerName, params, config) {
105
+ const provider = this.getProvider(providerName);
106
+ return provider.createInvoice(params, config);
107
+ }
108
+ async verifyCallback(providerName, body, config) {
109
+ const provider = this.getProvider(providerName);
110
+ return provider.verifyCallback(body, config);
111
+ }
112
+ };
113
+ var paymentManager = new PaymentManager();
114
+ export {
115
+ BasePaymentProvider,
116
+ DuitkuProvider,
117
+ PaymentManager,
118
+ paymentManager
119
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@crediblemark/buayar",
3
+ "version": "0.1.0",
4
+ "description": "Unified Payment Gateway SDK for CredBuild platforms and sites",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "package.json",
19
+ "README.md"
20
+ ],
21
+ "license": "MIT",
22
+ "scripts": {
23
+ "build": "tsup src/index.ts --format cjs,esm --dts",
24
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "^24.12.2",
28
+ "tsup": "^8.5.1",
29
+ "typescript": "^5.9.3"
30
+ }
31
+ }