-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_moneris.php
executable file
·250 lines (225 loc) · 8.07 KB
/
payment_moneris.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
<?php
/**
* @package J2Store
* @copyright Copyright (c)2014-17 Ramesh Elamathi / J2Store.org
* @license GNU GPL v3 or later
*/
/** ensure this file is being included by a parent file */
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_ADMINISTRATOR . '/components/com_j2store/library/plugins/payment.php');
class plgJ2StorePayment_moneris extends J2StorePaymentPlugin
{
/**
* @var $_element string Should always correspond with the plugin's filename,
* forcing it to be unique
*/
var $_element = 'payment_moneris';
var $config;
/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for plugins
* because func_get_args ( void ) returns a copy of all passed arguments NOT references.
* This causes problems with cross-referencing necessary for the observer design pattern.
*
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
* @since 2.5
*/
function __construct(&$subject, $config)
{
$this->config = json_decode($config['params']);
parent::__construct($subject, $config);
$this->loadLanguage('com_j2store', JPATH_ADMINISTRATOR);
}
/**
* Prepares the payment form
* and returns HTML Form to be displayed to the user
* generally will have a message saying, 'confirm entries, then click complete order'
*
* @param $data array form post data
* @return string HTML to display
*/
function _prePayment($data)
{
$user = JFactory::getUser();
$order = F0FTable::getInstance('Order', 'J2StoreTable')->getClone();
$order->load([
'order_id' => $data['order_id']
]);
$orderInfo = F0FTable::getInstance('Orderinfo', 'J2StoreTable')->getClone();
$orderInfo->load([
'order_id' => $data['order_id']
]);
$url = "https://gatewayt.moneris.com/chktv2/request/request.php";
if ($this->config->moneris_environment == 'prod') {
$url = "https://gateway.moneris.com/chktv2/request/request.php";
}
$config = [
"store_id" => $this->config->moneris_store_id,
"api_token" => $this->config->moneris_api_token,
"checkout_id" => $this->config->moneris_checkout_id,
"environment" => $this->config->moneris_environment,
"txn_total" => number_format($order->order_total, 2, ".", ""),
"action" => "preload"
];
$config["ask_cvv"] = "Y";
$config["order_no"] = (string) uniqid('ID', true);
$config["cust_id"] = (string) $order->order_id;
$config["dynamic_descriptor"] = "";
$config["language"] = "en";
$config["contact_details"] = [
"first_name" => $user->name,
"last_name" => "",
"email" => $user->email,
"phone" => $user->phone ?? '',
];
$config["shipping_details"] = [
"address_1" => (string) $orderInfo->shipping_address_1,
"address_2" => (string) $orderInfo->shipping_address_2,
"city" => $orderInfo->shipping_city,
"province" => $orderInfo->shipping_zone_name,
"country" => $orderInfo->shipping_country_name,
"postal_code" => $orderInfo->shipping_zip
];
$config["billing_details"] = [
"address_1" => (string) $orderInfo->billing_address_1,
"address_2" => (string) $orderInfo->billing_address_2,
"city" => $orderInfo->billing_city,
"province" => $orderInfo->billing_zone_name,
"country" => $orderInfo->billing_country_name,
"postal_code" => $orderInfo->billing_zip
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //0 for a get request
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($config));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$serverOutput = json_decode(curl_exec($ch));
curl_close($ch);
if (!empty($serverOutput)) {
if ($serverOutput->response->success == true) {
$vars = new JObject();
$vars->ticket = $serverOutput->response->ticket;
$vars->moneris_environment = $this->config->moneris_environment;
$vars->redirect = JRoute::_('index.php?option=com_j2store&view=checkout&task=confirmPayment&paction=process&orderpayment_type=' . $this->_element . '&ticket=' . $serverOutput->response->ticket);
$html = $this->_getLayout('checkout', $vars);
echo $html;
die();
}
}
echo "payment method failed, please try again."; die();
}
/**
* Processes the payment form
* and returns HTML to be displayed to the user
* generally with a success/failed message
*
* @param $data array form post data
* @return string HTML to display
*/
function _postPayment($data)
{
$vars = new JObject();
$app = JFactory::getApplication();
$paction = $app->input->getString('paction');
switch ($paction) {
case 'display':
$vars->onafterpayment_text = JText::_('Payment Success');
$html = $this->_getLayout('postpayment', $vars);
$html .= $this->_displayArticle();
break;
case 'process':
$ticket = $app->input->getString('ticket');
$url = "https://gatewayt.moneris.com/chktv2/request/request.php";
if ($this->config->moneris_environment == 'prod') {
$url = "https://gateway.moneris.com/chktv2/request/request.php";
}
$config = [
"store_id" => $this->config->moneris_store_id,
"api_token" => $this->config->moneris_api_token,
"checkout_id" => $this->config->moneris_checkout_id,
"environment" => $this->config->moneris_environment,
"ticket" => $ticket,
"action" => "receipt",
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //0 for a get request
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($config));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$serverOutput = json_decode(curl_exec($ch));
curl_close($ch);
if (!empty($serverOutput)) {
if ($serverOutput->response->success == true) {
if (strtolower($serverOutput->response->receipt->result) == 'a') {
$result = $this->_process($data);
header("location:" . $result['redirect'] ?? '');
$app->close();
}
}
}
$url = 'index.php?option=com_j2store&view=checkout&task=confirmPayment&layout=postpayment&orderpayment_type='.$this->_element;
header("location:" . $url);
break;
default:
$vars->message = JText::_('Payment Failed');
$html = $this->_getLayout('message', $vars);
break;
}
return $html;
}
/**
* Processes the payment form
* and returns HTML to be displayed to the user
* generally with a success/failed message
*
* @param $data array form post data
* @return string HTML to display
*/
function _process($data)
{
// Process the payment
$json = array();
F0FTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_j2store/tables');
$order = F0FTable::getInstance('Order', 'J2StoreTable')->getClone();
if (
$order->load(array(
'order_id' => $data['order_id']
))
) {
$order->payment_complete();
if ($order->store()) {
//empty the cart
$order->empty_cart();
$json['success'] = JText::_( $this->config->onafterpayment);
$return_url = $this->getReturnUrl();
$json['redirect'] = JRoute::_($return_url);//JRoute::_ ( 'index.php?option=com_j2store&view=checkout&task=confirmPayment&orderpayment_type=' . $this->_element . '&paction=display' );
} else {
$json['error'] = $order->getError();
}
} else {
$json['error'] = 'Order not found.';
}
return $json;
}
/**
* Prepares variables and
* Renders the form for collecting payment info
*
* @return unknown_type
*/
function _renderForm($data)
{
$vars = new JObject();
$vars->onselection_text = $this->config->onselection;
$html = $this->_getLayout('form', $vars);
return $html;
}
}