1 package Business::BatchPayment::BillBuddy;
7 Business::BatchPayment::BillBuddy - BillBuddy batch payment format and transport
11 See L<Business::BatchPayment> for general usage notes.
15 use Business::BatchPayment;
18 my @items = Business::BatchPayment::Item->new( ... );
19 my $batch = Business::BatchPayment->create(Batch =>
20 batch_id => $self->batchnum,
24 my $processor = Business::BatchPayment->processor('BillBuddy',
26 password => 'API_KEY',
27 host => 'xmlrpc.billbuddy.com',
34 my $result = $processor->submit($batch);
36 # this gets set by submit, and is needed for receive
37 my $processor_id = $batch->processor_id;
40 my @reply = $processor->receive(@process_ids);
42 =head2 PROCESSOR ATTRIBUTES
46 =item username - the user_id provided to you by BillBuddy
48 =item password - the api_key (NOT the web portal password) provided to you by BillBuddy
50 =item host - the domain name for BillBuddy XMLRPC requests
52 =item path - the path for BillBuddy XMLRPC requests
54 =item port - the port for BillBuddy XMLRPC requests (optional, default 443)
56 =item debug - print debug warnings if true, including XML requests and responses
62 Jonathan Prykop, jonathan@freeside.biz
66 You can find documentation for this module with the perldoc command.
68 perldoc Business::BatchPayment::BillBuddy
70 Commercial support is available from Freeside Internet Services,
71 L<http://www.freeside.biz>
73 =head1 LICENSE AND COPYRIGHT
75 Copyright 2015 Freeside Internet Services
77 This program is free software; you can redistribute it and/or modify it
78 under the terms of either: the GNU General Public License as published
79 by the Free Software Foundation; or the Artistic License.
81 See http://dev.perl.org/licenses/ for more information.
85 use Business::BatchPayment;
87 with 'Business::BatchPayment::Processor';
89 our $VERSION = '0.01';
91 has [ qw(username password host) ] => (
109 sub default_transport {
111 Business::BatchPayment->create('BillBuddy::Transport',
112 username => $self->username,
113 password => $self->password,
117 debug => $self->debug,
122 my ($self,$item,$batch) = @_;
123 #Position Length Content
126 #2-17 16 Reference Number
127 $line .= sprintf("%-16s",$item->tid);
128 #18-18 1 blank, filled with space
130 #19-28 10 amount, numbers only, by cents, zero padded to the left
131 $line .= sprintf("%10s",$item->amount * 100);
132 #29-30 2 blank, filled with spaces
134 #31-32 2 account type: "BC" for bank account, "CC" for credit card account
135 my $pt = $item->payment_type;
137 #we currently don't support CC, but leaving the code in place for future development
138 die 'Business::BatchPayment::BillBuddy currently does not handle credit card transactions';
140 } elsif ($pt eq 'ECHECK') {
143 die "Unknown payment type";
147 #34-40 7 BSB for bank account, formatted in 000-000. blank for credit card account
148 my $bsb = ($pt eq 'CC') ? sprintf("%7s",'') : $item->routing_code;
149 $bsb =~ s/^(\d{3})(\d{3})/$1\-$2/;
150 die "Bad routing code $bsb" if ($pt ne 'CC') && ($bsb !~ /^\d{3}\-\d{3}$/);
154 #42-50 9 Account number for bank accounts. blank for credit card account
155 my $anum = ($pt eq 'CC') ? sprintf("%9s",'') : sprintf("%09s",$item->account_number);
157 #51-66 16 credit card number, left padded with zero if less than 16 digits. Blank for bank accounts
158 my $cnum = ($pt eq 'CC') ? sprintf("%016s",$item->card_number) : sprintf("%16s",'');
160 #67-98 32 bank account name or name on the credit card
161 my $name = $item->first_name . ' ' . $item->last_name;
162 $line .= sprintf("%-32.32s",$name);
165 #100-103 4 credit card expiry date, formatted as mmdd. "0000" for bank account.
166 my $exp = ($pt eq 'CC') ? $item->expiration : '';
167 $line .= sprintf("%04s",$exp);
169 #105-111 7 reserved, always "0000000"
170 #112-114 3 reserved, blank
171 $line .= ' 0000000 ';
172 #115-120 6 line number, left padded with zero
173 $line .= sprintf("%06s",$batch->num);
178 #overriding this just to be able to pass batch to upload
179 #but maybe this should go in standard module?
183 my $request = $self->format_request($batch);
184 $self->transport->upload($request,$batch);
187 #overriding this to pass process_ids to download,
188 #but maybe this should go in standard module?
191 return $self->transport->download(@_);
194 package Business::BatchPayment::BillBuddy::Transport;
196 use XML::Simple qw(:strict);
200 extends 'Business::BatchPayment::Transport::HTTPS';
202 has [ qw(username password) ] => (
220 # this is really specific to BillBuddy, not a generic XML formatting routine
222 my ($self,$sid,@param) = @_;
224 my $xml = XML::Writer->new(
228 $xml->startTag('postdata');
229 $xml->dataElement('sessionid',$sid);
230 $xml->dataElement('clientidentifier','');
231 $xml->startTag('parameters');
232 foreach my $param (@param) {
233 if (ref($param) eq 'ARRAY') {
234 my $type = $$param[0];
235 my $value = $$param[1];
236 $xml->$type('parameter',$value);
238 $xml->dataElement('parameter',$param);
241 $xml->endTag('parameters');
242 $xml->endTag('postdata');
247 # also specific to BillBuddy, doesn't actually follow XMLRPC standard for response
249 my ($self,$func,$sid,@param) = @_;
250 my $path = $self->path;
251 $path = '/' . $path unless $path =~ /^\//;
252 $path .= '/' unless $path =~ /\/$/;
254 my $xmlcontent = $self->xml_format($sid,@param);
255 warn $self->host . ' ' . $self->port . ' ' . $path . "\n" . $xmlcontent if $self->debug;
256 my ($response, $rcode, %rheaders) = $self->https_post($path,$xmlcontent);
257 die "Bad response from gateway: $rcode" unless $rcode eq '200 OK';
258 warn $response . "\n" if $self->debug;
259 my $rref = XMLin($response, KeyAttr => ['ResponseData'], ForceArray => []);
260 die "Error from gateway: " . $rref->{'ResponseStatusDescription'} if $rref->{'ResponseStatus'};
264 #gets date from batch & sets processor_id in batch
266 my ($self,$request,$batch) = @_;
268 # get date from batch
269 my ($date) = $batch->process_date =~ /^(....-..-..)/;
271 my $resp = $self->xmlrpc_post('xmlrpc_tp_Login.asp','',$self->username,$self->password);
272 my $sid = $resp->{'ResponseData'}->{'sessionID'};
273 die "Could not parse sessionid from gateway response" unless $sid;
274 # start a payment batch
275 $resp = $self->xmlrpc_post('xmlrpc_tp_DDRBatch_Open.asp',$sid,$self->username,$date);
276 my $batchno = $resp->{'ResponseData'}->{'batchno'};
277 die "Could not parse batchno from gateway response" unless $batchno;
278 $batch->processor_id($batchno);
279 # post a payment transaction
280 foreach my $line (split(/\n/,$request)) {
281 $self->xmlrpc_post('xmlrpc_tp_DDRTransaction_Add.asp',$sid,$self->username,$batchno,['cdataElement',$line]);
283 # close payment batch
284 $self->xmlrpc_post('xmlrpc_tp_DDRBatch_Close.asp',$sid,$self->username,$batchno);
285 # submit payment batch
286 $self->xmlrpc_post('xmlrpc_tp_DDRBatch_Submit.asp',$sid,$self->username,$batchno);
288 $self->xmlrpc_post('xmlrpc_tp_Logout.asp',$sid,$self->username);
292 # caution--this method developed without access to completed test payments
293 # built with best guesses, cross your fingers...
296 my @processor_ids = @_;
297 return () unless @processor_ids;
299 my $resp = $self->xmlrpc_post('xmlrpc_tp_Login.asp','',$self->username,$self->password);
300 my $sid = $resp->{'ResponseData'}->{'sessionID'};
301 die "Could not parse sessionid from gateway response" unless $sid;
303 foreach my $batchno (@processor_ids) {
304 #get BillBuddy transaction ids for batch
305 $resp = $self->xmlrpc_post('xmlrpc_tp_DDRBatch_getTranList.asp',$sid,$self->username,$batchno);
306 my $tids = $resp->{'ResponseData'}->{'id'};
307 next unless $tids; #error/die instead?
309 $tids = ref($tids) ? $tids : [ $tids ];
310 #get status by individual transaction
311 foreach my $tid (@$tids) {
312 $resp = $self->xmlrpc_post('xmlrpc_tp_DDRBatch_getTranStatus.asp',$sid,$self->username,$tid);
313 my $status = lc($resp->{'ResponseData'}->{'bankprocessstatus'});
315 next if grep(/^$status$/,('submitted','processing','scheduled'));
316 $error = "Unknown return status: $status"
317 unless grep(/^$status$/,('deleted','declined'));
318 my $item = Business::BatchPayment->create(Item =>
319 order_number => $tid,
320 tid => $resp->{'ResponseData'}->{'referencenumber'},
321 approved => ($status eq 'approved') ? 1 : 0,
322 error_message => $error,
325 #not sure what format date gets returned in, item creation will fail on bad format,
326 #so I'm taking a guess, and not recording the date if my guess is wrong
327 if ($resp->{'ResponseData'}->{'actualprocessdate'} =~ /^(\d\d\d\d).(\d\d).(\d\d)/) {
328 $item->payment_date($1.'-'.$2.'-'.$3);
330 push(@batchitems,$item);
333 push(@batches, Business::BatchPayment->create('Batch', items => \@batchitems));
337 $self->xmlrpc_post('xmlrpc_tp_Logout.asp',$sid,$self->username);