File Coverage

blib/lib/WWW/PayPal/API/Orders.pm
Criterion Covered Total %
statement 13 44 29.5
branch 0 24 0.0
condition 0 9 0.0
subroutine 5 10 50.0
pod 4 4 100.0
total 22 91 24.1


line stmt bran cond sub pod time code
1             package WWW::PayPal::API::Orders;
2              
3             # ABSTRACT: PayPal Checkout / Orders v2 API
4              
5 2     2   13 use Moo;
  2         12  
  2         13  
6 2     2   789 use Carp qw(croak);
  2         8  
  2         155  
7 2     2   1197 use WWW::PayPal::Order;
  2         10  
  2         98  
8 2     2   20 use namespace::clean;
  2         5  
  2         21  
9              
10             our $VERSION = '0.001';
11              
12              
13             has client => (
14             is => 'ro',
15             required => 1,
16             weak_ref => 1,
17             );
18              
19              
20             has openapi_operations => (
21             is => 'lazy',
22             builder => sub {
23             # Pre-computed from paypal-rest-api-specifications
24             # (openapi/checkout_orders_v2.json). Regenerate manually when the
25             # spec changes.
26             return {
27 1     1   38 'orders.create' => { method => 'POST', path => '/v2/checkout/orders' },
28             'orders.get' => { method => 'GET', path => '/v2/checkout/orders/{id}' },
29             'orders.patch' => { method => 'PATCH', path => '/v2/checkout/orders/{id}' },
30             'orders.confirm' => { method => 'POST', path => '/v2/checkout/orders/{id}/confirm-payment-source' },
31             'orders.authorize' => { method => 'POST', path => '/v2/checkout/orders/{id}/authorize' },
32             'orders.capture' => { method => 'POST', path => '/v2/checkout/orders/{id}/capture' },
33             };
34             },
35             );
36              
37              
38             with 'WWW::PayPal::Role::OpenAPI';
39              
40             sub _wrap {
41 0     0     my ($self, $data) = @_;
42 0           return WWW::PayPal::Order->new(client => $self->client, data => $data);
43             }
44              
45             sub create {
46 0     0 1   my ($self, %args) = @_;
47              
48 0 0         croak 'intent required' unless $args{intent};
49             croak 'purchase_units required'
50 0 0 0       unless ref $args{purchase_units} eq 'ARRAY' && @{$args{purchase_units}};
  0            
51              
52             my $body = {
53             intent => $args{intent},
54             purchase_units => $args{purchase_units},
55 0           };
56              
57             # Application context: return/cancel URLs and branding
58 0           my %ctx;
59 0 0         $ctx{return_url} = $args{return_url} if $args{return_url};
60 0 0         $ctx{cancel_url} = $args{cancel_url} if $args{cancel_url};
61 0 0         $ctx{brand_name} = $args{brand_name} if $args{brand_name};
62 0 0         $ctx{locale} = $args{locale} if $args{locale};
63 0   0       $ctx{user_action} = $args{user_action} // 'PAY_NOW';
64             $ctx{shipping_preference} = $args{shipping_preference}
65 0 0         if $args{shipping_preference};
66 0 0         if (%ctx) {
67             # PayPal supports both payment_source.paypal.experience_context (new)
68             # and application_context (legacy). We use application_context for
69             # broad compatibility.
70 0           $body->{application_context} = \%ctx;
71             }
72              
73 0 0         $body->{payer} = $args{payer} if $args{payer};
74              
75 0           my $data = $self->call_operation('orders.create', body => $body);
76 0           return $self->_wrap($data);
77             }
78              
79              
80             sub get {
81 0     0 1   my ($self, $id) = @_;
82 0 0         croak 'order id required' unless $id;
83 0           my $data = $self->call_operation('orders.get', path => { id => $id });
84 0           return $self->_wrap($data);
85             }
86              
87              
88             sub capture {
89 0     0 1   my ($self, $id, %args) = @_;
90 0 0         croak 'order id required' unless $id;
91              
92             my $data = $self->call_operation('orders.capture',
93             path => { id => $id },
94             body => $args{body} || {},
95 0   0       );
96 0           return $self->_wrap($data);
97             }
98              
99              
100             sub authorize {
101 0     0 1   my ($self, $id, %args) = @_;
102 0 0         croak 'order id required' unless $id;
103             my $data = $self->call_operation('orders.authorize',
104             path => { id => $id },
105             body => $args{body} || {},
106 0   0       );
107 0           return $self->_wrap($data);
108             }
109              
110              
111             1;
112              
113             __END__