File Coverage

blib/lib/WWW/PayPal.pm
Criterion Covered Total %
statement 63 65 96.9
branch 17 28 60.7
condition 4 7 57.1
subroutine 18 19 94.7
pod 2 2 100.0
total 104 121 85.9


line stmt bran cond sub pod time code
1             package WWW::PayPal;
2              
3             # ABSTRACT: Perl client for the PayPal REST API
4              
5 2     2   510460 use Moo;
  2         15607  
  2         10  
6 2     2   3442 use Carp qw(croak);
  2         4  
  2         112  
7 2     2   1181 use WWW::PayPal::API::Orders;
  2         8  
  2         108  
8 2     2   1317 use WWW::PayPal::API::Payments;
  2         10  
  2         100  
9 2     2   1261 use WWW::PayPal::API::Products;
  2         9  
  2         96  
10 2     2   1366 use WWW::PayPal::API::Plans;
  2         11  
  2         111  
11 2     2   1294 use WWW::PayPal::API::Subscriptions;
  2         9  
  2         90  
12 2     2   15 use namespace::clean;
  2         4  
  2         9  
13              
14             our $VERSION = '0.002';
15              
16              
17             has client_id => (
18             is => 'ro',
19             default => sub { $ENV{PAYPAL_CLIENT_ID} },
20             );
21              
22              
23             has secret => (
24             is => 'ro',
25             default => sub { $ENV{PAYPAL_SECRET} },
26             );
27              
28              
29             has sandbox => (
30             is => 'ro',
31             default => sub { $ENV{PAYPAL_SANDBOX} ? 1 : 0 },
32             );
33              
34              
35             has base_url => (
36             is => 'lazy',
37             builder => sub {
38 1 50   1   34 $_[0]->sandbox
39             ? 'https://api-m.sandbox.paypal.com'
40             : 'https://api-m.paypal.com';
41             },
42             );
43              
44              
45             with 'WWW::PayPal::Role::HTTP';
46              
47             has orders => (
48             is => 'lazy',
49 1     1   16 builder => sub { WWW::PayPal::API::Orders->new(client => $_[0]) },
50             );
51              
52              
53             has payments => (
54             is => 'lazy',
55 0     0   0 builder => sub { WWW::PayPal::API::Payments->new(client => $_[0]) },
56             );
57              
58              
59             has products => (
60             is => 'lazy',
61 1     1   24 builder => sub { WWW::PayPal::API::Products->new(client => $_[0]) },
62             );
63              
64              
65             has plans => (
66             is => 'lazy',
67 1     1   1459 builder => sub { WWW::PayPal::API::Plans->new(client => $_[0]) },
68             );
69              
70              
71             has subscriptions => (
72             is => 'lazy',
73 1     1   39 builder => sub { WWW::PayPal::API::Subscriptions->new(client => $_[0]) },
74             );
75              
76              
77             sub js_sdk_url {
78 4     4 1 5847 my ($self, %args) = @_;
79 4 50       23 croak 'client_id required' unless $self->client_id;
80              
81             my %p = (
82             'client-id' => $self->client_id,
83 4   100     34 intent => lc($args{intent} // 'capture'),
84             );
85 4 100       16 $p{currency} = uc $args{currency} if $args{currency};
86 4 50       13 $p{locale} = $args{locale} if $args{locale};
87 4 100       11 $p{'disable-funding'} = _join_list($args{disable_funding}) if $args{disable_funding};
88 4 50       11 $p{'enable-funding'} = _join_list($args{enable_funding}) if $args{enable_funding};
89 4   50     23 $p{components} = _join_list($args{components} // ['buttons']);
90 4 100       14 $p{vault} = 'true' if $args{vault};
91 4 50       11 $p{'merchant-id'} = $args{merchant_id} if $args{merchant_id};
92 4 50 33     14 $p{'buyer-country'} = $args{buyer_country} if $args{buyer_country} && $self->sandbox;
93 4 50       9 $p{debug} = 'true' if $args{debug};
94              
95             # Extra opaque params (e.g. data-partner-attribution-id is an HTML attr,
96             # not a URL param, so we don't mix those in).
97 4 50       10 if (my $extra = $args{params}) {
98 0         0 $p{$_} = $extra->{$_} for keys %$extra;
99             }
100              
101             my $qs = join('&',
102 17         33 map { _uri_escape($_) . '=' . _uri_escape($p{$_}) }
103 4         14 sort grep { defined $p{$_} } keys %p
  17         45  
104             );
105 4         26 return 'https://www.paypal.com/sdk/js?' . $qs;
106             }
107              
108              
109             sub js_sdk_script_tag {
110 1     1 1 543 my ($self, %args) = @_;
111 1         4 my $url = $self->js_sdk_url(%args);
112 1         6 return '';
113             }
114              
115             sub _join_list {
116 5     5   10 my ($v) = @_;
117 5 50       31 return ref $v eq 'ARRAY' ? join(',', @$v) : $v;
118             }
119              
120             sub _uri_escape {
121 34     34   72 my ($s) = @_;
122 34 50       70 $s = '' unless defined $s;
123 34         97 $s =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
  1         11  
124 34         101 return $s;
125             }
126              
127             sub _html_escape {
128 1     1   3 my ($s) = @_;
129 1 50       6 $s = '' unless defined $s;
130 1         7 $s =~ s/&/&/g;
131 1         5 $s =~ s/
132 1         3 $s =~ s/>/>/g;
133 1         3 $s =~ s/"/"/g;
134 1         6 return $s;
135             }
136              
137              
138             1;
139              
140             __END__