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
|
package Business::OnlineThirdPartyPayment::PayPal;
use strict;
use base 'Business::OnlineThirdPartyPayment';
use vars qw($VERSION $DEBUG);
use strict;
use LWP;
use JSON;
use Net::PayPal; # for authentication, mostly
use URI;
use Cache::FileCache; # for ID strings
$VERSION = '0.01';
$DEBUG = 2;
sub set_defaults {
my $self = shift;
$self->build_subs(qw(order_number result_code error_message error_object
cache_root));
}
sub client {
my $self = shift;
my %content = $self->content;
$self->{'client'} ||=
Net::PayPal->new($content{'login'}, $content{'password'});
}
sub cache {
my $self = shift;
$self->{'cache'} ||=
Cache::FileCache->new(
{ namespace => 'PayPal',
default_expires_in => 3600,
cache_root => $self->cache_root
} );
}
sub submit {
my $self = shift;
my %content = $self->content;
my $action = lc($content{'action'});
if ( $action eq 'authorization only' ) {
$self->create_payment;
} elsif ( $action eq 'post authorization' ) {
$self->execute_payment;
}
}
sub rest {
# a wrapper for the one in Net::PayPal, with better error handling
my ($self, $path, $request) = @_;
my $json_request = encode_json($request);
warn "REQUEST:\n$json_request\n\n" if $DEBUG >= 2;
my $raw_res =
$self->client->rest('POST', $path, $json_request, 1);
# last argument is "dump_responce" [sic]--tells Net::PayPal to dump the
# HTTP::Response object instead of returning (part of) the error status
my $res;
# deal with certain ambiguities from Data::Dumper
{ my $VAR1;
eval "$raw_res";
$res = $VAR1; }
if ( !defined($res) || !ref($res) || !$res->isa('HTTP::Response') ) {
die "Nonsense output from Net::PayPal REST call:\n$raw_res\n\n";
}
warn "RESPONSE:" . $res->status_line . "\n" . $res->content . "\n\n"
if $DEBUG >= 2;
if ( $res->is_success ) {
$self->is_success(1);
return decode_json($res->content);
} else {
$self->is_success(0);
if ( $res->content ) {
my $response = decode_json($res->content);
$self->error_object($response);
my $error = sprintf("%s: %s",
$response->{'name'}, $response->{'message'});
if ( $response->{'details'} ) {
foreach (@{ $response->{'details'} }) {
$error .= sprintf("\n%s:\t%s", $_->{'field'}, $_->{'issue'});
}
}
$self->error_message($error);
return $response;
} else {
$self->error_object({});
$self->error_message($res->status_line);
return {};
}
}
}
sub create_payment {
my $self = shift;
my %content = $self->content;
my $ref = $content{'reference'}
or die "reference required";
my $return_url = URI->new($content{'callback_url'})
or die "callback_url required";
$return_url->query_form( $return_url->query_form(), 'ref' => $ref );
my $cancel_url = URI->new($content{'cancel_url'})
or die "cancel_url required";
$cancel_url->query_form( $cancel_url->query_form(), 'ref' => $ref );
my $request =
{
intent => 'sale',
payer => {
payment_method => 'paypal',
},
transactions => [
{
amount => {
total => $content{'amount'},
currency => ($content{'currency'} || 'USD'),
},
description => $content{'description'},
},
],
redirect_urls => {
return_url => $return_url->as_string,
cancel_url => $cancel_url->as_string,
},
};
my $response = $self->rest('/v1/payments/payment', $request);
if ( $self->is_success ) {
$self->order_number($response->{'id'});
$self->cache->set( "REF-$ref" => $response->{'id'} );
my %links = map { $_->{rel} => $_->{href} } @{ $response->{'links'} };
$self->popup_url($links{'approval_url'});
# other links are "self", which is where we just posted,
# and "execute_url", which we can determine from the payment id
}
}
sub execute_payment {
my $self = shift;
my %content = $self->content;
# at this point the transaction is already set up
# (right? the workflow in this is horribly confusing...)
if ( !$self->authorization ) {
die "No authorization was received for this payment.\n";
}
my $request = { 'payer_id' => $self->authorization };
my $execute_path = '/v1/payments/payment/' . $self->order_number . '/execute';
$self->rest($execute_path, $request);
}
sub reference {
my $self = shift;
my $data = shift; # hashref of query params included in the callback URL
$self->authorization($data->{'PayerID'});
my $ref = $data->{'ref'};
my $id = $self->cache->get("REF-$ref");
if (!$id) {
$self->error_message("Payment reference '$ref' not found.");
$self->is_success(0);
}
$self->order_number($id);
$ref;
}
1;
__END__
=head1 NAME
Business::OnlineThirdPartyPayment::PayPal
=head1 DESCRIPTION
=head1 AUTHOR
Mark Wells <mark@freeside.biz>
=head1 SEE ALSO
perl(1). L<Business::OnlineThirdPartyPayment>.
=cut
|