Skip to content
This repository was archived by the owner on Apr 3, 2018. It is now read-only.

Commit 35e6788

Browse files
committed
Totally rewritten Datatrans payment module, for Isotope 1.3 only!
1 parent 6ca26a1 commit 35e6788

12 files changed

+291
-107
lines changed

PaymentDatatrans.php

+109-58
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323
* PHP version 5
2424
* @copyright Isotope eCommerce Workgroup 2009-2011
2525
* @author Andreas Schempp <[email protected]>
26-
* @author Leo Unglaub <leo[email protected]>
26+
* @author Leo Unglaub <leo@leo-unglaub.net>
2727
* @license http://opensource.org/licenses/lgpl-3.0.html
2828
* @version $Id: $
2929
*/
3030

3131

3232
class PaymentDatatrans extends IsotopePayment
3333
{
34+
3435
/**
3536
* Return a list of status options.
36-
*
37-
* @access public
3837
* @return array
3938
*/
4039
public function statusOptions()
@@ -44,78 +43,96 @@ public function statusOptions()
4443

4544

4645
/**
47-
* Server 2 Server check
46+
* Perform server to server data check
4847
*/
4948
public function processPostSale()
5049
{
51-
$this->import('Input');
52-
53-
// stop if something went wrong
50+
// Verify payment status
5451
if ($this->Input->post('status') != 'success')
5552
{
56-
$this->log('Order ID "' . $this->Input->post('refno') . '" has NOT succeedet. UPP Transaction Id: ' . $this->Input->post('uppTransactionId'), __METHOD__, TL_ERROR);
57-
return;
53+
$this->log('Payment for order ID "' . $this->Input->post('refno') . '" failed.', __METHOD__, TL_ERROR);
54+
return false;
5855
}
59-
56+
6057
$objOrder = new IsotopeOrder();
6158

6259
if (!$objOrder->findBy('id', $this->Input->post('refno')))
6360
{
6461
$this->log('Order ID "' . $this->Input->post('refno') . '" not found', __METHOD__, TL_ERROR);
65-
return;
62+
return false;
6663
}
6764

68-
// check if the details are okay
69-
if ($this->Input->post('merchantId') == $this->datatrans_id)
65+
// Validate HMAC sign
66+
if ($this->Input->post('sign2') != hash_hmac('md5', $this->datatrans_id.$this->Input->post('amount').$this->Input->post('currency').$this->Input->post('uppTransactionId'), $this->datatrans_sign))
7067
{
71-
// do the optional sign check
72-
if ($this->datatrans_sign == 1)
73-
{
74-
if ($this->datatrans_sign_value != $this->Input->post('sign'))
75-
{
76-
$this->log('Call without a valid sign id', __METHOD__, TL_ERROR);
77-
return;
78-
}
79-
}
80-
81-
// new in isotope 1.3
82-
if (version_compare(ISO_VERSION, '0.2', '>'))
83-
{
84-
$objOrder->checkout();
85-
}
86-
87-
$objOrder->date_payed = time();
88-
$objOrder->save();
68+
$this->log('Invalid HMAC signature for Order ID ' . $this->Input->post('refno'), __METHOD__, TL_ERROR);
69+
return false;
70+
}
8971

72+
// For maximum security, also validate individual parameters
73+
if (!$this->validateParameters(array
74+
(
75+
'refno' => $objOrder->id,
76+
'currency' => $objOrder->currency,
77+
'amount' => round($objOrder->grandTotal * 100),
78+
'reqtype' => ($this->trans_type == 'auth' ? 'NOA' : 'CAA'),
79+
)))
80+
{
81+
return false;
9082
}
83+
84+
$objOrder->checkout();
85+
$objOrder->date_payed = time();
86+
$objOrder->save();
9187
}
9288

9389

9490
/**
95-
* Check if the server to server check was sucessfull before we tag the order as payed
91+
* Validate post parameters and complete order
9692
* @return bool
9793
*/
9894
public function processPayment()
9995
{
10096
$objOrder = new IsotopeOrder();
101-
10297
if (!$objOrder->findBy('cart_id', $this->Isotope->Cart->id))
10398
{
104-
$this->log('Cart ID "' . $this->Isotope->Cart->id . '" not found', __METHOD__, TL_ERROR);
105-
$this->redirect($this->addToUrl('step=failed', true));
99+
return false;
106100
}
107101

108-
if ($objOrder->date_payed > 0)
102+
if ($objOrder->date_payed > 0 && $objOrder->date_payed <= time())
103+
{
104+
unset($_SESSION['PAYMENT_TIMEOUT']);
109105
return true;
106+
}
107+
108+
if (!isset($_SESSION['PAYMENT_TIMEOUT']))
109+
{
110+
$_SESSION['PAYMENT_TIMEOUT'] = 60;
111+
}
112+
else
113+
{
114+
$_SESSION['PAYMENT_TIMEOUT'] = $_SESSION['PAYMENT_TIMEOUT'] - 5;
115+
}
116+
117+
if ($_SESSION['PAYMENT_TIMEOUT'] === 0)
118+
{
119+
global $objPage;
120+
$this->log('Payment could not be processed.', __METHOD__, TL_ERROR);
121+
$this->redirect($this->generateFrontendUrl($objPage->row(), '/step/failed'));
122+
}
123+
124+
// Reload page every 5 seconds and check if payment was successful
125+
$GLOBALS['TL_HEAD'][] = '<meta http-equiv="refresh" content="5,' . $this->Environment->base . $this->Environment->request . '">';
110126

111-
$this->redirect($this->addToUrl('step=failed', true));
127+
$objTemplate = new FrontendTemplate('mod_message');
128+
$objTemplate->type = 'processing';
129+
$objTemplate->message = $GLOBALS['TL_LANG']['MSC']['payment_processing'];
130+
return $objTemplate->parse();
112131
}
113132

114133

115134
/**
116-
* Generate the submit form for datatrans and if javascript
117-
* is enabled redirect automaticly
118-
*
135+
* Generate the submit form for datatrans and if javascript is enabled redirect automaticly
119136
* @return string
120137
*/
121138
public function checkoutForm()
@@ -126,33 +143,67 @@ public function checkoutForm()
126143
{
127144
$this->redirect($this->addToUrl('step=failed', true));
128145
}
146+
147+
$arrAddress = $this->Isotope->Cart->billing_address;
129148

130-
$this->loadLanguageFile('tl_iso_payment_modules');
131149
$arrParams = array
132150
(
133-
'merchantId' => $objOrder->Payment->datatrans_id,
134-
'amount' => $this->Isotope->Cart->grandTotal,
135-
'currency' => $this->Isotope->Config->currency,
136-
'refno' => $objOrder->id, // Order or transaction ID
137-
'mod' => 'pay',
138-
'id' => $this->id
151+
'merchantId' => $this->datatrans_id,
152+
'amount' => round($this->Isotope->Cart->grandTotal * 100),
153+
'currency' => $this->Isotope->Config->currency,
154+
'refno' => $objOrder->id,
155+
'language' => $GLOBALS['TL_LANGUAGE'],
156+
'reqtype' => ($this->trans_type == 'auth' ? 'NOA' : 'CAA'),
157+
'uppCustomerDetails' => 'yes',
158+
'uppCustomerTitle' => $arrAddress['salutation'],
159+
'uppCustomerFirstName' => $arrAddress['firstname'],
160+
'uppCustomerLastName' => $arrAddress['lastname'],
161+
'uppCustomerStreet' => $arrAddress['street_1'],
162+
'uppCustomerStreet2' => $arrAddress['street_2'],
163+
'uppCustomerCity' => $arrAddress['city'],
164+
'uppCustomerCountry' => $arrAddress['country'],
165+
'uppCustomerZipCode' => $arrAddress['postal'],
166+
'uppCustomerPhone' => $arrAddress['phone'],
167+
'uppCustomerEmail' => $arrAddress['email'],
168+
'successUrl' => ampersand($this->Environment->base . $this->addToUrl('step=complete', true)),
169+
'errorUrl' => ampersand($this->Environment->base . $this->addToUrl('step=failed', true)),
170+
'cancelUrl' => ampersand($this->Environment->base . $this->addToUrl('step=failed', true)),
171+
'mod' => 'pay',
172+
'id' => $this->id,
139173
);
140-
141-
// add the security sign
142-
if ($this->datatrans_sign == 1)
143-
{
144-
$arrParams['sign'] = $this->datatrans_sign_value;
145-
}
174+
175+
// Security signature (see Security Level 2)
176+
$arrParams['sign'] = hash_hmac('md5', $arrParams['merchantId'].$arrParams['amount'].$arrParams['currency'].$arrParams['refno'], $this->datatrans_sign);
146177

147178
$objTemplate = new FrontendTemplate('iso_payment_datatrans');
148-
$objTemplate->params = $arrParams;
149-
$objTemplate->action = 'https://pilot.datatrans.biz/upp/jsp/upStart.jsp'; // Live URL: https://payment.datatrans.biz/upp/jsp/upStart.jsp
150-
$objTemplate->slabel = $GLOBALS['TL_LANG']['tl_iso_payment_modules']['datatrans_label_pay'];
151179
$objTemplate->id = $this->id;
180+
$objTemplate->action = ('https://' . ($this->debug ? 'pilot' : 'payment') . '.datatrans.biz/upp/jsp/upStart.jsp');
181+
$objTemplate->params = $arrParams;
182+
$objTemplate->headline = $GLOBALS['TL_LANG']['MSC']['pay_with_redirect'][0];
183+
$objTemplate->message = $GLOBALS['TL_LANG']['MSC']['pay_with_redirect'][1];
184+
$objTemplate->slabel = specialchars($GLOBALS['TL_LANG']['MSC']['pay_with_redirect'][2]);
152185

153186
return $objTemplate->parse();
154187
}
188+
189+
190+
/**
191+
* Validate array of post parameter agains required values
192+
* @param array
193+
* @return boolean
194+
*/
195+
private function validateParameters(array $arrData)
196+
{
197+
foreach ($arrData as $key => $value)
198+
{
199+
if ($this->Input->post($key) != $value)
200+
{
201+
$this->log('Wrong data for parameter "' . $key . '" (Order ID "' . $this->Input->post('refno') . ').', __METHOD__, TL_ERROR);
202+
return false;
203+
}
204+
}
205+
206+
return true;
207+
}
155208
}
156209

157-
158-
?>

config/config.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
*
2323
* PHP version 5
2424
* @copyright Isotope eCommerce Workgroup 2009-2011
25-
* @author Leo Unglaub <[email protected]>
25+
* @author Andreas Schempp <[email protected]>
26+
* @author Leo Unglaub <[email protected]>
2627
* @license http://opensource.org/licenses/lgpl-3.0.html
2728
* @version $Id: $
2829
*/
@@ -33,4 +34,3 @@
3334
*/
3435
$GLOBALS['ISO_PAY']['datatrans'] = 'PaymentDatatrans';
3536

36-
?>

config/database.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
--
1414

1515
CREATE TABLE `tl_iso_payment_modules` (
16-
`datatrans_id` varchar(100) NOT NULL default '',
17-
`datatrans_sign` char(1) NOT NULL default '0',
18-
`datatrans_sign_value` varchar(100) NOT NULL default '',
19-
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
16+
`datatrans_id` varchar(16) NOT NULL default '',
17+
`datatrans_sign` varchar(128) NOT NULL default '',
18+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
19+

dca/tl_iso_payment_modules.php

+5-19
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@
2222
*
2323
* PHP version 5
2424
* @copyright Isotope eCommerce Workgroup 2009-2011
25-
* @author Leo Unglaub <[email protected]>
25+
* @author Andreas Schempp <[email protected]>
26+
* @author Leo Unglaub <[email protected]>
2627
* @license http://opensource.org/licenses/lgpl-3.0.html
2728
* @version $Id: $
2829
*/
2930

3031
/**
3132
* Palettes
3233
*/
33-
$GLOBALS['TL_DCA']['tl_iso_payment_modules']['palettes']['__selector__'][] = 'datatrans_sign';
34-
$GLOBALS['TL_DCA']['tl_iso_payment_modules']['palettes']['datatrans'] = '{type_legend},name,label,type;{config_legend},new_order_status,trans_type,postsale_mail,minimum_total,maximum_total,countries,shipping_modules,product_types;{gateway_legend},datatrans_id,datatrans_sign;{expert_legend:hide},guests,protected;{enabled_legend},enabled';
35-
36-
37-
/**
38-
* Subpalettes
39-
*/
40-
$GLOBALS['TL_DCA']['tl_iso_payment_modules']['subpalettes']['datatrans_sign'] = 'datatrans_sign_value';
34+
$GLOBALS['TL_DCA']['tl_iso_payment_modules']['palettes']['datatrans'] = '{type_legend},name,label,type;{note_legend:hide},note;{config_legend},new_order_status,trans_type,minimum_total,maximum_total,countries,shipping_modules,product_types;{gateway_legend},datatrans_id,datatrans_sign;{price_legend:hide},price,tax_class;{expert_legend:hide},guests,protected;{enabled_legend},debug,enabled';
4135

4236

4337
/**
@@ -55,15 +49,7 @@
5549
(
5650
'label' => &$GLOBALS['TL_LANG']['tl_iso_payment_modules']['datatrans_sign'],
5751
'exclude' => true,
58-
'inputType' => 'checkbox',
59-
'eval' => array('tl_class'=>'clr', 'submitOnChange'=>true)
60-
);
61-
62-
$GLOBALS['TL_DCA']['tl_iso_payment_modules']['fields']['datatrans_sign_value'] = array
63-
(
64-
'label' => &$GLOBALS['TL_LANG']['tl_iso_payment_modules']['datatrans_sign_value'],
65-
'exclude' => true,
6652
'inputType' => 'text',
67-
'eval' => array('mandatory'=>true, 'tl_class'=>'w50')
53+
'eval' => array('mandatory'=>true, 'decodeEntities'=>true, 'tl_class'=>'w50')
6854
);
69-
?>
55+

languages/de/default.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
2+
3+
/**
4+
* Contao Open Source CMS
5+
* Copyright (C) 2005-2011 Leo Feyer
6+
*
7+
* Formerly known as TYPOlight Open Source CMS.
8+
*
9+
* This program is free software: you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation, either
12+
* version 3 of the License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with this program. If not, please visit the Free
21+
* Software Foundation website at <http://www.gnu.org/licenses/>.
22+
*
23+
* PHP version 5
24+
* @copyright Isotope eCommerce Workgroup 2009-2011
25+
* @author Andreas Schempp <[email protected]>
26+
* @license http://opensource.org/licenses/lgpl-3.0.html
27+
* @version $Id$
28+
*/
29+
30+
31+
/**
32+
* Payment modules
33+
*/
34+
$GLOBALS['ISO_LANG']['PAY']['datatrans'] = array('Datatrans', 'Ein Zahlungsmodul für den Schweizer Anbieter "Datatrans".');
35+

languages/de/modules.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
2+
3+
/**
4+
* Contao Open Source CMS
5+
* Copyright (C) 2005-2011 Leo Feyer
6+
*
7+
* Formerly known as TYPOlight Open Source CMS.
8+
*
9+
* This program is free software: you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation, either
12+
* version 3 of the License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with this program. If not, please visit the Free
21+
* Software Foundation website at <http://www.gnu.org/licenses/>.
22+
*
23+
* PHP version 5
24+
* @copyright Isotope eCommerce Workgroup 2009-2011
25+
* @author Andreas Schempp <[email protected]>
26+
* @license http://opensource.org/licenses/lgpl-3.0.html
27+
* @version $Id$
28+
*/
29+
30+
31+
/**
32+
* Extension folder
33+
*/
34+
$GLOBALS['TL_LANG']['MOD']['isotope_datatrans'] = array('Isotope eCommerce: Datatrans Zahlungsmodul');
35+

0 commit comments

Comments
 (0)