Added choice of multiple payment gateways to commerce system.

This commit is contained in:
Martin Kamerbeek 2004-11-30 19:55:14 +00:00
parent 00074fd16d
commit 1ab8746c3a
7 changed files with 175 additions and 21 deletions

View file

@ -4,12 +4,49 @@ use strict;
use WebGUI::SQL;
use WebGUI::International;
use Tie::IxHash;
use WebGUI::HTMLForm;
#-------------------------------------------------------------------
sub configurationForm {
my ($self, $form, $f);
$self = shift;
$form = shift;
$f = WebGUI::HTMLForm->new;
$f->yesNo(
-name => $self->prepend('enabled'),
-value => $self->enabled,
#### intl ####
-label => 'Enable',
);
$f->raw($form);
return $f->printRowsOnly;
}
#-------------------------------------------------------------------
sub enabled {
return $_[0]->{_enabled};
}
#-------------------------------------------------------------------
sub get {
return $_[0]->{_properties}{$_[1]};
}
#-------------------------------------------------------------------
sub getEnabledPlugins {
my (@enabledPlugins, $plugin, @plugins);
@enabledPlugins = WebGUI::SQL->buildArray("select namespace from commerceSettings where type='Payment' and fieldName='enabled' and fieldValue='1'");
foreach (@enabledPlugins) {
$plugin = WebGUI::Commerce::Payment->load($_);
push(@plugins, $plugin) if ($plugin);
}
return \@plugins;
}
#-------------------------------------------------------------------
sub init {
my ($class, $namespace, $properties);
@ -18,7 +55,7 @@ sub init {
$properties = WebGUI::SQL->buildHashRef("select fieldName, fieldValue from commerceSettings where namespace=".quote($namespace)." and type='Payment'");
bless {_properties => $properties, _namespace=>$namespace}, $class;
bless {_properties=>$properties, _namespace=>$namespace, _enabled=>$properties->{enabled}}, $class;
}
#-------------------------------------------------------------------

View file

@ -10,7 +10,6 @@ use WebGUI::International;
our @ISA = qw(WebGUI::Commerce::Payment);
#-------------------------------------------------------------------
sub cancelRecurringPayment {
my ($self, $recurring);
@ -138,7 +137,7 @@ sub configurationForm {
-subText=> $i18n->get('testModeEnabled')
);
return $f->printRowsOnly;
return $self->SUPER::configurationForm($f->printRowsOnly);
}
#-------------------------------------------------------------------

View file

@ -59,6 +59,22 @@ our $HELP = {
}
]
},
'select payment gateway template' => {
title => 'help select payment template title',
body => 'help select payment template body',
related => [
{
tag => 'template language',
namespace => 'WebGUI'
},
{
tag => 'templates manage',
namespace => 'WebGUI'
}
]
},
};
1;

View file

@ -263,6 +263,7 @@ sub getOperations {
'listPendingTransactions' => 'WebGUI::Operation::Commerce',
'cancelTransaction' => 'WebGUI::Operation::Commerce',
'completePendingTransaction' => 'WebGUI::Operation::Commerce',
'selectPaymentGateway' => 'WebGUI::Operation::Commerce',
'viewPurchaseHistory' => 'WebGUI::Operation::TransactionLog',
'cancelRecurringTransaction' => 'WebGUI::Operation::TransactionLog',
};

View file

@ -17,6 +17,7 @@ use WebGUI::International;
use WebGUI::Template;
use WebGUI::HTTP;
use WebGUI::Paginator;
use WebGUI::Form;
#-------------------------------------------------------------------
sub _submenu {
@ -35,6 +36,14 @@ sub _submenu {
return $ac->render($workarea, $title);
}
#-------------------------------------------------------------------
sub _gatewaySelected {
return 0 unless ($session{form}{paymentGateway});
my $plugin = WebGUI::Commerce::Payment->load($session{form}{paymentGateway});
return 1 if ($plugin && $plugin->enabled);
return 0;
}
#-------------------------------------------------------------------
sub www_cancelTransaction {
my ($transaction, %var);
@ -61,12 +70,16 @@ sub www_checkoutConfirm {
$errors = shift;
$i18n = WebGUI::International->new('Commerce');
unless ($session{setting}{commercePaymentPlugin}) {
$var{errorLoop} = [{message=>$i18n->get('no payment gateway')}];
return WebGUI::Template::process($session{setting}{commerceConfirmCheckoutTemplateId}, 'Commerce/ConfirmCheckout', \%var);
}
# If the user isn't logged in yet, let him do so or have him create an account
if ($session{user}{userId} == 1) {
WebGUI::Session::setScratch('redirectAfterLogin', WebGUI::URL::page('op=checkout'));
return WebGUI::Operation::execute('displayLogin');
}
# If no payment gateway has been selected yet, have the user do so now.
return WebGUI::Operation::execute('selectPaymentGateway') unless _gatewaySelected;
$var{errorLoop} = [ map {{message => $_}} @{$errors} ] if $errors;
# Put contents of cart in template vars
@ -78,16 +91,12 @@ sub www_checkoutConfirm {
$var{recurringLoop} = $recurring;
$var{recurringItems} = scalar(@$recurring);
$plugin = WebGUI::Commerce::Payment->load($session{setting}{commercePaymentPlugin});
$plugin = WebGUI::Commerce::Payment->load($session{form}{paymentGateway});
# If the user isn't logged in yet, let him do so or have him create an account
if ($session{user}{userId} == 1) {
WebGUI::Session::setScratch('redirectAfterLogin', WebGUI::URL::page('op=checkout'));
return WebGUI::Operation::execute('displayLogin');
}
$f = WebGUI::HTMLForm->new;
$f->hidden('op', 'checkoutSubmit');
$f->hidden('paymentGateway', $session{form}{paymentGateway});
$f->raw($plugin->checkoutForm);
$f->submit($i18n->get('pay button'));
@ -103,14 +112,17 @@ sub www_checkoutSubmit {
@normal, $currentPurchase, $checkoutError, @resultLoop, %param, $normal, $recurring, $formError);
$i18n = WebGUI::International->new('Commerce');
# check if user has already logged in
if ($session{user}{userId} == 1) {
WebGUI::Session::setScratch('redirectAfterLogin', WebGUI::URL::page('op=checkout'));
return WebGUI::Operation::execute('displayLogin');
}
$plugin = WebGUI::Commerce::Payment->load($session{setting}{commercePaymentPlugin});
# Check if a valid payment gateway has bee selected. If not have the user do so.
return WebGUI::Operation::execute('selectPaymentGateway') unless _gatewaySelected;
$plugin = WebGUI::Commerce::Payment->load($session{form}{paymentGateway});
$shoppingCart = WebGUI::Commerce::ShoppingCart->new;
($normal, $recurring) = $shoppingCart->getItems;
@ -215,7 +227,7 @@ sub www_completePendingTransaction {
#-------------------------------------------------------------------
sub www_confirmTransaction {
my($plugin, %var);
$plugin = WebGUI::Commerce::Payment->load($session{setting}{commercePaymentPlugin});
$plugin = WebGUI::Commerce::Payment->load($session{form}{pg});
if ($plugin->confirmTransaction) {
WebGUI::Commerce::Transaction->new($plugin->getTransactionId)->completeTransaction;
@ -235,7 +247,7 @@ sub www_editCommerceSettings {
payment=>{label=>$i18n->get('payment tab')},
);
$paymentPlugin = $session{setting}{commercePaymentPlugin} || $session{config}{paymentPlugins}->[0];
$paymentPlugin = $session{config}{paymentPlugins}->[0];
$tabform = WebGUI::TabForm->new(\%tabs);
$tabform->hidden({name => 'op', value => 'editCommerceSettingsSave'});
@ -259,6 +271,12 @@ sub www_editCommerceSettings {
-value => $session{setting}{commerceCheckoutCanceledTemplateId},
-namespace => 'Commerce/CheckoutCanceled'
);
$tabform->getTab('general')->template(
-name => 'commerceSelectPaymentGatewayTemplateId',
-label => $i18n->get('checkout select payment template'),
-value => $session{setting}{commerceSelectPaymentGatewayTemplateId},
-namespace => 'Commerce/SelectPaymentGateway'
);
$tabform->getTab('general')->email(
-name => 'commerceSendDailyReportTo',
-label => $i18n->get('daily report email'),
@ -281,7 +299,7 @@ sub www_editCommerceSettings {
$tabform->getTab('payment')->raw('<script language="JavaScript" > var activePayment="'.$paymentPlugin.'"; </script>');
$tabform->getTab("payment")->selectList(
-name => 'commercePaymentPlugin',
-options => \%paymentPlugins, #{map {$_ => $_} @{$session{config}{paymentPlugins}}},
-options => \%paymentPlugins,
-label => $i18n->get('payment form'),
-value => [$paymentPlugin],
-extras => 'onChange="activePayment=operateHidden(this.options[this.selectedIndex].value,activePayment)"'
@ -366,6 +384,32 @@ sub www_listPendingTransactions {
_submenu($output, 'list pending transactions', 'list pending transactions');
}
#-------------------------------------------------------------------
sub www_selectPaymentGateway {
my ($plugins, $f, $i18n, @pluginLoop, %var);
$i18n = WebGUI::International->new('Commerce');
$plugins = WebGUI::Commerce::Payment->getEnabledPlugins;
foreach (@$plugins) {
push(@pluginLoop, {
name => $_->name,
namespace => $_->namespace,
formElement => WebGUI::Form::radio({name=>'paymentGateway', value=>$_->namespace})
});
}
$var{pluginLoop} = \@pluginLoop;
$var{message} = $i18n->get('select payment gateway');
$var{pluginsAvailable} = @$plugins;
$var{noPluginsMessage} = $i18n->get('no payment gateway');
$var{formHeader} = WebGUI::Form::formHeader.WebGUI::Form::hidden({name=>'op', value=>'checkoutConfirm'});
$var{formSubmit} = WebGUI::Form::submit({value=>$i18n->get('payment gateway select')});
$var{formFooter} = WebGUI::Form::formFooter;
return WebGUI::Template::process($session{setting}{commerceSelectPaymentGatewayTemplateId}, 'Commerce/SelectPaymentGateway', \%var);
}
#-------------------------------------------------------------------
sub www_transactionComplete {
return WebGUI::Operation::execute('viewPurchaseHistory');

View file

@ -365,6 +365,63 @@ A template loop containing the items that were checked out. The following templa
lastUpdated => 0,
context => q|The message that sais which payment plugins did not compile.|
},
'select payment gateway'=> {
message => q|Please select a payment gateway.|,
lastUpdated => 0,
context => q|The message that asks the user to select a payment gateway.|
},
'payment gateway select' => {
message => q|Select gateway|,
lastUpdated => 0,
context => q|The text on the submit button of the select gateway form.|
},
'checkout select payment template' => {
message => q|Select payment gateway template|,
lastUpdated => 0,
context => q|The formlabel for the 'select payment gateway template' option in the commerce part of the admin console.|
},
'help select payment template title' => {
message => q|Select payment gateway template|,
lastUpdated => 0,
context => q|The title of the 'select payment gateway' help page.|
},
'help select payment template body' => {
message => q|In this template the following template variables are available:<br>
<br>
<b>message</b><br>
This is the message that ask the user to select a payment gateway.<br>
<br>
<b>pluginsAvailable</b><br>
A boolean value that is true when one or more payment plugins can be loaded and are enabled.<br>
<br>
<b>noPluginsMessage</b><br>
A message that sais that there are no payment plugins that ca be used.<br>
<br>
<b>formHeader</b><br>
This contains the form header and all hidden form variables that are needed for a successfull checkout.<br>
<br>
<b>formFooter</b><br>
The form footer.<br>
<br>
<b>formSubmit</b><br>
The submit button for this form.<br>
<br>
<b>pluginLoop</b><br>
A template loop containing all enabled payment plugins. Within this loop the following template variables are provided:
<blockquote>
<b>name</b><br>
The name of the plugin.<br>
<br>
<b>namespace</b><br>
The namespace of the plugin. You only need this if you want to create your own custom foem elements.<br>
<br>
<b>formElement</b><br>
A radio button tied to this plugin.<br>
</blockquote>|,
lastUpdated => 0,
context => q|The body of the help page of the select payment gateway template.|
},
};
1;