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   10 use Moo;
  2         3  
  2         9  
6 2     2   585 use Carp qw(croak);
  2         2  
  2         91  
7 2     2   954 use namespace::clean;
  2         37195  
  2         16  
8              
9             our $VERSION = '0.002';
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 2     2 1 13071 sub id { $_[0]->data->{id} }
26 1     1 1 7 sub status { $_[0]->data->{status} }
27 0     0 1 0 sub intent { $_[0]->data->{intent} }
28              
29              
30             sub _links {
31 1     1   2 my ($self) = @_;
32 1   50     5 return $self->data->{links} || [];
33             }
34              
35             sub link_for {
36 1     1 1 2 my ($self, $rel) = @_;
37 1         2 for my $l (@{ $self->_links }) {
  1         2  
38 2 100 66     13 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 3 my ($self) = @_;
46             # Modern flows return 'payer-action'; legacy returned 'approve'.
47 1   33     3 return $self->link_for('payer-action') // $self->link_for('approve');
48             }
49              
50              
51             sub _capture_node {
52 2     2   3 my ($self) = @_;
53 2   50     8 my $pu = $self->data->{purchase_units} || [];
54 2 50       5 return unless @$pu;
55 2   50     5 my $captures = $pu->[0]{payments}{captures} || [];
56 2         6 return $captures->[0];
57             }
58              
59             sub capture_id {
60 1     1 1 3 my ($self) = @_;
61 1 50       3 my $c = $self->_capture_node or return;
62 1         4 return $c->{id};
63             }
64              
65              
66             sub fee_in_cent {
67 1     1 1 2 my ($self) = @_;
68 1 50       3 my $c = $self->_capture_node or return;
69 1 50       4 my $fee = $c->{seller_receivable_breakdown}{paypal_fee}{value} or return;
70             # PayPal returns decimal strings like "1.23"
71 1         9 return int($fee * 100 + 0.5);
72             }
73              
74              
75             sub total {
76 1     1 1 3 my ($self) = @_;
77 1   50     6 my $pu = $self->data->{purchase_units} || [];
78 1 50       4 return unless @$pu;
79 1         3 return $pu->[0]{amount}{value};
80             }
81              
82             sub currency {
83 1     1 1 2 my ($self) = @_;
84 1   50     6 my $pu = $self->data->{purchase_units} || [];
85 1 50       3 return unless @$pu;
86 1         4 return $pu->[0]{amount}{currency_code};
87             }
88              
89              
90             sub payer_email {
91 1     1 1 2 my ($self) = @_;
92 1         18 return $self->data->{payer}{email_address};
93             }
94              
95             sub payer_name {
96 1     1 1 3 my ($self) = @_;
97 1 50       6 my $n = $self->data->{payer}{name} or return;
98 1 50       3 return join(' ', grep { defined && length } $n->{given_name}, $n->{surname});
  2         12  
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__