Skip to content

xct007/midtrans-client

Repository files navigation

Unofficial Midtrans Client Library

An unofficial Midtrans Payment API Client Library for Node.js.

npm version license CI Status

Installation

npm install @xct007/midtrans-client

Usage

import the library

// CommonJS
const MidtransClient = require("@xct007/midtrans-client").default;

// ES Modules
import MidtransClient from "@xct007/midtrans-client";

Initialize the client

const { Core, Snap, Iris } = new MidtransClient({
 sandbox: true, // Set to false for production
 clientKey: "YOUR_CLIENT_KEY",
 serverKey: "YOUR_SERVER_KEY",
 throwHttpErrors: true,
});

Core API Example

Core.charge({
 payment_type: "bank_transfer",
 transaction_details: {
  order_id: "order-id-" + new Date().getTime(),
  gross_amount: 10000,
 },
 bank_transfer: {
  bank: "bca",
 },
})
 .then((response) => {
  console.log("Charge Response:", response);
 })
 .catch((error) => {
  console.error("Charge Error:", error);
 });

Explore more Core API methods on examples/core.

Snap API Example

Snap.create({
 transaction_details: {
  order_id: "order-id-" + new Date().getTime(),
  gross_amount: 10000,
 },
})
 .then((response) => {
  console.log("Snap Token:", response.token);
  console.log("Redirect URL:", response.redirect_url);
 })
 .catch((error) => {
  console.error("Snap Error:", error);
 });

Explore more Snap API methods on examples/snap.

Iris API Example

Iris.ping()
 .then((response) => {
  console.log("Ping Response:", response);
 })
 .catch((error) => {
  console.error("Ping Error:", error);
 });

Explore more Iris API methods on examples/iris.

Documentation

For hands-on examples, please refer to the examples directory.

For detailed API documentation, please refer to the Midtrans API Documentation.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments