summaryrefslogtreecommitdiff
path: root/CardFortress.pm
blob: 3690f1c561a0bedf1b0482d785e5f6e15b01d661 (plain)
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
package Business::BatchPayment::CardFortress;

use 5.006;
use strict;
use warnings;
our $VERSION = '0.01';

=head1 NAME

Business::BatchPayment::CardFortress

=head1 DESCRIPTION

Batch payment processing via the CardFortress secure payment proxy service.

=head1 USAGE

See Business::BatchPayment for general usage information.

=head2 PROCESSOR ATTRIBUTES

=over 4

=item login, password

Your login credentials for CardFortress.

=item gateway

The L<Business::BatchPayment> module to use for upstream processing.  You 
don't need to have this module installed, but the proxy server does.

=item gateway_*

Processor options to pass along to the gateway module.  The 'gateway_' prefix
will be stripped.

=back

=cut

use Business::BatchPayment;
use Moose;
with 'Business::BatchPayment::Processor';
with 'Business::BatchPayment::TestMode';
with 'Business::BatchPayment::Debug';

use Data::Serializer;

has [ qw(login password private_key gateway) ] => (
  is  => 'ro',
  isa => 'Str',
  required => 1,
);

has 'gateway_opts' => (
  is => 'ro',
  isa => 'HashRef',
  default => sub { {login => ''} },
);

has 'serializer' => (
  is => 'ro',
  lazy => 1,
  handles => [qw(serialize deserialize)],
  default => sub {
# doesn't have to be Storable, but it must support blessed objects
    Data::Serializer->new(serializer => 'Storable', encoding => 'b64') 
  },
);

# pull any arg named "gateway_foo" into gateway_opts
around 'BUILDARGS' => sub {
  my ($orig, $class, %args) = @_;
  foreach (keys %args) {
    if (/^gateway_(.*)/) {
      $args{'gateway_opts'}->{$1} = delete $args{$_};
    }
  }
  $class->$orig(%args);
};

sub default_transport {
  my $self = shift;
  Business::BatchPayment->create('CardFortress::Transport',
    login       => $self->login,
    password    => $self->password,
    debug       => $self->debug,
    test_mode   => $self->test_mode,
    private_key => $self->private_key,
    serializer  => $self->serializer,
  );
}

sub format_request {
  my $self = shift;
  my $batch = shift;
  bless $batch, 'Business::BatchPayment::CardFortress::Batch';
  # things that get encoded in the batch:
  $batch->gateway($self->gateway);
  $batch->gateway_opts($self->gateway_opts);
  $self->serialize($batch); # that's all folks
}

sub parse_response {
  my $self = shift;
  my $input = shift;
  my $batch = $self->deserialize($input);
}

package Business::BatchPayment::CardFortress::Batch;

use Moose;
extends 'Business::BatchPayment::Batch';

has 'gateway' => (
  is => 'rw',
  isa => 'Str',
);
has 'gateway_opts' => (
  is => 'rw',
  isa => 'HashRef',
);

package Business::BatchPayment::CardFortress::Transport;

use Moose;
use Moose::Util::TypeConstraints;
extends 'Business::BatchPayment::Transport::HTTPS';
with 'Business::BatchPayment::TestMode';

use File::Slurp;
use MIME::Base64;
use Crypt::OpenSSL::RSA;

has '+host' => (
  default => sub { 
    my $self = shift;
    $self->test_mode ? 'gw.cardfortress.com'
                     : 'test.cardfortress.com'
  },
  lazy => 1,
);

has ['login', 'password', 'private_key'] => (isa => 'Str', is => 'rw');

has 'serializer' => ( handles => [qw(serialize deserialize)] );

sub upload {
  my ($self, $content) = @_;
  warn "Sending batch...\n" if $self->debug;
  my ($page, $response, %reply_headers) =
    $self->https_post('/batch/submit', {
        login    => $self->login,
        password => $self->password,
        content  => $content,
    });
  $page = $self->deserialize($page);
  die $page->{error} if $page->{error};
  my $batchid = $page->{batchid};

  my $private_key = read_file($self->private_key)
    or die "No private key available";
  my $rsa = Crypt::OpenSSL::RSA->new_private_key($private_key);

  my %answers;
  foreach my $item (@{ $page->{items} }) {
    if ( $item->{error} ) {
      # We have no reliable way to report an error in a specific transaction 
      # at this stage.  The server will send the error in the reply batch.
      # For now do nothing.
    } elsif ( $item->{challenge} ) {
      my $challenge = $item->{challenge};
      $answers{ $item->{tid} } = $rsa->decrypt( decode_base64($challenge) );
    } else {
      # newly created card--doesn't have a challenge, so do nothing
    }
  }
  # post the response
  warn "Answering cryptographic challenge...\n" if $self->debug;
  my $answer_content =
    $self->serialize({ batchid => $batchid, answers => \%answers });
  ($page, $response, %reply_headers) =
    $self->https_post('/batch/run', {
        login    => $self->login,
        password => $self->password,
        content  => $answer_content,
    });
  $page = $self->deserialize($page);
  die $page->{error} if $page->{error};
  return;
}

=head1 AUTHOR

Mark Wells, C<< <mark at freeside.biz> >>

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Business::BatchPayment::CardFortress

Commercial support is available from Freeside Internet Services, Inc.

L<http://www.freeside.biz>

=head1 ACKNOWLEDGEMENTS

=head1 LICENSE AND COPYRIGHT

Copyright 2012 Mark Wells.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


=cut

1; # End of Business::BatchPayment::Paymentech