From fc3ccba4f36963336ab6e8a2311f5dae90d34228 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Fri, 7 Mar 2008 20:16:09 +0000 Subject: [PATCH] added payment manager --- docs/upgrades/upgrade_7.5.2-7.5.3.pl | 10 ++ etc/WebGUI.conf.original | 2 +- lib/WebGUI/Shop/Pay.pm | 238 +++++++++++++++++++++++++++ 3 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 lib/WebGUI/Shop/Pay.pm diff --git a/docs/upgrades/upgrade_7.5.2-7.5.3.pl b/docs/upgrades/upgrade_7.5.2-7.5.3.pl index aa827288a..d6ebecafb 100644 --- a/docs/upgrades/upgrade_7.5.2-7.5.3.pl +++ b/docs/upgrades/upgrade_7.5.2-7.5.3.pl @@ -33,6 +33,7 @@ createDonationAsset($session); addShippingDrivers($session); addShoppingHandler($session); addAddressBook($session); +addPaymentDrivers($session); finish($session); # this line required @@ -196,6 +197,15 @@ EOSQL } +#------------------------------------------------- +sub addPaymentDrivers { + my $session = shift; + print "\tSet up the default payment dirvers.\n" unless ($quiet); + # and here's our code + $session->config->delete('paymentPlugins'); + $session->config->addToArray('paymentDrivers', 'WebGUI::Shop::PayDriver::Cash'); +} + #------------------------------------------------- sub addShippingDrivers { my $session = shift; diff --git a/etc/WebGUI.conf.original b/etc/WebGUI.conf.original index 5d6c29441..f5d12ec40 100644 --- a/etc/WebGUI.conf.original +++ b/etc/WebGUI.conf.original @@ -171,7 +171,7 @@ # List the merchant gateways you have installed and wish to be # available on this site. -"paymentPlugins" : ["ITransact","Cash"], +"paymentDrivers" : ["WebGUI::Shop::PayDriver::Cash"], # List the shipping drivers you have installed and wish to be # available for configuration on the site. diff --git a/lib/WebGUI/Shop/Pay.pm b/lib/WebGUI/Shop/Pay.pm new file mode 100644 index 000000000..795e5bbdc --- /dev/null +++ b/lib/WebGUI/Shop/Pay.pm @@ -0,0 +1,238 @@ +package WebGUI::Shop::Pay; + +use strict; + +use Class::InsideOut qw{ :std }; +use WebGUI::Exception; +use WebGUI::International; +use WebGUI::Pluggable; +use WebGUI::Shop::Admin; +#use WebGUI::Shop::PayDriver; +use WebGUI::Utility; + +=head1 NAME + +Package WebGUI::Shop::Pay + +=head1 DESCRIPTION + +This is the master class to manage pay drivers. + +=head1 SYNOPSIS + + use WebGUI::Shop::Pay; + +=head1 METHODS + +These subroutines are available from this package: + +=cut + +readonly session => my %session; + + +#------------------------------------------------------------------- + +=head2 addPaymentGateway ( $class, $options ) + +The interface method for creating new, configured instances of PayDriver. If the PayDriver throws an exception, it is propagated +back up to the top. + +=head4 $class + +The class of the new PayDriver object to create. + +=head4 $options + +A list of properties to assign to this PayDriver. See C for details. + +=cut + +sub addPaymentGateway { + my $self = shift; + my $session = shift; + my $requestedClass = shift; + WebGUI::Error::InvalidParam->throw(error => q{Must provide a class to create an object}) + unless defined $requestedClass; + WebGUI::Error::InvalidParam->throw(error => q{The requested class is not enabled in your WebGUI configuration file}, param => $requestedClass) + unless isIn($requestedClass, keys %{$self->getDrivers($session) } ); + my $options = shift; + WebGUI::Error::InvalidParam->throw(error => q{You must pass a hashref of options to create a new PayDriver object}) + unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options }; + my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'create', [ $session, $options ]) }; + return $driver; +} + +#------------------------------------------------------------------- + +=head2 getDrivers ( ) + +This subroutine returns a hash reference of available shipping driver classes as keys with their human readable names as values, read from the WebGUI config file in the shippingDrivers directive. + +=cut + +sub getDrivers { + my $self = shift; + my %drivers = (); + foreach my $class (@{$self->session->config->get('paymentDrivers')}) { + $drivers{$class} = eval { WebGUI::Pluggable::instanciate($class, 'getName', [ $self->session ])}; + } + return \%drivers; +} + +#------------------------------------------------------------------- + +=head2 getOptions ( $cart ) + +Returns a list of options for the user to pay to. It is a hash of hashrefs, with the key of the primary hash being the paymentGatewayId of the driver, and sub keys of label and button. + +=head3 $cart + +A WebGUI::Shop::Cart object. A WebGUI::Error::InvalidParam exception will be thrown if it doesn't get one. + +=head3 + +=cut + +sub getOptions { + my ($self, $cart) = @_; + WebGUI::Error::InvalidParam->throw(error => q{Need a cart.}) unless defined $cart and $cart->isa("WebGUI::Shop::Cart"); + my $session = $cart->session; + my %options = (); + foreach my $gateway (@{$self->getPaymentGateways()}) { + $options{$gateway->getId} = { + label => $gateway->get("label"), + button => $gateway->getButton($cart), + }; + } + return \%options; +} + +#------------------------------------------------------------------- + +=head2 getPaymentGateway ( ) + +Looks up an existing PayDriver in the db by paymentGatewayId and returns +that object. If the PayDriver throws an exception, it is propagated +back up to the top. + +=head3 id + +The id of the gateway to instanciate. + +=cut + +sub getPaymentGateway { + my ($self, $gatewayId) = @_; + my $session = $self->session; + WebGUI::Error::InvalidParam->throw(error => q{Must provide a paymentGatewayId}) + unless defined $gatewayId; + my $requestedClass = $session->db->quickScalar('select className from paymentGateway where paymentGatewayId=?',[$gatewayId]); + WebGUI::Error::ObjectNotFound->throw(error => q{payment gateway not found in db}, id => $gatewayId) + unless $requestedClass; + my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'new', [ $session, $gatewayId ]) }; + return $driver; +} + +#------------------------------------------------------------------- + +=head2 getPaymentGateways ( ) + +Returns an array ref of all payment gateway objects in the db. + +=head3 + +=cut + +sub getPaymentGateways { + my $self = shift; + my @drivers = (); + my $sth = $self->session->db->prepare('select paymentGatewayId from paymentGateway'); + $sth->execute(); + while (my $driver = $sth->hashRef()) { + push @drivers, $self->getPaymentGateway($driver->{paymentGatewayId}); + } + $sth->finish; + return \@drivers; +} + +#------------------------------------------------------------------- + +=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_do ( ) + +Let's payment gateway drivers do method calls. Requires a driver param in the post form vars which contains the id of the driver to load. + +=cut + +sub www_do { + my ($self) = @_; + my $form = $self->session->form; + WebGUI::Error::InvalidParam->throw(error => q{must have a form var called driver with a driver id }) if ($form->get("driver") eq ""); + WebGUI::Error::InvalidParam->throw(error => q{must have a form var called do with a www_ method to call }) if ($form->get("do") eq ""); + my $driver = $self->getPaymentGateway($form->get("driver")); + my $output = undef; + my $method = "www_". ( $form->get("do")); + if ($driver->can($method)) { + $output = $driver->$method(); + } + return $output; +} + +#------------------------------------------------------------------- + +=head2 www_manage ( ) + +The main management screen for payment gateways. + +=cut + +sub www_manage { + my ($self) = @_; + my $session = $self->session; + return $session->privilege->adminOnly() unless ($session->user->isInGroup("3")); + my $admin = WebGUI::Shop::Admin->new($session); + my $i18n = WebGUI::International->new($session, "Shop"); + my $output = WebGUI::Form::formHeader($session) + .WebGUI::Form::hidden($session, {name=>"shop", value=>"pay"}) + .WebGUI::Form::hidden($session, {name=>"method", value=>"addDriver"}) + .WebGUI::Form::selectBox($session, {name=>"className", options=>$self->getDrivers}) + .WebGUI::Form::submit($session, {value=>$i18n->get("add payment method")}) + .WebGUI::Form::formFooter($session); + foreach my $payer (@{$self->getPaymentGateways}) { + + } + my $console = $admin->getAdminConsole; + return $console->render($output, $i18n->get("payment methods")); +} + +1;