API/Resource/Carts.pm

filter: Statementstmt 25.0% 11 / 44 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 25.0% 3 / 12 Podpod 100% 9 / 9 totaltotal 21.9% 23 / 105 CC 17 SCAR 33.8
CC
17
Cov
14%
get_cart
72.0
update_cart
30.0
request
12.0
CRAP 29.3
« API/Resource/PAR.pm ↑ Summary API/Resource.pm »
1 # Copyright 2013 Digital River, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Net::MyCommerce::API::Resource::Carts;
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use base qw( Net::MyCommerce::API::Resource );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23
24 Net::MyCommerce::API::Resource::Carts
25
26 =head1 VERSION
27
28 version 1.0.1
29
30 =cut
31
32 2 our $VERSION = '1.0.1';
33
34 =head1 SCHEMA
35  
36 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/14-schemas-cart
37  
38 =head1 METHODS
39  
40 =head2 new ($args)
41  
42 Subclass Net::MyCommerce::API::Resource
43  
44 =cut
45  
46 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
47 0 my ($pkg, %args) = @_;
48 0 return $pkg->SUPER::new( %args, scope=>'carts' );
49 }
50  
51 =head2 request (%opts)
52  
53 Subclass Net::MyCommerce::API::Resource::request
54  
55 =cut
56  
57 0 ▶sub request {
Subroutine request: not called CC=3 SCAR=24.8
Pod documented
58 0 my ($self, %opts) = @_;
59 0 ▶ $opts{params} ||= {};
Condition0%0/2
$opts{"params"} ||= {}
l!l
00
MC/DC0%0/1
$opts{"params"} ||= {}
$opts{"params"}
Truth table0%0/2
A
A= $opts{"params"}
Aresult
11
00
60 0 ▶ $opts{params}->{language} ||= 'en_US';
Condition0%0/2
$opts{"params"}{"language"} ||= "en_US"
l!l
00
MC/DC0%0/1
$opts{"params"}{"language"} ||= "en_US"
$opts{"params"}{"language"}
Truth table0%0/2
A
A= $opts{"params"}{"language"}
Aresult
11
00
61 0 return $self->SUPER::request(%opts);
62 }
63  
64 =head2 create_cart
65  
66 Create a new cart
67  
68 Examples:
69  
70 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/7-example-create-cart-simple-one-product
71 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/21-example-create-cart-fill-in-shopper-information
72 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/18-example-create-cart-affiliate
73 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/35-example-create-shopping-cart-custom-fields
74 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/36-example-create-shopping-cart-offers
75  
76 =cut
77  
78 0 ▶sub create_cart {
Subroutine create_cart: not called CC=2 SCAR=17.9
Pod documented
79 0 my ($self, %opts) = @_;
80 0 ▶ $opts{item} ||= {};
Condition0%0/2
$opts{"item"} ||= {}
l!l
00
MC/DC0%0/1
$opts{"item"} ||= {}
$opts{"item"}
Truth table0%0/2
A
A= $opts{"item"}
Aresult
11
00
81 return $self->request(
82 method => 'POST',
83 path => '/carts',
84 params => $opts{params},
85 data => {
86 items => [
87 {
88 product_id => $opts{item}->{product_id},
89 quantity => $opts{item}->{quantity},
90 },
91 0 ]
92 },
93 );
94 }
95  
96 =head2 add_item
97  
98 Add a new item to an existing shopping cart
99  
100 =cut
101  
102 0 ▶sub add_item {
Subroutine add_item: not called CC=3 SCAR=24.8
Pod documented
103 0 my ($self, %opts) = @_;
104 0 ▶ $opts{item} ||= {};
Condition0%0/2
$opts{"item"} ||= {}
l!l
00
MC/DC0%0/1
$opts{"item"} ||= {}
$opts{"item"}
Truth table0%0/2
A
A= $opts{"item"}
Aresult
11
00
105 return $self->request(
106 method => 'POST',
107 path => $opts{cart_id} ? [ '/carts', $opts{cart_id} ] : '/carts',
108 params => $opts{params},
109 data => {
110 items => [
111 {
112 product_id => $opts{item}->{product_id},
113 quantity => $opts{item}->{quantity},
114 custom_fields => $opts{item}->{custom_fields},
115 offer_id => $opts{item}->{offer_id},
116 },
117 0 ▶ ]
Branch: 1 branch
BranchTrueFalseTotal
$opts{'cart_id'} ? : 0 0 0
118 },
119 );
120 }
121  
122 =head2 update_items
123  
124 Add/modify one or more items in an existing shopping cart
125  
126 Examples:
127  
128 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/20-example-update-cart-set-quantity
129  
130 =cut
131  
132 0 ▶sub update_items {
Subroutine update_items: not called CC=1 SCAR=6.9
Pod documented
133 0 my ($self, %opts) = @_;
134 return $self->request(
135 method => 'POST',
136 path => [ '/carts', $opts{cart_id} ],
137 params => $opts{params},
138 data => {
139 items => $opts{items},
140 coupon_code => $opts{coupon_code},
141 },
142 0 );
143 }
144  
145 =head2 update_cart
146  
147 Update shopping cart: one or more items, billing/shipping address, and/or coupon
148  
149 =cut
150  
151 0 ▶sub update_cart {
Subroutine update_cart: not called CC=5 SCAR=34.0
Pod documented
152 0 my ($self, %opts) = @_;
153 return $self->request(
154 method => 'POST',
155 path => [ '/carts', $opts{cart_id} ],
156 params => $opts{params},
157 data => {
158 ( $opts{billing_address} ? (billing_address => $opts{billing_address}) : () ),
159 ( $opts{shipping_address} ? (shipping_address => $opts{shipping_address}) : () ),
160 ( $opts{items} ? (items => $opts{items}) : () ),
161 0 ▶ ( $opts{coupon_code} ? (coupon_code => $opts{coupon_code}) : () ),
Branch: 4 branches
BranchTrueFalseTotal
$opts{'billing_address'} ? : 0 0 0
$opts{'shipping_address'} ? : 0 0 0
$opts{'items'} ? : 0 0 0
$opts{'coupon_code'} ? : 0 0 0
162 },
163 );
164 }
165  
166 =head2 remove_item
167  
168 Remove an item from an existing shopping cart
169  
170 =cut
171  
172 0 ▶sub remove_item {
Subroutine remove_item: not called CC=1 SCAR=6.9
Pod documented
173 0 my ($self, %opts) = @_;
174 return $self->request(
175 method => 'DELETE',
176 0 path => [ '/carts', $opts{cart_id}, 'items', $opts{item_id} ],
177 );
178 }
179  
180 =head2 get_cart
181  
182 Retrieve an existing shopping cart
183
184 Examples:
185  
186 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/19-example-get-cart
187 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/38-example-get-shopping-cart-select-or-hide-fields
188  
189 =cut
190  
191 0 ▶sub get_cart {
Subroutine get_cart: not called CC=8 SCAR=42.8
Pod documented
192 0 my ($self, %opts) = @_;
193 my ($error, $result) = $self->request(
194 method => 'GET',
195 path => [ '/carts', $opts{cart_id} ],
196 params => $opts{params},
197 0 );
198 0 ▶ return ($error, $result) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
199 0 ▶ if (ref($result) eq 'HASH' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH" and ref $result->{"content"} eq "HASH" and ref $result->{"content"}{"items"} eq "ARRAY") 0 0 0
Condition0%0/6
ref $result eq "HASH" and ref $result->{"content"} eq "HASH" and ref $result->{"content"}{"items"} eq "ARRAY"
!ll&&!rl&&r
000
ref $result eq "HASH" and ref $result->{"content"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $result eq "HASH" and ref $result->{"content"} eq "HASH" and ref $result->{"content"}{"items"} eq "ARRAY"
ref $result eq "HASH" ref $result->{"content"} eq "HASH" ref $result->{"content"}{"items"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= ref $result eq "HASH"B= ref $result->{"content"} eq "HASH"C= ref $result->{"content"}{"items"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
200 ref($result->{content}) eq 'HASH' &&
201 ref($result->{content}{items}) eq 'ARRAY') {
202 0 foreach my $item (@{$result->{content}{items}}) {
203 0 my $p = $item->{pricing};
204 0 ▶ my $q = $item->{quantity} || 1;
Condition0%0/2
$item->{'quantity'} || 1
l!l
00
MC/DC0%0/1
$item->{'quantity'} || 1
$item->{'quantity'}
Truth table0%0/2
A
A= $item->{'quantity'}
Aresult
11
00
205 0 $p->{non_discount_total} = sprintf("%.2f", $p->{total} + $p->{discount});
206 0 $p->{discount_unit_price} = sprintf("%.2f", $p->{unit_price} - $p->{discount}/$q);
207 }
208 0 my $discount = $result->{content}{pricing}{discount};
209 0 ▶ $result->{content}{pricing}{discount} = sprintf("%.2f",$discount) if $discount;
Branch: 1 branch
BranchTrueFalseTotal
if $discount 0 0 0
210 }
211 0 return ($error, $result);
212 }
213  
214 =head2 get_item
215  
216 Retrieve a single item from an existing shopping cart
217  
218 Examples:
219  
220 http://help.mycommerce.com/index.php/mycommerce-apis/cart-resource/19-example-get-cart
221  
222 =cut
223  
224 0 ▶sub get_item {
Subroutine get_item: not called CC=1 SCAR=6.9
Pod documented
225 0 my ($self, %opts) = @_;
226 return $self->request(
227 method => 'GET',
228 path => [ '/carts', $opts{cart_id}, 'items', $opts{item_id} ],
229 params => $opts{params},
230 0 );
231 }
232  
233 2 1;
« API/Resource/PAR.pm ↑ Summary API/Resource.pm »