Skip to content

Commit

Permalink
Merge branch 'feat/simplexSell' of github.com:Gamboster/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed Nov 22, 2024
2 parents c963b6e + e33c62d commit b7dbb14
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 9 deletions.
22 changes: 22 additions & 0 deletions packages/bitcore-wallet-client/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3447,6 +3447,15 @@ export class API extends EventEmitter {
});
}

simplexGetSellQuote(data): Promise<any> {
return new Promise((resolve, reject) => {
this.request.post('/v1/service/simplex/sellQuote', data, (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});
}

simplexPaymentRequest(data): Promise<any> {
return new Promise((resolve, reject) => {
this.request.post(
Expand All @@ -3460,6 +3469,19 @@ export class API extends EventEmitter {
});
}

simplexSellPaymentRequest(data): Promise<any> {
return new Promise((resolve, reject) => {
this.request.post(
'/v1/service/simplex/sellPaymentRequest',
data,
(err, data) => {
if (err) return reject(err);
return resolve(data);
}
);
});
}

simplexGetEvents(data): Promise<any> {
return new Promise((resolve, reject) => {
let qs = [];
Expand Down
24 changes: 20 additions & 4 deletions packages/bitcore-wallet-service/bws.example.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ module.exports = {
moonpay: {
disabled: false,
removed: false
},
simplex: {
disabled: false,
removed: false
}
},
swapCrypto: {
Expand Down Expand Up @@ -285,22 +289,34 @@ module.exports = {
// sandbox: {
// apiKey: 'simplex_sandbox_api_key_here',
// api: 'https://sandbox.test-simplexcc.com',
// appProviderId: 'simplex_provider_id_here'
// apiSell: 'https://sell-checkout-waf.sandbox.test-simplexcc.com',
// appProviderId: 'simplex_provider_id_here',
// appSellRefId: 'simplex_sell_ref_id_here',
// publicKey: 'simplex_sandbox_public_key_here',
// },
// production: {
// apiKey: 'simplex_production_api_key_here',
// api: 'https://backend-wallet-api.simplexcc.com',
// appProviderId: 'simplex_provider_id_here'
// apiSell: 'https://sell-checkout-widget.simplex.com',
// appProviderId: 'simplex_provider_id_here',
// appSellRefId: 'simplex_sell_ref_id_here',
// publicKey: 'simplex_public_key_here',
// },
// sandboxWeb: {
// apiKey: 'simplex_sandbox_web_api_key_here',
// api: 'https://sandbox.test-simplexcc.com',
// appProviderId: 'simplex_web_provider_id_here'
// apiSell: 'https://sell-checkout-waf.sandbox.test-simplexcc.com',
// appProviderId: 'simplex_web_provider_id_here',
// appSellRefId: 'simplex_web_sell_ref_id_here',
// publicKey: 'simplex_web_sandbox_public_key_here',
// },
// productionWeb: {
// apiKey: 'simplex_production_web_api_key_here',
// api: 'https://backend-wallet-api.simplexcc.com',
// appProviderId: 'simplex_web_provider_id_here'
// apiSell: 'https://sell-checkout-widget.simplex.com',
// appProviderId: 'simplex_web_provider_id_here',
// appSellRefId: 'simplex_web_sell_ref_id_here',
// publicKey: 'simplex_web_public_key_here',
// }
// },
// thorswap : {
Expand Down
24 changes: 20 additions & 4 deletions packages/bitcore-wallet-service/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ const Config = (): any => {
moonpay: {
disabled: false,
removed: false
},
simplex: {
disabled: false,
removed: false
}
},
swapCrypto: {
Expand Down Expand Up @@ -304,22 +308,34 @@ const Config = (): any => {
// sandbox: {
// apiKey: 'simplex_sandbox_api_key_here',
// api: 'https://sandbox.test-simplexcc.com',
// appProviderId: 'simplex_provider_id_here'
// apiSell: 'https://sell-checkout-waf.sandbox.test-simplexcc.com',
// appProviderId: 'simplex_provider_id_here',
// appSellRefId: 'simplex_sell_ref_id_here',
// publicKey: 'simplex_sandbox_public_key_here',
// },
// production: {
// apiKey: 'simplex_production_api_key_here',
// api: 'https://backend-wallet-api.simplexcc.com',
// appProviderId: 'simplex_provider_id_here'
// apiSell: 'https://sell-checkout-widget.simplex.com',
// appProviderId: 'simplex_provider_id_here',
// appSellRefId: 'simplex_sell_ref_id_here',
// publicKey: 'simplex_public_key_here',
// },
// sandboxWeb: {
// apiKey: 'simplex_sandbox_web_api_key_here',
// api: 'https://sandbox.test-simplexcc.com',
// appProviderId: 'bitpaywidget'
// apiSell: 'https://sell-checkout-waf.sandbox.test-simplexcc.com',
// appProviderId: 'simplex_web_provider_id_here',
// appSellRefId: 'simplex_web_sell_ref_id_here',
// publicKey: 'simplex_web_sandbox_public_key_here',
// },
// productionWeb: {
// apiKey: 'simplex_production_web_api_key_here',
// api: 'https://backend-wallet-api.simplexcc.com',
// appProviderId: 'simplex_web_provider_id_here'
// apiSell: 'https://sell-checkout-widget.simplex.com',
// appProviderId: 'simplex_web_provider_id_here',
// appSellRefId: 'simplex_web_sell_ref_id_here',
// publicKey: 'simplex_web_public_key_here',
// }
// },
// thorswap : {
Expand Down
44 changes: 44 additions & 0 deletions packages/bitcore-wallet-service/src/lib/expressapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,24 @@ export class ExpressApp {
});
});

router.post('/v1/service/simplex/getCurrencies', (req, res) => {
let server;
try {
server = getServer(req, res);
} catch (ex) {
return returnError(ex, res, req);
}

server
.simplexGetCurrencies(req)
.then(response => {
res.json(response);
})
.catch(err => {
return returnError(err ?? 'unknown', res, req);
});
});

router.post('/v1/service/simplex/quote', (req, res) => {
getServerWithAuth(req, res, server => {
server
Expand All @@ -1809,6 +1827,19 @@ export class ExpressApp {
});
});

router.post('/v1/service/simplex/sellQuote', (req, res) => {
getServerWithAuth(req, res, server => {
server
.simplexGetSellQuote(req)
.then(response => {
res.json(response);
})
.catch(err => {
return returnError(err ?? 'unknown', res, req);
});
});
});

router.post('/v1/service/simplex/paymentRequest', (req, res) => {
getServerWithAuth(req, res, server => {
server
Expand All @@ -1822,6 +1853,19 @@ export class ExpressApp {
});
});

router.post('/v1/service/simplex/sellPaymentRequest', (req, res) => {
getServerWithAuth(req, res, server => {
server
.simplexSellPaymentRequest(req)
.then(response => {
res.json(response);
})
.catch(err => {
return returnError(err ?? 'unknown', res, req);
});
});
});

router.get('/v1/service/simplex/events', (req, res) => {
getServerWithAuth(req, res, server => {
const opts = { env: req.query.env };
Expand Down
122 changes: 121 additions & 1 deletion packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5905,13 +5905,45 @@ export class WalletService implements IWalletService {

const keys = {
API: config.simplex[env].api,
API_SELL: config.simplex[env].apiSell,
API_KEY: config.simplex[env].apiKey,
APP_PROVIDER_ID: config.simplex[env].appProviderId
PUBLIC_KEY: config.simplex[env].publicKey,
APP_PROVIDER_ID: config.simplex[env].appProviderId,
APP_SELL_REF_ID: config.simplex[env].appSellRefId
};

return keys;
}

simplexGetCurrencies(req): Promise<any> {
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): Promise<any> {
return new Promise((resolve, reject) => {
const keys = this.simplexGetKeys(req);
Expand Down Expand Up @@ -5951,6 +5983,51 @@ export class WalletService implements IWalletService {
});
}

simplexGetSellQuote(req): Promise<any> {
return new Promise((resolve, reject) => {
const keys = this.simplexGetKeys(req);

const API = keys.API_SELL;
const API_KEY = keys.API_KEY;

if (!checkRequired(req.body, ['base_currency', 'base_amount', 'quote_currency', 'pp_payment_method'])) {
return reject(new 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: string = 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): Promise<any> {
return new Promise((resolve, reject) => {
const keys = this.simplexGetKeys(req);
Expand Down Expand Up @@ -6012,6 +6089,49 @@ export class WalletService implements IWalletService {
});
}

simplexSellPaymentRequest(req): Promise<any> {
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 (
!checkRequired(req.body, ['referer_url', 'return_url']) ||
!checkRequired(req.body.txn_details, ['quote_id'])
) {
return reject(new 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): Promise<any> {
return new Promise((resolve, reject) => {
if (!config.simplex) return reject(new Error('Simplex missing credentials'));
Expand Down
Loading

0 comments on commit b7dbb14

Please sign in to comment.