forked from Payfast/mod-hikashop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payfast.php
336 lines (275 loc) · 11.7 KB
/
payfast.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* @package PayFast Plugin for HikaShop Joomla!
* @version 1.0.0
* @author payfast.co.za
* Copyright (c) 2008 PayFast (Pty) Ltd
* You (being anyone who is not PayFast (Pty) Ltd) may download and use this plugin / code in your own website in conjunction with a registered and active PayFast account. If your PayFast account is terminated for any reason, you may not use this plugin / code or part thereof.
* Except as expressly indicated in this licence, you may not use, copy, modify or distribute this plugin / code or part thereof in any way.
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
?><?php
class plgHikashoppaymentPayfast extends hikashopPaymentPlugin
{
var $accepted_currencies = array(
'ZAR',
);
var $multiple = true;
var $name = 'payfast';
var $doc_form = 'payfast';
const SANDBOX_MERCHANT_KEY = '46f0cd694581a';
const SANDBOX_MERCHANT_ID = '10000100';
function __construct( &$subject, $config )
{
parent::__construct( $subject, $config );
$lang = JFactory::getLanguage();
$lang->load( 'plg_hikashoppayfast', JPATH_ADMINISTRATOR );
}
function onBeforeOrderCreate( &$order, &$do )
{
if ( parent::onBeforeOrderCreate( $order, $do ) === true )
{
return true;
}
if ( ( empty( $this->payment_params->payfast_merchant_id ) || empty( $this->payment_params->payfast_merchant_key ) ) && !$this->payment_params->payfast_sandbox )
{
$this->app->enqueueMessage( 'Please check your "PayFast" plugin configuration, a merchant ID and Key are required' );
$do = false;
}
}
function onAfterOrderConfirm( &$order, &$methods, $method_id )
{
parent::onAfterOrderConfirm( $order, $methods, $method_id );
if ( $this->currency->currency_locale['int_frac_digits'] > 2 )
{
$this->currency->currency_locale['int_frac_digits'] = 2;
}
$notify_url = HIKASHOP_LIVE . 'index.php?option=com_hikashop&ctrl=checkout&task=notify¬if_payment=' . $this->name . '&tmpl=component&lang=' . $this->locale . $this->url_itemid;
$return_url = HIKASHOP_LIVE . 'index.php?option=com_hikashop&ctrl=checkout&task=after_end&order_id=' . $order->order_id . $this->url_itemid;
$cancel_url = HIKASHOP_LIVE . 'index.php?option=com_hikashop&ctrl=order&task=cancel_order&order_id=' . $order->order_id . $this->url_itemid;
if ( !empty( $this->payment_params->payfast_return_url ) )
{
$return_url = $this->payment_params->payfast_return_url;
}
if ( !empty( $this->payment_params->payfast_cancel_url ) )
{
$cancel_url = $this->payment_params->payfast_cancel_url;
}
$tax_total = '';
$discount_total = '';
if ( $this->payment_params->payfast_sandbox )
{
$payfast_url = 'https://sandbox.payfast.co.za/eng/process';
$merchant_id = self::SANDBOX_MERCHANT_ID;
$merchant_key = self::SANDBOX_MERCHANT_KEY;
}
else
{
$payfast_url = 'https://www.payfast.co.za/eng/process';
$merchant_id = $this->payment_params->payfast_merchant_id;
$merchant_key = $this->payment_params->payfast_merchant_key;
}
$config = JFactory::getConfig();
$sitename = $config->get( 'sitename' );
$vars = array(
'merchant_id' => $merchant_id,
'merchant_key' => $merchant_key,
'return_url' => $return_url,
'cancel_url' => $cancel_url,
'notify_url' => $notify_url,
'email_address' => $this->user->user_email,
'm_payment_id' => $order->order_id,
'amount' => round( $order->cart->full_total->prices[0]->price_value_with_tax, (int) $this->currency->currency_locale['int_frac_digits'] ),
'item_name' => $sitename . ' - Order #' . $order->order_id,
);
$pfOutput = '';
foreach ( $vars as $key => $val )
{
$pfOutput .= $key . '=' . urlencode( trim( $val ) ) . '&';
}
$passPhrase = $this->payment_params->payfast_passphrase;
if ( empty( $passPhrase ) || $this->payment_params->payfast_sandbox )
{
$pfOutput = substr( $pfOutput, 0, -1 );
}
else
{
$pfOutput = $pfOutput . "passphrase=" . urlencode( $passPhrase );
}
$vars['signature'] = md5( $pfOutput );
$vars['user_agent'] = 'HikaShop 3.5.1';
$vars['payfast_url'] = $payfast_url;
$this->vars = $vars;
return $this->showPage( 'end' );
}
function onPaymentNotification( &$statuses )
{
$pfError = false;
$pfErrMsg = '';
$pfDone = false;
$pfData = array();
$pfParamString = '';
//// Notify PayFast that information has been received
if ( !$pfError && !$pfDone )
{
header( 'HTTP/1.0 200 OK' );
flush();
}
$dbOrder = $this->getOrder( (int) @$_POST['m_payment_id'] );
$this->loadPaymentParams( $dbOrder );
if ( empty( $this->payment_params ) )
{
return false;
}
$pfHost = $this->payment_params->payfast_sandbox ? 'sandbox.payfast.co.za' : 'www.payfast.co.za';
define( 'PF_DEBUG', $this->payment_params->payfast_debug );
include_once 'payfast_common.inc';
pflog( 'PayFast ITN call received' );
//// Get data sent by PayFast
if ( !$pfError && !$pfDone )
{
pflog( 'Get posted data' );
// Posted variables from ITN
$pfData = pfGetData();
pflog( 'PayFast Data: ' . print_r( $pfData, true ) );
if ( $pfData === false )
{
$pfError = true;
$pfErrMsg = PF_ERR_BAD_ACCESS;
}
}
//// Verify security signature
if ( !$pfError && !$pfDone )
{
pflog( 'Verify security signature' );
$passPhrase = $this->payment_params->payfast_passphrase;
$pfPassPhrase = empty( $passPhrase ) ? null : $passPhrase;
// If signature different, log for debugging
if ( !pfValidSignature( $pfData, $pfParamString, $pfPassPhrase ) )
{
$pfError = true;
$pfErrMsg = PF_ERR_INVALID_SIGNATURE;
}
}
//// Verify source IP (If not in debug mode)
if ( !$pfError && !$pfDone )
{
pflog( 'Verify source IP' );
if ( !pfValidIP( $_SERVER['REMOTE_ADDR'] ) )
{
$pfError = true;
$pfErrMsg = PF_ERR_BAD_SOURCE_IP;
}
}
//// Get internal cart
if ( !$pfError && !$pfDone )
{
$this->loadOrderData( $dbOrder );
if ( empty( $dbOrder ) )
{
pflog( 'Could not load any order for your notification ' . $pfData['m_payment_id'] );
return false;
}
$order_id = $dbOrder->order_id;
pflog( "Purchase:\n" . print_r( $order_info, true ) );
}
//// Verify data received
if ( !$pfError )
{
pflog( 'Verify data received' );
$pfValid = pfValidData( $pfHost, $pfParamString );
if ( !$pfValid )
{
$pfError = true;
$pfErrMsg = PF_ERR_BAD_ACCESS;
}
}
//// Check data against internal order
if ( !$pfError && !$pfDone )
{
pflog( 'Check data against internal order' );
$amount = round( $dbOrder->order_full_price, (int) $this->currency->currency_locale['int_frac_digits'] );
// Check order amount
if ( !pfAmountsEqual( $pfData['amount_gross'], $amount ) )
{
$pfError = true;
$pfErrMsg = PF_ERR_AMOUNT_MISMATCH;
}
}
//// Check status and update order
if ( !$pfError && !$pfDone )
{
pflog( 'Check status and update order' );
$transaction_id = $pfData['pf_payment_id'];
switch ( $pfData['payment_status'] )
{
case 'COMPLETE':
pflog( '- Complete' );
pflog( 'PayFast transaction id: ' . $pfData['pf_payment_id'] );
$history = new stdClass();
$history->notified = 0;
$history->amount = $pfData['amount_gross'];
$history->data = ob_get_clean();
$order_status = $this->payment_params->verified_status;
if ( $dbOrder->order_status == $order_status )
{
return true;
}
$config = &hikashop_config();
if ( $config->get( 'order_confirmed_status', 'confirmed' ) == $order_status )
{
$history->notified = 1;
}
$email = new stdClass();
$email->subject = JText::sprintf( 'PAYMENT_NOTIFICATION_FOR_ORDER', 'Payfast', $pfData['payment_status'], $dbOrder->order_number );
$email->body = str_replace( '<br/>', "\r\n", JText::sprintf( 'PAYMENT_NOTIFICATION_STATUS', 'Payfast', $pfData['payment_status'] ) ) . ' ' . JText::sprintf( 'ORDER_STATUS_CHANGED', $order_status ) . "\r\n\r\n" . $order_text;
$this->modifyOrder( $order_id, $order_status, $history, $email );
return true;
break;
case 'FAILED':
pflog( '- Failed' );
$email = new stdClass();
$email->subject = JText::sprintf( 'NOTIFICATION_REFUSED_FOR_THE_ORDER', 'Payfast' ) . ' ' . JText::sprintf( 'PAYPAL_CONNECTION_FAILED', $dbOrder->order_number );
$email->body = str_replace( '<br/>', "\r\n", JText::sprintf( 'NOTIFICATION_REFUSED_NO_CONNECTION', 'Payfast' ) ) . "\r\n\r\n" . JText::sprintf( 'CHECK_DOCUMENTATION', HIKASHOP_HELPURL . 'payment-payfast-error#connection' ) . $order_text;
$action = false;
$this->modifyOrder( $action, null, null, $email );
JError::raiseError( 403, JText::_( 'Access Forbidden' ) );
return false;
break;
case 'PENDING':
pflog( '- Pending' );
// Need to wait for "Completed" before processing
break;
default:
// If unknown status, do nothing (safest course of action)
break;
}
}
else
{
pflog( "Errors:\n" . print_r( $pfErrMsg, true ) );
}
}
function onPaymentConfiguration( &$element )
{
$app = JFactory::getApplication();
$subtask = $app->input->getCmd( 'subtask', '' );
parent::onPaymentConfiguration( $element );
}
function onPaymentConfigurationSave( &$element )
{
return true;
}
function getPaymentDefaultValues( &$element )
{
$element->payment_name = 'PayFast';
$element->payment_description = 'You can pay with PayFast using this payment method';
$element->payment_images = 'payfast';
$element->payment_params->payfast_debug = 1;
$element->payment_params->payfast_sandbox = 1;
$element->payment_params->invalid_status = 'cancelled';
$element->payment_params->pending_status = 'created';
$element->payment_params->verified_status = 'confirmed';
$element->payment_params->address_override = 1;
}
}