akua-sdk
Version:
TypeScript SDK for Akua Acquiring Processor
261 lines (260 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentService = void 0;
const mapper_1 = require("../helpers/mapper");
const enums_1 = require("../types/enums");
/**
* Service responsible for handling payment operations in the Akua API.
* @class PaymentService
*/
class PaymentService {
httpClient;
/**
* Creates an instance of PaymentService.
* @param {HttpClient} httpClient - The HTTP client instance used for making API requests.
*/
constructor(httpClient) {
this.httpClient = httpClient;
}
/**
* Authorizes a payment transaction using PAN (Primary Account Number).
* @async
* @param {AuthorizeWithPanRequest} authorizeWithPanRequest - The payment authorization request data with PAN.
* @returns {Promise<ApiResponse<AuthorizePaymentDTO>>} A promise that resolves to the payment authorization response.
* @example
* // Authorize a payment with PAN
* await paymentService.authorizeWithPan({
* amount: {
* value: 25.25,
* currency: "USD"
* },
* intent: "authorization",
* trace_id: "any-format-34324366",
* entry_mode: "manual",
* capture: {
* mode: "AUTOMATIC"
* },
* merchant_id: "mer-d0r0b17npsai542l2400",
* installments: {
* quantity: 2,
* type: "03"
* },
* instrument: {
* type: "CARD",
* card: {
* number: "4024007108904086",
* expiration_month: "12",
* expiration_year: "25",
* holder_name: "Fede Delgado",
* cvv: "917"
* },
* user_data: {
* billing_address: {
* street: "Av. Libertador",
* number: "1234",
* city: "Buenos Aires",
* state: "Buenos Aires",
* zip_code: "C1001",
* country: "Argentina",
* phone_number: "+54 9 11 1234-5678"
* }
* }
* }
* });
*/
async authorizeWithPan(authorizeWithPanRequest) {
const { idempotencyKey, ...requestData } = authorizeWithPanRequest;
const config = idempotencyKey ? { headers: { 'Idempotency-Key': idempotencyKey } } : undefined;
const response = await this.httpClient.post('/v1/authorizations', requestData, config);
const mappedData = mapper_1.Mapper.mapToAuthorizePaymentDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Authorizes a payment transaction using a tokenized instrument.
* @async
* @param {AuthorizeWithTokenRequest} authorizeWithTokenRequest - The payment authorization request data with token.
* @returns {Promise<ApiResponse<AuthorizePaymentDTO>>} A promise that resolves to the payment authorization response.
* @example
* // Authorize a payment with token
* await paymentService.authorizeWithToken({
* amount: {
* value: 25.25,
* currency: "USD"
* },
* intent: "authorization",
* trace_id: "any-format-34324366",
* entry_mode: "manual",
* capture: {
* mode: "AUTOMATIC"
* },
* merchant_id: "mer-d0r0b17npsai542l2400",
* installments: {
* quantity: 2,
* type: "03"
* },
* });
*/
async authorizeWithToken(authorizeWithTokenRequest) {
const { idempotencyKey, ...requestData } = authorizeWithTokenRequest;
const config = idempotencyKey ? { headers: { 'Idempotency-Key': idempotencyKey } } : undefined;
const response = await this.httpClient.post('/v1/authorizations', requestData, config);
const mappedData = mapper_1.Mapper.mapToAuthorizePaymentDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Captures a previously authorized payment.
* @async
* @param {string} paymentId - The unique identifier of the payment to capture.
* @param {CapturePaymentRequest} capturePaymentRequest - The payment capture request data.
* @returns {Promise<ApiResponse<CapturePaymentResponseDTO>>} A promise that resolves to the payment capture response.
* @example
* // Capture a payment
* await paymentService.capture('pay_123', {
* amount: {
* value: 25.25,
* currency: "USD"
* }
* });
* @note Only payments created with capture_manual mode can be captured.
*/
async capture(paymentId, capturePaymentRequest) {
const { idempotencyKey, ...requestData } = capturePaymentRequest;
const config = idempotencyKey ? { headers: { 'Idempotency-Key': idempotencyKey } } : undefined;
const response = await this.httpClient.post(`/v1/payments/${paymentId}/captures`, requestData, config);
const mappedData = mapper_1.Mapper.mapToCapturePaymentResponseDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Verifies a payment instrument (card) status.
* @async
* @param {VerifyPaymentRequest} verifyPaymentRequest - The payment verification request data.
* @returns {Promise<ApiResponse<AuthorizePaymentDTO>>} A promise that resolves to the payment verification response.
* @example
* // Verify a payment with PAN
* await paymentService.verify({
* intent: "account-status",
* merchant_id: "mer-d0r0b17npsai542l2400",
* trace_id: "any-format-34324366",
* instrument: {
* type: "CARD",
* card: {
* number: "4024007108904086",
* expiration_month: "12",
* expiration_year: "25",
* holder_name: "Fede Delgado",
* cvv: "917"
* },
* user_data: {
* billing_address: {
* street: "Av. Libertador",
* number: "1234",
* city: "Buenos Aires",
* state: "Buenos Aires",
* zip_code: "C1001",
* country: "Argentina",
* phone_number: "+54 9 11 1234-5678"
* }
* }
* }
* });
*
* // Or verify with token
* await paymentService.verify({
* intent: "account-status",
* merchant_id: "mer-d0r0b17npsai542l2400",
* trace_id: "any-format-34324366",
* instrument: {
* id: "instr_123456789"
* }
* });
*/
async verify(verifyPaymentRequest) {
const { idempotencyKey, ...requestData } = verifyPaymentRequest;
const config = idempotencyKey ? { headers: { 'Idempotency-Key': idempotencyKey } } : undefined;
const response = await this.httpClient.post('/v1/authorizations', requestData, config);
const mappedData = mapper_1.Mapper.mapToAuthorizePaymentDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Cancels a payment transaction.
* @async
* @param {string} paymentId - The unique identifier of the payment to cancel.
* @param {CancelPaymentRequest} cancelPaymentRequest - The cancel request data.
* @returns {Promise<ApiResponse<CancelPaymentDTO>>} A promise that resolves to the payment cancellation response.
* @example
* // Cancel a payment
* await paymentService.cancel('pay-d0v32ak2mts03heh7amg', {
* idempotencyKey: 'unique-key-123'
* });
*/
async cancel(paymentId, cancelPaymentRequest = {}) {
const { idempotencyKey, ...requestData } = cancelPaymentRequest;
const config = idempotencyKey ? { headers: { 'Idempotency-Key': idempotencyKey } } : undefined;
const response = await this.httpClient.post(`/v1/payments/${paymentId}/cancel`, requestData, config);
const mappedData = mapper_1.Mapper.mapToCancelPaymentDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Refunds a payment transaction.
* @async
* @param {string} paymentId - The unique identifier of the payment to refund.
* @param {RefundPaymentRequest} refundPaymentRequest - The refund request data containing the amount.
* @returns {Promise<ApiResponse<RefundPaymentDTO>>} A promise that resolves to the payment refund response.
* @example
* // Refund a payment
* await paymentService.refund('pay-d0v32ak2mts03heh7amg', {
* amount: {
* value: 25.25,
* currency: "USD"
* }
* });
*/
async refund(paymentId, refundPaymentRequest) {
const { idempotencyKey, ...requestData } = refundPaymentRequest;
const config = idempotencyKey ? { headers: { 'Idempotency-Key': idempotencyKey } } : undefined;
const response = await this.httpClient.post(`/v1/payments/${paymentId}/refund`, requestData, config);
const mappedData = mapper_1.Mapper.mapToRefundPaymentDTO(response.data);
return {
...response,
data: mappedData,
};
}
/**
* Clears a payment transaction. This method is only available in the sandbox environment.
* @async
* @param {string} paymentId - The unique identifier of the payment to clear.
* @param {ClearPaymentRequest} clearPaymentRequest - The clear request data.
* @returns {Promise<ApiResponse<void | null>>} A promise that resolves when the payment is successfully cleared, or rejects with an error if used in production.
* @example
* // Clear a payment (sandbox only)
* await paymentService.clear('pay-d0v32ak2mts03heh7amg', {
* idempotencyKey: 'unique-key-123'
* });
*/
async clear(paymentId, clearPaymentRequest = {}) {
const validationError = this.httpClient.validateEnvironment(enums_1.Environment.SANDBOX, 'payment clear');
if (validationError) {
return validationError;
}
const { idempotencyKey, ...requestData } = clearPaymentRequest;
const config = idempotencyKey ? { headers: { 'Idempotency-Key': idempotencyKey } } : undefined;
const response = await this.httpClient.post(`/v1/payments/${paymentId}/clear`, requestData, config);
return response;
}
}
exports.PaymentService = PaymentService;