UNPKG

ts-midtrans-client

Version:

This library is an UNOFFICIAL TypeScript version of the Midtrans Client - Node.js.

75 lines (74 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MidtransNotificationError = exports.Transaction = void 0; /** * These are wrapper/implementation of API methods described on: * https://api-docs.midtrans.com/#midtrans-api * @return {Promise} - Promise that contains JSON API response decoded as Object */ class Transaction { constructor(parentObj = {}) { this.parent = parentObj; } status(transactionId = '') { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/status`; return this.parent.httpClient.request('get', this.parent.apiConfig.get().serverKey, apiUrl, null); } statusb2b(transactionId = '') { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/status/b2b`; return this.parent.httpClient.request('get', this.parent.apiConfig.get().serverKey, apiUrl, null); } approve(transactionId = '') { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/approve`; return this.parent.httpClient.request('post', this.parent.apiConfig.get().serverKey, apiUrl, null); } deny(transactionId = '') { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/deny`; return this.parent.httpClient.request('post', this.parent.apiConfig.get().serverKey, apiUrl, null); } cancel(transactionId = '') { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/cancel`; return this.parent.httpClient.request('post', this.parent.apiConfig.get().serverKey, apiUrl, null); } expire(transactionId = '') { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/expire`; return this.parent.httpClient.request('post', this.parent.apiConfig.get().serverKey, apiUrl, null); } refund(transactionId = '', parameter = {}) { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/refund`; return this.parent.httpClient.request('post', this.parent.apiConfig.get().serverKey, apiUrl, parameter); } refundDirect(transactionId = '', parameter = {}) { const apiUrl = `${this.parent.apiConfig.getCoreApiBaseUrl()}/v2/${transactionId}/refund/online/direct`; return this.parent.httpClient.request('post', this.parent.apiConfig.get().serverKey, apiUrl, parameter); } notification(notificationObj = {}) { return new Promise((resolve, reject) => { if (typeof notificationObj === 'string') { try { notificationObj = JSON.parse(notificationObj); } catch (err) { if (err instanceof Error) { reject(new MidtransNotificationError(`fail to parse 'notification' string as JSON. Use JSON string or Object as 'notification'. with message: ${err.message}`)); } else { reject(new MidtransNotificationError(`fail to parse 'notification' string as JSON. Use JSON string or Object as 'notification'.`)); } } } const transactionId = notificationObj.transaction_id; this.status(transactionId) .then((res) => { resolve(res); }) .catch((err) => { reject(err); }); }); } } exports.Transaction = Transaction; class MidtransNotificationError extends Error { } exports.MidtransNotificationError = MidtransNotificationError;