blib/lib/Lib/Pepper/Simple.pm

filter: Statementstmt 24.2% 65 / 268 Branchbran 7.0% 9 / 128 Conditioncond 1.0% 1 / 98 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 65.3% 17 / 26 Podpod 100% 6 / 6 totaltotal 16.9% 98 / 577 SCAR 89.2
CC
110
Cov
15%
doPayment
930.0
new
849.5
cancelPayment
506.0
CRAP 7493.2
↑ Summary blib/lib/Lib/Pepper/Instance.pm »
1 package Lib::Pepper::Simple;
2 #---AUTOPRAGMASTART---
3 5 ▶use v5.42;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp qw[carp croak confess cluck longmess shortmess];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 our $VERSION = 0.5;
10 5 ▶use autodie qw( close );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Array::Contains;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Data::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 #---AUTOPRAGMAEND---
16  
17  
18 5 ▶use Lib::Pepper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Lib::Pepper::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Lib::Pepper::OptionList;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Lib::Pepper::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Package-level state for multi-terminal support
24 # These variables track the library initialization state across all instances
25 5 our $LIBRARY_INITIALIZED = 0; # Boolean: Library initialized in this process?
26 5 our $INSTANCE_COUNT = 0; # Integer: Number of active instances
27 5 our $INIT_LIBRARY_PATH = ''; # String: Library path used for initialization
28 5 our $INIT_CONFIG_XML = undef; # String: Config XML used for initialization
29 5 our $INIT_LICENSE_XML = undef; # String: License XML used for initialization
30 5 our %INSTANCE_ID_COUNTERS = (); # Hash: terminal_type => next_available_id
31  
32 # High-level wrapper for Lib::Pepper providing simple payment terminal operations
33  
34 ▶sub new($proto, %params) {
Subroutine new: called CC=36 SCAR=67.4
Pod documented
35 5 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
36  
37 # Validate required parameters
38 5 ▶ if(!defined $params{terminal_type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"terminal_type"}) 1 4 5
39 1 croak("new: terminal_type parameter is required");
40 }
41 4 ▶ if(!defined $params{terminal_address}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"terminal_address"}) 1 3 4
42 1 croak("new: terminal_address parameter is required");
43 }
44  
45 # Handle config: accept either config_file OR config_xml
46 3 my $configXml;
47 3 ▶ if(defined $params{config_xml}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params{'config_xml'}) { } 2 1 3
elsif (defined $params{'config_file'}) { } 0 1 1
48 2 $configXml = $params{config_xml};
49 } elsif(defined $params{config_file}) {
50 # Load config from file
51 0 ▶ if(!-f $params{config_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $params{"config_file"}) 0 0 0
52 0 croak("new: config_file '$params{config_file}' does not exist");
53 }
54 0 ▶ open(my $fh, '<', $params{config_file}) or croak("new: cannot open config_file '$params{config_file}': $ERRNO");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $params{"config_file"} or croak(qq[new: cannot open config_file '$params{"config_file"}': $Lib::Pepper::Simple::ERRNO]) 0 0 0
55 0 $configXml = do { local $INPUT_RECORD_SEPARATOR = undef; <$fh> };
56 0 close($fh);
57 } else {
58 1 croak("new: either config_xml or config_file parameter is required");
59 }
60  
61 # Handle license: accept either license_file OR license_xml
62 2 my $licenseXml;
63 2 ▶ if(defined $params{license_xml}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params{'license_xml'}) { } 0 2 2
elsif (defined $params{'license_file'}) { } 0 2 2
64 0 $licenseXml = $params{license_xml};
65 } elsif(defined $params{license_file}) {
66 # Load license from file
67 0 ▶ if(!-f $params{license_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $params{"license_file"}) 0 0 0
68 0 croak("new: license_file '$params{license_file}' does not exist");
69 }
70 0 ▶ open(my $fh, '<', $params{license_file}) or croak("new: cannot open license_file '$params{license_file}': $ERRNO");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $params{"license_file"} or croak(qq[new: cannot open license_file '$params{"license_file"}': $Lib::Pepper::Simple::ERRNO]) 0 0 0
71 0 $licenseXml = do { local $INPUT_RECORD_SEPARATOR = undef; <$fh> };
72 0 close($fh);
73 } else {
74 2 croak("new: either license_xml or license_file parameter is required");
75 }
76  
77 # Create object with default values
78 my $self = bless {
79 # Library state
80 initialized => 0,
81 library_path => $params{library_path} // '',
82 config_xml => $configXml,
83 license_xml => $licenseXml,
84  
85 # Instance state
86 instance => undef,
87 terminal_type => $params{terminal_type},
88 terminal_address => $params{terminal_address},
89 configured => 0,
90  
91 # Configuration
92 pos_number => $params{pos_number} // '0001',
93 merchant_password => $params{merchant_password} // '000000',
94 language => $params{language} // PEP_LANGUAGE_ENGLISH,
95 ticket_width => $params{ticket_width} // 40,
96 ticket_printing_mode => $params{ticket_printing_mode} // 0, # Default: POS prints (disables terminal printer)
97 tip_enabled => $params{tip_enabled} // 0, # Default: no tip dialog
98  
99 # Callback handling
100 callback => $params{callback},
101 userdata => $params{userdata} // {},
102  
103 # Logging
104 reph => $params{reph}, # Optional reporting handler with debuglog() method
105  
106 # State tracking
107 0 ▶ connection_open => 0,
Condition0%0/16
$params{"library_path"} // ""
l!l
00
$params{"pos_number"} // "0001"
l!l
00
$params{"merchant_password"} // "000000"
l!l
00
$params{"language"} // 25966
l!l
00
$params{"ticket_width"} // 40
l!l
00
$params{"ticket_printing_mode"} // 0
l!l
00
$params{"tip_enabled"} // 0
l!l
00
$params{"userdata"} // {}
l!l
00
MC/DC0%0/1
$params{"library_path"} // ""
$params{"library_path"}
MC/DC0%0/1
$params{"pos_number"} // "0001"
$params{"pos_number"}
MC/DC0%0/1
$params{"merchant_password"} // "000000"
$params{"merchant_password"}
MC/DC0%0/1
$params{"language"} // 25966
$params{"language"}
MC/DC0%0/1
$params{"ticket_width"} // 40
$params{"ticket_width"}
MC/DC0%0/1
$params{"ticket_printing_mode"} // 0
$params{"ticket_printing_mode"}
MC/DC0%0/1
$params{"tip_enabled"} // 0
$params{"tip_enabled"}
MC/DC0%0/1
$params{"userdata"} // {}
$params{"userdata"}
Truth table0%0/2
A
A= $params{"library_path"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"pos_number"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"merchant_password"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"language"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"ticket_width"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"ticket_printing_mode"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"tip_enabled"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"userdata"}
Aresult
11
00
108 last_error => undef,
109 last_transaction => undef,
110  
111 # Multi-instance support: Track if this instance was counted
112 _instance_counted => 0, # Set to 1 only after successful initialization
113 }, $class;
114  
115 # Set config_byte based on ticket_printing_mode if not explicitly provided
116 # Mode 1 (EFT prints) = PEP_CONFIG_BYTE_NORMAL (normal operation)
117 # Mode 0 (POS prints) = PEP_CONFIG_BYTE_DISABLE_PRINTER (disable terminal printer)
118 # Other modes = PEP_CONFIG_BYTE_NORMAL (normal operation)
119 0 ▶ if(exists $params{config_byte}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'config_byte'}) { } 0 0 0
120 # User explicitly provided config_byte, use it
121 0 $self->{config_byte} = $params{config_byte};
122 } else {
123 # Auto-calculate based on ticket_printing_mode
124 0 ▶ $self->{config_byte} = ($self->{ticket_printing_mode} == 0)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ticket_printing_mode'} == 0 ? : 0 0 0
125 ? PEP_CONFIG_BYTE_DISABLE_PRINTER
126 : PEP_CONFIG_BYTE_NORMAL;
127 }
128  
129 # Handle CARDTYPES_AUTODETECT placeholder if present
130 0 ▶ if($self->{config_xml} =~ /CARDTYPES_AUTODETECT/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config_xml"} =~ /CARDTYPES_AUTODETECT/u) 0 0 0
131 0 my $cardtypesPath = Lib::Pepper->cardtypesFile();
132 0 ▶ if(!defined $cardtypesPath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cardtypesPath) 0 0 0
133 0 croak("Could not find installed pepper_cardtypes.xml file");
134 }
135 0 $self->{config_xml} =~ s/CARDTYPES_AUTODETECT/$cardtypesPath/g;
136 }
137  
138 # Attempt full initialization
139 0 my $success = 0;
140 0 my $needsInitialization = !$LIBRARY_INITIALIZED; # Declared before eval for error handling
141  
142 0 eval {
143  
144 # ========================================
145 # MULTI-INSTANCE SUPPORT: Library initialization
146 # ========================================
147 # Initialize Pepper library only if not already initialized
148 # This allows multiple terminals to share the same library instance
149  
150 0 ▶ if($needsInitialization) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needsInitialization) { } 0 0 0
151 # First instance - initialize library
152 Lib::Pepper->initialize(
153 library_path => $self->{library_path},
154 config_xml => $self->{config_xml},
155 license_xml => $self->{license_xml},
156 0 );
157  
158 # Store initialization parameters for validation of subsequent instances
159 0 $INIT_LIBRARY_PATH = $self->{library_path};
160 0 $INIT_CONFIG_XML = $self->{config_xml};
161 0 $INIT_LICENSE_XML = $self->{license_xml};
162 0 $LIBRARY_INITIALIZED = 1;
163  
164 0 $self->{initialized} = 1;
165 } else {
166 # Library already initialized - validate config compatibility
167 0 ▶ if(!_validate_config($self->{library_path}, $self->{config_xml}, $self->{license_xml})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_validate_config($self->{"library_path"}, $self->{"config_xml"}, $self->{"license_xml"})) 0 0 0
168 0 croak("Configuration mismatch: Lib::Pepper library already initialized with different config. " .
169 "All Lib::Pepper::Simple instances in the same process must use identical " .
170 "library_path, config_xml, and license_xml parameters.");
171 }
172 0 $self->{initialized} = 1; # Library is already initialized
173 }
174  
175 # ========================================
176 # MULTI-INSTANCE SUPPORT: Instance ID allocation
177 # ========================================
178 # Allocate instance_id automatically if not provided by user
179 # Instance IDs are managed per terminal_type
180 0 my $instanceId = $params{instance_id};
181 0 ▶ if(!defined $instanceId) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $instanceId) 0 0 0
182 0 $instanceId = _allocate_instance_id($self->{terminal_type});
183 }
184 # Store instance_id in object for status reporting
185 0 $self->{instance_id} = $instanceId;
186  
187 # Create instance
188 $self->{instance} = Lib::Pepper::Instance->new(
189 terminal_type => $self->{terminal_type},
190 instance_id => $instanceId,
191 reph => $self->{reph}, # Pass reporting handler down
192 0 );
193  
194 # Configure instance
195 my $configResult = $self->{instance}->configure(
196 callback => $self->{callback} // \&_defaultCallback,
197 options => {
198 sHostName => $self->{terminal_address},
199 iLanguageValue => $self->{language},
200 sPosIdentificationString => $self->{pos_number},
201 iTicketWidthValue => $self->{ticket_width},
202 iConfigByteValue => $self->{config_byte},
203 sMerchantPasswordString => $self->{merchant_password},
204 iTicketPrintingModeValue => $self->{ticket_printing_mode},
205 },
206 userdata => $self->{userdata},
207 0 ▶ );
Condition0%0/2
$self->{"callback"} // \&Lib::Pepper::Simple::_defaultCallback
l!l
00
MC/DC0%0/1
$self->{"callback"} // \&Lib::Pepper::Simple::_defaultCallback
$self->{"callback"}
Truth table0%0/2
A
A= $self->{"callback"}
Aresult
11
00
208 0 $self->{configured} = 1;
209  
210 # Check and handle recovery flag automatically
211 0 ▶ if(defined $configResult) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configResult) 0 0 0
212 0 my $configData = $configResult->toHashref();
213 0 ▶ my $recoveryFlag = $configData->{iRecoveryFlag} // 0;
Condition0%0/2
$configData->{"iRecoveryFlag"} // 0
l!l
00
MC/DC0%0/1
$configData->{"iRecoveryFlag"} // 0
$configData->{"iRecoveryFlag"}
Truth table0%0/2
A
A= $configData->{"iRecoveryFlag"}
Aresult
11
00
214  
215 0 ▶ if($recoveryFlag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recoveryFlag) 0 0 0
216 # Perform recovery operation automatically
217 0 my $recoveryOptions = Lib::Pepper::OptionList->new();
218  
219 0 my ($op1, $out1) = $self->{instance}->prepareOperation(PEP_OPERATION_RECOVERY, $recoveryOptions);
220 0 my ($op2, $out2) = $self->{instance}->startOperation(PEP_OPERATION_RECOVERY, $recoveryOptions);
221 0 my ($op3, $out3) = $self->{instance}->executeOperation(PEP_OPERATION_RECOVERY, $recoveryOptions);
222 0 my ($op4, $out4) = $self->{instance}->finalizeOperation(PEP_OPERATION_RECOVERY, $recoveryOptions);
223  
224 0 my $recoveryStatus = $self->{instance}->operationStatus($op4, 1);
225 0 ▶ if(!$recoveryStatus) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($recoveryStatus) 0 0 0
226 0 croak("Recovery operation did not complete successfully");
227 }
228 }
229 }
230  
231 # Open connection (required for ZVT terminals)
232 my $openResult = $self->{instance}->openConnection(
233 options => {
234 0 ▶ sOperatorIdentificationString => $params{operator_id} // 'OPERATOR01',
Condition0%0/2
$params{"operator_id"} // "OPERATOR01"
l!l
00
MC/DC0%0/1
$params{"operator_id"} // "OPERATOR01"
$params{"operator_id"}
Truth table0%0/2
A
A= $params{"operator_id"}
Aresult
11
00
235 }
236 );
237  
238 # Wait for OPEN to complete if pending
239 0 ▶ if(!$openResult->{status}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($openResult->{"status"}) 0 0 0
240 0 my $opHandle = $openResult->{operation_handle};
241 0 my $waitStatus = $self->{instance}->operationStatus($opHandle, 1);
242 0 ▶ if(!$waitStatus) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($waitStatus) 0 0 0
243 0 croak("OPEN operation did not complete successfully");
244 }
245 }
246 0 $self->{connection_open} = 1;
247  
248 0 $success = 1;
249 };
250  
251 0 ▶ if(!$success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
252 0 my $error = $EVAL_ERROR;
253  
254 # ========================================
255 # MULTI-INSTANCE SUPPORT: Cleanup on failure
256 # ========================================
257 # Note: We do NOT finalize the library on failure.
258 # The library stays initialized, allowing retry attempts.
259 # Instance counter was NOT incremented yet, so nothing to decrement.
260 # This is consistent with our never-finalize design.
261  
262 0 croak("Failed to initialize Lib::Pepper::Simple: $error");
263 }
264  
265 # ========================================
266 # MULTI-INSTANCE SUPPORT: Increment instance counter
267 # ========================================
268 # Only increment after successful initialization
269 # This ensures the counter stays accurate even if initialization fails
270 0 $INSTANCE_COUNT++;
271 0 $self->{_instance_counted} = 1; # Mark that this instance was counted
272  
273 0 return $self;
274 }
275  
276 ▶sub checkStatus($self) {
Subroutine checkStatus: not called CC=3 SCAR=24.8
Pod documented
277 my $status = {
278 # Instance-level status
279 library_initialized => $self->{initialized},
280 instance_configured => $self->{configured},
281 connection_open => $self->{connection_open},
282 terminal_type => $self->{terminal_type},
283 terminal_address => $self->{terminal_address},
284 instance_id => $self->{instance_id},
285 ready_for_transactions => 0,
286 last_error => $self->{last_error},
287  
288 # Process-level status (multi-terminal support)
289 0 process_instance_count => $INSTANCE_COUNT,
290 process_library_initialized => $LIBRARY_INITIALIZED,
291 };
292  
293 # Ready if all critical components are initialized
294 $status->{ready_for_transactions} = (
295 $status->{library_initialized} &&
296 $status->{instance_configured} &&
297 $status->{connection_open}
298 0 ▶ );
Condition0%0/6
$status->{'library_initialized'} && $status->{'instance_configured'} && $status->{'connection_open'}
!ll&&!rl&&r
000
$status->{"library_initialized"} and $status->{"instance_configured"}
!ll&&!rl&&r
000
MC/DC0%0/3
$status->{'library_initialized'} && $status->{'instance_configured'} && $status->{'connection_open'}
$status->{"library_initialized"} $status->{"instance_configured"} $status->{'connection_open'}
Truth table0%0/4
A and B && C
A= $status->{"library_initialized"}B= $status->{"instance_configured"}C= $status->{'connection_open'}
ABCresult
0XX0
10X0
1100
1111
299  
300 0 return $status;
301 }
302  
303 ▶sub doPayment($self, $amount, %options) {
Subroutine doPayment: not called CC=30 SCAR=68.4
Pod documented
304 # Validate state
305 0 ▶ if(!$self->{connection_open}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"connection_open"}) 0 0 0
306 0 croak("doPayment: connection not open");
307 }
308 0 ▶ if(!$self->{configured}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"configured"}) 0 0 0
309 0 croak("doPayment: instance not configured");
310 }
311  
312 # Validate amount
313 0 ▶ if(!defined $amount || $amount !~ /^\d+$/ || $amount <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $amount && $amount =~ /^\d+$/u or $amount <= 0) 0 0 0
Condition0%0/6
not defined $amount && $amount =~ /^\d+$/u or $amount <= 0
l!l&&r!l&&!r
000
defined $amount && $amount =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $amount && $amount =~ /^\d+$/u or $amount <= 0
defined $amount $amount =~ /^\d+$/u $amount <= 0
Truth table0%0/4
not(A && B) or C
A= defined $amountB= $amount =~ /^\d+$/uC= $amount <= 0
ABCresult
0XX1
10X1
1111
1100
314 0 croak("doPayment: amount must be a positive integer (cents)");
315 }
316  
317 # Build transaction parameters
318 0 ▶ my $transactionType = $options{transaction_type} // PEP_TRANSACTION_TYPE_GOODS_PAYMENT;
Condition0%0/2
$options{"transaction_type"} // 11
l!l
00
MC/DC0%0/1
$options{"transaction_type"} // 11
$options{"transaction_type"}
Truth table0%0/2
A
A= $options{"transaction_type"}
Aresult
11
00
319  
320 # Convert string transaction types to constants
321 0 ▶ if(defined $transactionType && $transactionType !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $transactionType and not $transactionType =~ /^\d+$/u) 0 0 0
Condition0%0/3
defined $transactionType and not $transactionType =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $transactionType and not $transactionType =~ /^\d+$/u
defined $transactionType not $transactionType =~ /^\d+$/u
Truth table0%0/3
A and not(B)
A= defined $transactionTypeB= not $transactionType =~ /^\d+$/u
ABresult
0X0
100
111
322 0 ▶ if($transactionType eq 'goods') {
Branch: 1 branch
BranchTrueFalseTotal
if ($transactionType eq 'goods') { } 0 0 0
323 0 $transactionType = PEP_TRANSACTION_TYPE_GOODS_PAYMENT;
324 } else {
325 0 croak("doPayment: invalid transaction_type '$transactionType' (use 'goods' or numeric constant)");
326 }
327 }
328  
329 0 my $currency = $options{currency};
330  
331 # Build additional options for the transaction
332 0 ▶ my $txnOptions = $options{options} // {};
Condition0%0/2
$options{"options"} // {}
l!l
00
MC/DC0%0/1
$options{"options"} // {}
$options{"options"}
Truth table0%0/2
A
A= $options{"options"}
Aresult
11
00
333  
334 # Tip support via Pepper API:
335 # - Transaction type 13 (GoodsPaymentWithTip): NOT supported by GlobalPayments ZVT (returns -1402)
336 # - iServiceByteValue bit 3 (tippable): Transaction completes but tip dialog depends on terminal config
337 # - The tip prompt/dialog must be enabled at the TERMINAL level by your payment processor
338 # - If you need tips, either:
339 # 1. Contact GlobalPayments to enable tip prompting on your terminal
340 # 2. Collect tip amount in your POS and pass it via doPayment(..., options => {iTipAmount => $tip})
341 0 ▶ if($self->{tip_enabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tip_enabled"}) 0 0 0
342 0 ▶ if(!exists $txnOptions->{iServiceByteValue}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $txnOptions->{'iServiceByteValue'}) { } 0 0 0
343 0 $txnOptions->{iServiceByteValue} = 8; # Bit 3 = tippable
344 } else {
345 0 $txnOptions->{iServiceByteValue} |= 8;
346 }
347 }
348  
349 # Execute transaction
350 0 my $result;
351 0 my $success = 0;
352 0 eval {
353 $result = $self->{instance}->transaction(
354 amount => $amount,
355 transaction_type => $transactionType,
356 (defined $currency ? (currency => $currency) : ()),
357 0 ▶ (keys %{$txnOptions} ? (options => $txnOptions) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $currency ? : 0 0 0
keys %{$txnOptions;} ? : 0 0 0
358 );
359 0 $success = 1;
360 };
361  
362 0 ▶ if(!$success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
363 0 $self->{last_error} = $EVAL_ERROR;
364 return {
365 0 success => 0,
366 authorized => 0,
367 error => $EVAL_ERROR,
368 };
369 }
370  
371 # Parse result output
372 0 my $outputData = {};
373 0 ▶ if(defined $result->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result->{"output"}) 0 0 0
374 0 $outputData = $result->{output}->toHashref();
375 }
376  
377 # Extract transaction result - CRITICAL: check iTransactionResultValue, not iFunctionResultValue!
378 0 ▶ my $transactionResult = $outputData->{iTransactionResultValue} // -999;
Condition0%0/2
$outputData->{"iTransactionResultValue"} // -999
l!l
00
MC/DC0%0/1
$outputData->{"iTransactionResultValue"} // -999
$outputData->{"iTransactionResultValue"}
Truth table0%0/2
A
A= $outputData->{"iTransactionResultValue"}
Aresult
11
00
379 0 ▶ my $transactionText = $outputData->{sTransactionText} || '';
Condition0%0/2
$outputData->{'sTransactionText'} || ''
l!l
00
MC/DC0%0/1
$outputData->{'sTransactionText'} || ''
$outputData->{'sTransactionText'}
Truth table0%0/2
A
A= $outputData->{'sTransactionText'}
Aresult
11
00
380  
381 # Build response
382 my $response = {
383 success => $result->{status} ? 1 : 0,
384 authorized => ($transactionResult == 0) ? 1 : 0,
385 amount_charged => ($transactionResult == 0) ? $amount : 0,
386 transaction_result => $transactionResult,
387 transaction_text => $transactionText,
388 trace_number => $outputData->{sTraceNumberString} || undef,
389 authorization_code => $outputData->{sAuthorizationNumberString} || undef,
390 reference_number => $outputData->{sTransactionReferenceNumberString} || undef,
391 terminal_id => $outputData->{sTerminalIdentificationString} || undef,
392 card_type => $outputData->{sCardNameString} || undef,
393 card_number => $outputData->{sCardNumberString} || undef,
394 transaction_date => $outputData->{sTransactionDate} || undef,
395 transaction_time => $outputData->{sTransactionTime} || undef,
396 0 ▶ raw_output => $outputData,
Branch: 3 branches
BranchTrueFalseTotal
$result->{'status'} ? : 0 0 0
$transactionResult == 0 ? : 0 0 0
$transactionResult == 0 ? : 0 0 0
Condition0%0/16
$outputData->{'sTraceNumberString'} || undef
l!l
00
$outputData->{'sAuthorizationNumberString'} || undef
l!l
00
$outputData->{'sTransactionReferenceNumberString'} || undef
l!l
00
$outputData->{'sTerminalIdentificationString'} || undef
l!l
00
$outputData->{'sCardNameString'} || undef
l!l
00
$outputData->{'sCardNumberString'} || undef
l!l
00
$outputData->{'sTransactionDate'} || undef
l!l
00
$outputData->{'sTransactionTime'} || undef
l!l
00
MC/DC0%0/1
$outputData->{'sTraceNumberString'} || undef
$outputData->{'sTraceNumberString'}
MC/DC0%0/1
$outputData->{'sAuthorizationNumberString'} || undef
$outputData->{'sAuthorizationNumberString'}
MC/DC0%0/1
$outputData->{'sTransactionReferenceNumberString'} || undef
$outputData->{'sTransactionReferenceNumberString'}
MC/DC0%0/1
$outputData->{'sTerminalIdentificationString'} || undef
$outputData->{'sTerminalIdentificationString'}
MC/DC0%0/1
$outputData->{'sCardNameString'} || undef
$outputData->{'sCardNameString'}
MC/DC0%0/1
$outputData->{'sCardNumberString'} || undef
$outputData->{'sCardNumberString'}
MC/DC0%0/1
$outputData->{'sTransactionDate'} || undef
$outputData->{'sTransactionDate'}
MC/DC0%0/1
$outputData->{'sTransactionTime'} || undef
$outputData->{'sTransactionTime'}
Truth table0%0/2
A
A= $outputData->{'sTraceNumberString'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sAuthorizationNumberString'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sTransactionReferenceNumberString'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sTerminalIdentificationString'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sCardNameString'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sCardNumberString'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sTransactionDate'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sTransactionTime'}
Aresult
11
00
397 };
398  
399 # Store last transaction details
400 0 $self->{last_transaction} = $response;
401 0 $self->{last_error} = undef;
402  
403 0 return $response;
404 }
405  
406 ▶sub cancelPayment($self, $traceNumber, $amount, %options) {
Subroutine cancelPayment: not called CC=22 SCAR=62.3
Pod documented
407 # Validate state
408 0 ▶ if(!$self->{connection_open}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"connection_open"}) 0 0 0
409 0 croak("cancelPayment: connection not open");
410 }
411 0 ▶ if(!$self->{configured}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"configured"}) 0 0 0
412 0 croak("cancelPayment: instance not configured");
413 }
414  
415 # Validate parameters
416 0 ▶ if(!defined $traceNumber) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $traceNumber) 0 0 0
417 0 croak("cancelPayment: trace_number parameter is required");
418 }
419 0 ▶ if(!defined $amount || $amount !~ /^\d+$/ || $amount <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $amount && $amount =~ /^\d+$/u or $amount <= 0) 0 0 0
Condition0%0/6
not defined $amount && $amount =~ /^\d+$/u or $amount <= 0
l!l&&r!l&&!r
000
defined $amount && $amount =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $amount && $amount =~ /^\d+$/u or $amount <= 0
defined $amount $amount =~ /^\d+$/u $amount <= 0
Truth table0%0/4
not(A && B) or C
A= defined $amountB= $amount =~ /^\d+$/uC= $amount <= 0
ABCresult
0XX1
10X1
1111
1100
420 0 croak("cancelPayment: amount must be a positive integer (cents)");
421 }
422 0 ▶ if(!exists $options{reference_number} || !defined $options{reference_number}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"reference_number"} and defined $options{"reference_number"}) 0 0 0
Condition0%0/3
exists $options{"reference_number"} and defined $options{"reference_number"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options{"reference_number"} and defined $options{"reference_number"}
exists $options{"reference_number"} defined $options{"reference_number"}
Truth table0%0/3
A and B
A= exists $options{"reference_number"}B= defined $options{"reference_number"}
ABresult
0X0
100
111
423 0 croak("cancelPayment: reference_number parameter is required for card-not-present refunds");
424 }
425  
426 0 my $response = {
427 success => 0,
428 trace_number => undef,
429 amount_refunded => 0,
430 transaction_text => '',
431 raw_output => {},
432 };
433  
434 # Perform VOID (Transaction Type 12 - VoidGoodsPayment)
435 # Using sTransactionReferenceNumberString for referenced void
436 #
437 # IMPORTANT: Despite being called "VOID", this works AFTER settlement when using
438 # the reference number. This is the correct ZVT method for card-not-present refunds.
439 # Transaction Type 41 (Credit) would require card swipe even with reference number.
440 #
441 # This discovery is not documented in Pepper docs but confirmed working on GP PAY
442 # terminals with Generic ZVT protocol.
443 0 my $refundResult;
444 0 my $refundSuccess = 0;
445  
446 0 eval {
447 # Build options for referenced void (no card required)
448 my $refundOptions = {
449 sTransactionReferenceNumberString => $options{reference_number},
450 0 };
451  
452 0 ▶ if($ENV{DEBUG_PEPPER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG_PEPPER"}) 0 0 0
453 0 $self->_log("=== VOID/REFUND Parameters ===");
454 0 $self->_log("Transaction Type: VoidGoodsPayment (12)");
455 0 $self->_log("Amount: $amount");
456 0 for my $key (sort keys %{$refundOptions}) {
457 0 $self->_log("$key: $refundOptions->{$key}");
458 }
459 }
460  
461 $refundResult = $self->{instance}->transaction(
462 0 amount => $amount,
463 transaction_type => PEP_TRANSACTION_TYPE_VOID_GOODS_PAYMENT,
464 options => $refundOptions,
465 );
466 0 $refundSuccess = 1;
467 };
468  
469 0 ▶ if(!$refundSuccess) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($refundSuccess) 0 0 0
470 0 $self->{last_error} = "REFUND failed: $EVAL_ERROR";
471 0 return $response;
472 }
473  
474 0 ▶ if($ENV{DEBUG_PEPPER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG_PEPPER"}) 0 0 0
475 0 $self->_log("=== REFUND Result Check ===");
476 0 $self->_log("refundSuccess = $refundSuccess");
477 0 ▶ $self->_log("refundResult defined = " . (defined $refundResult ? 'YES' : 'NO'));
Branch: 1 branch
BranchTrueFalseTotal
defined $refundResult ? : 0 0 0
478 0 ▶ if(defined $refundResult) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refundResult) 0 0 0
479 0 ▶ $self->_log("refundResult->{output} defined = " . (defined $refundResult->{output} ? 'YES' : 'NO'));
Branch: 1 branch
BranchTrueFalseTotal
defined $refundResult->{'output'} ? : 0 0 0
480 }
481 }
482  
483 0 ▶ if(defined $refundResult->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refundResult->{"output"}) 0 0 0
484 0 my $refundData = $refundResult->{output}->toHashref();
485 0 ▶ my $refundTransResult = $refundData->{iTransactionResultValue} // -999;
Condition0%0/2
$refundData->{"iTransactionResultValue"} // -999
l!l
00
MC/DC0%0/1
$refundData->{"iTransactionResultValue"} // -999
$refundData->{"iTransactionResultValue"}
Truth table0%0/2
A
A= $refundData->{"iTransactionResultValue"}
Aresult
11
00
486 0 ▶ my $refundTransText = $refundData->{sTransactionText} || '';
Condition0%0/2
$refundData->{'sTransactionText'} || ''
l!l
00
MC/DC0%0/1
$refundData->{'sTransactionText'} || ''
$refundData->{'sTransactionText'}
Truth table0%0/2
A
A= $refundData->{'sTransactionText'}
Aresult
11
00
487  
488 0 ▶ if($ENV{DEBUG_PEPPER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG_PEPPER"}) 0 0 0
489 0 $self->_log("iTransactionResultValue: $refundTransResult");
490 0 $self->_log("sTransactionText: '$refundTransText'");
491 }
492  
493 0 $response->{transaction_text} = $refundTransText;
494 0 $response->{raw_output} = $refundData;
495  
496 0 ▶ if($refundTransResult == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refundTransResult == 0) { } 0 0 0
497 # REFUND succeeded!
498 0 $response->{success} = 1;
499 0 ▶ $response->{trace_number} = $refundData->{sTraceNumberString} || undef;
Condition0%0/2
$refundData->{'sTraceNumberString'} || undef
l!l
00
MC/DC0%0/1
$refundData->{'sTraceNumberString'} || undef
$refundData->{'sTraceNumberString'}
Truth table0%0/2
A
A= $refundData->{'sTraceNumberString'}
Aresult
11
00
500 0 $response->{amount_refunded} = $amount;
501  
502 0 $self->{last_error} = undef;
503 0 return $response;
504 } else {
505 # REFUND failed
506 0 $self->{last_error} = "REFUND failed: $refundTransText";
507 0 return $response;
508 }
509 }
510  
511 0 $self->{last_error} = "REFUND failed: no output data";
512 0 return $response;
513 }
514  
515 ▶sub endOfDay($self, %options) {
Subroutine endOfDay: not called CC=14 SCAR=53.5
Pod documented
516 # Validate state
517 0 ▶ if(!$self->{connection_open}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"connection_open"}) 0 0 0
518 0 croak("endOfDay: connection not open");
519 }
520 0 ▶ if(!$self->{configured}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"configured"}) 0 0 0
521 0 croak("endOfDay: instance not configured");
522 }
523  
524 # Execute settlement operation
525 0 my $result;
526 0 my $success = 0;
527 0 eval {
528 $result = $self->{instance}->settlement(
529 options => $options{options} // {},
530 0 ▶ );
Condition0%0/2
$options{"options"} // {}
l!l
00
MC/DC0%0/1
$options{"options"} // {}
$options{"options"}
Truth table0%0/2
A
A= $options{"options"}
Aresult
11
00
531 0 $success = 1;
532 };
533  
534 0 ▶ if(!$success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
535 0 $self->{last_error} = $EVAL_ERROR;
536 return {
537 0 success => 0,
538 error => $EVAL_ERROR,
539 };
540 }
541  
542 # Parse settlement output
543 0 my $outputData = {};
544 0 ▶ if(defined $result->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result->{"output"}) 0 0 0
545 0 $outputData = $result->{output}->toHashref();
546 }
547  
548 # Check settlement result (use iFunctionResultValue for settlement)
549 0 ▶ my $functionResult = $outputData->{iFunctionResultValue} // -999;
Condition0%0/2
$outputData->{"iFunctionResultValue"} // -999
l!l
00
MC/DC0%0/1
$outputData->{"iFunctionResultValue"} // -999
$outputData->{"iFunctionResultValue"}
Truth table0%0/2
A
A= $outputData->{"iFunctionResultValue"}
Aresult
11
00
550 0 ▶ my $functionText = $outputData->{sFunctionText} || '';
Condition0%0/2
$outputData->{'sFunctionText'} || ''
l!l
00
MC/DC0%0/1
$outputData->{'sFunctionText'} || ''
$outputData->{'sFunctionText'}
Truth table0%0/2
A
A= $outputData->{'sFunctionText'}
Aresult
11
00
551  
552 # Build response
553 my $response = {
554 success => ($functionResult == 0) ? 1 : 0,
555 function_result => $functionResult,
556 function_text => $functionText,
557 transaction_count => $outputData->{iNumberOfTransactions} || 0,
558 total_amount => $outputData->{iTotalAmount} || 0,
559 settlement_date => $outputData->{sSettlementDate} || undef,
560 settlement_time => $outputData->{sSettlementTime} || undef,
561 0 ▶ raw_output => $outputData,
Branch: 1 branch
BranchTrueFalseTotal
$functionResult == 0 ? : 0 0 0
Condition0%0/8
$outputData->{'iNumberOfTransactions'} || 0
l!l
00
$outputData->{'iTotalAmount'} || 0
l!l
00
$outputData->{'sSettlementDate'} || undef
l!l
00
$outputData->{'sSettlementTime'} || undef
l!l
00
MC/DC0%0/1
$outputData->{'iNumberOfTransactions'} || 0
$outputData->{'iNumberOfTransactions'}
MC/DC0%0/1
$outputData->{'iTotalAmount'} || 0
$outputData->{'iTotalAmount'}
MC/DC0%0/1
$outputData->{'sSettlementDate'} || undef
$outputData->{'sSettlementDate'}
MC/DC0%0/1
$outputData->{'sSettlementTime'} || undef
$outputData->{'sSettlementTime'}
Truth table0%0/2
A
A= $outputData->{'iNumberOfTransactions'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'iTotalAmount'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sSettlementDate'}
Aresult
11
00
Truth table0%0/2
A
A= $outputData->{'sSettlementTime'}
Aresult
11
00
562 };
563  
564 0 ▶ $self->{last_error} = ($functionResult == 0) ? undef : $functionText;
Branch: 1 branch
BranchTrueFalseTotal
$functionResult == 0 ? : 0 0 0
565  
566 0 return $response;
567 }
568  
569 # Allocate a unique instance_id for the given terminal_type
570 # Instance IDs are managed per terminal type (e.g., Generic ZVT terminals get IDs 1, 2, 3, ...)
571 ▶sub _allocate_instance_id($terminal_type) {
Subroutine _allocate_instance_id: not called CC=2 SCAR=17.9
572 # Initialize counter for this terminal type if not exists
573 0 ▶ $INSTANCE_ID_COUNTERS{$terminal_type} //= 1;
Condition0%0/2
$Lib::Pepper::Simple::INSTANCE_ID_COUNTERS{$terminal_type} //= 1
l!l
00
MC/DC0%0/1
$Lib::Pepper::Simple::INSTANCE_ID_COUNTERS{$terminal_type} //= 1
$Lib::Pepper::Simple::INSTANCE_ID_COUNTERS{$terminal_type}
Truth table0%0/2
A
A= $Lib::Pepper::Simple::INSTANCE_ID_COUNTERS{$terminal_type}
Aresult
11
00
574  
575 # Get current ID and increment for next time
576 0 my $id = $INSTANCE_ID_COUNTERS{$terminal_type};
577 0 $INSTANCE_ID_COUNTERS{$terminal_type}++;
578  
579 0 return $id;
580 }
581  
582 # Validate that library initialization parameters match the first instance
583 # Returns 1 if valid, 0 if mismatch detected
584 ▶sub _validate_config($library_path, $config_xml, $license_xml) {
Subroutine _validate_config: not called CC=5 SCAR=34.0
585 # If library not initialized yet, any config is valid (this is first instance)
586 0 ▶ return 1 if !$LIBRARY_INITIALIZED;
Branch: 1 branch
BranchTrueFalseTotal
unless $Lib::Pepper::Simple::LIBRARY_INITIALIZED 0 0 0
587  
588 # Validate library_path matches
589 0 ▶ if($library_path ne $INIT_LIBRARY_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($library_path ne $Lib::Pepper::Simple::INIT_LIBRARY_PATH) 0 0 0
590 0 return 0;
591 }
592  
593 # Validate config_xml matches (strict - must be identical)
594 0 ▶ if($config_xml ne $INIT_CONFIG_XML) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_xml ne $Lib::Pepper::Simple::INIT_CONFIG_XML) 0 0 0
595 0 return 0;
596 }
597  
598 # Validate license_xml matches
599 0 ▶ if($license_xml ne $INIT_LICENSE_XML) {
Branch: 1 branch
BranchTrueFalseTotal
if ($license_xml ne $Lib::Pepper::Simple::INIT_LICENSE_XML) 0 0 0
600 0 return 0;
601 }
602  
603 0 return 1;
604 }
605  
606 # Class method to check process-wide library status
607 # Returns hashref with library state information
608 ▶sub library_status($class) {
Subroutine library_status: called CC=1 SCAR=0.0
Pod documented
609 return {
610 1 initialized => $LIBRARY_INITIALIZED,
611 instance_count => $INSTANCE_COUNT,
612 library_path => $INIT_LIBRARY_PATH,
613 instance_ids => { %INSTANCE_ID_COUNTERS },
614 };
615 }
616  
617 ▶sub DESTROY($self) {
Subroutine DESTROY: not called CC=3 SCAR=24.8
618 # Cleanup instance resources
619 0 ▶ if($self->{instance}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"instance"}) 0 0 0
620 0 eval {
621 # Connection cleanup handled by Instance destructor
622 0 undef $self->{instance};
623 };
624 }
625  
626 # ========================================
627 # MULTI-INSTANCE SUPPORT: Reference counting
628 # ========================================
629 # CRITICAL: Only decrement if this instance was actually counted!
630 # If constructor failed before incrementing, don't decrement
631 0 ▶ return unless $self->{_instance_counted};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_instance_counted"} 0 0 0
632  
633 # Decrement the instance counter
634 0 $INSTANCE_COUNT--;
635  
636 # ========================================
637 # DESIGN DECISION: Never finalize the library
638 # ========================================
639 # The Pepper C library has a critical limitation: once pepFinalize() is called,
640 # pepInitialize() cannot be called again in the same process (returns error -103).
641 #
642 # SOLUTION: We never call pepFinalize(), keeping the library loaded in memory.
643 # This allows creating new instances even after all previous instances are destroyed.
644 #
645 # Trade-offs:
646 # PRO: Can create instances → destroy all → create new instances ✓
647 # PRO: No -103 errors, can run unlimited test iterations ✓
648 # PRO: Simpler lifecycle management ✓
649 # CON: Library stays in memory until process exit (~few MB)
650 # CON: Cannot reset library state without restarting process
651 #
652 # Note: Instance-level resources (connections, handles) are still properly cleaned up.
653 # Only the library initialization state persists.
654  
655 0 return;
656 }
657  
658 # Internal logging helper - uses reph if available, falls back to STDERR
659 ▶sub _log($self, @parts) {
Subroutine _log: not called CC=3 SCAR=24.8
660 0 ▶ if($self->{reph} && $self->{reph}->can('debuglog')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'reph'} and $self->{'reph'}->can('debuglog')) { } 0 0 0
Condition0%0/3
$self->{"reph"} and $self->{"reph"}->can("debuglog")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"reph"} and $self->{"reph"}->can("debuglog")
$self->{"reph"} $self->{"reph"}->can("debuglog")
Truth table0%0/3
A and B
A= $self->{"reph"}B= $self->{"reph"}->can("debuglog")
ABresult
0X0
100
111
661 0 $self->{reph}->debuglog(@parts);
662 } else {
663 # Fallback to STDERR if no reph provided
664 0 print STDERR join('', @parts), "\n";
665 }
666 0 return;
667 }
668  
669 # Default callback if user doesn't provide one
670 sub _defaultCallback {
671 # Silent default callback - user can provide their own for logging/debugging
672 0 ▶ return;
Subroutine _defaultCallback: not called CC=1 SCAR=6.9
673 }
674  
675 5 1;
676 __END__
↑ Summary blib/lib/Lib/Pepper/Instance.pm »