Skip to content

Commit

Permalink
refactor: Adds processes.ts and adds search schema
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-herasme committed May 4, 2024
1 parent ec51990 commit 895227d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 51 deletions.
34 changes: 7 additions & 27 deletions src/azul-api/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PostSchema, PostSchemaInput } from './schemas';
import { PostSchema, PostSchemaInput, SearchSchema, SearchSchemaInput } from './schemas';
import AzulRequester, { Config } from './request';
import DataVault from './data-vault/data-vault';
import ProcessPayment from './process-payment/process-payment ';
import { ProcessPaymentResponse } from './process-payment/schemas';
import { Process } from './processes';

class AzulAPI {
private requester: AzulRequester;
Expand All @@ -24,12 +25,7 @@ class AzulAPI {
* anularse.
*/
async void(azulOrderId: string): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest({
url: this.requester.url + '?ProcessVoid',
body: {
azulOrderId
}
});
return await this.requester.safeRequest({ azulOrderId }, Process.Void);
}

/**
Expand All @@ -40,12 +36,7 @@ class AzulAPI {
* fondos retenidos a la tarjeta.
*/
async post(input: PostSchemaInput): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest({
url: this.requester.url + '?ProcessPost',
body: {
...PostSchema.parse(input)
}
});
return await this.requester.safeRequest(PostSchema.parse(input), Process.Post);
}

/**
Expand All @@ -57,27 +48,16 @@ class AzulAPI {
* valores de la última transacción (más reciente) de ellas.
*/
async verifyPayment(customOrderId: string): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest({
url: this.requester.url + '?VerifyPayment',
body: {
customOrderId
}
});
return await this.requester.safeRequest({ customOrderId }, Process.VerifyPayment);
}

/**
* Este método permite extraer los detalles de una o varias transacciones
* vía Webservices, anteriormente procesadas de un rango de fechas
* previamente seleccionado.
*/
async search({ from, to }: { from: string; to: string }): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest({
url: this.requester.url + '?SearchPayments',
body: {
dateFrom: from,
dateTo: to
}
});
async search(input: SearchSchemaInput): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest(SearchSchema.parse(input), Process.SearchPayments);
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/azul-api/data-vault/data-vault.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Process } from '../processes';
import AzulRequester from '../request';
import { Create, CreateInput, Delete, DeleteInput, DataVaultResponse } from './shemas';

Expand All @@ -19,27 +20,27 @@ class DataVault {
* sin necesidad de realizar una venta.
*/
async create(input: CreateInput): Promise<DataVaultResponse> {
return await this.requester.safeRequest({
url: this.requester.url + '?ProcessDatavault',
body: {
return await this.requester.safeRequest(
{
...Create.parse(input),
trxType: DataVaultTransaction.CREATE
}
});
},
Process.Datavault
);
}

/**
* ### Delete: Eliminación de Token de Bóveda de Datos (DataVault)
* Con esta transacción se solicita la eliminación de un token de la Bóveda de Datos.
*/
async delete(input: DeleteInput): Promise<DataVaultResponse> {
return await this.requester.safeRequest({
url: this.requester.url + '?ProcessDatavault',
body: {
return await this.requester.safeRequest(
{
...Delete.parse(input),
trxType: DataVaultTransaction.DELETE
}
});
},
Process.Datavault
);
}
}

Expand Down
18 changes: 6 additions & 12 deletions src/azul-api/process-payment/process-payment .ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ class ProcessPayment {
*/
async sale(input: ProcessPaymentSchemaInput): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest({
body: {
...ProcessPaymentSchema.parse(input),
trxType: ProcessPaymentTransaction.SALE
}
...ProcessPaymentSchema.parse(input),
trxType: ProcessPaymentTransaction.SALE
});
}

Expand All @@ -51,10 +49,8 @@ class ProcessPayment {
*/
async refund(input: ProcessPaymentSchemaInput): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest({
body: {
...ProcessPaymentSchema.parse(input),
trxType: ProcessPaymentTransaction.REFUND
}
...ProcessPaymentSchema.parse(input),
trxType: ProcessPaymentTransaction.REFUND
});
}

Expand All @@ -78,10 +74,8 @@ class ProcessPayment {
*/
async hold(input: ProcessPaymentSchemaInput): Promise<ProcessPaymentResponse> {
return await this.requester.safeRequest({
body: {
...ProcessPaymentSchema.parse(input),
trxType: ProcessPaymentTransaction.HOLD
}
...ProcessPaymentSchema.parse(input),
trxType: ProcessPaymentTransaction.HOLD
});
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/azul-api/processes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum Process {
Void = 'ProcessVoid',
Post = 'ProcessPost',
Datavault = 'ProcessDatavault',
VerifyPayment = 'VerifyPayment',
SearchPayments = 'SearchPayments'
}
11 changes: 9 additions & 2 deletions src/azul-api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import https from 'https';
import fs from 'fs/promises';
import fetch from 'node-fetch';
import { capitalizeKeys } from '../utils';
import { Process } from './processes';

enum AzulURL {
DEV = 'https://pruebas.azul.com.do/webservices/JSON/Default.aspx',
Expand Down Expand Up @@ -51,8 +52,14 @@ class AzulRequester {
}
}

async safeRequest({ body, url }: { body: any; url?: string }) {
const response = await fetch(url || this.url, {
async safeRequest(body: any, process?: Process) {
let url = this.url;

if (process) {
url = url + '?' + process;
}

const response = await fetch(url, {
method: 'POST',
headers: {
Auth1: this.auth1,
Expand Down
14 changes: 14 additions & 0 deletions src/azul-api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ export const PostSchema = z.object({
ITBIS
});

export const SearchSchema = z.object({
// YYYY-MM-DD
dateFrom: z
.string()
.length(10)
.regex(/^\d{4}-\d{2}-\d{2}$/),
// YYYY-MM-DD
dateTo: z
.string()
.length(10)
.regex(/^\d{4}-\d{2}-\d{2}$/)
});

export type SearchSchemaInput = z.infer<typeof SearchSchema>;
export type PostSchemaInput = z.infer<typeof PostSchema>;

0 comments on commit 895227d

Please sign in to comment.