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