File Coverage

blib/lib/WWW/PayPal/Order.pm
Criterion Covered Total %
statement 45 57 78.9
branch 10 18 55.5
condition 8 16 50.0
subroutine 15 19 78.9
pod 14 14 100.0
total 92 124 74.1


line stmt bran cond sub pod time code
1             package WWW::PayPal::Order;
2              
3             # ABSTRACT: PayPal Orders v2 order entity
4              
5 2     2   16 use Moo;
  2         4  
  2         13  
6 2     2   859 use Carp qw(croak);
  2         5  
  2         140  
7 2     2   1251 use namespace::clean;
  2         48370  
  2         22  
8              
9             our $VERSION = '0.001';
10              
11              
12             has _client => (
13             is => 'ro',
14             required => 1,
15             weak_ref => 1,
16             init_arg => 'client',
17             );
18              
19             has data => (
20             is => 'rw',
21             required => 1,
22             );
23              
24              
25 1     1 1 2959 sub id { $_[0]->data->{id} }
26 1     1 1 9 sub status { $_[0]->data->{status} }
27 0     0 1 0 sub intent { $_[0]->data->{intent} }
28              
29              
30             sub _links {
31 1     1   4 my ($self) = @_;
32 1   50     10 return $self->data->{links} || [];
33             }
34              
35             sub link_for {
36 1     1 1 5 my ($self, $rel) = @_;
37 1         3 for my $l (@{ $self->_links }) {
  1         3  
38 2 100 66     22 return $l->{href} if $l->{rel} && $l->{rel} eq $rel;
39             }
40 0         0 return;
41             }
42              
43              
44             sub approve_url {
45 1     1 1 4 my ($self) = @_;
46             # Modern flows return 'payer-action'; legacy returned 'approve'.
47 1   33     4 return $self->link_for('payer-action') // $self->link_for('approve');
48             }
49              
50              
51             sub _capture_node {
52 2     2   5 my ($self) = @_;
53 2   50     14 my $pu = $self->data->{purchase_units} || [];
54 2 50       9 return unless @$pu;
55 2   50     8 my $captures = $pu->[0]{payments}{captures} || [];
56 2         10 return $captures->[0];
57             }
58              
59             sub capture_id {
60 1     1 1 5 my ($self) = @_;
61 1 50       6 my $c = $self->_capture_node or return;
62 1         6 return $c->{id};
63             }
64              
65              
66             sub fee_in_cent {
67 1     1 1 4 my ($self) = @_;
68 1 50       5 my $c = $self->_capture_node or return;
69 1 50       6 my $fee = $c->{seller_receivable_breakdown}{paypal_fee}{value} or return;
70             # PayPal returns decimal strings like "1.23"
71 1         16 return int($fee * 100 + 0.5);
72             }
73              
74              
75             sub total {
76 1     1 1 6 my ($self) = @_;
77 1   50     9 my $pu = $self->data->{purchase_units} || [];
78 1 50       7 return unless @$pu;
79 1         8 return $pu->[0]{amount}{value};
80             }
81              
82             sub currency {
83 1     1 1 6 my ($self) = @_;
84 1   50     8 my $pu = $self->data->{purchase_units} || [];
85 1 50       5 return unless @$pu;
86 1         8 return $pu->[0]{amount}{currency_code};
87             }
88              
89              
90             sub payer_email {
91 1     1 1 5 my ($self) = @_;
92 1         9 return $self->data->{payer}{email_address};
93             }
94              
95             sub payer_name {
96 1     1 1 5 my ($self) = @_;
97 1 50       9 my $n = $self->data->{payer}{name} or return;
98 1 50       5 return join(' ', grep { defined && length } $n->{given_name}, $n->{surname});
  2         17  
99             }
100              
101             sub payer_id {
102 0     0 1   my ($self) = @_;
103 0           return $self->data->{payer}{payer_id};
104             }
105              
106              
107             sub refresh {
108 0     0 1   my ($self) = @_;
109 0           my $fresh = $self->_client->orders->get($self->id);
110 0           $self->data($fresh->data);
111 0           return $self;
112             }
113              
114              
115             sub capture {
116 0     0 1   my ($self) = @_;
117 0           my $captured = $self->_client->orders->capture($self->id);
118 0           $self->data($captured->data);
119 0           return $self;
120             }
121              
122              
123             1;
124              
125             __END__