PayPal/API/Orders.pm

filter: Statementstmt 83.7% 67 / 80 Branchbran 48.2% 27 / 56 Conditioncond 33.3% 8 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 66.6% 8 / 12 Podpod 100% 5 / 5 totaltotal 60.2% 115 / 191 CC 41 SCAR 32.4
CC
41
Cov
64%
checkout
41.7
create
15.9
capture
12.0
CRAP 25.4
« PayPal/API/Subscriptions.pm ↑ Summary PayPal/API/Products.pm »
1 package WWW::PayPal::API::Orders;
2  
3 # ABSTRACT: PayPal Checkout / Orders v2 API
4  
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use WWW::PayPal::Order;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.003';
11  
12  
13 3 has client => (
14 is => 'ro',
15 required => 1,
16 weak_ref => 1,
17 );
18  
19  
20 has openapi_operations => (
21 is => 'lazy',
22 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build_openapi_operations: called CC=1 SCAR=1.2
23 # Pre-computed from paypal-rest-api-specifications
24 # (openapi/checkout_orders_v2.json). Regenerate manually when the
25 # spec changes.
26 return {
27 0 '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 3 );
36  
37  
38 3 with 'WWW::PayPal::Role::OpenAPI';
39  
40 1 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
41 1 my ($self, $data) = @_;
42 1 return WWW::PayPal::Order->new(client => $self->client, data => $data);
43 }
44  
45 2 ▶sub checkout {
Subroutine checkout: called CC=25 SCAR=37.3
Pod documented
46 2 my ($self, %args) = @_;
47  
48 2 ▶ croak 'amount required' unless defined $args{amount};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"amount"} 1 1 2
49 1 ▶ croak 'currency required' unless defined $args{currency};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"currency"} 0 1 1
50 1 ▶ croak 'return_url required' unless defined $args{return_url};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"return_url"} 0 1 1
51 1 ▶ croak 'cancel_url required' unless defined $args{cancel_url};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"cancel_url"} 0 1 1
52  
53 1 my $currency = $args{currency};
54 1 my $amount = $args{amount};
55  
56 1 my $pu = {
57 amount => { currency_code => $currency, value => $amount },
58 };
59 1 ▶ $pu->{description} = $args{description} if defined $args{description};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"description"} 1 0 1
60 1 ▶ $pu->{invoice_id} = $args{invoice_id} if defined $args{invoice_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"invoice_id"} 1 0 1
61 1 ▶ $pu->{custom_id} = $args{custom_id} if defined $args{custom_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"custom_id"} 1 0 1
62 1 ▶ $pu->{soft_descriptor} = $args{soft_descriptor} if defined $args{soft_descriptor};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"soft_descriptor"} 0 1 1
63 1 ▶ $pu->{reference_id} = $args{reference_id} if defined $args{reference_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"reference_id"} 0 1 1
64  
65 1 ▶ if ($args{items} && @{ $args{items} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"items"} and @{$args{"items"};}) 1 0 1
Condition33%1/3
$args{"items"} and @{$args{"items"};}
!ll&&!rl&&r
001
MC/DC0%0/2
$args{"items"} and @{$args{"items"};}
$args{"items"} @{$args{"items"};}
Truth table33%1/3
A and B
A= $args{"items"}B= @{$args{"items"};}
ABresult
0X0
100
111
66 1 my @items;
67 1 my $item_total = 0;
68 1 for my $i (@{ $args{items} }) {
69 my $unit = $i->{unit_amount} // $i->{price}
70 1 ▶ or croak 'item unit_amount required';
Branch: 1 branch
BranchTrueFalseTotal
my $unit = $i->{"unit_amount"} // $i->{"price"} or croak("item unit_amount required") 1 0 1
Condition33%1/3
$i->{"unit_amount"} // $i->{"price"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$i->{"unit_amount"} // $i->{"price"}
$i->{"unit_amount"} $i->{"price"}
Truth table33%1/3
A // B
A= $i->{"unit_amount"}B= $i->{"price"}
ABresult
1X1
011
000
71 1 ▶ my $qty = $i->{quantity} // 1;
Condition50%1/2
$i->{"quantity"} // 1
l!l
10
MC/DC0%0/1
$i->{"quantity"} // 1
$i->{"quantity"}
Truth table50%1/2
A
A= $i->{"quantity"}
Aresult
11
00
72 push @items, {
73 name => $i->{name} // croak('item name required'),
74 quantity => "$qty",
75 unit_amount => {
76 currency_code => $i->{currency_code} // $currency,
77 value => "$unit",
78 },
79 (defined $i->{sku} ? (sku => $i->{sku}) : ()),
80 (defined $i->{description} ? (description => $i->{description}) : ()),
81 1 ▶ (defined $i->{category} ? (category => $i->{category}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $i->{'sku'} ? : 1 0 1
defined $i->{'description'} ? : 0 1 1
defined $i->{'category'} ? : 0 1 1
Condition40%2/5
$i->{"name"} // croak("item name required")
l!l
10
$i->{"currency_code"} // $currency
l!l&&r!l&&!r
010
MC/DC0%0/1
$i->{"name"} // croak("item name required")
$i->{"name"}
MC/DC0%0/2
$i->{"currency_code"} // $currency
$i->{"currency_code"} $currency
Truth table50%1/2
A
A= $i->{"name"}
Aresult
11
00
Truth table33%1/3
A // B
A= $i->{"currency_code"}B= $currency
ABresult
1X1
011
000
82 };
83 1 $item_total += $unit * $qty;
84 }
85 1 $pu->{items} = \@items;
86 # PayPal requires a breakdown when items are present
87 $pu->{amount}{breakdown} = {
88 1 item_total => {
89 currency_code => $currency,
90 value => sprintf('%.2f', $item_total),
91 },
92 };
93 }
94  
95 1 ▶ $pu->{shipping} = $args{shipping} if $args{shipping};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"shipping"} 0 1 1
96  
97 my %create = (
98 intent => $args{intent} // 'CAPTURE',
99 purchase_units => [$pu],
100 return_url => $args{return_url},
101 cancel_url => $args{cancel_url},
102 1 ▶ );
Condition50%1/2
$args{"intent"} // "CAPTURE"
l!l
01
MC/DC0%0/1
$args{"intent"} // "CAPTURE"
$args{"intent"}
Truth table50%1/2
A
A= $args{"intent"}
Aresult
11
00
103 1 for my $k (qw(brand_name locale user_action shipping_preference payer)) {
104 5 ▶ $create{$k} = $args{$k} if defined $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$k} 2 3 5
105 }
106  
107 1 return $self->create(%create);
108 }
109  
110  
111 1 ▶sub create {
Subroutine create: called CC=12 SCAR=27.7
Pod documented
112 1 my ($self, %args) = @_;
113  
114 1 ▶ croak 'intent required' unless $args{intent};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"intent"} 0 1 1
115 croak 'purchase_units required'
116 1 ▶ unless ref $args{purchase_units} eq 'ARRAY' && @{$args{purchase_units}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"purchase_units"} eq "ARRAY" and @{$args{"purchase_units"};} 0 1 1
Condition33%1/3
ref $args{"purchase_units"} eq "ARRAY" and @{$args{"purchase_units"};}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $args{"purchase_units"} eq "ARRAY" and @{$args{"purchase_units"};}
ref $args{"purchase_units"} eq "ARRAY" @{$args{"purchase_units"};}
Truth table33%1/3
A and B
A= ref $args{"purchase_units"} eq "ARRAY"B= @{$args{"purchase_units"};}
ABresult
0X0
100
111
117  
118 my $body = {
119 intent => $args{intent},
120 purchase_units => $args{purchase_units},
121 1 };
122  
123 # Application context: return/cancel URLs and branding
124 1 my %ctx;
125 1 ▶ $ctx{return_url} = $args{return_url} if $args{return_url};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"return_url"} 1 0 1
126 1 ▶ $ctx{cancel_url} = $args{cancel_url} if $args{cancel_url};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"cancel_url"} 1 0 1
127 1 ▶ $ctx{brand_name} = $args{brand_name} if $args{brand_name};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"brand_name"} 1 0 1
128 1 ▶ $ctx{locale} = $args{locale} if $args{locale};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"locale"} 1 0 1
129 1 ▶ $ctx{user_action} = $args{user_action} // 'PAY_NOW';
Condition50%1/2
$args{"user_action"} // "PAY_NOW"
l!l
01
MC/DC0%0/1
$args{"user_action"} // "PAY_NOW"
$args{"user_action"}
Truth table50%1/2
A
A= $args{"user_action"}
Aresult
11
00
130 $ctx{shipping_preference} = $args{shipping_preference}
131 1 ▶ if $args{shipping_preference};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"shipping_preference"} 0 1 1
132 1 ▶ if (%ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (%ctx) 1 0 1
133 # PayPal supports both payment_source.paypal.experience_context (new)
134 # and application_context (legacy). We use application_context for
135 # broad compatibility.
136 1 $body->{application_context} = \%ctx;
137 }
138  
139 1 ▶ $body->{payer} = $args{payer} if $args{payer};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"payer"} 0 1 1
140  
141 1 my $data = $self->call_operation('orders.create', body => $body);
142 1 return $self->_wrap($data);
143 }
144  
145  
146 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
147 0 my ($self, $id) = @_;
148 0 ▶ croak 'order id required' unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
149 0 my $data = $self->call_operation('orders.get', path => { id => $id });
150 0 return $self->_wrap($data);
151 }
152  
153  
154 0 ▶sub capture {
Subroutine capture: not called CC=3 SCAR=24.8
Pod documented
155 0 my ($self, $id, %args) = @_;
156 0 ▶ croak 'order id required' unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
157  
158 my $data = $self->call_operation('orders.capture',
159 path => { id => $id },
160 body => $args{body} || {},
161 0 ▶ );
Condition0%0/2
$args{'body'} || {}
l!l
00
MC/DC0%0/1
$args{'body'} || {}
$args{'body'}
Truth table0%0/2
A
A= $args{'body'}
Aresult
11
00
162 0 return $self->_wrap($data);
163 }
164  
165  
166 0 ▶sub authorize {
Subroutine authorize: not called CC=3 SCAR=24.8
Pod documented
167 0 my ($self, $id, %args) = @_;
168 0 ▶ croak 'order id required' unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
169 my $data = $self->call_operation('orders.authorize',
170 path => { id => $id },
171 body => $args{body} || {},
172 0 ▶ );
Condition0%0/2
$args{'body'} || {}
l!l
00
MC/DC0%0/1
$args{'body'} || {}
$args{'body'}
Truth table0%0/2
A
A= $args{'body'}
Aresult
11
00
173 0 return $self->_wrap($data);
174 }
175  
176  
177 3 1;
178  
179 __END__
« PayPal/API/Subscriptions.pm ↑ Summary PayPal/API/Products.pm »