-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment_agate.php
More file actions
executable file
·277 lines (238 loc) · 7.4 KB
/
payment_agate.php
File metadata and controls
executable file
·277 lines (238 loc) · 7.4 KB
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
<?php
// no direct access
defined('_JEXEC') or die('Restricted access');
//include necessary libraries for plugin
require_once JPATH_ADMINISTRATOR . '/components/com_j2store/library/plugins/payment.php';
require_once JPATH_ADMINISTRATOR . '/components/com_j2store/helpers/j2store.php';
/**
* Class plgJ2StorePayment_agate
*/
class plgJ2StorePayment_agate extends J2StorePaymentPlugin {
/**
* Name of plugin
* @var string
*/
var $_element = 'payment_agate';
/**
* Log errors
* @var bool
*/
var $_isLog = false;
/**
* Status OK
*/
const STATUS_OK = 'OK';
/**
* Status FAIL
*/
const STATUS_FAIL = 'FAIL';
/**
* Default config for plugin
* @var array
*/
protected $_default = array(
'api_version' => 'dev',
'channel' => '',
'ch_lock' => 0,
'type' => 0,
);
/**
* @inheritdoc
* @param object $subject
* @param array $config
*/
public function __construct($subject, $config) {
parent::__construct($subject, $config);
$this->loadLanguage( 'plg_j2store_payment_agate', JPATH_ADMINISTRATOR );
}
/**
* @inheritdoc
* Prepare for payment
*
* @param array $data
* @return string
*/
public function _prePayment( $data ) {
$vars = new JObject();
$info = $this->getOrderInformation($data);
$price = number_format($data['orderpayment_amount'], 2, '.', '');
$baseUri = "http://gateway.agate.services/";
$convertUrl = "http://gateway.agate.services/convert/";
$api_key = $this->params->get('api_key');
$currencySymbol = $data['order']->currency_code;
$redirect_url = $this->getBaseUrl(). "index.php?option=com_j2store&task=checkout.confirmPayment&orderpayment_type=payment_agate";
$order_total = $price;
error_log(" ".$baseUri." ".$api_key." ".$redirect_url." ".$currencySymbol);
error_log("Cost = ". $price);
$amount_iUSD = $this->convertCurToIUSD($convertUrl, $order_total, $api_key, $currencySymbol);
//Needed for Agate
$vars->baseUri = "http://gateway.agate.services/";
$vars->amount_iUSD = $amount_iUSD;
$vars->order_total = $order_total;
$vars->currencySymbol = $currencySymbol;
$vars->api_key = $api_key;
$vars->redirect_url = $redirect_url;
$html = $this->_getLayout('prepayment', $vars);
return $html;
}
/**
* Get base url depend on option. This method override original base url
* with https if it is necessary
*
* @return mixed|string
*/
private function getBaseUrl()
{
if($this->params->get('ssl', 0) == 0){
return JURI::base();
}
return str_replace("http://", "https://", JURI::base());
}
/**
* This method is responsible both form processing notification and
* generate 'thank you' message. Depend on notification parameter getting from
* $_GET it trigger processing notification or generateing 'thank you' message
*
* @inheritdoc
* Response status
* @param array $data
* @throws Exception
*/
public function _postPayment( $data ) {
$app = JFactory::getApplication();
$isNotification = $app->input->get->get('notification');
if($isNotification){
$this->processNotification($app->input);
$app->close();
}
$status = $app->input->get->getString('status');
error_log("Call back response => " . var_export($_REQUEST, TRUE) );
$vars = new JObject();
if($_REQUEST['stateId']==3)
$vars->message = JText::_('J2STORE_CONFIRMED');
else {
$vars->message = JText::_('J2STORE_FAILED');
}
// $message =$this->createConfirmMessage($status);
return $this->_getLayout('postpayment', $vars);
}
/**
* Set complete status. This method is internal j2store method which should
* set everything what is necessary after complete payment
*
* @param $orderId
*/
private function setCompleteStatus($orderId)
{
$order = $this->getOrder($orderId);
$order->payment_complete();
$this->save($order);
}
/**
* This method change order status. Status is defined as $order_state_id
* allowed id are:
* 1 confirmed
* 2 processing
* 3 something goes wrong
* 4 pending
* 5 new
* 6 canceled
*
* @param $orderId
* @param $order_state_id
*/
private function setWrongStatus($orderId , $order_state_id)
{
$order = $this->getOrder($orderId);
$order->update_status( $order_state_id, true );
$order->reduce_order_stock();
$this->save($order);
}
/**
* Saveing order model and trigger remove item from card if everything goes ok
*
* @param $order
*/
private function save($order)
{
if($order->store()){
$order->empty_cart();
}
}
/**
* Get order object from model
*
* @param $orderId
* @return static
*/
private function getOrder($orderId)
{
F0FTable::addIncludePath ( JPATH_ADMINISTRATOR . '/components/com_j2store/tables' );
$order = F0FTable::getInstance ( 'Order', 'J2StoreTable' )->getClone();
$order->load(array('order_id' => $orderId));
return $order;
}
/**
* Based on status set error or ok message displaying to customer
*
* @param $status
* @return JObject
*/
private function createConfirmMessage($status)
{
$vars = new JObject();
switch ($status){
case self::STATUS_OK:
$vars->message = JText::_('J2STORE_CONFIRMED');
break;
default:
$vars->message = JText::_('J2STORE_FAILED');
}
return $vars;
}
/**
* Get actual language for page
* @return mixed
*/
private function getLanguage() {
$lang = JFactory::getLanguage();
$lang = explode( '-', $lang->getTag() );
return $lang[0];
}
/**
* Get order information
* @param $data
* @return mixed
*/
private function getOrderInformation( $data ) {
$order = $data['order']->getOrderInformation();
return $order;
}
/**
* Get price from order
* @param $order_id
* @return int
*/
private function getPrice($order_id) {
$order = $this->getOrder($order_id);
if($order){
return $order->order_total;
}
return 0;
}
// Convert all the currency to USD.
public function convertCurToIUSD($url, $amount, $api_key, $currencySymbol) {
error_log("Entered into Convert Amount");
$ch = curl_init($url.'?api_key='.$api_key.'¤cy='.$currencySymbol.'&amount='. $amount);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
$result = curl_exec($ch);
$data = json_decode( $result , true);
error_log("Response => ".var_export($data, TRUE));;
// Return the equivalent value acquired from Agate server.
return (float) $data["result"];
}
}