Skip to content

Commit de60b59

Browse files
committed
refactor: unify payment handling with enums for type safety and improved error handling
1 parent b31fd72 commit de60b59

4 files changed

Lines changed: 175 additions & 73 deletions

File tree

android/src/main/java/com/margelo/nitro/plugpagnitro/PlugpagNitro.kt

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,18 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
7979
val activationData = PlugPagActivationData(activationCode)
8080
val result = plugPag.initializeAndActivatePinpad(activationData)
8181

82+
val errorCode = when (result.result) {
83+
PlugPag.RET_OK -> ErrorCode.OK
84+
PlugPag.OPERATION_ABORTED -> ErrorCode.OPERATION_ABORTED
85+
PlugPag.AUTHENTICATION_FAILED -> ErrorCode.AUTHENTICATION_FAILED
86+
PlugPag.COMMUNICATION_ERROR -> ErrorCode.COMMUNICATION_ERROR
87+
PlugPag.NO_PRINTER_DEVICE -> ErrorCode.NO_PRINTER_DEVICE
88+
PlugPag.NO_TRANSACTION_DATA -> ErrorCode.NO_TRANSACTION_DATA
89+
else -> ErrorCode.COMMUNICATION_ERROR
90+
}
91+
8292
PlugpagInitializationResult(
83-
result = result.result.toDouble(),
93+
result = errorCode,
8494
errorCode = result.errorCode ?: "",
8595
errorMessage = result.errorMessage ?: ""
8696
)
@@ -94,8 +104,8 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
94104

95105
override fun doPayment(
96106
amount: Double,
97-
type: Double,
98-
installmentType: Double,
107+
type: PaymentType,
108+
installmentType: InstallmentType,
99109
installments: Double,
100110
printReceipt: Boolean,
101111
userReference: String
@@ -105,19 +115,43 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
105115
try {
106116
initializePlugPag()
107117

118+
// Convert enum to PlugPag SDK constants
119+
val paymentType = when (type) {
120+
PaymentType.CREDIT -> PlugPag.TYPE_CREDITO
121+
PaymentType.DEBIT -> PlugPag.TYPE_DEBITO
122+
PaymentType.VOUCHER -> PlugPag.TYPE_VOUCHER
123+
PaymentType.PIX -> PlugPag.TYPE_PIX
124+
}
125+
126+
val installmentTypeInt = when (installmentType) {
127+
InstallmentType.NO_INSTALLMENT -> PlugPag.INSTALLMENT_TYPE_A_VISTA
128+
InstallmentType.SELLER_INSTALLMENT -> PlugPag.INSTALLMENT_TYPE_PARC_VENDEDOR
129+
InstallmentType.BUYER_INSTALLMENT -> PlugPag.INSTALLMENT_TYPE_PARC_COMPRADOR
130+
}
131+
108132
val plugPagPaymentData = PlugPagPaymentData(
109-
type.toInt(),
133+
paymentType,
110134
amount.toInt(),
111-
installmentType.toInt(),
135+
installmentTypeInt,
112136
installments.toInt(),
113137
userReference,
114138
printReceipt
115139
)
116140

117141
val result = plugPag.doPayment(plugPagPaymentData)
118142

143+
val errorCode = when (result.result) {
144+
PlugPag.RET_OK -> ErrorCode.OK
145+
PlugPag.OPERATION_ABORTED -> ErrorCode.OPERATION_ABORTED
146+
PlugPag.AUTHENTICATION_FAILED -> ErrorCode.AUTHENTICATION_FAILED
147+
PlugPag.COMMUNICATION_ERROR -> ErrorCode.COMMUNICATION_ERROR
148+
PlugPag.NO_PRINTER_DEVICE -> ErrorCode.NO_PRINTER_DEVICE
149+
PlugPag.NO_TRANSACTION_DATA -> ErrorCode.NO_TRANSACTION_DATA
150+
else -> ErrorCode.COMMUNICATION_ERROR
151+
}
152+
119153
PlugpagTransactionResult(
120-
result = result.result?.toDouble() ?: 0.0,
154+
result = errorCode,
121155
errorCode = result.errorCode ?: "",
122156
message = result.message ?: "",
123157
transactionCode = result.transactionCode ?: "",
@@ -145,7 +179,7 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
145179
}
146180
}
147181

148-
override fun voidPayment(
182+
override fun refundPayment(
149183
transactionCode: String,
150184
transactionId: String,
151185
printReceipt: Boolean
@@ -163,8 +197,18 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
163197

164198
val result = plugPag.voidPayment(plugPagVoidData)
165199

200+
val errorCode = when (result.result) {
201+
PlugPag.RET_OK -> ErrorCode.OK
202+
PlugPag.OPERATION_ABORTED -> ErrorCode.OPERATION_ABORTED
203+
PlugPag.AUTHENTICATION_FAILED -> ErrorCode.AUTHENTICATION_FAILED
204+
PlugPag.COMMUNICATION_ERROR -> ErrorCode.COMMUNICATION_ERROR
205+
PlugPag.NO_PRINTER_DEVICE -> ErrorCode.NO_PRINTER_DEVICE
206+
PlugPag.NO_TRANSACTION_DATA -> ErrorCode.NO_TRANSACTION_DATA
207+
else -> ErrorCode.COMMUNICATION_ERROR
208+
}
209+
166210
PlugpagTransactionResult(
167-
result = result.result?.toDouble() ?: 0.0,
211+
result = errorCode,
168212
errorCode = result.errorCode ?: "",
169213
message = result.message ?: "",
170214
transactionCode = result.transactionCode ?: "",
@@ -185,8 +229,8 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
185229
extendedHolderName = result.extendedHolderName ?: ""
186230
)
187231
} catch (e: Exception) {
188-
Log.e(TAG, "Error processing void payment", e)
189-
throw Exception("VOID_PAYMENT_ERROR: ${e.message ?: "Unknown error"}")
232+
Log.e(TAG, "Error processing refund payment", e)
233+
throw Exception("REFUND_PAYMENT_ERROR: ${e.message ?: "Unknown error"}")
190234
}
191235
}
192236
}

