Skip to content

Instantly share code, notes, and snippets.

@RamEduard
Created June 13, 2016 21:08
Show Gist options
  • Save RamEduard/6cd1c8e2ab4145d780127d298a31b737 to your computer and use it in GitHub Desktop.
Save RamEduard/6cd1c8e2ab4145d780127d298a31b737 to your computer and use it in GitHub Desktop.
CryptoPay rest client service
'use strict';
const request = require('request');
const options = require('../config/env.config.js');
class CryptoPay {
constructor() {
this.apiUrl = 'https://cryptopay.me/api/v2';
this.apiKey = options.cryptopay.apiKey;
}
setApiKey(value) {
this.apiKey = value;
}
/**
* [jsonRequest]
* @param method
* @param uri
* @param data
* @param callback
* @returns {*}
*/
jsonRequest(method, uri, data, callback) {
var _this = this;
return request({
method: method,
headers: {
'x-api-key': this.apiKey
},
uri: this.apiUrl + uri,
json: data
}, function (error, response, body) {
if (error) {
_this.handleError(response);
// Return callback
callback(error, body);
} else if(response.statusCode == 200 || response.statusCode == 201) {
// Return callback
callback(null, body);
} else {
_this.handleError(response);
// Return callback
callback(body);
}
});
}
/**
* [accounts]
* @param callback
*/
accounts(callback) {
this.jsonRequest('GET', '/accounts', {}, callback);
}
/**
* [accountTransactions]
* @param uuid
* @param callback
*/
accountTransactions(uuid, callback) {
this.jsonRequest('GET', '/accounts/' + uuid + '/transactions', {}, callback);
}
/**
* [generateBtcAddress]
* @param uuid
* @param callback
*/
generateBtcAddress(uuid, callback) {
this.jsonRequest('GET', '/accounts/' + uuid + '/address', {}, callback);
}
/**
* [exchangeTransfers]
* @param amount
* @param currency
* @param uuidDebit
* @param uuidCredit
* @param callback
*/
exchangeTransfers(amount, currency, uuidDebit, uuidCredit, callback) {
this.jsonRequest('POST', '/exchange_transfers', {
amount: amount,
amount_currency: currency,
from_account: uuidDebit,
to_account: uuidCredit
}, callback);
}
/**
* [exchangeTransfersList]
* @param callback
*/
exchangeTransfersList(callback) {
this.jsonRequest('GET', '/exchange_transfers', {}, callback);
}
/**
* [exchangeTransferDetails]
* @param uuid
* @param callback
*/
exchangeTransferDetails(uuid, callback) {
this.jsonRequest('GET', '/exchange_transfers/' + uuid, {}, callback);
}
/**
* [bitcoinTransfers]
* @param amount
* @param currency
* @param uuidDebit
* @param uuidCredit
* @param callback
*/
bitcoinTransfers(amount, currency, uuid, btcAddress, callback) {
this.jsonRequest('POST', '/bitcoin_transfers', {
amount: amount,
amount_currency: currency,
account: uuid,
address: btcAddress
}, callback);
}
/**
* [bitcoinTransfersList]
* @param callback
*/
bitcoinTransfersList(callback) {
this.jsonRequest('GET', '/bitcoin_transfers', {}, callback);
}
/**
* [bitcoinTransferDetails]
* @param uuid
* @param callback
*/
bitcoinTransferDetails(uuid, callback) {
this.jsonRequest('GET', '/bitcoin_transfers/' + uuid, {}, callback);
}
/**
* [cardOrders]
* @param cardData
* @param callback
*/
cardOrders(cardData, callback) {
this.jsonRequest('POST', '/card_orders', cardData, callback);
}
/**
* [cardOrderDetails]
* @param uuid
* @param callback
*/
cardOrderDetails(uuid, callback) {
this.jsonRequest('GET', '/card_orders/' + uuid, {}, callback);
}
/**
* [cardOrderDetails]
* @param callback
*/
cardOrdersList(callback) {
this.jsonRequest('GET', '/card_orders', {}, callback);
}
/**
* [cardDetails]
* @param uuid
* @param callback
*/
cardDetails(uuid, callback) {
this.jsonRequest('GET', '/cards/' + uuid, {}, callback);
}
/**
* [cardVirtualDetails]
* @param uuid
* @param callback
*/
cardVirtualDetails(uuid, callback) {
this.jsonRequest('GET', '/cards/' + uuid + '/details', {}, callback);
}
/**
* [cardsList]
* @param callback
*/
cardsList(callback) {
this.jsonRequest('GET', '/cards', {}, callback);
}
/**
* [cardBalance]
* @param uuid
* @param callback
*/
cardBalance(uuid, callback) {
this.jsonRequest('GET', '/cards/' + uuid + '/balance', {}, callback);
}
/**
* [cardLoadMoney]
* @param amountCents
* @param uuid
* @param callback
*/
cardLoadMoney(amountCents, uuid, callback) {
this.jsonRequest('POST', '/cards/' + uuid + '/loads', {amount_cents: amountCents}, callback);
}
/**
* [cardLoads]
* @param uuid
* @param callback
*/
cardLoads(uuid, callback) {
this.jsonRequest('GET', '/cards/' + uuid + '/loads', {}, callback);
}
/**
* [cardLoadDetails]
* @param uuidCard
* @param uuidTransaction
* @param callback
*/
cardLoadDetails(uuidCard, uuidTransaction, callback) {
this.jsonRequest('GET', '/cards/' + uuidCard + '/loads/' + uuidTransaction, {}, callback);
}
/**
* [cardTransactions]
* @param uuid
* @param callback
*/
cardTransactions(uuid, callback) {
this.jsonRequest('GET', '/cards/' + uuid + '/transactions', {}, callback);
}
/**
* [cardTransaction]
* @param uuidCard
* @param uuidTransaction
* @param callback
*/
cardTransaction(uuidCard, uuidTransaction, callback) {
this.jsonRequest('GET', '/cards/' + uuidCard + '/transactions/' + uuidTransaction, {}, callback);
}
/**
* [cardTransfers]
* @param amountCents
* @param uuidFromCard
* @param uuidToCard
* @param callback
*/
cardTransfers(amountCents, uuidFromCard, uuidToCard, callback) {
this.jsonRequest('POST', '/cards/' + uuidFromCard + '/transfers', {
amount_cents: amountCents,
to_card_uuid: uuidToCard
}, callback)
}
/**
* [cardTransfersDetails]
* @param uuidCard
* @param uuidTransfer
* @param callback
*/
cardTransfersDetails(uuidCard, uuidTransfer, callback) {
this.jsonRequest('GET', '/cards/' + uuidCard + '/transfers/' + uuidTransfer, {}, callback);
}
/**
* [cardTransfersList]
* @param uuid
* @param callback
*/
cardTransfersList(uuid, callback) {
this.jsonRequest('GET', '/cards/' + uuid + '/transfers', {}, callback);
}
/**
* [users]
* @param callback
*/
users(callback) {
this.jsonRequest('GET', '/users', {}, callback);
}
/**
* [userRegister]
* @param firstName
* @param lastName
* @param email
* @param skipWelcomeEmail
* @param callback
*/
userRegister(firstName, lastName, email, skipWelcomeEmail, callback) {
this.jsonRequest('POST', '/users', {
"first_name": firstName,
"last_name": lastName,
"email": email,
"skip_welcome_email": skipWelcomeEmail
}, callback);
}
/**
* [userDetails]
* @param uuid
* @param callback
*/
userDetails(uuid, callback) {
this.jsonRequest('GET', '/users/' + uuid, {}, callback);
}
/**
* [uploadUserDocuments]
* @param uuid User ID
* @param idFrontside Image on base64
* @param idBackside Image on base64
* @param residence Image on base64
* @param callback
* @returns {string}
*/
uploadUserDocuments (uuid, idFrontside, idBackside, residence, callback) {
var _this = this;
this.jsonRequest('POST', '/users/' + uuid + '/documents', {
id_document_frontside: idFrontside,
id_document_backside: idBackside,
residence_document: residence
}, callback);
}
/**
* [userSetPassword]
* @param password
* @param uuid
* @param callback
*/
userSetPassword(password, uuid, callback) {
this.jsonRequest('POST', '/users/' + uuid + '/password', {password: password}, callback);
}
/**
* [invoices]
* @param callback
*/
invoices(callback) {
this.jsonRequest('GET', '/invoices', {}, callback);
}
/**
* [createInvoice]
* @param price
* @param currency
* @param name
* @param description
* @param referenceId
* @param redirectUrl
* @param callbackUrl
* @param callbackParams
* @param callback
*/
invoice(price, currency, name, description, referenceId, redirectUrl, callbackUrl, callbackParams, callback) {
this.jsonRequest('POST', '/invoices', {
price: price,
currency: currency,
name: name,
description: description,
id: referenceId,
success_redirect_url: redirectUrl,
callback_url: callbackUrl,
callback_params: callbackParams
}, callback);
}
/**
* [invoiceDetails]
* @param uuid
* @param callback
*/
invoiceDetails(uuid, callback) {
this.jsonRequest('GET', '/invoices/' + uuid, {}, callback);
}
/**
* [userGetAccounts]
* @param uuid
* @param callback
*/
userGetAccounts(uuid, callback) {
var _this = this;
this.userDetails(uuid, function(err, user) {
if (!err && user) {
// Change API Key for Api key's user
_this.setApiKey(user['apikey']);
// Request accounts
_this.accounts(function(err, accounts) {
if (!err && accounts) {
callback(null, accounts);
} else {
callback('Accounts not found.');
}
});
} else {
// Return not found
callback('User not found');
}
});
}
/**
* [handleError]
* @param response
*/
handleError(response) {
var msg = 'CryptoPay Error: ';
switch (response.statusCode) {
case 400:
msg += 'Bad Request';
break;
case 401:
msg += 'Unauthorized – API key is wrong';
break;
case 403:
msg += 'Forbidden';
break;
case 404:
msg += 'Not Found';
break;
case 410:
msg += 'Gone – The kitten requested has been removed from our servers';
break;
case 422:
msg += 'User already exists';
break;
case 500:
msg += 'Internal Server Error';
break;
case 503:
msg += 'Service Unavailable – Site offline for maintanance';
break;
default:
msg += JSON.stringify(response);
break;
}
console.log(msg);
}
}
module.exports = CryptoPay;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment