diff --git a/lib/WebGUI/Asset/Sku/Subscription.pm b/lib/WebGUI/Asset/Sku/Subscription.pm index 9d89dd4ec..76d55e991 100644 --- a/lib/WebGUI/Asset/Sku/Subscription.pm +++ b/lib/WebGUI/Asset/Sku/Subscription.pm @@ -77,7 +77,7 @@ property duration => ( ); sub _duration_options { my $session = shift->session; - return WebGUI::Shop::Pay->new( $session )->getRecurringPeriodValues, + return WebGUI::Shop::Pay->new( session => $session )->getRecurringPeriodValues, } property executeOnSubscription => ( fieldType => 'text', diff --git a/lib/WebGUI/Content/Shop.pm b/lib/WebGUI/Content/Shop.pm index f52b4c8a9..8fd954c87 100644 --- a/lib/WebGUI/Content/Shop.pm +++ b/lib/WebGUI/Content/Shop.pm @@ -177,7 +177,7 @@ sub www_pay { my $session = shift; my $output = undef; my $method = "www_".$session->form->get("method"); - my $pay = WebGUI::Shop::Pay->new($session); + my $pay = WebGUI::Shop::Pay->new(session => $session); if ($method ne "www_" && $pay->can($method)) { $output = $pay->$method(); } diff --git a/lib/WebGUI/Shop/Pay.pm b/lib/WebGUI/Shop/Pay.pm index bdb54fad0..6f4e66147 100644 --- a/lib/WebGUI/Shop/Pay.pm +++ b/lib/WebGUI/Shop/Pay.pm @@ -16,7 +16,8 @@ package WebGUI::Shop::Pay; use strict; -use Class::InsideOut qw{ :std }; +#use Class::InsideOut qw{ :std }; +use Moose; use WebGUI::Exception; use WebGUI::International; use WebGUI::Pluggable; @@ -24,6 +25,7 @@ use WebGUI::Shop::Admin; #use WebGUI::Shop::PayDriver; use WebGUI::Utility; use Tie::IxHash; +use Scalar::Util; =head1 NAME @@ -43,8 +45,34 @@ These subroutines are available from this package: =cut -readonly session => my %session; +#------------------------------------------------------------------- +=head2 session () + +Returns a reference to the current session. + +=cut + + +has session => ( + is => 'ro', + required => 1, +); + +around BUILDARGS => sub { + my $orig = shift; + my $className = shift; + + ##Original arguments start here. + if (ref $_[0] eq 'HASH') { + return $className->$orig(@_); + } + my $protoSession = $_[0]; + if (blessed $protoSession && $protoSession->isa('WebGUI::Session')) { + return $className->$orig(session => $protoSession); + } + return $className->$orig(@_); +}; #------------------------------------------------------------------- @@ -215,36 +243,6 @@ sub getRecurringPeriodValues { } -#------------------------------------------------------------------- - -=head2 new ( $session ) - -Constructor. - -=head3 $session - -A WebGUI::Session object. - -=cut - -sub new { - my $class = shift; - my $session = shift; - WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error => q{Must provide a session variable}) unless ref $session eq 'WebGUI::Session'; - my $self = register $class; - my $id = id $self; - $session{ $id } = $session; - return $self; -} - -#------------------------------------------------------------------- - -=head2 session () - -Returns a reference to the current session. - -=cut - #------------------------------------------------------------------- =head2 www_addPaymentGateway ( $session ) diff --git a/lib/WebGUI/Shop/Transaction.pm b/lib/WebGUI/Shop/Transaction.pm index 3eeb7e318..0fc69f615 100644 --- a/lib/WebGUI/Shop/Transaction.pm +++ b/lib/WebGUI/Shop/Transaction.pm @@ -367,7 +367,7 @@ Returns a reference to the payment gateway attached to this transaction. sub getPaymentGateway { my ($self) = @_; - my $pay = WebGUI::Shop::Pay->new($self->session); + my $pay = WebGUI::Shop::Pay->new(session => $self->session); return $pay->getPaymentGateway($self->get('paymentDriverId')); } diff --git a/t/Shop/Pay.t b/t/Shop/Pay.t index 1d1e87055..ebafcc0a9 100644 --- a/t/Shop/Pay.t +++ b/t/Shop/Pay.t @@ -18,7 +18,7 @@ use strict; use lib "$FindBin::Bin/../lib"; use Test::More; use Test::Deep; -#use Test::Exception; +use Test::Exception; use JSON; use HTML::Form; @@ -30,12 +30,6 @@ use WebGUI::TestException; # Init my $session = WebGUI::Test->session; -#---------------------------------------------------------------------------- -# Tests - -my $tests = 18; -plan tests => 1 + $tests; - #---------------------------------------------------------------------------- # put your tests here @@ -45,31 +39,19 @@ my $storage; my $newDriver; my $anotherDriver; -SKIP: { - -skip 'Unable to load module WebGUI::Shop::Pay', $tests unless $loaded; - ####################################################################### # # new # ####################################################################### -my $e; my $pay; +dies_ok { $pay = WebGUI::Shop::Pay->new(); } + 'new takes an exception to not giving it a session variable'; -throws_deeply ( sub { $pay = WebGUI::Shop::Pay->new(); }, - 'WebGUI::Error::InvalidObject', - { - error => 'Must provide a session variable', - got => '', - expected => 'WebGUI::Session', - }, - 'new takes an exception to not giving it a session variable' -); - -$pay = WebGUI::Shop::Pay->new($session); +lives_ok { $pay = WebGUI::Shop::Pay->new(session => $session); } 'new called with hash arguments'; +lives_ok { $pay = WebGUI::Shop::Pay->new($session); } 'new called only with session'; isa_ok($pay, 'WebGUI::Shop::Pay', 'new returned the right kind of object'); ####################################################################### @@ -232,14 +214,11 @@ cmp_bag( # ####################################################################### - -} +done_testing(); #---------------------------------------------------------------------------- # Cleanup END { defined $newDriver and $newDriver->delete; defined $newDriver and $anotherDriver->delete; - my $count = $session->db->quickScalar('select count(*) from paymentGateway'); - is($count, 2, 'WebGUI ships with two drivers by default'); }