UNPKG

rocketfuel-node-sdk

Version:

JS SDK for RocketFuel Payment Method

36 lines (31 loc) 1.02 kB
import crypto from 'crypto'; import CustomError from '../../CustomError'; import { STRING_CONST } from '../const'; const { publicKey } = STRING_CONST.keys; function encryptStringWithRsaPublicKey(toEncrypt) { const buffer = Buffer.from(toEncrypt); const encrypted = crypto.publicEncrypt(publicKey, buffer); return encrypted.toString('base64'); } export default function lookup(api) { return async (transactionId, merchantId) => { try { if (!transactionId) { throw new Error('TransactionId is required'); } if (!merchantId) { throw new Error('MerchantId is required'); } const toEncrypt = { merchantId, transactionId, }; const url = STRING_CONST.route.lookup; const encryptedData = encryptStringWithRsaPublicKey(JSON.stringify(toEncrypt)); const { data } = await api.post(url, { data: encryptedData }); return data; } catch (err) { throw new CustomError(err.message, err.response.data); } }; }