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) ] => (
108 sub default_transport {
110 Business::BatchPayment->create('BillBuddy::Transport',
111 username => $self->username,
112 password => $self->password,
116 debug => $self->debug,
121 my ($self,$item,$batch) = @_;
122 #Position Length Content
125 #2-17 16 Reference Number
126 $line .= sprintf("%-16s",$item->tid);
127 #18-18 1 blank, filled with space
129 #19-28 10 amount, numbers only, by cents, zero padded to the left
130 $line .= sprintf("%10s",$item->amount * 100);
131 #29-30 2 blank, filled with spaces
133 #31-32 2 account type: "BC" for bank account, "CC" for credit card account
134 my $pt = $item->payment_type;
136 #we currently don't support CC, but leaving the code in place for future development
137 die 'Business::BatchPayment::BillBuddy currently does not handle credit card transactions';
139 } elsif ($pt eq 'ECHECK') {
142 die "Unknown payment type";
146 #34-40 7 BSB for bank account, formatted in 000-000. blank for credit card account
147 my $bsb = ($pt eq 'CC') ? sprintf("%7s",'') : $item->routing_code;
148 $bsb =~ s/^(\d{3})(\d{3})/$1\-$2/;
149 die "Bad routing code $bsb" if ($pt ne 'CC') && ($bsb !~ /^\d{3}\-\d{3}$/);
153 #42-50 9 Account number for bank accounts. blank for credit card account
154 my $anum = ($pt eq 'CC') ? sprintf("%9s",'') : sprintf("%09s",$item->account_number);
156 #51-66 16 credit card number, left padded with zero if less than 16 digits. Blank for bank accounts
157 my $cnum = ($pt eq 'CC') ? sprintf("%016s",$item->card_number) : sprintf("%16s",'');
159 #67-98 32 bank account name or name on the credit card
160 my $name = $item->first_name . ' ' . $item->last_name;
161 $line .= sprintf("%-32.32s",$name);
164 #100-103 4 credit card expiry date, formatted as mmdd. "0000" for bank account.
165 my $exp = ($pt eq 'CC') ? $item->expiration : '';
166 $line .= sprintf("%04s",$exp);
168 #105-111 7 reserved, always "0000000"
169 #112-114 3 reserved, blank
170 $line .= ' 0000000 ';
171 #115-120 6 line number, left padded with zero
172 $line .= sprintf("%06s",$batch->num);
177 #overriding this just to be able to pass batch to upload
178 #but maybe this should go in standard module?
182 my $request = $self->format_request($batch);
183 $self->transport->upload($request,$batch);
186 #overriding this to pass process_ids to download,
187 #but maybe this should go in standard module?
190 return $self->transport->download(@_);
193 package Business::BatchPayment::BillBuddy::Transport;
195 use XML::Simple qw(:strict);
199 extends 'Business::BatchPayment::Transport::HTTPS';
201 has [ qw(username password) ] => (
219 # this is really specific to BillBuddy, not a generic XML formatting routine
221 my ($self,$sid,@param) = @_;
223 my $xml = XML::Writer->new(
227 $xml->startTag('postdata');
228 $xml->dataElement('sessionid',$sid);
229 $xml->dataElement('clientidentifier','');
230 $xml->startTag('parameters');
231 foreach my $param (@param) {
232 if (ref($param) eq 'ARRAY') {
233 my $type = $$param[0];
234 my $value = $$param[1];
235 $xml->$type('parameter',$value);
237 $xml->dataElement('parameter',$param);
240 $xml->endTag('parameters');
241 $xml->endTag('postdata');
246 # also specific to BillBuddy, doesn't actually follow XMLRPC standard for response
248 my ($self,$func,$sid,@param) = @_;
249 my $path = $self->path;
250 $path = '/' . $path unless $path =~ /^\//;
251 $path .= '/' unless $path =~ /\/$/;
253 my $xmlcontent = $self->xml_format($sid,@param);
254 warn $self->host . ' ' . $self->port . ' ' . $path . "\n" . $xmlcontent if $self->debug;
255 my ($response, $rcode, %rheaders) = $self->https_post($path,$xmlcontent);
256 die "Bad response from gateway: $rcode" unless $rcode eq '200 OK';
257 warn $response . "\n" if $self->debug;
258 my $rref = XMLin($response, KeyAttr => ['ResponseData'], ForceArray => []);
259 die "Error from gateway: " . $rref->{'ResponseStatusDescription'} if $rref->{'ResponseStatus'};
263 #gets date from batch & sets processor_id in batch
265 my ($self,$request,$batch) = @_;
267 # get date from batch
268 my ($date) = $batch->process_date =~ /^(....-..-..)/;
270 my $resp = $self->xmlrpc_post('xmlrpc_tp_Login.asp','',$self->username,$self->password);
271 my $sid = $resp->{'ResponseData'}->{'sessionID'};
272 die "Could not parse sessionid from gateway response" unless $sid;
273 # start a payment batch
274 $resp = $self->xmlrpc_post('xmlrpc_tp_DDRBatch_Open.asp',$sid,$self->username,$date);
275 my $batchno = $resp->{'ResponseData'}->{'batchno'};
276 die "Could not parse batchno from gateway response" unless $batchno;
277 $batch->processor_id($batchno);
278 # post a payment transaction
279 foreach my $line (split(/\n/,$request)) {
280 $self->xmlrpc_post('xmlrpc_tp_DDRTransaction_Add.asp',$sid,$self->username,$batchno,['cdataElement',$line]);
282 # close payment batch
283 $self->xmlrpc_post('xmlrpc_tp_DDRBatch_Close.asp',$sid,$self->username,$batchno);
284 # submit payment batch
285 $self->xmlrpc_post('xmlrpc_tp_DDRBatch_Submit.asp',$sid,$self->username,$batchno);
287 $self->xmlrpc_post('xmlrpc_tp_Logout.asp',$sid,$self->username);
291 # caution--this method developed without access to completed test payments
292 # built with best guesses, cross your fingers...
295 my @processor_ids = @_;
296 return () unless @processor_ids;
298 my $resp = $self->xmlrpc_post('xmlrpc_tp_Login.asp','',$self->username,$self->password);
299 my $sid = $resp->{'ResponseData'}->{'sessionID'};
300 die "Could not parse sessionid from gateway response" unless $sid;
302 foreach my $batchno (@processor_ids) {
303 #get BillBuddy transaction ids for batch
304 $resp = $self->xmlrpc_post('xmlrpc_tp_DDRBatch_getTranList.asp',$sid,$self->username,$batchno);
305 my $tids = $resp->{'ResponseData'}->{'id'};
306 next unless $tids; #error/die instead?
308 $tids = ref($tids) ? $tids : [ $tids ];
309 #get status by individual transaction
310 foreach my $tid (@$tids) {
311 $resp = $self->xmlrpc_post('xmlrpc_tp_DDRBatch_getTranStatus.asp',$sid,$self->username,$tid);
312 my $status = lc($resp->{'ResponseData'}->{'bankprocessstatus'});
314 next if grep(/^$status$/,('submitted','processing','scheduled'));
315 $error = "Unknown return status: $status"
316 unless grep(/^$status$/,('deleted','declined'));
317 my $item = Business::BatchPayment->create(Item =>
318 order_number => $tid,
319 tid => $resp->{'ResponseData'}->{'referencenumber'},
320 approved => ($status eq 'approved') ? 1 : 0,
321 error_message => $error,
324 #not sure what format date gets returned in, item creation will fail on bad format,
325 #so I'm taking a guess, and not recording the date if my guess is wrong
326 if ($resp->{'ResponseData'}->{'actualprocessdate'} =~ /^(\d\d\d\d).(\d\d).(\d\d)/) {
327 $item->payment_date($1.'-'.$2.'-'.$3);
329 push(@batchitems,$item);
332 push(@batches, Business::BatchPayment->create('Batch', items => \@batchitems));
336 $self->xmlrpc_post('xmlrpc_tp_Logout.asp',$sid,$self->username);