line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Business::CPI::Role::Cart; |
2
|
|
|
|
|
|
|
# ABSTRACT: Shopping cart or an order |
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
1933
|
use Moo::Role; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
26
|
|
5
|
4
|
|
|
4
|
|
1109
|
use Scalar::Util qw/blessed/; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
277
|
|
6
|
4
|
|
|
4
|
|
20
|
use Carp qw/croak/; |
|
4
|
|
|
|
|
19
|
|
|
4
|
|
|
|
|
204
|
|
7
|
4
|
|
|
4
|
|
22
|
use Business::CPI::Util::Types qw/Money/; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
35
|
|
8
|
4
|
|
|
4
|
|
1614
|
use Types::Standard qw/ArrayRef/; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
51
|
|
9
|
4
|
|
|
4
|
|
2130
|
use List::Util qw/sum/; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
3484
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.922'; # VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has id => ( is => 'rw' ); |
14
|
|
|
|
|
|
|
has gateway_id => ( is => 'rw' ); |
15
|
|
|
|
|
|
|
has gateway_fee => ( is => 'rwp' ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has buyer => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
isa => sub { |
20
|
|
|
|
|
|
|
$_[0]->does('Business::CPI::Role::Buyer') |
21
|
|
|
|
|
|
|
or $_[0]->does('Business::CPI::Role::Account') |
22
|
|
|
|
|
|
|
or die "Must implement Business::CPI::Role::Buyer or Business::CPI::Role::Account"; |
23
|
|
|
|
|
|
|
}, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has tax => ( |
27
|
|
|
|
|
|
|
coerce => Money->coercion, |
28
|
|
|
|
|
|
|
isa => Money, |
29
|
|
|
|
|
|
|
is => 'rw', |
30
|
|
|
|
|
|
|
default => sub { 0 }, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has handling => ( |
34
|
|
|
|
|
|
|
coerce => Money->coercion, |
35
|
|
|
|
|
|
|
isa => Money, |
36
|
|
|
|
|
|
|
is => 'rw', |
37
|
|
|
|
|
|
|
default => sub { 0 }, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has discount => ( |
41
|
|
|
|
|
|
|
coerce => Money->coercion, |
42
|
|
|
|
|
|
|
isa => Money, |
43
|
|
|
|
|
|
|
is => 'rw', |
44
|
|
|
|
|
|
|
default => sub { 0 }, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has shipping => ( |
48
|
|
|
|
|
|
|
coerce => Money->coercion, |
49
|
|
|
|
|
|
|
isa => Money, |
50
|
|
|
|
|
|
|
is => 'rw', |
51
|
|
|
|
|
|
|
default => sub { 0 }, |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
has _gateway => ( |
56
|
|
|
|
|
|
|
is => 'ro', |
57
|
|
|
|
|
|
|
required => 1, |
58
|
|
|
|
|
|
|
isa => sub { |
59
|
|
|
|
|
|
|
$_[0]->isa('Business::CPI::Gateway::Base') |
60
|
|
|
|
|
|
|
or die "Must be a Business::CPI::Gateway::Base"; |
61
|
|
|
|
|
|
|
}, |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
has _items => ( |
65
|
|
|
|
|
|
|
isa => ArrayRef, |
66
|
|
|
|
|
|
|
is => 'ro', |
67
|
|
|
|
|
|
|
default => sub { [] }, |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
has _receivers => ( |
71
|
|
|
|
|
|
|
isa => ArrayRef, |
72
|
|
|
|
|
|
|
is => 'ro', |
73
|
|
|
|
|
|
|
default => sub { [] }, |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub get_total_shipping { |
77
|
4
|
|
|
4
|
1
|
10286
|
my ($self) = @_; |
78
|
|
|
|
|
|
|
|
79
|
4
|
|
|
|
|
7
|
my $amount = 0; |
80
|
|
|
|
|
|
|
|
81
|
4
|
|
|
|
|
7
|
foreach my $item (@{ $self->_items }) { |
|
4
|
|
|
|
|
15
|
|
82
|
12
|
|
|
|
|
13
|
my $item_shipping = 0; |
83
|
|
|
|
|
|
|
|
84
|
12
|
50
|
|
|
|
35
|
if ($item->has_shipping) { |
85
|
12
|
50
|
|
|
|
56
|
$item_shipping = $item->shipping + |
86
|
|
|
|
|
|
|
( $item->quantity - 1 ) * |
87
|
|
|
|
|
|
|
( $item->has_shipping_additional |
88
|
|
|
|
|
|
|
? $item->shipping_additional |
89
|
|
|
|
|
|
|
: $item->shipping ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
12
|
|
|
|
|
22
|
$amount += $item_shipping; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
4
|
|
|
|
|
18
|
return $amount + $self->shipping; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get_total_amount { |
99
|
2
|
|
|
2
|
1
|
1117
|
my ($self) = @_; |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
8
|
my $amount = sum( map { $_->price * $_->quantity } @{ $self->_items } ); |
|
6
|
|
|
|
|
41
|
|
|
2
|
|
|
|
|
11
|
|
102
|
|
|
|
|
|
|
|
103
|
2
|
|
|
|
|
7
|
$amount += |
104
|
|
|
|
|
|
|
$self->get_total_shipping + |
105
|
|
|
|
|
|
|
$self->tax + |
106
|
|
|
|
|
|
|
$self->handling - |
107
|
|
|
|
|
|
|
$self->discount; |
108
|
|
|
|
|
|
|
|
109
|
2
|
|
|
|
|
261
|
return $amount; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub get_item { |
113
|
4
|
|
|
4
|
1
|
6083
|
my ($self, $item_id) = @_; |
114
|
|
|
|
|
|
|
|
115
|
4
|
|
|
|
|
6
|
for (my $i = 0; $i < @{ $self->_items }; $i++) { |
|
10
|
|
|
|
|
25
|
|
116
|
10
|
|
|
|
|
11
|
my $item = $self->_items->[$i]; |
117
|
10
|
100
|
|
|
|
21
|
if ($item->id eq "$item_id") { |
118
|
4
|
|
|
|
|
10
|
return $item; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
0
|
return undef; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub add_item { |
126
|
12
|
|
|
12
|
1
|
70
|
my ($self, $info) = @_; |
127
|
|
|
|
|
|
|
|
128
|
12
|
50
|
|
|
|
49
|
if (blessed $info) { |
129
|
0
|
|
|
|
|
0
|
croak q|Usage: $cart->add_item({ ... })|; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
12
|
|
|
|
|
80
|
my $item = $self->_gateway->item_class->new($info); |
133
|
|
|
|
|
|
|
|
134
|
12
|
|
|
|
|
484
|
push @{ $self->_items }, $item; |
|
12
|
|
|
|
|
48
|
|
135
|
|
|
|
|
|
|
|
136
|
12
|
|
|
|
|
35
|
return $item; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub add_receiver { |
140
|
2
|
|
|
2
|
1
|
3
|
my ($self, $info) = @_; |
141
|
|
|
|
|
|
|
|
142
|
2
|
50
|
|
|
|
9
|
if (blessed $info) { |
143
|
0
|
|
|
|
|
0
|
croak q|Usage: $cart->add_receiver({ ... })|; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
2
|
|
|
|
|
8
|
my $gateway = $self->_gateway; |
147
|
2
|
|
|
|
|
3
|
$info->{_gateway} = $gateway; |
148
|
|
|
|
|
|
|
|
149
|
2
|
|
|
|
|
17
|
my $item = $gateway->receiver_class->new($info); |
150
|
|
|
|
|
|
|
|
151
|
2
|
|
|
|
|
20
|
push @{ $self->_receivers }, $item; |
|
2
|
|
|
|
|
8
|
|
152
|
|
|
|
|
|
|
|
153
|
2
|
|
|
|
|
8
|
return $item; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub get_form_to_pay { |
157
|
3
|
|
|
3
|
1
|
2117
|
my ($self, $payment) = @_; |
158
|
|
|
|
|
|
|
|
159
|
3
|
|
|
|
|
45
|
return $self->_gateway->get_form({ |
160
|
|
|
|
|
|
|
payment_id => $payment, |
161
|
3
|
|
|
|
|
15
|
items => [ @{ $self->_items } ], # make a copy for security |
162
|
|
|
|
|
|
|
buyer => $self->buyer, |
163
|
|
|
|
|
|
|
cart => $self, |
164
|
|
|
|
|
|
|
}); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub get_checkout_code { |
169
|
0
|
|
|
0
|
1
|
|
my ($self, $payment) = @_; |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
return $self->_gateway->get_checkout_code({ |
172
|
|
|
|
|
|
|
payment_id => $payment, |
173
|
0
|
|
|
|
|
|
items => [ @{ $self->_items } ], |
174
|
|
|
|
|
|
|
buyer => $self->buyer, |
175
|
|
|
|
|
|
|
cart => $self, |
176
|
|
|
|
|
|
|
}); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
__END__ |