ts-midtrans-client
Version:
This library is an UNOFFICIAL TypeScript version of the Midtrans Client - Node.js.
68 lines (67 loc) • 2.99 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Snap = void 0;
const apiConfig_1 = require("./apiConfig");
const httpClient_1 = require("./httpClient");
const transaction_1 = require("./transaction");
const midtransError_1 = require("./midtransError");
/**
* Snap object used to do request to Midtrans Snap API
*/
class Snap {
constructor(options = { isProduction: false, serverKey: '', clientKey: '' }) {
this.apiConfig = new apiConfig_1.ApiConfig(options);
this.httpClient = new httpClient_1.HttpClient(this);
this.transaction = new transaction_1.Transaction(this);
}
/**
* Do `/transactions` API request to Snap API
* @param {Object} parameter - object of Core API JSON body as parameter, will be converted to JSON
* @return {Promise<Object>} - Promise contains Object from JSON decoded response
*/
createTransaction(parameter) {
return __awaiter(this, void 0, void 0, function* () {
const apiUrl = `${this.apiConfig.getSnapApiBaseUrl()}/transactions`;
return this.httpClient.request('post', this.apiConfig.get().serverKey, apiUrl, parameter);
});
}
/**
* Wrapper function that call `createTransaction` then:
* @return {Promise<string>} - Promise of String token
*/
createTransactionToken(parameter) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.createTransaction(parameter);
if (res instanceof midtransError_1.MidtransError) {
throw res; // Re-throw the error to be handled by the caller
}
return res.token;
});
}
/**
* Wrapper function that call `createTransaction` then:
* @return {Promise<string>} - Promise of String redirect_url
*/
createTransactionRedirectUrl(parameter) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.createTransaction(parameter);
if (res instanceof midtransError_1.MidtransError) {
throw res; // Re-throw the error to be handled by the caller
}
return res.redirect_url;
});
}
getTransaction() {
return this.transaction;
}
}
exports.Snap = Snap;