privacy.com
Version:
Wrapper for the Privacy.com API using Axios and TypeScript
148 lines (147 loc) • 5.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const HostedCardUi_1 = require("./endpoints/get/HostedCardUi");
const ListCards_1 = require("./endpoints/get/ListCards");
const ListFundingAccounts_1 = require("./endpoints/get/ListFundingAccounts");
const ListTransactions_1 = require("./endpoints/get/ListTransactions");
const AddBank_1 = require("./endpoints/post/AddBank");
const CreateCard_1 = require("./endpoints/post/CreateCard");
const Authorization_1 = require("./endpoints/post/simulations/Authorization");
const Clearing_1 = require("./endpoints/post/simulations/Clearing");
const Return_1 = require("./endpoints/post/simulations/Return");
const Void_1 = require("./endpoints/post/simulations/Void");
const UpdateCard_1 = require("./endpoints/put/UpdateCard");
class PrivacyApi {
/**
*
* @param apiKey API Key to use for requests
* @param sandbox Execute requests in sandbox mode or not @defaultValue true
* @param version API version to use for requests @defaultValue "v1"
*/
constructor(apiKey, sandbox = true, version = "v1") {
this.apiKey = apiKey;
this.sandbox = sandbox;
this.version = version;
this.baseUrl = this.getBaseUrl();
this.headers = { Authorization: `api-key ${this.apiKey}` };
}
execute(request) {
if (request.beforeExecute)
request.beforeExecute(this);
const requestUrl = `${this.baseUrl}${request.path}`;
const method = request.method;
const params = request.params;
const headers = {
...this.headers,
};
switch (method) {
case "GET":
return axios_1.default.get(requestUrl, {
headers,
params,
});
case "PUT":
return axios_1.default.put(requestUrl, params, {
headers,
});
case "POST":
return axios_1.default.post(requestUrl, params, {
headers,
});
default:
return new Promise((_, reject) => reject("INVALID_REQUEST_METHOD"));
}
}
/**
* Create a new card for the privacy account
*/
createCard(params) {
return this.execute(new CreateCard_1.CreateCardRequest(params));
}
/**
* Adds a bank account as a funding source using routing and account numbers. Returns a FundingAccount object containing the bank information.
*/
addBank(params) {
return this.execute(new AddBank_1.AddBankRequest(params));
}
/**
* List cards associated with the privacy account
*/
listCards(params) {
return this.execute(new ListCards_1.ListCardsRequest(params));
}
/**
* List all the funding accounts associated with the privacy account
*/
listFundingAccounts(params) {
return this.execute(new ListFundingAccounts_1.ListFundingAccountsRequest(params));
}
/**
* List transactions associated with the privacy account or a specific card
*/
listTransactions(params) {
return this.execute(new ListTransactions_1.ListTransactionsRequest(params));
}
/**
* Get iframe data to display card details
* @remarks The iframe body will provide consistent markup of the following form.
* It is up to the API client to provide css styles for these elements in the EmbedRequest.
* ```html
* <div id="card">
* <div id="pan">{PAN}</div>
* <div id="expiry">
* <span id="month">{expMonth}</span>
* <span id="separator">/</span>
* <span id="year">{expYear}</span>
* </div>
* <div id="cvv">{CVV}</div>
* </div>
* ```
*/
hostedCardUi(params) {
return this.execute(new HostedCardUi_1.HostedCardUiRequest(params));
}
/**
* Update a card by its token for the privacy account
*/
updateCard(params) {
return this.execute(new UpdateCard_1.UpdateCardRequest(params));
}
/**
* Simulates an authorization request from the payment network as if it came from a merchant acquirer.
* @remarks The API sends an event for all approvals. Decline events are available with API Issuing accounts and cannot be simulated.
* @link https://developer.privacy.com/docs#transaction-webhooks
*/
simulateAuthorization(params) {
return this.execute(new Authorization_1.SimulateAuthorizationRequest(params));
}
/**
* Voids an existing, uncleared (aka pending) authorization.
* @remarks Previous pending authorization is voided
*/
simulateVoid(params) {
return this.execute(new Void_1.SimulateVoidRequest(params));
}
/**
* Clears an existing authorization. After this event, the transaction is no longer pending.
* @remarks Clearing for an existing, pending authorization
*/
simulateClearing(params) {
return this.execute(new Clearing_1.SimulateClearingRequest(params));
}
/**
* Returns (aka refunds) an amount back to a card. Returns are cleared immediately and do not spend time in a "pending" state.
* @remarks Refund — value is pushed onto card
*/
simulateReturn(params) {
return this.execute(new Return_1.SimulateReturnRequest(params));
}
getBaseUrl() {
return `https://${this.sandbox ? "sandbox" : "api"}.privacy.com/${this.version}`;
}
}
exports.PrivacyApi = PrivacyApi;