expresspayments
Version:
ExpressPayments API wrapper
147 lines (146 loc) • 6.3 kB
JavaScript
"use strict";
/* eslint-disable camelcase */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressPaymentsUnknownError = exports.ExpressPaymentsInvalidGrantError = exports.ExpressPaymentsIdempotencyError = exports.ExpressPaymentsSignatureVerificationError = exports.ExpressPaymentsConnectionError = exports.ExpressPaymentsRateLimitError = exports.ExpressPaymentsPermissionError = exports.ExpressPaymentsAuthenticationError = exports.ExpressPaymentsAPIError = exports.ExpressPaymentsInvalidRequestError = exports.ExpressPaymentsCardError = exports.ExpressPaymentsError = exports.generate = void 0;
const generate = (rawExpressPaymentsError) => {
switch (rawExpressPaymentsError.type) {
case 'card_error':
return new ExpressPaymentsCardError(rawExpressPaymentsError);
case 'invalid_request_error':
return new ExpressPaymentsInvalidRequestError(rawExpressPaymentsError);
case 'api_error':
return new ExpressPaymentsAPIError(rawExpressPaymentsError);
case 'authentication_error':
return new ExpressPaymentsAuthenticationError(rawExpressPaymentsError);
case 'rate_limit_error':
return new ExpressPaymentsRateLimitError(rawExpressPaymentsError);
case 'idempotency_error':
return new ExpressPaymentsIdempotencyError(rawExpressPaymentsError);
case 'invalid_grant':
return new ExpressPaymentsInvalidGrantError(rawExpressPaymentsError);
default:
return new ExpressPaymentsUnknownError(rawExpressPaymentsError);
}
};
exports.generate = generate;
/**
* ExpressPaymentsError is the base error from which all other more specific ExpressPayments errors derive.
* Specifically for errors returned from ExpressPayments' REST API.
*/
class ExpressPaymentsError extends Error {
constructor(raw = {}) {
super(raw.message);
this.type = this.constructor.name;
this.raw = raw;
this.rawType = raw.type;
this.code = raw.code;
this.doc_url = raw.doc_url;
this.param = raw.param;
this.detail = raw.detail;
this.headers = raw.headers;
this.requestId = raw.requestId;
this.statusCode = raw.statusCode;
// @ts-ignore
this.message = raw.message;
this.charge = raw.charge;
this.decline_code = raw.decline_code;
this.payment_intent = raw.payment_intent;
this.payment_method = raw.payment_method;
this.payment_method_type = raw.payment_method_type;
this.setup_intent = raw.setup_intent;
this.source = raw.source;
}
}
exports.ExpressPaymentsError = ExpressPaymentsError;
/**
* Helper factory which takes raw ExpressPayments errors and outputs wrapping instances
*/
ExpressPaymentsError.generate = exports.generate;
// Specific ExpressPayments Error types:
/**
* CardError is raised when a user enters a card that can't be charged for
* some reason.
*/
class ExpressPaymentsCardError extends ExpressPaymentsError {
}
exports.ExpressPaymentsCardError = ExpressPaymentsCardError;
/**
* InvalidRequestError is raised when a request is initiated with invalid
* parameters.
*/
class ExpressPaymentsInvalidRequestError extends ExpressPaymentsError {
}
exports.ExpressPaymentsInvalidRequestError = ExpressPaymentsInvalidRequestError;
/**
* APIError is a generic error that may be raised in cases where none of the
* other named errors cover the problem. It could also be raised in the case
* that a new error has been introduced in the API, but this version of the
* Node.js SDK doesn't know how to handle it.
*/
class ExpressPaymentsAPIError extends ExpressPaymentsError {
}
exports.ExpressPaymentsAPIError = ExpressPaymentsAPIError;
/**
* AuthenticationError is raised when invalid credentials are used to connect
* to ExpressPayments' servers.
*/
class ExpressPaymentsAuthenticationError extends ExpressPaymentsError {
}
exports.ExpressPaymentsAuthenticationError = ExpressPaymentsAuthenticationError;
/**
* PermissionError is raised in cases where access was attempted on a resource
* that wasn't allowed.
*/
class ExpressPaymentsPermissionError extends ExpressPaymentsError {
}
exports.ExpressPaymentsPermissionError = ExpressPaymentsPermissionError;
/**
* RateLimitError is raised in cases where an account is putting too much load
* on ExpressPayments' API servers (usually by performing too many requests). Please
* back off on request rate.
*/
class ExpressPaymentsRateLimitError extends ExpressPaymentsError {
}
exports.ExpressPaymentsRateLimitError = ExpressPaymentsRateLimitError;
/**
* ExpressPaymentsConnectionError is raised in the event that the SDK can't connect to
* ExpressPayments' servers. That can be for a variety of different reasons from a
* downed network to a bad TLS certificate.
*/
class ExpressPaymentsConnectionError extends ExpressPaymentsError {
}
exports.ExpressPaymentsConnectionError = ExpressPaymentsConnectionError;
/**
* SignatureVerificationError is raised when the signature verification for a
* webhook fails
*/
class ExpressPaymentsSignatureVerificationError extends ExpressPaymentsError {
constructor(header, payload, raw = {}) {
super(raw);
this.header = header;
this.payload = payload;
}
}
exports.ExpressPaymentsSignatureVerificationError = ExpressPaymentsSignatureVerificationError;
/**
* IdempotencyError is raised in cases where an idempotency key was used
* improperly.
*/
class ExpressPaymentsIdempotencyError extends ExpressPaymentsError {
}
exports.ExpressPaymentsIdempotencyError = ExpressPaymentsIdempotencyError;
/**
* InvalidGrantError is raised when a specified code doesn't exist, is
* expired, has been used, or doesn't belong to you; a refresh token doesn't
* exist, or doesn't belong to you; or if an API key's mode (live or test)
* doesn't match the mode of a code or refresh token.
*/
class ExpressPaymentsInvalidGrantError extends ExpressPaymentsError {
}
exports.ExpressPaymentsInvalidGrantError = ExpressPaymentsInvalidGrantError;
/**
* Any other error from ExpressPayments not specifically captured above
*/
class ExpressPaymentsUnknownError extends ExpressPaymentsError {
}
exports.ExpressPaymentsUnknownError = ExpressPaymentsUnknownError;