234 lines
7.6 KiB
TypeScript
234 lines
7.6 KiB
TypeScript
import axios from 'axios';
|
|
import {HttpException, HttpStatus, NotFoundException} from "@nestjs/common";
|
|
import {createHmac} from "crypto";
|
|
|
|
const irs_url = 'http://h2h.elangpixiu.com/api/h2h';
|
|
const irs_id = 'PT0005';
|
|
const irs_pin = '04JFGC';
|
|
const irs_user = 'D10BD0';
|
|
const irs_pass = '6251F3';
|
|
|
|
export const doTransaction = async (
|
|
productCode,
|
|
destination,
|
|
idtrx,
|
|
supplier,
|
|
authorization,
|
|
typePaid,
|
|
billTrxId,
|
|
productVocaSubCategoryId,
|
|
productPrice
|
|
) => {
|
|
try {
|
|
if (supplier.code == 'IRS') {
|
|
const res = await axios.get(
|
|
`${irs_url}?id=${irs_id}&pin=${irs_pin}&user=${irs_user}&pass=${irs_pass}&kodeproduk=${productCode}&tujuan=${destination}&counter=1&idtrx=${idtrx}`,
|
|
);
|
|
|
|
return res.data;
|
|
} else if (supplier.code == 'NIRS') {
|
|
const res = await axios.get(
|
|
`${supplier.url}?id=${supplier.irs_id}&pin=${supplier.irs_pin}&user=${supplier.irs_user}&pass=${supplier.irs_pass}&kodeproduk=${productCode}&tujuan=${destination}&counter=1&idtrx=${idtrx}`,
|
|
);
|
|
|
|
return res.data;
|
|
} else if (supplier.code == 'Vocagame') {
|
|
const merchantId = supplier.irs_id;
|
|
const reference = idtrx
|
|
const endpoint = `/transaction/${reference}`;
|
|
const secretKey = supplier.irs_pass;
|
|
const signature = createHmac('sha256', secretKey)
|
|
.update(merchantId + endpoint)
|
|
.digest('hex');
|
|
|
|
const options = {
|
|
headers: {'Content-Type': 'application/json',
|
|
'X-Merchant': `${merchantId}`},
|
|
};
|
|
|
|
const mergedDestination = destination;
|
|
const regex = /\(([^)]+)\)/;
|
|
const match = regex.exec(mergedDestination);
|
|
|
|
let productDestination = ""
|
|
let productRegion = ""
|
|
if (match) {
|
|
var productSplit = destination.split('(')
|
|
productDestination = productSplit[0]
|
|
const insideBrackets = match[1];
|
|
productRegion = insideBrackets // This will print inside brackets
|
|
} else {
|
|
productDestination = destination
|
|
}
|
|
|
|
const data =
|
|
{
|
|
productId: productVocaSubCategoryId, // Sub Kategori ID
|
|
productItemId: productCode, // Product Code
|
|
data: { // Get object from user input within endpoint /products/detail
|
|
userId: productDestination,
|
|
zoneId: productRegion // Optional
|
|
},
|
|
price: productPrice, // Price equals from items
|
|
reference: reference, // Your reference I
|
|
callbackUrl: "https://ppob-backend.k3s.bangun-kreatif.com/v1/ppob_callback/vocagame" // Set your endpoint callback
|
|
}
|
|
|
|
const res = await axios.post(
|
|
`${supplier.url}/v1/core/transaction?signature=${signature}`,
|
|
data,
|
|
options,
|
|
);
|
|
|
|
var responseVoca = res.data.data;
|
|
console.log('iniresponsevoca', responseVoca)
|
|
if (responseVoca != null) {
|
|
const endpointDetail = `/transaction/${responseVoca.invoiceId}/detail`;
|
|
const signatureDetail = createHmac('sha256', secretKey)
|
|
.update(merchantId + endpointDetail)
|
|
.digest('hex');
|
|
const resDetail = await axios.get(
|
|
`${supplier.url}/v1/core/transaction/${responseVoca.invoiceId}/detail?signature=${signatureDetail}`,
|
|
options,
|
|
);
|
|
|
|
return resDetail.data
|
|
}
|
|
|
|
} else if (supplier.code == 'Digiflazz') {
|
|
if (typePaid == 'INQUIRY') {
|
|
const md5HashDigiflazz = `${supplier.irs_user}${supplier.irs_pass}${idtrx}`;
|
|
const md5Hash = require("blueimp-md5");
|
|
|
|
const options = {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
};
|
|
const data = {
|
|
commands: 'inq-pasca',
|
|
username: supplier.irs_user,
|
|
customer_no: `${destination}`,
|
|
buyer_sku_code: `${productCode}`,
|
|
ref_id: `${idtrx}`,
|
|
sign: md5Hash(md5HashDigiflazz),
|
|
};
|
|
const res = await axios.post(
|
|
`${supplier.url}/transaction`,
|
|
data,
|
|
options,
|
|
);
|
|
|
|
return res.data.data;
|
|
} else if (typePaid == 'PAYMENT') {
|
|
const md5HashDigiflazz = `${supplier.irs_user}${supplier.irs_pass}${idtrx}`;
|
|
const md5Hash = require("blueimp-md5");
|
|
|
|
const options = {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
};
|
|
const data = {
|
|
commands: 'pay-pasca',
|
|
username: supplier.irs_user,
|
|
customer_no: `${destination}`,
|
|
buyer_sku_code: `${productCode}`,
|
|
ref_id: `${billTrxId}`,
|
|
sign: md5Hash(md5HashDigiflazz),
|
|
};
|
|
const res = await axios.post(
|
|
`${supplier.url}/transaction`,
|
|
data,
|
|
options,
|
|
);
|
|
|
|
return res.data.data;
|
|
} else {
|
|
const md5HashDigiflazz = `${supplier.irs_user}${supplier.irs_pass}${idtrx}`;
|
|
const md5Hash = require("blueimp-md5");
|
|
console.log('testmd5', md5HashDigiflazz);
|
|
const options = {
|
|
headers: {'Content-Type': 'application/json'},
|
|
};
|
|
const data = {
|
|
username: supplier.irs_user,
|
|
customer_no: `${destination}`,
|
|
buyer_sku_code: `${productCode}`,
|
|
ref_id: `${idtrx}`,
|
|
sign: md5Hash(md5HashDigiflazz),
|
|
};
|
|
const res = await axios.post(
|
|
`${supplier.url}/transaction`,
|
|
data,
|
|
options,
|
|
);
|
|
|
|
console.log('resdigiflazz', res);
|
|
return res.data.data;
|
|
}
|
|
} else if (supplier.code == 'Hemat') {
|
|
if (authorization != '') {
|
|
console.log('initoken', authorization);
|
|
|
|
const options = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${authorization}`,
|
|
},
|
|
};
|
|
const data = {
|
|
idtransaction: idtrx,
|
|
destination: `${destination}`,
|
|
code: `${productCode}`,
|
|
type: `${typePaid}`,
|
|
};
|
|
const res = await axios.post(
|
|
`${supplier.url}/v1/transaction/request`,
|
|
data,
|
|
options,
|
|
);
|
|
|
|
return res.data;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const res = await axios.get(
|
|
`${supplier.url}?memberID=${supplier.irs_id}&pin=${supplier.irs_pin}&password=${supplier.irs_pass}&product=${productCode}&dest=${destination}&counter=1&refID=${idtrx}`,
|
|
);
|
|
console.log('restransini', res);
|
|
console.log('restransini1', res.status);
|
|
if (res.data.includes('Saldo anda tidak mencukupi')) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
error: 'maaf saat ini transaksi sedang tidak bisa diproses, silahkan hubungi WND Solutions untuk bisa di proses kembali',
|
|
},
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
} else {
|
|
return res.data;
|
|
}
|
|
} catch (e) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
error: 'maaf saat ini transaksi sedang tidak bisa diproses, silahkan hubungi WND Solutions untuk bisa di proses kembali',
|
|
},
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
|
|
} catch (err) {
|
|
console.log('errtransaction', err);
|
|
console.log('errtransaction2', err.response.data);
|
|
console.log('errtransaction3', err.response.status);
|
|
console.log('errtransaction4', err.message);
|
|
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
error: 'maaf saat ini transaksi sedang tidak bisa diproses, silahkan hubungi WND Solutions untuk bisa di proses kembali',
|
|
},
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
};
|