| 1 | package Bluepay::Bluepay20DailyReport; | ||
| 2 | |||
| 3 | $VERSION = '0.15'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | # Required modules | ||
| 9 | use Digest::MD5 qw(md5_hex); | ||
| 10 | use LWP::UserAgent; | ||
| 11 | use URI::Escape; | ||
| 12 | use Text::CSV; | ||
| 13 | |||
| 14 | |||
| 15 | ## Bluepay20post default fields ## | ||
| 16 | my $URL = 'https://secure.bluepay.com/interfaces/bpdailyreport'; | ||
| 17 | |||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Bluepay::Bluepay20DailyReport | ||
| 22 | |||
| 23 | =head1 VERSION | ||
| 24 | |||
| 25 | Version: 0.15 | ||
| 26 | April 2008 | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | Bluepay::Bluepay20DailyReport - The BluePay 2.0 Daily Report interface | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Bluepay::Bluepay20DailyReport is a Perl based implementation for interaction with the | ||
| 35 | Bluepay 2.0 Daily Report interface. The Bluepay 2.0 Daily Report interface is intended | ||
| 36 | to be polled by a merchant on a daily basis, to get updates on VOID/DECLINED/SETTLED | ||
| 37 | status for transactions on the day prior. Bluepay20DailyReport interface has been | ||
| 38 | developed on Windows XP, but should work on any OS where Perl is installed. | ||
| 39 | |||
| 40 | =head1 RUNNING Bluepay::Bluepay20DailyReport | ||
| 41 | |||
| 42 | use Bluepay::Bluepay20DailyReport; | ||
| 43 | |||
| 44 | # Create the object | ||
| 45 | my $bp20Obj = Bluepay::Bluepay20DailyReport->new(); | ||
| 46 | |||
| 47 | # Populate fields for tx | ||
| 48 | $bp20Obj->{ACCOUNT_ID} = "myaccountid"; | ||
| 49 | $bp20Obj->{SECRET_KEY} = 'mysecretkey'; | ||
| 50 | $bp20Obj->{REPORT_START_DATE} = "2008-05-01"; # These are the dates within which transactions will be reported. | ||
| 51 | $bp20Obj->{REPORT_END_DATE} = "2008-05-02"; | ||
| 52 | $bp20Obj->{PAYMENT_TYPE} = 'ACH'; # ACH or CREDIT | ||
| 53 | |||
| 54 | # Post data to retrieve tx results | ||
| 55 | my $postResults = $bp20Obj->post(); | ||
| 56 | |||
| 57 | # If result is array, SUCCESS | ||
| 58 | if(ref($postResults) eq 'ARRAY') { | ||
| 59 | foreach my $result (@$postResults) { | ||
| 60 | print "Tx Info: " . $result->{id} . "\n"; | ||
| 61 | while ( my ($key, $value) = each(%$result) ) { | ||
| 62 | print "$key => $value\n"; | ||
| 63 | } | ||
| 64 | print "\n"; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | # ELSE, print error | ||
| 68 | else { print "$postResults\n"; } | ||
| 69 | |||
| 70 | =head1 METHODS | ||
| 71 | |||
| 72 | =head2 new | ||
| 73 | |||
| 74 | Creates a new instance of a Bluepay::Bluepay20DailyReport object | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | # New | ||
| 79 | sub new { | ||
| 80 | my $class = shift; | ||
| 81 | my $self = {}; # allocate new hash for object | ||
| 82 | bless($self, $class); | ||
| 83 | |||
| 84 | # Set defaults | ||
| 85 | $self->{URL} = $URL; | ||
| 86 | |||
| 87 | # return object | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 post | ||
| 92 | |||
| 93 | Posts the data to the Bluepay::Bluepay20post interface | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub post { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | ## Create TAMPER_PROOF_SEAL: | ||
| 101 | # TAMPER_PROOF_SEAL - And md5 hash used to verify the request. This md5 should be generated as follows: | ||
| 102 | # 1) Create a string consisting of your Bluepay SECRET_KEY, ACCOUNT_ID, and the REPORT_START_DATE | ||
| 103 | # and REPORT_END_DATE concatenated together. For example: | ||
| 104 | # "ABCDABCDABCDABCDABCDABCD1234123412342006-08-082006-08-09" | ||
| 105 | # 2) Calculate the md5 of the string created in step 1. | ||
| 106 | # 3) Convert the md5 to hexadecimal encoding, if necessary. At this point it should | ||
| 107 | # be exactly 32 characters in length. | ||
| 108 | # 4) Submit the hexadecimal md5 with your request, with the name "TAMPER_PROOF_SEAL". | ||
| 109 | my $TAMPER_PROOF_DATA = ($self->{SECRET_KEY} || '') . ($self->{ACCOUNT_ID} || '') | ||
| 110 | . ($self->{REPORT_START_DATE} || '') . ($self->{REPORT_END_DATE} || ''); | ||
| 111 | my $TAMPER_PROOF_SEAL = md5_hex $TAMPER_PROOF_DATA;; | ||
| 112 | |||
| 113 | # Create request (encode) | ||
| 114 | my $request = $self->{URL} . "\?TAMPER_PROOF_SEAL=" . uri_escape($TAMPER_PROOF_SEAL || ''); | ||
| 115 | while ( my ($key, $value) = each(%$self) ) { | ||
| 116 | if ($key eq 'SECRET_KEY') { next; } $request .= "&$key=" . uri_escape($value || ''); | ||
| 117 | } | ||
| 118 | |||
| 119 | # Create Agent | ||
| 120 | my $ua = new LWP::UserAgent; | ||
| 121 | #my $response = $ua->post("$request"); #OLD | ||
| 122 | my $response = $ua->get("$request"); | ||
| 123 | my $content = $response->content; | ||
| 124 | chomp $content; | ||
| 125 | |||
| 126 | # Parse Response | ||
| 127 | my @rptResponse; my $rptPos = 0; my $rowPos = 0; my @columns; | ||
| 128 | my @records = split(/\n/, $content); my $numberOfACHs = 0; | ||
| 129 | my $csv = Text::CSV->new; | ||
| 130 | foreach my $record (@records) { | ||
| 131 | chomp $record; | ||
| 132 | |||
| 133 | if ($csv->parse($record)) { | ||
| 134 | my %reportRepsonseHash; | ||
| 135 | my @fields = $csv->fields; | ||
| 136 | my $fieldPos = 0; | ||
| 137 | if ($rowPos == 0) { @columns = @fields; $rowPos++; next; } | ||
| 138 | for my $field (@fields) { | ||
| 139 | my $colName = $columns[$fieldPos]; | ||
| 140 | $reportRepsonseHash{$colName} = $field || ""; $fieldPos++; | ||
| 141 | } | ||
| 142 | $rptResponse[$rptPos] = \%reportRepsonseHash; $rptPos++; | ||
| 143 | } | ||
| 144 | else { return $csv->error_input . ' - ' . $content; } # Return CSV error | ||
| 145 | } | ||
| 146 | |||
| 147 | # Return | ||
| 148 | if ($rowPos == 1 and length(@columns) == 1) { return $content; } # If nothing parsed, print error | ||
| 149 | return \@rptResponse; | ||
| 150 | } | ||
| 151 | |||
| 152 | =head1 MODULES | ||
| 153 | |||
| 154 | This script has some dependencies that need to be installed before it | ||
| 155 | can run. You can use cpan to install the modules. They are: | ||
| 156 | - Digest::MD5 | ||
| 157 | - LWP::UserAgent | ||
| 158 | - URI::Escape | ||
| 159 | - Text::CSV | ||
| 160 | |||
| 161 | =head1 AUTHOR | ||
| 162 | |||
| 163 | The Bluepay::Bluepay20DailyReport perl module was written by Christopher Kois <ckois@bluepay.com>. | ||
| 164 | |||
| 165 | =head1 COPYRIGHTS | ||
| 166 | |||
| 167 | The Bluepay::Bluepay20DailyReport package is Copyright (c) April, 2008 by BluePay, Inc. | ||
| 168 | http://www.bluepay.com All rights reserved. You may distribute this module under the terms | ||
| 169 | of GNU General Public License (GPL). | ||
| 170 | |||
| 171 | Module Copyrights: | ||
| 172 | - The Digest::MD5 module is Copyright (c) 1998-2003 Gisle Aas. | ||
| 173 | Available at: http://search.cpan.org/~gaas/Digest-MD5-2.36/MD5.pm | ||
| 174 | - The LWP::UserAgent module is Copyright (c) 1995-2008 Gisle Aas. | ||
| 175 | Available at: http://search.cpan.org/~gaas/libwww-perl-5.812/lib/LWP/UserAgent.pm | ||
| 176 | - The URI::Escape module is Copyright (c) 1995-2004 Gisle Aas. | ||
| 177 | Available at: http://search.cpan.org/~gaas/URI-1.36/URI/Escape.pm | ||
| 178 | - The Text::CSV module is Copyright (c) 2007-2008 Makamaka Hannyaharamitu. | ||
| 179 | Available at: http://search.cpan.org/~makamaka/Text-CSV-1.04/lib/Text/CSV.pm | ||
| 180 | |||
| 181 | NOTE: Each of these modules may have other dependencies. The modules listed here are | ||
| 182 | the modules that Bluepay::Bluepay20DailyReport specifically references. | ||
| 183 | |||
| 184 | =head1 SUPPORT/WARRANTY | ||
| 185 | |||
| 186 | Bluepay::Bluepay20DailyReport is free Open Source software. This code is Free. You may use it, modify it, | ||
| 187 | redistribute it, post it on the bathroom wall, or whatever. If you do make modifications that are | ||
| 188 | useful, Bluepay would love it if you donated them back to us! | ||
| 189 | |||
| 190 | =head1 KNOWN BUGS: | ||
| 191 | |||
| 192 | This is version 0.15 of Bluepay::Bluepay20DailyReport. There are currently no known bugs. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | 1; |