summaryrefslogtreecommitdiff
path: root/FCMB.pm
blob: cae090cb03fd4e607898e16a197dd5551bff5d0b (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
package Business::OnlineThirdPartyPayment::FCMB;

use strict;
use base 'Business::OnlineThirdPartyPayment';

use strict;
use LWP;
use URI;
use Data::Dumper;
use Date::Format 'time2str';
use XML::LibXML;

our $VERSION = '0.01';
our $ENDPOINT_SANDBOX = 'localhost';
our $ENDPOINT_LIVE    = 'fcmbwebpay.firstcitygrouponline.com';

our $DEBUG = 3;

# ISO 4217 currency codes (the relevant ones)
our %ALPHA_TO_NUM = (
  USD => 840,
  UKP => 826,
  NGN => 566,
);

sub set_defaults {
  my $self = shift;
  my %args = @_;
  $self->build_subs(qw(username password host));
  if ( $args{debug} ) {
    $DEBUG = $args{debug};
  }
}

sub create {
  my $self = shift;
  my %content = @_;

  my %params;
  $params{'orderId'}  = time2str('%Y%m%d%H%M%S', time) . '-' .
                       sprintf('%06d', int(rand(1000000)));
  $params{'mercId'}   = $self->username
    or die "FCMB merchant ID (username) must be configured\n";
  $params{'currCode'} = $ALPHA_TO_NUM{$content{currency}};
  $params{'prod'}     = $content{'description'};
  $params{'email'}    = $content{'email'};
  $params{'amt'}      = $content{'amount'};

  my $host = $self->host;
  if ( $self->test_transaction ) {
    $host ||= $ENDPOINT_SANDBOX;
  } else {
    $host ||= $ENDPOINT_LIVE;
  }
  my $url = 'https://' . $host .
    '/customerportal/MerchantServices/MakePayment.aspx';

  warn Dumper \%params if $DEBUG > 2;

  # we don't post to the url ourselves, just give it to the user
  $self->is_success(1);
  $self->redirect($url);
  $self->post_params(\%params);

  $self->token( $params{'orderId'} );
}

sub execute {
  my $self = shift;
  my %params = @_;

# URL looks like
# http://merchantA.com/Success?OrderID=988676&TransactionReference=8765678998989779
  warn Dumper(\%params) if $DEBUG > 2;
 
  if ($params{'OrderID'}) {
    $self->token($params{'OrderID'});
  } else {
    die 'No order ID returned from processor';
  }
  if ($params{'TransactionReference'}) {
    $self->order_number($params{'TransactionReference'});
  } else {
    die 'No transaction reference returned from processor';
  }

  my $host = $self->host;
  if ( $self->test_transaction ) {
    $host ||= $ENDPOINT_SANDBOX;
    $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
  } else {
    $host ||= $ENDPOINT_LIVE;
  }
  my $url = URI->new('https://' . $host .
    '/customerportal/MerchantServices/UpayTransactionStatus.ashx');
  $url->query_form('MERCHANT_ID'  => $self->username,
                   'ORDER_ID'     => $self->token);
  my $ua = LWP::UserAgent->new;
  warn "Querying transaction status at $url\n" if $DEBUG;
  my $response = $ua->get($url);

  if ( $response->is_success ) {

    local $@;
    my $parser = XML::LibXML->new;
    my $doc = eval { $parser->parse_string($response->content) };
    if ( $@ ) {
      die "malformed response to transaction status request: $@\n".
          ($DEBUG ? ("\n\n".$response->content) : '');
    }
    my $root = $doc->documentElement;
    my %hash = map { $_->nodeName => $_->textContent }
               $root->nonBlankChildNodes;

    warn Dumper \%hash if $DEBUG > 2;
    if ( $hash{StatusCode} == 0 ) {
      $self->is_success(1);
      $self->authorization($hash{PaymentRef});
    } else {
      $self->is_success(0);
      $self->error_message($hash{Status} . ' - ' . $hash{ResponseDescription});
    }
  } else {
    die "No confirmation received: ".$response->status_line;
  }
}

1;
__END__

=head1 NAME

Business::OnlineThirdPartyPayment::FCMB

=head1 DESCRIPTION

Business::OnlineThirdPartyPayment interface to the First City Monument Bank
(Nigeria) web-based payment processing system.

=head1 NOTES

=over 4

=item FCMB requires the callback URL to be configured statically;
I<return_url> and I<cancel_url> parameters will be ignored.

=item Set I<username> to your merchant ID (5 digits).  No password is needed.

=item The only required transaction fields are currency (NGN, USD, or UKP),
amount, description, and email.

=item The 'faker' directory contains a simple mock-up of the FCMB interface
for testing.  This was created from the documentation, without reference
to the real FCMB system, and is NOT known to be accurate.

=back

=head1 AUTHOR

Mark Wells <mark@freeside.biz>

=head1 SEE ALSO

perl(1). L<Business::OnlineThirdPartyPayment>.

=cut