akua-sdk
Version:
TypeScript SDK for Akua Acquiring Processor
132 lines (131 loc) • 4.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstrumentService = void 0;
const mapper_1 = require("../helpers/mapper");
/**
* Service responsible for managing payment instruments (payment methods) in the Akua API.
* @class InstrumentService
*/
class InstrumentService {
httpClient;
/**
* Creates an instance of InstrumentService.
* @param {HttpClient} httpClient - The HTTP client instance used for making API requests.
*/
constructor(httpClient) {
this.httpClient = httpClient;
}
/**
* Retrieves all payment instruments associated with the merchant.
* @async
* @returns {Promise<ApiResponse<InstrumentDTO[]>>} A promise that resolves to an array of payment instruments.
* @example
* // Get all payment instruments
* await instrumentService.get();
*/
async get() {
const response = await this.httpClient.get('/v1/instruments');
const mappedData = mapper_1.Mapper.mapToInstrumentDTO(response.data.data);
return { ...response, data: mappedData };
}
/**
* Retrieves a specific payment instrument by its ID.
* @async
* @param {string} instrumentId - The unique identifier of the payment instrument.
* @returns {Promise<ApiResponse<InstrumentDTO>>} A promise that resolves to the requested payment instrument.
* @example
* // Get a payment instrument by ID
* await instrumentService.getById('instr_123');
*/
async getById(instrumentId) {
const response = await this.httpClient.get(`/v1/instruments/${instrumentId}`);
const mappedData = mapper_1.Mapper.mapToInstrumentDTO(response.data);
return { ...response, data: mappedData };
}
/**
* Creates a new payment instrument.
* @async
* @param {CreateInstrumentRequest} createInstrumentRequest - The data required to create a new payment instrument.
* @returns {Promise<ApiResponse<InstrumentDTO>>} A promise that resolves to the newly created payment instrument.
* @example
* // Create a new credit card instrument
* await instrumentService.create({
* type: 'CARD',
* card: {
* number: '4242424242424242',
* expiration_month: '12',
* expiration_year: '2025',
* first_name: 'John',
* last_name: 'Doe',
* cvv: '123'
* },
* user_data: {
* billing_address: {
* street: '123 Main St',
* number: 'Apt 4B',
* city: 'New York',
* state: 'NY',
* zip_code: '10001',
* country: 'US'
* }
* }
* });
*/
async create(createInstrumentRequest) {
const response = await this.httpClient.post('/v1/instruments', createInstrumentRequest);
const mappedData = mapper_1.Mapper.mapToInstrumentDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Updates an existing payment instrument.
* @async
* @param {string} instrumentId - The unique identifier of the payment instrument to update.
* @param {UpdateInstrumentRequest} data - The data to update the payment instrument with.
* @returns {Promise<ApiResponse<InstrumentDTO>>} A promise that resolves to the updated payment instrument.
* @example
* // Update a payment instrument
* await instrumentService.update('instr_123', {
* card: {
* expiration_month: '12',
* expiration_year: '2026',
* first_name: 'John',
* last_name: 'Smith'
* },
* user_data: {
* billing_address: {
* street: '123 Main St',
* number: 'Apt 4B',
* city: 'New York',
* state: 'NY',
* zip_code: '10001',
* country: 'US'
* }
* }
* });
*/
async update(instrumentId, data) {
const response = await this.httpClient.patch(`/v1/instruments/${instrumentId}`, data);
const mappedData = mapper_1.Mapper.mapToInstrumentDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Deletes a payment instrument.
* @async
* @param {string} instrumentId - The unique identifier of the payment instrument to delete.
* @returns {Promise<ApiResponse<void>>} A promise that resolves when the deletion is successful.
* @example
* // Delete a payment instrument
* await instrumentService.delete('instr_123');
*/
async delete(instrumentId) {
const response = await this.httpClient.delete(`/v1/instruments/${instrumentId}`);
return response;
}
}
exports.InstrumentService = InstrumentService;