@crediblemark/buayar 0.1.6 → 0.1.8
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 +56 -175
- package/dist/index.d.mts +78 -1
- package/dist/index.d.ts +78 -1
- package/dist/index.js +713 -0
- package/dist/index.mjs +711 -0
- package/package.json +5 -1
package/dist/index.mjs
CHANGED
|
@@ -264,6 +264,701 @@ var DuitkuProvider = class extends BasePaymentProvider {
|
|
|
264
264
|
};
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
|
+
async probePaymentMethods(config) {
|
|
268
|
+
const res = await this.getPaymentMethods({ amount: 1e4 }, config);
|
|
269
|
+
if (res.success) {
|
|
270
|
+
return { success: true, enabled: res.methods.map((m) => m.paymentMethod) };
|
|
271
|
+
}
|
|
272
|
+
return { success: false, enabled: [], error: res.error };
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/providers/midtrans.ts
|
|
277
|
+
import crypto2 from "crypto";
|
|
278
|
+
|
|
279
|
+
// src/clients/midtrans.ts
|
|
280
|
+
var MidtransClient = class {
|
|
281
|
+
apiKey;
|
|
282
|
+
sandbox;
|
|
283
|
+
constructor(config) {
|
|
284
|
+
this.apiKey = config.apiKey;
|
|
285
|
+
this.sandbox = config.sandbox;
|
|
286
|
+
}
|
|
287
|
+
getApiBaseUrl() {
|
|
288
|
+
return this.sandbox ? "https://api.sandbox.midtrans.com/v2" : "https://api.midtrans.com/v2";
|
|
289
|
+
}
|
|
290
|
+
async request(method, path, body) {
|
|
291
|
+
let url = path.startsWith("http") ? path : `${this.getApiBaseUrl()}${path}`;
|
|
292
|
+
if (!path.startsWith("http") && (path.startsWith("/v1/") || path.startsWith("/v2/"))) {
|
|
293
|
+
url = `${this.sandbox ? "https://api.sandbox.midtrans.com" : "https://api.midtrans.com"}${path}`;
|
|
294
|
+
}
|
|
295
|
+
const authHeader = `Basic ${Buffer.from(this.apiKey + ":").toString("base64")}`;
|
|
296
|
+
const headers = {
|
|
297
|
+
"Content-Type": "application/json",
|
|
298
|
+
"Accept": "application/json",
|
|
299
|
+
"Authorization": authHeader
|
|
300
|
+
};
|
|
301
|
+
const fetchOptions = {
|
|
302
|
+
method,
|
|
303
|
+
headers
|
|
304
|
+
};
|
|
305
|
+
if (body) {
|
|
306
|
+
fetchOptions.body = JSON.stringify(body);
|
|
307
|
+
}
|
|
308
|
+
const response = await fetch(url, fetchOptions);
|
|
309
|
+
const text = await response.text();
|
|
310
|
+
let data = null;
|
|
311
|
+
try {
|
|
312
|
+
data = JSON.parse(text);
|
|
313
|
+
} catch (e) {
|
|
314
|
+
}
|
|
315
|
+
if (!response.ok) {
|
|
316
|
+
throw new Error(data?.message || data?.status_message || `HTTP error! Status: ${response.status} - ${text}`);
|
|
317
|
+
}
|
|
318
|
+
return data || text;
|
|
319
|
+
}
|
|
320
|
+
// ─── TRANSACTION ACTIONS ─────────────────────────────────────────────────────
|
|
321
|
+
async cancelTransaction(orderId) {
|
|
322
|
+
return this.request("POST", `/${orderId}/cancel`, null);
|
|
323
|
+
}
|
|
324
|
+
async refundTransaction(orderId, payload) {
|
|
325
|
+
return this.request("POST", `/${orderId}/refund`, payload);
|
|
326
|
+
}
|
|
327
|
+
async expireTransaction(orderId) {
|
|
328
|
+
return this.request("POST", `/${orderId}/expire`, null);
|
|
329
|
+
}
|
|
330
|
+
async approveTransaction(orderId) {
|
|
331
|
+
return this.request("POST", `/${orderId}/approve`, null);
|
|
332
|
+
}
|
|
333
|
+
async denyTransaction(orderId) {
|
|
334
|
+
return this.request("POST", `/${orderId}/deny`, null);
|
|
335
|
+
}
|
|
336
|
+
async captureTransaction(transactionId, amount) {
|
|
337
|
+
const payload = amount ? { transaction_id: transactionId, gross_amount: Math.round(amount) } : { transaction_id: transactionId };
|
|
338
|
+
return this.request("POST", "/capture", payload);
|
|
339
|
+
}
|
|
340
|
+
// ─── GOPAY TOKENIZATION API ──────────────────────────────────────────────────
|
|
341
|
+
async linkPayAccount(payload) {
|
|
342
|
+
return this.request("POST", "/v2/pay/account", payload);
|
|
343
|
+
}
|
|
344
|
+
async getPayAccount(accountId) {
|
|
345
|
+
return this.request("GET", `/v2/pay/account/${accountId}`, null);
|
|
346
|
+
}
|
|
347
|
+
async unbindPayAccount(accountId) {
|
|
348
|
+
return this.request("POST", `/v2/pay/account/${accountId}/unbind`, null);
|
|
349
|
+
}
|
|
350
|
+
async getGoPayPromo(accountId, grossAmount, currency = "IDR") {
|
|
351
|
+
return this.request("GET", `/v2/gopay/promo/${accountId}?gross_amount=${grossAmount}¤cy=${currency}`, null);
|
|
352
|
+
}
|
|
353
|
+
// ─── SUBSCRIPTION API ────────────────────────────────────────────────────────
|
|
354
|
+
async createSubscription(payload) {
|
|
355
|
+
return this.request("POST", "/v1/subscriptions", payload);
|
|
356
|
+
}
|
|
357
|
+
async getSubscription(subscriptionId) {
|
|
358
|
+
return this.request("GET", `/v1/subscriptions/${subscriptionId}`, null);
|
|
359
|
+
}
|
|
360
|
+
async updateSubscription(subscriptionId, payload) {
|
|
361
|
+
return this.request("PATCH", `/v1/subscriptions/${subscriptionId}`, payload);
|
|
362
|
+
}
|
|
363
|
+
async disableSubscription(subscriptionId) {
|
|
364
|
+
return this.request("POST", `/v1/subscriptions/${subscriptionId}/disable`, null);
|
|
365
|
+
}
|
|
366
|
+
async enableSubscription(subscriptionId) {
|
|
367
|
+
return this.request("POST", `/v1/subscriptions/${subscriptionId}/enable`, null);
|
|
368
|
+
}
|
|
369
|
+
// ─── PAYMENT LINK API ────────────────────────────────────────────────────────
|
|
370
|
+
async createPaymentLink(payload) {
|
|
371
|
+
return this.request("POST", "/v1/payment-links", payload);
|
|
372
|
+
}
|
|
373
|
+
async getPaymentLink(paymentLinkId) {
|
|
374
|
+
return this.request("GET", `/v1/payment-links/${paymentLinkId}`, null);
|
|
375
|
+
}
|
|
376
|
+
async deletePaymentLink(paymentLinkId) {
|
|
377
|
+
return this.request("DELETE", `/v1/payment-links/${paymentLinkId}`, null);
|
|
378
|
+
}
|
|
379
|
+
// ─── BALANCE API ─────────────────────────────────────────────────────────────
|
|
380
|
+
async getBalance() {
|
|
381
|
+
try {
|
|
382
|
+
return await this.request("GET", "/v1/balance", null);
|
|
383
|
+
} catch (e) {
|
|
384
|
+
try {
|
|
385
|
+
const irisUrl = `${this.sandbox ? "https://api.sandbox.midtrans.com" : "https://api.midtrans.com"}/iris/api/v1/balance`;
|
|
386
|
+
const authHeader = `Basic ${Buffer.from(this.apiKey + ":").toString("base64")}`;
|
|
387
|
+
const response = await fetch(irisUrl, {
|
|
388
|
+
method: "GET",
|
|
389
|
+
headers: {
|
|
390
|
+
"Accept": "application/json",
|
|
391
|
+
"Authorization": authHeader
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
const text = await response.text();
|
|
395
|
+
let data = null;
|
|
396
|
+
try {
|
|
397
|
+
data = JSON.parse(text);
|
|
398
|
+
} catch (err) {
|
|
399
|
+
}
|
|
400
|
+
if (response.ok) return data || text;
|
|
401
|
+
} catch (irisErr) {
|
|
402
|
+
}
|
|
403
|
+
throw e;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
// ─── INVOICING API ───────────────────────────────────────────────────────────
|
|
407
|
+
async createBillingInvoice(payload) {
|
|
408
|
+
return this.request("POST", "/v1/invoices", payload);
|
|
409
|
+
}
|
|
410
|
+
async getBillingInvoice(invoiceId) {
|
|
411
|
+
return this.request("GET", `/v1/invoices/${invoiceId}`, null);
|
|
412
|
+
}
|
|
413
|
+
async voidBillingInvoice(invoiceId) {
|
|
414
|
+
return this.request("PATCH", `/v1/invoices/${invoiceId}/void`, null);
|
|
415
|
+
}
|
|
416
|
+
async convertBillingInvoice(invoiceId) {
|
|
417
|
+
return this.request("PATCH", `/v1/invoices/${invoiceId}/convert`, null);
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
// src/providers/midtrans.ts
|
|
422
|
+
var MidtransProvider = class extends BasePaymentProvider {
|
|
423
|
+
name = "midtrans";
|
|
424
|
+
getSnapBaseUrl(sandbox) {
|
|
425
|
+
return sandbox ? "https://app.sandbox.midtrans.com/snap/v1/transactions" : "https://app.midtrans.com/snap/v1/transactions";
|
|
426
|
+
}
|
|
427
|
+
getApiBaseUrl(sandbox) {
|
|
428
|
+
return sandbox ? "https://api.sandbox.midtrans.com/v2" : "https://api.midtrans.com/v2";
|
|
429
|
+
}
|
|
430
|
+
async createInvoice(params, config) {
|
|
431
|
+
const { orderId, amount, productDetails, customer, returnUrl } = params;
|
|
432
|
+
const { apiKey, sandbox } = config;
|
|
433
|
+
const integerAmount = Math.round(amount);
|
|
434
|
+
const method = params.paymentMethod?.toLowerCase() || "";
|
|
435
|
+
const coreApiMethods = [
|
|
436
|
+
"bca_va",
|
|
437
|
+
"bni_va",
|
|
438
|
+
"bri_va",
|
|
439
|
+
"permata_va",
|
|
440
|
+
"cimb_va",
|
|
441
|
+
"danamon_va",
|
|
442
|
+
"bsi_va",
|
|
443
|
+
"seabank_va",
|
|
444
|
+
"mandiri_va",
|
|
445
|
+
"qris",
|
|
446
|
+
"gopay",
|
|
447
|
+
"shopeepay",
|
|
448
|
+
"ovo",
|
|
449
|
+
"dana",
|
|
450
|
+
"linkaja",
|
|
451
|
+
"alfamart",
|
|
452
|
+
"indomaret",
|
|
453
|
+
"credit_card",
|
|
454
|
+
"googlepay",
|
|
455
|
+
"kredivo",
|
|
456
|
+
"akulaku"
|
|
457
|
+
];
|
|
458
|
+
if (method && coreApiMethods.includes(method)) {
|
|
459
|
+
const url2 = `${this.getApiBaseUrl(sandbox)}/charge`;
|
|
460
|
+
let payload2 = {
|
|
461
|
+
transaction_details: {
|
|
462
|
+
order_id: orderId,
|
|
463
|
+
gross_amount: integerAmount
|
|
464
|
+
},
|
|
465
|
+
customer_details: {
|
|
466
|
+
first_name: customer.name,
|
|
467
|
+
email: customer.email,
|
|
468
|
+
phone: customer.phone || ""
|
|
469
|
+
},
|
|
470
|
+
item_details: [
|
|
471
|
+
{
|
|
472
|
+
id: orderId,
|
|
473
|
+
price: integerAmount,
|
|
474
|
+
quantity: 1,
|
|
475
|
+
name: productDetails.length > 50 ? productDetails.substring(0, 47) + "..." : productDetails
|
|
476
|
+
}
|
|
477
|
+
],
|
|
478
|
+
...params.providerParams
|
|
479
|
+
};
|
|
480
|
+
if (["bca_va", "bni_va", "bri_va", "cimb_va", "permata_va", "danamon_va", "bsi_va", "seabank_va"].includes(method)) {
|
|
481
|
+
const bankName = method.split("_")[0];
|
|
482
|
+
payload2.payment_type = "bank_transfer";
|
|
483
|
+
payload2.bank_transfer = {
|
|
484
|
+
bank: bankName
|
|
485
|
+
};
|
|
486
|
+
} else if (method === "mandiri_va") {
|
|
487
|
+
payload2.payment_type = "echannel";
|
|
488
|
+
payload2.echannel = {
|
|
489
|
+
bill_info1: "Payment for",
|
|
490
|
+
bill_info2: productDetails.length > 30 ? productDetails.substring(0, 27) + "..." : productDetails
|
|
491
|
+
};
|
|
492
|
+
} else if (method === "qris") {
|
|
493
|
+
payload2.payment_type = "qris";
|
|
494
|
+
} else if (method === "gopay") {
|
|
495
|
+
payload2.payment_type = "gopay";
|
|
496
|
+
payload2.gopay = {
|
|
497
|
+
enable_callback: true,
|
|
498
|
+
callback_url: returnUrl
|
|
499
|
+
};
|
|
500
|
+
} else if (method === "shopeepay") {
|
|
501
|
+
payload2.payment_type = "shopeepay";
|
|
502
|
+
payload2.shopeepay = {
|
|
503
|
+
callback_url: returnUrl
|
|
504
|
+
};
|
|
505
|
+
} else if (method === "ovo") {
|
|
506
|
+
if (!customer.phone) {
|
|
507
|
+
return {
|
|
508
|
+
success: false,
|
|
509
|
+
rawResponse: null,
|
|
510
|
+
error: "OVO payment method requires a customer phone number in customer.phone"
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
payload2.payment_type = "ovo";
|
|
514
|
+
payload2.ovo = {
|
|
515
|
+
phone: customer.phone
|
|
516
|
+
};
|
|
517
|
+
} else if (method === "dana") {
|
|
518
|
+
payload2.payment_type = "dana";
|
|
519
|
+
} else if (method === "linkaja") {
|
|
520
|
+
payload2.payment_type = "linkaja";
|
|
521
|
+
} else if (method === "credit_card") {
|
|
522
|
+
const token = params.providerParams?.credit_card?.token_id || params.providerParams?.tokenId;
|
|
523
|
+
if (!token) {
|
|
524
|
+
return {
|
|
525
|
+
success: false,
|
|
526
|
+
rawResponse: null,
|
|
527
|
+
error: "Credit Card payment method requires token_id (passed via providerParams.credit_card.token_id or providerParams.tokenId)"
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
payload2.payment_type = "credit_card";
|
|
531
|
+
payload2.credit_card = {
|
|
532
|
+
token_id: token,
|
|
533
|
+
authentication: params.providerParams?.credit_card?.authentication ?? true,
|
|
534
|
+
save_card: params.providerParams?.credit_card?.save_card,
|
|
535
|
+
bank: params.providerParams?.credit_card?.bank,
|
|
536
|
+
installment_term: params.providerParams?.credit_card?.installment_term,
|
|
537
|
+
bins: params.providerParams?.credit_card?.bins,
|
|
538
|
+
type: params.providerParams?.credit_card?.type
|
|
539
|
+
};
|
|
540
|
+
} else if (method === "googlepay") {
|
|
541
|
+
const token = params.providerParams?.googlepay?.token_id || params.providerParams?.tokenId;
|
|
542
|
+
if (!token) {
|
|
543
|
+
return {
|
|
544
|
+
success: false,
|
|
545
|
+
rawResponse: null,
|
|
546
|
+
error: "Google Pay payment method requires token_id (passed via providerParams.googlepay.token_id or providerParams.tokenId)"
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
payload2.payment_type = "googlepay";
|
|
550
|
+
payload2.googlepay = {
|
|
551
|
+
token_id: token
|
|
552
|
+
};
|
|
553
|
+
} else if (method === "kredivo") {
|
|
554
|
+
payload2.payment_type = "kredivo";
|
|
555
|
+
payload2.kredivo = {
|
|
556
|
+
address: params.providerParams?.kredivo?.address,
|
|
557
|
+
first_name: params.providerParams?.kredivo?.first_name || customer.name.split(" ")[0],
|
|
558
|
+
last_name: params.providerParams?.kredivo?.last_name || customer.name.split(" ").slice(1).join(" "),
|
|
559
|
+
email: params.providerParams?.kredivo?.email || customer.email,
|
|
560
|
+
phone: params.providerParams?.kredivo?.phone || customer.phone || ""
|
|
561
|
+
};
|
|
562
|
+
} else if (method === "akulaku") {
|
|
563
|
+
payload2.payment_type = "akulaku";
|
|
564
|
+
} else if (["alfamart", "indomaret"].includes(method)) {
|
|
565
|
+
payload2.payment_type = "cstore";
|
|
566
|
+
payload2.cstore = {
|
|
567
|
+
store: method,
|
|
568
|
+
message: productDetails.length > 30 ? productDetails.substring(0, 27) + "..." : productDetails
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
try {
|
|
572
|
+
const client = new MidtransClient(config);
|
|
573
|
+
const data = await client.request("POST", "/charge", payload2);
|
|
574
|
+
if (data.status_code === "201" || data.status_code === "200") {
|
|
575
|
+
const res = {
|
|
576
|
+
success: true,
|
|
577
|
+
reference: data.transaction_id || data.order_id,
|
|
578
|
+
rawResponse: data
|
|
579
|
+
};
|
|
580
|
+
if (["bca_va", "bni_va", "bri_va", "cimb_va", "danamon_va", "bsi_va", "seabank_va"].includes(method)) {
|
|
581
|
+
res.vaNumber = data.va_numbers?.[0]?.va_number;
|
|
582
|
+
} else if (method === "permata_va") {
|
|
583
|
+
res.vaNumber = data.permata_va_number;
|
|
584
|
+
} else if (method === "mandiri_va") {
|
|
585
|
+
res.vaNumber = `${data.biller_code}-${data.bill_key}`;
|
|
586
|
+
} else if (method === "qris") {
|
|
587
|
+
res.qrString = data.qr_string;
|
|
588
|
+
res.qrCodeUrl = data.actions?.find((a) => a.name === "generate-qr-code")?.url;
|
|
589
|
+
} else if (["gopay", "shopeepay", "dana", "linkaja", "kredivo", "akulaku", "googlepay"].includes(method)) {
|
|
590
|
+
res.paymentUrl = data.actions?.find((a) => a.name === "deeplink-redirect")?.url || data.actions?.find((a) => a.name === "web-redirect")?.url || data.actions?.find((a) => a.name === "generate-qr-code")?.url || data.redirect_url;
|
|
591
|
+
res.qrCodeUrl = data.actions?.find((a) => a.name === "generate-qr-code")?.url;
|
|
592
|
+
} else if (method === "credit_card") {
|
|
593
|
+
res.paymentUrl = data.redirect_url || data.actions?.find((a) => a.name === "redirect")?.url;
|
|
594
|
+
} else if (method === "ovo") {
|
|
595
|
+
res.paymentUrl = "";
|
|
596
|
+
} else if (["alfamart", "indomaret"].includes(method)) {
|
|
597
|
+
res.paymentCode = data.payment_code;
|
|
598
|
+
}
|
|
599
|
+
return res;
|
|
600
|
+
} else {
|
|
601
|
+
return {
|
|
602
|
+
success: false,
|
|
603
|
+
rawResponse: data,
|
|
604
|
+
error: data.status_message || `Midtrans Core Error: ${data.status_code}`
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
} catch (e) {
|
|
608
|
+
return {
|
|
609
|
+
success: false,
|
|
610
|
+
rawResponse: null,
|
|
611
|
+
error: e.message || "Failed to make request to Midtrans Core API"
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
const url = this.getSnapBaseUrl(sandbox);
|
|
616
|
+
const payload = {
|
|
617
|
+
transaction_details: {
|
|
618
|
+
order_id: orderId,
|
|
619
|
+
gross_amount: integerAmount
|
|
620
|
+
},
|
|
621
|
+
customer_details: {
|
|
622
|
+
first_name: customer.name,
|
|
623
|
+
email: customer.email,
|
|
624
|
+
phone: customer.phone || ""
|
|
625
|
+
},
|
|
626
|
+
item_details: [
|
|
627
|
+
{
|
|
628
|
+
id: orderId,
|
|
629
|
+
price: integerAmount,
|
|
630
|
+
quantity: 1,
|
|
631
|
+
name: productDetails.length > 50 ? productDetails.substring(0, 47) + "..." : productDetails
|
|
632
|
+
}
|
|
633
|
+
],
|
|
634
|
+
callbacks: {
|
|
635
|
+
finish: returnUrl
|
|
636
|
+
},
|
|
637
|
+
...params.paymentMethod ? { enabled_payments: [params.paymentMethod] } : {},
|
|
638
|
+
...params.providerParams
|
|
639
|
+
};
|
|
640
|
+
try {
|
|
641
|
+
const client = new MidtransClient(config);
|
|
642
|
+
const data = await client.request("POST", url, payload);
|
|
643
|
+
if (data.token) {
|
|
644
|
+
return {
|
|
645
|
+
success: true,
|
|
646
|
+
paymentUrl: data.redirect_url,
|
|
647
|
+
reference: data.token,
|
|
648
|
+
rawResponse: data
|
|
649
|
+
};
|
|
650
|
+
} else {
|
|
651
|
+
return {
|
|
652
|
+
success: false,
|
|
653
|
+
rawResponse: data,
|
|
654
|
+
error: data.error_messages?.[0] || "Failed to create Midtrans Snap transaction"
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
} catch (e) {
|
|
658
|
+
return {
|
|
659
|
+
success: false,
|
|
660
|
+
rawResponse: null,
|
|
661
|
+
error: e.message || "Failed to make request to Midtrans Snap API"
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
async verifyCallback(body, config) {
|
|
666
|
+
const { apiKey } = config;
|
|
667
|
+
const orderId = body.order_id || "";
|
|
668
|
+
const statusCode = body.status_code || "";
|
|
669
|
+
const grossAmount = body.gross_amount || "";
|
|
670
|
+
const signatureKey = body.signature_key || "";
|
|
671
|
+
const rawSignature = orderId + statusCode + grossAmount + apiKey;
|
|
672
|
+
const computedSignature = crypto2.createHash("sha512").update(rawSignature).digest("hex");
|
|
673
|
+
const isValid = signatureKey.toLowerCase() === computedSignature.toLowerCase();
|
|
674
|
+
const transactionStatus = body.transaction_status || "";
|
|
675
|
+
const fraudStatus = body.fraud_status || "";
|
|
676
|
+
let status = "pending";
|
|
677
|
+
if (transactionStatus === "capture" || transactionStatus === "settlement") {
|
|
678
|
+
if (fraudStatus === "accept" || !fraudStatus) {
|
|
679
|
+
status = "paid";
|
|
680
|
+
} else if (fraudStatus === "challenge") {
|
|
681
|
+
status = "pending";
|
|
682
|
+
} else {
|
|
683
|
+
status = "failed";
|
|
684
|
+
}
|
|
685
|
+
} else if (transactionStatus === "pending") {
|
|
686
|
+
status = "pending";
|
|
687
|
+
} else if (["deny", "cancel", "expire"].includes(transactionStatus)) {
|
|
688
|
+
status = "failed";
|
|
689
|
+
}
|
|
690
|
+
return {
|
|
691
|
+
isValid,
|
|
692
|
+
orderId,
|
|
693
|
+
amount: grossAmount ? Number(grossAmount) : 0,
|
|
694
|
+
status: isValid ? status : "failed",
|
|
695
|
+
rawPayload: body
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
async getPaymentMethods(params, config) {
|
|
699
|
+
const staticMethods = [
|
|
700
|
+
{
|
|
701
|
+
paymentMethod: "credit_card",
|
|
702
|
+
paymentName: "Credit / Debit Card",
|
|
703
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/credit_card.png",
|
|
704
|
+
totalFee: "2.9% + IDR 2,000",
|
|
705
|
+
category: "Kartu Kredit"
|
|
706
|
+
},
|
|
707
|
+
{
|
|
708
|
+
paymentMethod: "googlepay",
|
|
709
|
+
paymentName: "Google Pay\u2122",
|
|
710
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/googlepay.png",
|
|
711
|
+
totalFee: "2.9% + IDR 2,000",
|
|
712
|
+
category: "Kartu Kredit"
|
|
713
|
+
},
|
|
714
|
+
{
|
|
715
|
+
paymentMethod: "bca_va",
|
|
716
|
+
paymentName: "BCA Virtual Account",
|
|
717
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/bca_va.png",
|
|
718
|
+
totalFee: "IDR 4,000",
|
|
719
|
+
category: "Virtual Account"
|
|
720
|
+
},
|
|
721
|
+
{
|
|
722
|
+
paymentMethod: "bni_va",
|
|
723
|
+
paymentName: "BNI Virtual Account",
|
|
724
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/bni_va.png",
|
|
725
|
+
totalFee: "IDR 4,000",
|
|
726
|
+
category: "Virtual Account"
|
|
727
|
+
},
|
|
728
|
+
{
|
|
729
|
+
paymentMethod: "bri_va",
|
|
730
|
+
paymentName: "BRI Virtual Account",
|
|
731
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/bri_va.png",
|
|
732
|
+
totalFee: "IDR 4,000",
|
|
733
|
+
category: "Virtual Account"
|
|
734
|
+
},
|
|
735
|
+
{
|
|
736
|
+
paymentMethod: "mandiri_va",
|
|
737
|
+
paymentName: "Mandiri Bill Payment / VA",
|
|
738
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/mandiri_va.png",
|
|
739
|
+
totalFee: "IDR 4,000",
|
|
740
|
+
category: "Virtual Account"
|
|
741
|
+
},
|
|
742
|
+
{
|
|
743
|
+
paymentMethod: "permata_va",
|
|
744
|
+
paymentName: "Permata Virtual Account",
|
|
745
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/permata_va.png",
|
|
746
|
+
totalFee: "IDR 4,000",
|
|
747
|
+
category: "Virtual Account"
|
|
748
|
+
},
|
|
749
|
+
{
|
|
750
|
+
paymentMethod: "cimb_va",
|
|
751
|
+
paymentName: "CIMB Virtual Account",
|
|
752
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/cimb_va.png",
|
|
753
|
+
totalFee: "IDR 4,000",
|
|
754
|
+
category: "Virtual Account"
|
|
755
|
+
},
|
|
756
|
+
{
|
|
757
|
+
paymentMethod: "danamon_va",
|
|
758
|
+
paymentName: "Danamon Virtual Account",
|
|
759
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/danamon_va.png",
|
|
760
|
+
totalFee: "IDR 4,000",
|
|
761
|
+
category: "Virtual Account"
|
|
762
|
+
},
|
|
763
|
+
{
|
|
764
|
+
paymentMethod: "bsi_va",
|
|
765
|
+
paymentName: "BSI Virtual Account",
|
|
766
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/bsi_va.png",
|
|
767
|
+
totalFee: "IDR 4,000",
|
|
768
|
+
category: "Virtual Account"
|
|
769
|
+
},
|
|
770
|
+
{
|
|
771
|
+
paymentMethod: "seabank_va",
|
|
772
|
+
paymentName: "SeaBank Virtual Account",
|
|
773
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/seabank_va.png",
|
|
774
|
+
totalFee: "IDR 4,000",
|
|
775
|
+
category: "Virtual Account"
|
|
776
|
+
},
|
|
777
|
+
{
|
|
778
|
+
paymentMethod: "other_va",
|
|
779
|
+
paymentName: "Other Banks (ATM Bersama, Prima, Alto)",
|
|
780
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/other_va.png",
|
|
781
|
+
totalFee: "IDR 4,000",
|
|
782
|
+
category: "Virtual Account"
|
|
783
|
+
},
|
|
784
|
+
{
|
|
785
|
+
paymentMethod: "qris",
|
|
786
|
+
paymentName: "QRIS (GoPay, ShopeePay, Dana, LinkAja)",
|
|
787
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/qris.png",
|
|
788
|
+
totalFee: "0.7%",
|
|
789
|
+
category: "QRIS"
|
|
790
|
+
},
|
|
791
|
+
{
|
|
792
|
+
paymentMethod: "other_qris",
|
|
793
|
+
paymentName: "Other QRIS",
|
|
794
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/other_qris.png",
|
|
795
|
+
totalFee: "0.7%",
|
|
796
|
+
category: "QRIS"
|
|
797
|
+
},
|
|
798
|
+
{
|
|
799
|
+
paymentMethod: "gopay",
|
|
800
|
+
paymentName: "GoPay E-Wallet",
|
|
801
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/gopay.png",
|
|
802
|
+
totalFee: "2.0%",
|
|
803
|
+
category: "E-Wallet"
|
|
804
|
+
},
|
|
805
|
+
{
|
|
806
|
+
paymentMethod: "shopeepay",
|
|
807
|
+
paymentName: "ShopeePay E-Wallet",
|
|
808
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/shopeepay.png",
|
|
809
|
+
totalFee: "2.0%",
|
|
810
|
+
category: "E-Wallet"
|
|
811
|
+
},
|
|
812
|
+
{
|
|
813
|
+
paymentMethod: "ovo",
|
|
814
|
+
paymentName: "OVO E-Wallet",
|
|
815
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/ovo.png",
|
|
816
|
+
totalFee: "1.5%",
|
|
817
|
+
category: "E-Wallet"
|
|
818
|
+
},
|
|
819
|
+
{
|
|
820
|
+
paymentMethod: "dana",
|
|
821
|
+
paymentName: "DANA E-Wallet",
|
|
822
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/dana.png",
|
|
823
|
+
totalFee: "1.7%",
|
|
824
|
+
category: "E-Wallet"
|
|
825
|
+
},
|
|
826
|
+
{
|
|
827
|
+
paymentMethod: "linkaja",
|
|
828
|
+
paymentName: "LinkAja E-Wallet",
|
|
829
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/linkaja.png",
|
|
830
|
+
totalFee: "1.7%",
|
|
831
|
+
category: "E-Wallet"
|
|
832
|
+
},
|
|
833
|
+
{
|
|
834
|
+
paymentMethod: "indomaret",
|
|
835
|
+
paymentName: "Indomaret",
|
|
836
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/indomaret.png",
|
|
837
|
+
totalFee: "IDR 5,000",
|
|
838
|
+
category: "Retail / Gerai"
|
|
839
|
+
},
|
|
840
|
+
{
|
|
841
|
+
paymentMethod: "alfamart",
|
|
842
|
+
paymentName: "Alfamart / Alfamidi",
|
|
843
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/alfamart.png",
|
|
844
|
+
totalFee: "IDR 5,000",
|
|
845
|
+
category: "Retail / Gerai"
|
|
846
|
+
},
|
|
847
|
+
{
|
|
848
|
+
paymentMethod: "kredivo",
|
|
849
|
+
paymentName: "Kredivo Paylater",
|
|
850
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/kredivo.png",
|
|
851
|
+
totalFee: "2.3%",
|
|
852
|
+
category: "Paylater / Cicilan"
|
|
853
|
+
},
|
|
854
|
+
{
|
|
855
|
+
paymentMethod: "akulaku",
|
|
856
|
+
paymentName: "Akulaku Paylater",
|
|
857
|
+
paymentImage: "https://docs.midtrans.com/asset/payment_methods/akulaku.png",
|
|
858
|
+
totalFee: "1.7%",
|
|
859
|
+
category: "Paylater / Cicilan"
|
|
860
|
+
}
|
|
861
|
+
];
|
|
862
|
+
return {
|
|
863
|
+
success: true,
|
|
864
|
+
methods: staticMethods,
|
|
865
|
+
rawResponse: staticMethods
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
async checkTransaction(params, config) {
|
|
869
|
+
const { merchantOrderId } = params;
|
|
870
|
+
try {
|
|
871
|
+
const client = new MidtransClient(config);
|
|
872
|
+
const data = await client.request("GET", `/${merchantOrderId}/status`, null);
|
|
873
|
+
const transactionStatus = data.transaction_status || "";
|
|
874
|
+
const fraudStatus = data.fraud_status || "";
|
|
875
|
+
let status = "pending";
|
|
876
|
+
if (transactionStatus === "capture" || transactionStatus === "settlement") {
|
|
877
|
+
if (fraudStatus === "accept" || !fraudStatus) {
|
|
878
|
+
status = "paid";
|
|
879
|
+
} else if (fraudStatus === "challenge") {
|
|
880
|
+
status = "pending";
|
|
881
|
+
} else {
|
|
882
|
+
status = "failed";
|
|
883
|
+
}
|
|
884
|
+
} else if (transactionStatus === "pending") {
|
|
885
|
+
status = "pending";
|
|
886
|
+
} else if (["deny", "cancel", "expire"].includes(transactionStatus)) {
|
|
887
|
+
status = "failed";
|
|
888
|
+
}
|
|
889
|
+
return {
|
|
890
|
+
success: true,
|
|
891
|
+
orderId: data.order_id || merchantOrderId,
|
|
892
|
+
reference: data.transaction_id || "",
|
|
893
|
+
amount: data.gross_amount ? Number(data.gross_amount) : 0,
|
|
894
|
+
statusCode: data.status_code || "",
|
|
895
|
+
status,
|
|
896
|
+
statusMessage: data.status_message || "",
|
|
897
|
+
rawResponse: data
|
|
898
|
+
};
|
|
899
|
+
} catch (e) {
|
|
900
|
+
return {
|
|
901
|
+
success: false,
|
|
902
|
+
orderId: merchantOrderId,
|
|
903
|
+
reference: "",
|
|
904
|
+
amount: 0,
|
|
905
|
+
statusCode: "",
|
|
906
|
+
status: "failed",
|
|
907
|
+
statusMessage: "Network error",
|
|
908
|
+
error: e.message || "Failed to check transaction status with Midtrans",
|
|
909
|
+
rawResponse: null
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Get an instance of MidtransClient to perform transaction actions, subscriptions, invoicing, etc.
|
|
915
|
+
*/
|
|
916
|
+
getClient(config) {
|
|
917
|
+
return new MidtransClient(config);
|
|
918
|
+
}
|
|
919
|
+
async probePaymentMethods(config) {
|
|
920
|
+
const probePayloads = {
|
|
921
|
+
qris: { payment_type: "qris", qris: { acquirer: "gopay" } },
|
|
922
|
+
gopay: { payment_type: "gopay", gopay: { enable_callback: true, callback_url: "https://example.com" } },
|
|
923
|
+
shopeepay: { payment_type: "shopeepay", shopeepay: { callback_url: "https://example.com" } },
|
|
924
|
+
bca: { payment_type: "bank_transfer", bank_transfer: { bank: "bca" } },
|
|
925
|
+
bni: { payment_type: "bank_transfer", bank_transfer: { bank: "bni" } },
|
|
926
|
+
bri: { payment_type: "bank_transfer", bank_transfer: { bank: "bri" } },
|
|
927
|
+
cimb: { payment_type: "bank_transfer", bank_transfer: { bank: "cimb" } },
|
|
928
|
+
mandiri: { payment_type: "echannel", echannel: { bill_info1: "Payment", bill_info2: "Probe" } },
|
|
929
|
+
permata: { payment_type: "permata" },
|
|
930
|
+
alfamart: { payment_type: "cstore", cstore: { store: "alfamart", message: "Probe" } },
|
|
931
|
+
indomaret: { payment_type: "cstore", cstore: { store: "indomaret", message: "Probe" } },
|
|
932
|
+
akulaku: { payment_type: "akulaku" },
|
|
933
|
+
kredivo: {
|
|
934
|
+
payment_type: "kredivo",
|
|
935
|
+
seller_details: { address: { city: "Jakarta" } }
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
const enabled = [];
|
|
939
|
+
const client = new MidtransClient(config);
|
|
940
|
+
for (const [methodId, specificPayload] of Object.entries(probePayloads)) {
|
|
941
|
+
try {
|
|
942
|
+
const probeOrderId = `PROBE-${methodId}-${Date.now()}`;
|
|
943
|
+
const probeBody = {
|
|
944
|
+
...specificPayload,
|
|
945
|
+
transaction_details: { order_id: probeOrderId, gross_amount: 15e3 },
|
|
946
|
+
item_details: [{ id: probeOrderId, name: "Probe", price: 15e3, quantity: 1 }],
|
|
947
|
+
customer_details: { first_name: "Probe", email: "probe@test.com" }
|
|
948
|
+
};
|
|
949
|
+
const result = await client.request("POST", "/charge", probeBody);
|
|
950
|
+
if (result && ["200", "201", "202"].includes(result.status_code)) {
|
|
951
|
+
enabled.push(methodId);
|
|
952
|
+
try {
|
|
953
|
+
await client.cancelTransaction(probeOrderId);
|
|
954
|
+
} catch (e) {
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
} catch (e) {
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
return { success: true, enabled };
|
|
961
|
+
}
|
|
267
962
|
};
|
|
268
963
|
|
|
269
964
|
// src/index.ts
|
|
@@ -271,6 +966,7 @@ var PaymentManager = class {
|
|
|
271
966
|
providers = /* @__PURE__ */ new Map();
|
|
272
967
|
constructor() {
|
|
273
968
|
this.registerProvider(new DuitkuProvider());
|
|
969
|
+
this.registerProvider(new MidtransProvider());
|
|
274
970
|
}
|
|
275
971
|
registerProvider(provider) {
|
|
276
972
|
this.providers.set(provider.name.toLowerCase(), provider);
|
|
@@ -282,6 +978,12 @@ var PaymentManager = class {
|
|
|
282
978
|
}
|
|
283
979
|
return provider;
|
|
284
980
|
}
|
|
981
|
+
getMidtransProvider() {
|
|
982
|
+
return this.getProvider("midtrans");
|
|
983
|
+
}
|
|
984
|
+
getMidtransClient(config) {
|
|
985
|
+
return new MidtransClient(config);
|
|
986
|
+
}
|
|
285
987
|
async createInvoice(providerName, params, config) {
|
|
286
988
|
const provider = this.getProvider(providerName);
|
|
287
989
|
return provider.createInvoice(params, config);
|
|
@@ -298,11 +1000,20 @@ var PaymentManager = class {
|
|
|
298
1000
|
const provider = this.getProvider(providerName);
|
|
299
1001
|
return provider.checkTransaction(params, config);
|
|
300
1002
|
}
|
|
1003
|
+
async probePaymentMethods(providerName, config) {
|
|
1004
|
+
const provider = this.getProvider(providerName);
|
|
1005
|
+
if (provider.probePaymentMethods) {
|
|
1006
|
+
return provider.probePaymentMethods(config);
|
|
1007
|
+
}
|
|
1008
|
+
return { success: false, enabled: [], error: `Provider '${providerName}' does not support payment methods probing` };
|
|
1009
|
+
}
|
|
301
1010
|
};
|
|
302
1011
|
var paymentManager = new PaymentManager();
|
|
303
1012
|
export {
|
|
304
1013
|
BasePaymentProvider,
|
|
305
1014
|
DuitkuProvider,
|
|
1015
|
+
MidtransClient,
|
|
1016
|
+
MidtransProvider,
|
|
306
1017
|
PaymentManager,
|
|
307
1018
|
getPaymentMethodCategory,
|
|
308
1019
|
paymentManager
|