-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
180 lines (157 loc) · 5.01 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import type { ProcessPaymentResponse } from '../src/azul-api/process-payment/types';
import type { DataVaultResponse } from '../src/azul-api/data-vault/types';
import { expect } from 'vitest';
// Since we don't have direct access to VerifyPaymentResponse type, we'll define it
interface VerifyPaymentResponse extends Partial<ProcessPaymentResponse> {
Found?: boolean;
TransactionType?: string;
}
type PaymentSuccessResponse = ProcessPaymentResponse & {
IsoCode: '00';
ResponseCode: 'ISO8583';
ResponseMessage: 'APROBADA';
};
type PaymentErrorResponse = ProcessPaymentResponse & {
IsoCode: string;
ResponseCode: 'Error';
ErrorDescription: string;
};
type PaymentTemporaryError = ProcessPaymentResponse & {
IsoCode: '91';
};
type VaultSuccessResponse = DataVaultResponse & {
IsoCode: '00';
DataVaultToken: string;
Expiration: string;
};
type VaultDeleteResponse = Omit<DataVaultResponse, 'DataVaultToken' | 'Expiration'> & {
IsoCode: '00';
ResponseMessage: 'APROBADA';
};
type VoidSuccessResponse = ProcessPaymentResponse & {
IsoCode: '00';
ResponseMessage: 'APROBADA';
};
/**
* Type guard to check if a response indicates success (IsoCode: "00")
*/
export function isSuccessResponse(
result: ProcessPaymentResponse | VerifyPaymentResponse | DataVaultResponse
): result is
| PaymentSuccessResponse
| VaultSuccessResponse
| VaultDeleteResponse
| (VerifyPaymentResponse & { IsoCode: '00' }) {
return result.IsoCode === '00';
}
/**
* Type guard to check if a response indicates a temporary system error (IsoCode: "91")
*/
export function isTemporaryError(
result: ProcessPaymentResponse | VerifyPaymentResponse | DataVaultResponse
): result is PaymentTemporaryError | (VerifyPaymentResponse & { IsoCode: '91' }) {
return result.IsoCode === '91';
}
/**
* Type guard to check if a response indicates a validation error
*/
export function isValidationError(result: ProcessPaymentResponse): result is PaymentErrorResponse {
return (
result.ResponseCode === 'Error' &&
(result.ErrorDescription?.startsWith('VALIDATION_ERROR:') ?? false)
);
}
/**
* Helper to expect a validation error with a specific error description
*/
export function expectValidationError(result: ProcessPaymentResponse, expectedError: string) {
// If we get a temporary error, skip the validation
if (isTemporaryError(result)) {
console.warn('⚠️ Received temporary error (91), skipping validation check');
return;
}
expect(result).toMatchObject({
ResponseCode: 'Error',
ErrorDescription: expectedError
});
}
/**
* Helper to expect a successful payment response
*/
export function expectSuccessfulPayment(result: ProcessPaymentResponse) {
// If we get a temporary error, skip the validation
if (isTemporaryError(result)) {
console.warn('⚠️ Received temporary error (91), skipping success check');
return;
}
expect(result).toMatchObject<Partial<PaymentSuccessResponse>>({
IsoCode: '00',
ResponseMessage: 'APROBADA',
ResponseCode: 'ISO8583'
});
}
/**
* Helper to expect a successful verification response
*/
export function expectSuccessfulVerification(result: VerifyPaymentResponse, expectedType: string) {
// If we get a temporary error, skip the validation
if (isTemporaryError(result)) {
console.warn('⚠️ Received temporary error (91), skipping verification check');
return;
}
expect(result).toMatchObject<Partial<VerifyPaymentResponse>>({
Found: true,
TransactionType: expectedType
});
}
/**
* Helper to expect a response to have required order IDs
*/
export function expectOrderIds(result: ProcessPaymentResponse) {
expect(result.AzulOrderId).toBeDefined();
expect(result.AzulOrderId).not.toBe('');
if (result.CustomOrderId) {
expect(result.CustomOrderId).not.toBe('');
}
}
/**
* Helper to expect a successful DataVault token creation response
*/
export function expectSuccessfulVaultResponse(result: DataVaultResponse) {
if (isTemporaryError(result)) {
console.warn('⚠️ Received temporary error (91), skipping vault check');
return;
}
expect(result).toMatchObject<Partial<VaultSuccessResponse>>({
IsoCode: '00',
DataVaultToken: expect.any(String),
Expiration: expect.any(String)
});
}
/**
* Helper to expect a successful DataVault token deletion response
*/
export function expectSuccessfulVaultDeletion(result: DataVaultResponse) {
if (isTemporaryError(result)) {
console.warn('⚠️ Received temporary error (91), skipping vault deletion check');
return;
}
expect(result).toMatchObject<Partial<VaultDeleteResponse>>({
IsoCode: '00',
ResponseMessage: 'APROBADA'
});
}
/**
* Helper to expect a successful void response
*/
export function expectSuccessfulVoid(result: ProcessPaymentResponse) {
// If we get a temporary error, skip the validation
if (isTemporaryError(result)) {
console.warn('⚠️ Received temporary error (91), skipping void check');
return;
}
expect(result).toMatchObject<Partial<VoidSuccessResponse>>({
IsoCode: '00',
ResponseMessage: 'APROBADA'
});
}