UNPKG

bitcore-wallet-service

Version:
263 lines 11 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimplexService = void 0; const request = __importStar(require("request")); const config_1 = __importDefault(require("../config")); const utils_1 = require("../lib/common/utils"); const clienterror_1 = require("../lib/errors/clienterror"); const server_1 = require("../lib/server"); const Uuid = require('uuid'); class SimplexService { constructor() { this.request = request; } simplexGetKeys(req) { if (!config_1.default.simplex) throw new Error('Simplex missing credentials'); let env; env = req.body.env === 'production' ? 'production' : 'sandbox'; if (req.body.context === 'web') { env += 'Web'; } delete req.body.env; delete req.body.context; const keys = { API: config_1.default.simplex[env].api, API_SELL: config_1.default.simplex[env].apiSell, API_KEY: config_1.default.simplex[env].apiKey, PUBLIC_KEY: config_1.default.simplex[env].publicKey, APP_PROVIDER_ID: config_1.default.simplex[env].appProviderId, APP_SELL_REF_ID: config_1.default.simplex[env].appSellRefId }; return keys; } simplexGetCurrencies(req) { return new Promise((resolve, reject) => { const keys = this.simplexGetKeys(req); const API = keys.API; const PUBLIC_KEY = keys.PUBLIC_KEY; const headers = { 'Content-Type': 'application/json' }; const URL = API + `/v2/supported_crypto_currencies?public_key=${PUBLIC_KEY}`; this.request.get(URL, { headers, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : data); } }); }); } simplexGetQuote(req) { return new Promise((resolve, reject) => { const keys = this.simplexGetKeys(req); const API = keys.API; const API_KEY = keys.API_KEY; const ip = utils_1.Utils.getIpFromReq(req); req.body.client_ip = ip; req.body.wallet_id = keys.APP_PROVIDER_ID; const headers = { 'Content-Type': 'application/json', Authorization: 'ApiKey ' + API_KEY }; if (req.body && req.body.payment_methods && Array.isArray(req.body.payment_methods)) { req.body.payment_methods = req.body.payment_methods.map(item => item === 'simplex_account' ? 'sepa_open_banking' : item); } this.request.post(API + '/wallet/merchant/v2/quote', { headers, body: req.body, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : null); } }); }); } simplexGetSellQuote(req) { return new Promise((resolve, reject) => { const keys = this.simplexGetKeys(req); const API = keys.API_SELL; const API_KEY = keys.API_KEY; if (!(0, server_1.checkRequired)(req.body, ['base_currency', 'base_amount', 'quote_currency', 'pp_payment_method'])) { return reject(new clienterror_1.ClientError("Simplex's request missing arguments")); } const headers = { 'Content-Type': 'application/json', Authorization: 'ApiKey ' + API_KEY, }; if (req.body.userCountry && typeof req.body.userCountry === 'string') { headers['x-country-code'] = req.body.userCountry.toUpperCase(); } let qs = []; qs.push('base_currency=' + req.body.base_currency); qs.push('base_amount=' + req.body.base_amount); qs.push('quote_currency=' + req.body.quote_currency); qs.push('pp_payment_method=' + req.body.pp_payment_method); const URL = API + `/v3/quote?${qs.join('&')}`; this.request.get(URL, { headers, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : data); } }); }); } simplexPaymentRequest(req) { return new Promise((resolve, reject) => { const keys = this.simplexGetKeys(req); const API = keys.API; const API_KEY = keys.API_KEY; const appProviderId = keys.APP_PROVIDER_ID; const paymentId = Uuid.v4(); const orderId = Uuid.v4(); const apiHost = keys.API; const ip = utils_1.Utils.getIpFromReq(req); if (!(0, server_1.checkRequired)(req.body, ['account_details', 'transaction_details']) && !(0, server_1.checkRequired)(req.body.transaction_details, ['payment_details'])) { return reject(new clienterror_1.ClientError("Simplex's request missing arguments")); } req.body.account_details.app_provider_id = appProviderId; req.body.account_details.signup_login = { ip, location: '', uaid: '', accept_language: 'de,en-US;q=0.7,en;q=0.3', http_accept_language: 'de,en-US;q=0.7,en;q=0.3', user_agent: req.body.account_details.signup_login ? req.body.account_details.signup_login.user_agent : '', cookie_session_id: '', timestamp: req.body.account_details.signup_login ? req.body.account_details.signup_login.timestamp : '' }; req.body.transaction_details.payment_details.payment_id = paymentId; req.body.transaction_details.payment_details.order_id = orderId; const headers = { 'Content-Type': 'application/json', Authorization: 'ApiKey ' + API_KEY }; this.request.post(API + '/wallet/merchant/v2/payments/partner/data', { headers, body: req.body, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { data.body.payment_id = paymentId; data.body.order_id = orderId; data.body.app_provider_id = appProviderId; data.body.api_host = apiHost; return resolve(data.body); } }); }); } simplexSellPaymentRequest(req) { return new Promise((resolve, reject) => { const keys = this.simplexGetKeys(req); const API = keys.API_SELL; const API_KEY = keys.API_KEY; const appSellRefId = keys.APP_SELL_REF_ID; if (!(0, server_1.checkRequired)(req.body, ['referer_url', 'return_url']) || !(0, server_1.checkRequired)(req.body.txn_details, ['quote_id'])) { return reject(new clienterror_1.ClientError("Simplex's request missing arguments")); } const headers = { 'Content-Type': 'application/json', Authorization: 'ApiKey ' + API_KEY, }; if (req.body.userCountry && typeof req.body.userCountry === 'string') { headers['x-country-code'] = req.body.userCountry.toUpperCase(); } this.request.post(API + '/v3/initiate-sell/widget', { headers, body: req.body, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { data.body.app_sell_ref_id = appSellRefId; return resolve(data.body); } }); }); } simplexGetEvents(req) { return new Promise((resolve, reject) => { if (!config_1.default.simplex) return reject(new Error('Simplex missing credentials')); if (!req.env || (req.env != 'sandbox' && req.env != 'production')) return reject(new Error("Simplex's request wrong environment")); const API = config_1.default.simplex[req.env].api; const API_KEY = config_1.default.simplex[req.env].apiKey; const headers = { 'Content-Type': 'application/json', Authorization: 'ApiKey ' + API_KEY }; this.request.get(API + '/wallet/merchant/v2/events', { headers, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : null); } else { return resolve(data.body ? data.body : null); } }); }); } } exports.SimplexService = SimplexService; //# sourceMappingURL=simplex.js.map