/
TestPayment.pm
365 lines (275 loc) · 9.11 KB
/
TestPayment.pm
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# Vend::Payment::TestPayment - Interchange payment test module
#
# Copyright (C) 2002-2016 Interchange Development Group
# Copyright (C) 2002 Cursor Software Limited.
# All Rights Reserved.
#
# Author: Kevin Walsh <kevin@cursor.biz>
# Based on original code by Mike Heins <mheins@perusion.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
package Vend::Payment::TestPayment;
=head1 NAME
Vend::Payment::TestPayment - Interchange payment test module
=head1 SYNOPSIS
&charge=testpayment
or
[charge mode=testpayment param1=value1 param2=value2]
=head1 PREREQUISITES
None.
=head1 DESCRIPTION
The Vend::Payment::TestPayment module implements the testpayment() routine
for use with Interchange. It's compatible on a call level with the other
Interchange payment modules -- in theory (and even usually in practice) you
could switch from TestPayment to another payment module with a few
configuration file changes.
The module will perform one of three actions:
=over 4
=item *
If the card number is 4111111111111111 then the transaction will be approved.
Additional approval numbers include 6011333333333333 (discover), 5454545454545454
(mc), 341111111111111 (amex).
=item *
If the card number is 4111111111111129 then the transaction will be declined.
=item *
Any other card number will raise an error and the transaction will be declined.
=back
To enable this module, place this directive in C<interchange.cfg>:
Require module Vend::Payment::TestPayment
This I<must> be in interchange.cfg or a file included from it.
Make sure CreditCardAuto is off (default in Interchange demos).
The mode can be named anything, but the C<gateway> parameter must be set
to C<testpayment>. To make it the default payment gateway for all credit
card transactions in a specific catalog, you can set in C<catalog.cfg>:
Variable MV_PAYMENT_MODE testpayment
It uses several of the standard settings from Interchange payment. Any time
we speak of a setting, it is obtained either first from the tag/call options,
then from an Interchange order Route named for the mode, then finally a
default global payment variable, For example, the C<id> parameter would
be specified by:
[charge mode=testpayment id=testid]
or
Route testpayment id testid
or
Variable MV_PAYMENT_ID testid
The active settings are:
=over 4
=item id
A test account ID, which can be any value you like.
Global parameter is MV_PAYMENT_ID.
=item secret
A test account password, which can be any value you like.
Global parameter is MV_PAYMENT_SECRET. This is not needed for test
charges, using this module, but you may as well set it up anyway.
=item transaction
The type of transaction to be run. Valid values are:
auth
return
reverse
sale
settle
void
Actually, the transaction type is ignored in this version, but you may as
well set it anyway.
=item remap
This remaps the form variable names to the ones needed by TestPayment. See
the C<Payment Settings> heading in the Interchange documentation for use.
=back
=head2 Troubleshooting
=over 4
=item *
Make sure you "Require"d the module in interchange.cfg:
Require module Vend::Payment::TestPayment
=item *
Check the error logs, both catalog and global.
=item *
Make sure you set your payment parameters properly.
=item *
Try an order, then put this code in a page:
<XMP>
[calc]
my $string = $Tag->uneval( { ref => $Session->{payment_result} });
$string =~ s/{/{\n/;
$string =~ s/,/,\n/g;
return $string;
[/calc]
</XMP>
That should show what happened.
=item *
If all else fails, Cursor Software and other consultants are available to help
with integration for a fee.
=back
=head1 BUGS
There is actually nothing *in* Vend::Payment::TestPayment. It changes packages
to Vend::Payment and places things there.
=head1 AUTHORS
Kevin Walsh <kevin@cursor.biz>
Based on original code by Mike Heins <mheins@perusion.com>
=cut
BEGIN {
::logGlobal("%s payment module loaded",__PACKAGE__)
unless $Vend::Quiet or ! $Global::VendRoot or ! $Global::VendRoot;
}
$VERSION = '1.11';
package Vend::Payment;
sub testpayment {
my ($user,$amount) = @_;
my $opt;
if (ref $user){
$opt = $user;
$user = $opt->{id} || undef;
$secret = $opt->{secret} || undef;
}
else{
$opt = {};
}
my $actual;
if ($opt->{actual}){
$actual = $opt->{actual};
}
else {
my (%actual) = map_actual();
$actual = \%actual;
}
#::logDebug("actual map result: " . ::uneval($actual));
unless ($user) {
$user = charge_param('id') or return (
MStatus => 'failure-hard',
MErrMsg => errmsg('No account id'),
);
}
my $logfile;
my $log;
if($log = charge_param('log')) {
$logfile = charge_param('logfile') || $Vend::Cfg->{ErrorFile};
}
$secret ||= charge_param('secret');
my $precision = $opt->{precision} || 2;
my @override = qw/
order_id
auth_code
mv_credit_card_exp_month
mv_credit_card_exp_year
mv_credit_card_number
/;
for(@override) {
next unless defined $opt->{$_};
$actual->{$_} = $opt->{$_};
}
$actual->{mv_credit_card_exp_month} =~ s/\D//g;
$actual->{mv_credit_card_exp_year} =~ s/\D//g;
$actual->{mv_credit_card_number} =~ s/\D//g;
my $exp = sprintf(
'%02d%02d',
$actual->{mv_credit_card_exp_month},
$actual->{mv_credit_card_exp_year},
);
$opt->{transaction} ||= 'sale';
$amount = $opt->{total_cost} if $opt->{total_cost};
unless ($amount){
$amount = Vend::Interpolate::total_cost();
$amount = Vend::Util::round_to_frac_digits($amount,$precision);
}
$order_id = gen_order_id($opt);
my %result;
my $msg = $opt->{message_declined};
if($opt->{transaction} =~ /^settle/ ) {
$msg ||= 'Settlement failure: %s';
if(! $actual->{order_id}) {
$result{'pop.status'} = 'failure';
$result{'pop.error-message'} = errmsg($msg,'Need order-id');
}
elsif(! $actual->{auth_code}) {
$result{'pop.status'} = 'failure';
$result{'pop.error-message'} = errmsg($msg,'Need auth-code');
}
else {
$result{'pop.status'} = 'success';
$result{'pop.order-id'} = $opt->{order_id};
$result{'pop.auth-code'} = $opt->{auth_code};
}
}
elsif($opt->{transaction} eq 'void' ) {
$msg ||= 'Void failure: %s';
if(! $actual->{order_id}) {
$result{'pop.status'} = 'failure';
$result{'pop.error-message'} = errmsg($msg,'Need order-id');
}
elsif(! $actual->{auth_code}) {
$result{'pop.status'} = 'failure';
$result{'pop.error-message'} = errmsg($msg,'Need auth-code');
}
else {
$result{'pop.status'} = 'success';
$result{'pop.order-id'} = $opt->{order_id};
$result{'pop.auth-code'} = $opt->{auth_code};
}
}
elsif($opt->{transaction} eq 'return' ) {
$msg ||= 'Return/credit failure: %s';
if(! $opt->{order_id}) {
$result{'pop.status'} = 'failure';
$result{'pop.error-message'} = errmsg($msg,'Need order-id');
}
elsif($opt->{auth_code}) {
$result{'pop.status'} = 'failure';
$result{'pop.error-message'} = errmsg($msg,'Need auth-code');
}
else {
$result{'pop.status'} = 'success';
$result{'pop.order-id'} = $opt->{order_id};
$result{'pop.auth-code'} = $opt->{auth_code};
}
}
elsif ($actual->{mv_credit_card_number} =~ /^(4111111111111111|6011333333333333|5454545454545454|341111111111111)$/){
$result{'pop.status'} = 'success';
$result{'pop.order-id'} = $opt->{order_id};
$result{'pop.auth-code'} = 'test_auth_code';
}
elsif ($actual->{mv_credit_card_number} eq '4111111111111129'){
$msg ||= 'TestPayment error: %s. Please call in your order or try again.';
$result{'pop.status'} = 'failure';
$result{'pop.error-message'} =
errmsg($msg,'Payment declined by the card issuer');
}
else{
$result{'pop.status'} = 'failure';
delete $result{'pop.order-id'};
delete $result{'order-id'};
$msg ||= 'TestPayment error: %s. Please call in your order or try again.';
$result{'pop.error-message'} = errmsg($msg,'Invalid test card number');
}
if($log) {
logError(
errmsg('TestPayment %s id=%s: function=%s status=%s amount=%s error=%s',
$user,
$opt->{order_id},
$opt->{transaction},
$result{'pop.status'},
$amount,
$result{'pop.error-message'},
),
{ file => $logfile },
);
}
$result{MStatus} = $result{'pop.status'};
$result{MErrMsg} = $result{'pop.error-message'} if $result{'pop.error-message'};
$result{'order-id'} = $result{'pop.order-id'} if $result{'pop.order-id'};
return %result;
}
package Vend::Payment::TestPayment;
1;