@hudoro/central-payment
v0.0.1-beta.3
Published
Backend bundling for express
Keywords
Readme
Hudoro Central Payment
Hudoro Central Payment is a package that provides centralized integration for payment systems, designed to facilitate communication with multiple payment service providers from a single point.
Package instalation
Instal package using pnpm
pnpm add @hudoro/central-paymentInstal package using yarn
yarn add @hudoro/central-paymentInstal package using npm
npm i @hudoro/central-paymentConfiguration
To start using the Midtrans integration, you need to initialize it with your Midtrans credentials:
import { transactionConfig } from 'your-package-name';
const midtransClient = transactionConfig({
apiURL: 'https://api.midtrans.com', // Use 'https://api.sandbox.midtrans.com' for testing
snapURL: 'https://app.midtrans.com', // Use 'https://app.sandbox.midtrans.com' for testing
serverKey: 'YOUR_SERVER_KEY'
});Usage
Create Standard Transaction
Create a standard transaction via Midtrans Snap:
const createTransaction = async () => {
try {
const response = await midtransClient.createTransaction({
transaction_details: {
order_id: "ORDER-" + new Date().getTime(),
gross_amount: 100000
},
customer_details: {
name: "John Doe",
email: "[email protected]"
},
item_details: [
{
name: "Product Name",
price: 100000,
quantity: 1
}
],
callbacks: {
finish: "https://example.com/finish",
error: "https://example.com/error",
pending: "https://example.com/pending"
}
});
console.log("Transaction token:", response.data.token);
console.log("Redirect URL:", response.data.redirect_url);
return response.data;
} catch (error) {
console.error("Error creating transaction:", error);
}
};Create QRIS Transaction
Create a QRIS dynamic transaction:
const createQrisTransaction = async () => {
try {
const response = await midtransClient.createQrisMidtransTransaction({
transaction_details: {
order_id: "QRIS-" + new Date().getTime(),
gross_amount: 50000
},
customer_details: {
name: "Customer Name",
email: "[email protected]"
}
});
console.log("QRIS String:", response.data.qr_string);
console.log("Expiry Time:", response.data.expiry_time);
return response.data;
} catch (error) {
console.error("Error creating QRIS transaction:", error);
}
};Process Refund
Process a refund for a transaction:
const processRefund = async () => {
try {
const response = await midtransClient.refundTransaction({
transactionCode: "ORDER-123456789",
amount: 100000,
reason: "Customer requested refund"
});
console.log("Refund Status:", response.data.status_message);
return response.data;
} catch (error) {
console.error("Error processing refund:", error);
}
};Direct Refund
Process a direct online refund:
const processDirectRefund = async () => {
try {
const response = await midtransClient.directRefundTransaction({
transactionCode: "ORDER-123456789",
amount: 100000,
reason: "Customer requested refund"
});
console.log("Direct Refund Status:", response.data.status_message);
return response.data;
} catch (error) {
console.error("Error processing direct refund:", error);
}
};Cancel Transaction
Cancel a transaction:
try {
const response = await midtransClient.cancelTransaction("ORDER-123456789");
console.log("Cancel Status:", response.data.status_message);
return response.data;
} catch (error) {
console.error("Error cancelling transaction:", error);
}
};Data Types
The package includes TypeScript interfaces for all request and response objects:
ParameterTypes: Request parameters for creating transactions
MidtransResponse: Response for standard transactions
MidtransQrisDynamicResponse: Response for QRIS transactions
MidtransRefundResponse: Response for refund operations
RefundParams: Parameters for refund operations
ItemDetail: Structure for describing order items