src/PlugpagNitro.nitro.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,51 @@
11
import type { HybridObject } from 'react-native-nitro-modules';
22

3+
// Enum definitions for type safety
4+
export enum PaymentType {
5+
CREDIT = 1,
6+
DEBIT = 2,
7+
VOUCHER = 3,
8+
PIX = 5,
9+
}
10+
11+
export enum InstallmentType {
12+
NO_INSTALLMENT = 1,
13+
SELLER_INSTALLMENT = 2,
14+
BUYER_INSTALLMENT = 3,
15+
}
16+
17+
export enum ErrorCode {
18+
OK = 0,
19+
OPERATION_ABORTED = -1,
20+
AUTHENTICATION_FAILED = -2,
21+
COMMUNICATION_ERROR = -3,
22+
NO_PRINTER_DEVICE = -4,
23+
NO_TRANSACTION_DATA = -5,
24+
}
25+
26+
export enum ActionType {
27+
POST_OPERATION = 1,
28+
PRE_OPERATION = 2,
29+
UPDATE = 3,
30+
}
31+
332
export interface PlugpagInitializationResult {
4-
result: number;
33+
result: ErrorCode;
534
errorCode?: string;
635
errorMessage?: string;
736
}
837

938
export interface PlugpagPaymentData {
1039
amount: number;
11-
type: number;
12-
installmentType: number;
40+
type: PaymentType;
41+
installmentType: InstallmentType;
1342
installments: number;
1443
printReceipt: boolean;
1544
userReference?: string;
1645
}
1746

1847
export interface PlugpagTransactionResult {
19-
result: number;
48+
result: ErrorCode;
2049
errorCode?: string;
2150
message?: string;
2251
transactionCode?: string;
@@ -98,28 +127,28 @@ export interface PlugpagNitro extends HybridObject<{ android: 'kotlin' }> {
98127
/**
99128
* Process a payment transaction
100129
* @param amount Payment amount in cents
101-
* @param type Payment type (1=Credit, 2=Debit, 3=Voucher, 5=PIX)
102-
* @param installmentType Installment type (1=No installment, 2=Seller, 3=Buyer)
130+
* @param type Payment type (PaymentType.CREDIT, PaymentType.DEBIT, etc.)
131+
* @param installmentType Installment type (InstallmentType.NO_INSTALLMENT, etc.)
103132
* @param installments Number of installments
104133
* @param printReceipt Whether to print receipt
105134
* @param userReference Optional user reference
106135
*/
107136
doPayment(
108137
amount: number,
109-
type: number,
110-
installmentType: number,
138+
type: PaymentType,
139+
installmentType: InstallmentType,
111140
installments: number,
112141
printReceipt: boolean,
113142
userReference: string
114143
): Promise<PlugpagTransactionResult>;
115144

116145
/**
117-
* Void/refund a previous payment transaction - optimized with flattened parameters
118-
* @param transactionCode Transaction code to void
119-
* @param transactionId Transaction ID to void
146+
* Refund a previous payment transaction
147+
* @param transactionCode Transaction code to refund
148+
* @param transactionId Transaction ID to refund
120149
* @param printReceipt Whether to print receipt
121150
*/
122-
voidPayment(
151+
refundPayment(
123152
transactionCode: string,
124153
transactionId: string,
125154
printReceipt: boolean

src/index.tsx

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,53 @@ import type {
66
PlugpagAbortResult,
77
PlugpagConstants,
88
} from './PlugpagNitro.nitro';
9+
import { PaymentType, InstallmentType, ErrorCode } from './PlugpagNitro.nitro';
910

10-
const PlugpagNitroModule =
11-
NitroModules.createHybridObject<PlugpagNitro>('PlugpagNitro');
11+
// Re-export enums and types for easy access
12+
export {
13+
PaymentType,
14+
InstallmentType,
15+
ErrorCode,
16+
ActionType,
17+
} from './PlugpagNitro.nitro';
1218

13-
// Export types
1419
export type {
1520
PlugpagInitializationResult,
1621
PlugpagTransactionResult,
1722
PlugpagAbortResult,
1823
PlugpagConstants,
19-
};
24+
PlugpagPaymentData,
25+
PlugpagVoidData,
26+
} from './PlugpagNitro.nitro';
27+
28+
const PlugpagNitroModule =
29+
NitroModules.createHybridObject<PlugpagNitro>('PlugpagNitro');
2030

21-
// Payment options interface
31+
// Payment options interface using enum types
2232
export interface PaymentOptions {
2333
amount: number;
24-
type: number;
25-
installmentType?: number;
34+
type: PaymentType;
35+
installmentType?: InstallmentType;
2636
installments?: number;
2737
printReceipt?: boolean;
2838
userReference?: string;
2939
}
3040

31-
// Payment type constants from PagBank SDK
41+
// Payment type constants (deprecated - use PaymentType enum instead)
42+
/** @deprecated Use PaymentType enum instead */
3243
export const PaymentTypes = {
33-
CREDIT: 1,
34-
DEBIT: 2,
35-
VOUCHER: 3,
36-
PIX: 5,
44+
CREDIT: PaymentType.CREDIT,
45+
DEBIT: PaymentType.DEBIT,
46+
VOUCHER: PaymentType.VOUCHER,
47+
PIX: PaymentType.PIX,
3748
} as const;
3849

39-
// Installment type constants
50+
// Installment type constants (deprecated - use InstallmentType enum instead)
51+
/** @deprecated Use InstallmentType enum instead */
4052
export const InstallmentTypes = {
41-
NO_INSTALLMENT: 1,
42-
SELLER_INSTALLMENT: 2,
43-
BUYER_INSTALLMENT: 3,
53+
NO_INSTALLMENT: InstallmentType.NO_INSTALLMENT,
54+
SELLER_INSTALLMENT: InstallmentType.SELLER_INSTALLMENT,
55+
BUYER_INSTALLMENT: InstallmentType.BUYER_INSTALLMENT,
4456
} as const;
4557

4658
// Simple error handling wrapper
@@ -93,14 +105,14 @@ export async function initializeAndActivatePinPad(
93105
*/
94106
export async function doPayment(options: {
95107
amount: number;
96-
type: number;
97-
installmentType?: number;
108+
type: PaymentType;
109+
installmentType?: InstallmentType;
98110
installments?: number;
99111
printReceipt?: boolean;
100112
userReference?: string;
101113
}): Promise<PlugpagTransactionResult> {
102114
const paymentOptions = {
103-
installmentType: options.installmentType ?? InstallmentTypes.NO_INSTALLMENT,
115+
installmentType: options.installmentType ?? InstallmentType.NO_INSTALLMENT,
104116
installments: options.installments ?? 1,
105117
printReceipt: options.printReceipt ?? true,
106118
userReference: options.userReference ?? `payment-${Date.now()}`,
@@ -120,15 +132,15 @@ export async function doPayment(options: {
120132
}
121133

122134
/**
123-
* Void/refund a previous payment transaction
135+
* Refund a previous payment transaction
124136
*/
125-
export async function voidPayment(options: {
137+
export async function refundPayment(options: {
126138
transactionCode: string;
127139
transactionId: string;
128140
printReceipt?: boolean;
129141
}): Promise<PlugpagTransactionResult> {
130-
return safeModuleCall('voidPayment', () =>
131-
PlugpagNitroModule.voidPayment(
142+
return safeModuleCall('refundPayment', () =>
143+
PlugpagNitroModule.refundPayment(
132144
options.transactionCode,
133145
options.transactionId,
134146
options.printReceipt ?? true
@@ -166,7 +178,7 @@ export async function reprintCustomerReceipt(): Promise<void> {
166178
export function isTransactionSuccessful(
167179
result: PlugpagTransactionResult
168180
): boolean {
169-
return result.result === 0; // PlugPag.RET_OK = 0
181+
return result.result === ErrorCode.OK;
170182
}
171183

172184
/**
@@ -187,49 +199,61 @@ export function getTransactionError(
187199
export const PaymentPresets = {
188200
creditCard: (amount: number, installments: number = 1) => ({
189201
amount,
190-
type: PaymentTypes.CREDIT,
202+
type: PaymentType.CREDIT,
191203
installmentType:
192204
installments > 1
193-
? InstallmentTypes.BUYER_INSTALLMENT
194-
: InstallmentTypes.NO_INSTALLMENT,
205+
? InstallmentType.BUYER_INSTALLMENT
206+
: InstallmentType.NO_INSTALLMENT,
195207
installments,
196208
}),
197209

198210
debitCard: (amount: number) => ({
199211
amount,
200-
type: PaymentTypes.DEBIT,
201-
installmentType: InstallmentTypes.NO_INSTALLMENT,
212+
type: PaymentType.DEBIT,
213+
installmentType: InstallmentType.NO_INSTALLMENT,
202214
installments: 1,
203215
}),
204216

205217
pix: (amount: number) => ({
206218
amount,
207-
type: PaymentTypes.PIX,
208-
installmentType: InstallmentTypes.NO_INSTALLMENT,
219+
type: PaymentType.PIX,
220+
installmentType: InstallmentType.NO_INSTALLMENT,
209221
installments: 1,
210222
}),
211223

212224
voucher: (amount: number) => ({
213225
amount,
214-
type: PaymentTypes.VOUCHER,
215-
installmentType: InstallmentTypes.NO_INSTALLMENT,
226+
type: PaymentType.VOUCHER,
227+
installmentType: InstallmentType.NO_INSTALLMENT,
216228
installments: 1,
217229
}),
218230
} as const;
219231

220232
// Default export
221233
export default {
234+
// Core functions
222235
getConstants,
223236
getTerminalSerialNumber,
224237
initializeAndActivatePinPad,
225238
doPayment,
226-
voidPayment,
239+
refundPayment,
227240
doAbort,
228241
print,
229242
reprintCustomerReceipt,
243+
244+
// Helper functions
230245
isTransactionSuccessful,
231246
getTransactionError,
247+
248+
// Enums (recommended)
249+
PaymentType,
250+
InstallmentType,
251+
ErrorCode,
252+
253+
// Legacy constants (deprecated)
232254
PaymentTypes,
233255
InstallmentTypes,
256+
257+
// Presets
234258
PaymentPresets,
235259
};

0 commit comments

Comments
 (0)