aboutsummaryrefslogtreecommitdiffstats
path: root/public/admin/controller/extension/payment/securetrading_ws.php
blob: 8e702ecaade257ce0a2dbad3bc8ad5b62ec691a1 (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<?php
class ControllerExtensionPaymentSecureTradingWs extends Controller {
	private $error = array();

	public function index() {
		$this->load->model('setting/setting');
		$this->load->model('localisation/geo_zone');
		$this->load->model('localisation/order_status');
		$this->load->model('localisation/currency');
		$this->load->language('extension/payment/securetrading_ws');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->request->post['payment_securetrading_ws_site_reference'] = trim($this->request->post['payment_securetrading_ws_site_reference']);
			$this->request->post['payment_securetrading_ws_username'] = trim($this->request->post['payment_securetrading_ws_username']);

			$this->model_setting_setting->editSetting('payment_securetrading_ws', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true));
		}

		if (isset($this->request->post['payment_securetrading_ws_site_reference'])) {
			$data['payment_securetrading_ws_site_reference'] = $this->request->post['payment_securetrading_ws_site_reference'];
		} else {
			$data['payment_securetrading_ws_site_reference'] = $this->config->get('payment_securetrading_ws_site_reference');
		}

		if (isset($this->request->post['payment_securetrading_ws_username'])) {
			$data['payment_securetrading_ws_username'] = $this->request->post['payment_securetrading_ws_username'];
		} else {
			$data['payment_securetrading_ws_username'] = $this->config->get('payment_securetrading_ws_username');
		}

		if (isset($this->request->post['payment_securetrading_ws_password'])) {
			$data['payment_securetrading_ws_password'] = $this->request->post['payment_securetrading_ws_password'];
		} else {
			$data['payment_securetrading_ws_password'] = $this->config->get('payment_securetrading_ws_password');
		}

		if (isset($this->request->post['payment_securetrading_ws_csv_username'])) {
			$data['payment_securetrading_ws_csv_username'] = $this->request->post['payment_securetrading_ws_csv_username'];
		} else {
			$data['payment_securetrading_ws_csv_username'] = $this->config->get('payment_securetrading_ws_csv_username');
		}

		if (isset($this->request->post['payment_securetrading_ws_csv_password'])) {
			$data['payment_securetrading_ws_csv_password'] = $this->request->post['payment_securetrading_ws_csv_password'];
		} else {
			$data['payment_securetrading_ws_csv_password'] = $this->config->get('payment_securetrading_ws_csv_password');
		}

		$this->config->set('payment_securetrading_ws_3d_secure', 1);

		if (isset($this->request->post['payment_securetrading_ws_3d_secure'])) {
			$data['payment_securetrading_ws_3d_secure'] = $this->request->post['payment_securetrading_ws_3d_secure'];
		} else {
			$data['payment_securetrading_ws_3d_secure'] = $this->config->get('payment_securetrading_ws_3d_secure');
		}

		if (isset($this->request->post['payment_securetrading_ws_cards_accepted'])) {
			$data['payment_securetrading_ws_cards_accepted'] = $this->request->post['payment_securetrading_ws_cards_accepted'];
		} else {
			$data['payment_securetrading_ws_cards_accepted'] = $this->config->get('payment_securetrading_ws_cards_accepted');

			if ($data['payment_securetrading_ws_cards_accepted'] == null) {
				$data['payment_securetrading_ws_cards_accepted'] = array();
			}
		}

		if (isset($this->request->post['payment_securetrading_ws_order_status_id'])) {
			$data['payment_securetrading_ws_order_status_id'] = $this->request->post['payment_securetrading_ws_order_status_id'];
		} elseif ($this->config->get('payment_securetrading_ws_order_status_id') != '') {
			$data['payment_securetrading_ws_order_status_id'] = $this->config->get('payment_securetrading_ws_order_status_id');
		} else {
			$data['payment_securetrading_ws_order_status_id'] = 1;
		}

		if (isset($this->request->post['payment_securetrading_ws_failed_order_status_id'])) {
			$data['payment_securetrading_ws_failed_order_status_id'] = $this->request->post['payment_securetrading_ws_failed_order_status_id'];
		} elseif ($this->config->get('payment_securetrading_ws_failed_order_status_id') != '') {
			$data['payment_securetrading_ws_failed_order_status_id'] = $this->config->get('payment_securetrading_ws_failed_order_status_id');
		} else {
			$data['payment_securetrading_ws_failed_order_status_id'] = 10;
		}

		if (isset($this->request->post['payment_securetrading_ws_declined_order_status_id'])) {
			$data['payment_securetrading_ws_declined_order_status_id'] = $this->request->post['payment_securetrading_ws_declined_order_status_id'];
		} elseif ($this->config->get('payment_securetrading_ws_declined_order_status_id') != '') {
			$data['payment_securetrading_ws_declined_order_status_id'] = $this->config->get('payment_securetrading_ws_declined_order_status_id');
		} else {
			$data['payment_securetrading_ws_declined_order_status_id'] = 8;
		}

		if (isset($this->request->post['payment_securetrading_ws_refunded_order_status_id'])) {
			$data['payment_securetrading_ws_refunded_order_status_id'] = $this->request->post['payment_securetrading_ws_refunded_order_status_id'];
		} elseif ($this->config->get('payment_securetrading_ws_refunded_order_status_id') != '') {
			$data['payment_securetrading_ws_refunded_order_status_id'] = $this->config->get('payment_securetrading_ws_refunded_order_status_id');
		} else {
			$data['payment_securetrading_ws_refunded_order_status_id'] = 11;
		}

		if (isset($this->request->post['payment_securetrading_ws_authorisation_reversed_order_status_id'])) {
			$data['payment_securetrading_ws_authorisation_reversed_order_status_id'] = $this->request->post['payment_securetrading_ws_authorisation_reversed_order_status_id'];
		} elseif ($this->config->get('payment_securetrading_ws_authorisation_reversed_order_status_id') != '') {
			$data['payment_securetrading_ws_authorisation_reversed_order_status_id'] = $this->config->get('payment_securetrading_ws_authorisation_reversed_order_status_id');
		} else {
			$data['payment_securetrading_ws_authorisation_reversed_order_status_id'] = 12;
		}

		if (isset($this->request->post['payment_securetrading_ws_settle_status'])) {
			$data['payment_securetrading_ws_settle_status'] = $this->request->post['payment_securetrading_ws_settle_status'];
		} else {
			$data['payment_securetrading_ws_settle_status'] = $this->config->get('payment_securetrading_ws_settle_status');
		}

		if (isset($this->request->post['payment_securetrading_ws_settle_due_date'])) {
			$data['payment_securetrading_ws_settle_due_date'] = $this->request->post['payment_securetrading_ws_settle_due_date'];
		} else {
			$data['payment_securetrading_ws_settle_due_date'] = $this->config->get('payment_securetrading_ws_settle_due_date');
		}

		if (isset($this->request->post['payment_securetrading_ws_geo_zone'])) {
			$data['payment_securetrading_ws_geo_zone_id'] = $this->request->post['payment_securetrading_ws_geo_zone_id'];
		} else {
			$data['payment_securetrading_ws_geo_zone_id'] = $this->config->get('payment_securetrading_ws_geo_zone_id');
		}

		if (isset($this->request->post['payment_securetrading_ws_status'])) {
			$data['payment_securetrading_ws_status'] = $this->request->post['payment_securetrading_ws_status'];
		} else {
			$data['payment_securetrading_ws_status'] = $this->config->get('payment_securetrading_ws_status');
		}

		if (isset($this->request->post['payment_securetrading_ws_sort_order'])) {
			$data['payment_securetrading_ws_sort_order'] = $this->request->post['payment_securetrading_ws_sort_order'];
		} else {
			$data['payment_securetrading_ws_sort_order'] = $this->config->get('payment_securetrading_ws_sort_order');
		}

		if (isset($this->request->post['payment_securetrading_ws_total'])) {
			$data['payment_securetrading_ws_total'] = $this->request->post['payment_securetrading_ws_total'];
		} else {
			$data['payment_securetrading_ws_total'] = $this->config->get('payment_securetrading_ws_total');
		}

		$this->document->setTitle($this->language->get('heading_title'));

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['site_reference'])) {
			$data['error_site_reference'] = $this->error['site_reference'];
		} else {
			$data['error_site_reference'] = '';
		}

		if (isset($this->error['username'])) {
			$data['error_username'] = $this->error['username'];
		} else {
			$data['error_username'] = '';
		}

		if (isset($this->error['password'])) {
			$data['error_password'] = $this->error['password'];
		} else {
			$data['error_password'] = '';
		}

		if (isset($this->error['cards_accepted'])) {
			$data['error_cards_accepted'] = $this->error['cards_accepted'];
		} else {
			$data['error_cards_accepted'] = '';
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_extension'),
			'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('extension/payment/securetrading_ws', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
		$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

		$data['cards'] = array(
			'AMEX' => 'American Express',
			'VISA' => 'Visa',
			'DELTA' => 'Visa Debit',
			'ELECTRON' => 'Visa Electron',
			'PURCHASING' => 'Visa Purchasing',
			'VPAY' => 'V Pay',
			'MASTERCARD' => 'MasterCard',
			'MASTERCARDDEBIT' => 'MasterCard Debit',
			'MAESTRO' => 'Maestro',
			'PAYPAL' => 'PayPal',
		);

		$data['settlement_statuses'] = array(
			'0' => $this->language->get('text_pending_settlement'),
			'1' => $this->language->get('text_pending_settlement_manually_overriden'),
			'2' => $this->language->get('text_pending_suspended'),
			'100' => $this->language->get('text_pending_settled'),
		);

		$data['action'] = $this->url->link('extension/payment/securetrading_ws', 'user_token=' . $this->session->data['user_token'], true);

		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);

		$data['myst_status'] = !empty($data['securetrading_ws_csv_username']) && !empty($data['securetrading_ws_csv_password']);
		$data['hours'] = array();

		for ($i = 0; $i < 24; $i++) {
			$data['hours'][] = str_pad($i, 2, '0', STR_PAD_LEFT);
		}

		$data['minutes'] = array();

		for ($i = 0; $i < 60; $i++) {
			$data['minutes'][] = str_pad($i, 2, '0', STR_PAD_LEFT);
		}

		$data['currencies'] = $this->model_localisation_currency->getCurrencies();

		$data['user_token'] = $this->session->data['user_token'];

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/payment/securetrading_ws', $data));
	}

	public function install() {
		$this->load->model('extension/payment/securetrading_ws');
		$this->model_extension_payment_securetrading_ws->install();
	}

	public function uninstall() {
		$this->load->model('extension/payment/securetrading_ws');
		$this->model_extension_payment_securetrading_ws->uninstall();
	}

	public function downloadTransactions() {
		$this->load->model('extension/payment/securetrading_ws');
		$this->load->language('extension/payment/securetrading_ws');

		$csv_data = $this->request->post;
		$csv_data['detail'] = true;

		$response = $this->model_extension_payment_securetrading_ws->getCsv($csv_data);

		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename="' . $this->language->get('text_transactions') . '.csv"');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		header('Content-Length: ' . strlen($response));

		if (ob_get_level()) {
			ob_end_clean();
		}

		echo $response;
		exit();
	}

	public function showTransactions() {
		$this->load->model('extension/payment/securetrading_ws');
		$this->load->language('extension/payment/securetrading_ws');

		$csv_data = $this->request->post;
		$csv_data['detail'] = false;

		$response = $this->model_extension_payment_securetrading_ws->getCsv($csv_data);

		$data['transactions'] = array();

		$status_mapping = array(
			'0' => $this->language->get('text_ok'),
			'70000' => $this->language->get('text_denied'),
		);

		$settle_status_mapping = array(
			'0' => $this->language->get('text_pending_settlement'),
			'1' => $this->language->get('text_manual_settlement'),
			'2' => $this->language->get('text_suspended'),
			'3' => $this->language->get('text_cancelled'),
			'10' => $this->language->get('text_settling'),
			'100' => $this->language->get('text_settled'),
		);

		if ($response) {
			$lines = array_filter(explode("\n", $response));

			$csv = array();
			$keys = str_getcsv($lines[0]);

			for ($i = 1; $i < count($lines); $i++) {
				$csv[] = array_combine($keys, str_getcsv($lines[$i]));
			}

			foreach ($csv as $row) {
				$data['transactions'][] = array(
					'order_id' => $row['orderreference'],
					'order_href' => $this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $row['orderreference'], true),
					'transaction_reference' => $row['transactionreference'],
					'customer' => $row['billingfirstname'] . ' ' . $row['billinglastname'],
					'total' => $row['mainamount'],
					'currency' => $row['currencyiso3a'],
					'settle_status' => $settle_status_mapping[$row['settlestatus']],
					'status' => $status_mapping[$row['errorcode']],
					'type' => $row['requesttypedescription'],
					'payment_type' => $row['paymenttypedescription'],
				);
			}
		}

		return $this->load->view('extension/payment/securetrading_ws_transactions', $data);
	}

	public function order() {

		if ($this->config->get('payment_securetrading_ws_status')) {
			$this->load->model('extension/payment/securetrading_ws');

			$securetrading_ws_order = $this->model_extension_payment_securetrading_ws->getOrder($this->request->get['order_id']);

			if (!empty($securetrading_ws_order)) {
				$this->load->language('extension/payment/securetrading_ws');

				$securetrading_ws_order['total_released'] = $this->model_extension_payment_securetrading_ws->getTotalReleased($securetrading_ws_order['securetrading_ws_order_id']);

				$securetrading_ws_order['total_formatted'] = $this->currency->format($securetrading_ws_order['total'], $securetrading_ws_order['currency_code'], false, false);
				$securetrading_ws_order['total_released_formatted'] = $this->currency->format($securetrading_ws_order['total_released'], $securetrading_ws_order['currency_code'], false, false);

				$data['securetrading_ws_order'] = $securetrading_ws_order;

				$data['auto_settle'] = $securetrading_ws_order['settle_type'];

				$data['order_id'] = $this->request->get['order_id'];
				
				$data['user_token'] = $this->request->get['user_token'];
				
				return $this->load->view('extension/payment/securetrading_ws_order', $data);
			}
		}
	}

	public function void() {
		$this->load->language('extension/payment/securetrading_ws');
		$json = array();

		if (isset($this->request->post['order_id']) && $this->request->post['order_id'] != '') {
			$this->load->model('extension/payment/securetrading_ws');

			$securetrading_ws_order = $this->model_extension_payment_securetrading_ws->getOrder($this->request->post['order_id']);

			$void_response = $this->model_extension_payment_securetrading_ws->void($this->request->post['order_id']);

			$this->model_extension_payment_securetrading_ws->logger('Void result:\r\n' . print_r($void_response, 1));

			if ($void_response !== false) {
				$response_xml = simplexml_load_string($void_response);

				if ($response_xml->response['type'] == 'ERROR' || (string)$response_xml->response->error->code != '0') {
					$json['msg'] = (string)$response_xml->response->error->message;
					$json['error'] = true;
				} else {

					$this->model_extension_payment_securetrading_ws->addTransaction($securetrading_ws_order['securetrading_ws_order_id'], 'reversed', 0.00);
					$this->model_extension_payment_securetrading_ws->updateVoidStatus($securetrading_ws_order['securetrading_ws_order_id'], 1);

					$this->data = array(
						'order_status_id' => $this->config->get('payment_securetrading_ws_authorisation_reversed_order_status_id'),
						'notify' => false,
						'comment' => '',
					);

					$this->load->model('sale/order');

					$this->model_sale_order->addOrderHistory($this->request->post['order_id'], $this->data);

					$json['msg'] = $this->language->get('text_authorisation_reversed');
					$json['data']['created'] = date("Y-m-d H:i:s");
					$json['error'] = false;
				}
			} else {
				$json['msg'] = $this->language->get('error_connection');
				$json['error'] = true;
			}
		} else {
			$json['error'] = true;
			$json['msg'] = 'Missing data';
		}

		$this->response->setOutput(json_encode($json));
	}

	public function release() {
		$this->load->language('extension/payment/securetrading_ws');
		$json = array();

		$amount = number_format($this->request->post['amount'], 2);

		if (isset($this->request->post['order_id']) && $this->request->post['order_id'] != '' && isset($amount) && $amount > 0) {
			$this->load->model('extension/payment/securetrading_ws');

			$securetrading_ws_order = $this->model_extension_payment_securetrading_ws->getOrder($this->request->post['order_id']);

			$release_response = $this->model_extension_payment_securetrading_ws->release($this->request->post['order_id'], $amount);

			$this->model_extension_payment_securetrading_ws->logger('Release result:\r\n' . print_r($release_response, 1));

			if ($release_response !== false) {
				$response_xml = simplexml_load_string($release_response);

				if ($response_xml->response['type'] == 'ERROR' || (string)$response_xml->response->error->code != '0') {
					$json['error'] = true;
					$json['msg'] = (string)$response_xml->response->error->message;
				} else {
					$this->model_extension_payment_securetrading_ws->addTransaction($securetrading_ws_order['securetrading_ws_order_id'], 'payment', $amount);

					$total_released = $this->model_extension_payment_securetrading_ws->getTotalReleased($securetrading_ws_order['securetrading_ws_order_id']);

					if ($total_released >= $securetrading_ws_order['total'] || $securetrading_ws_order['settle_type'] == 100) {
						$this->model_extension_payment_securetrading_ws->updateReleaseStatus($securetrading_ws_order['securetrading_ws_order_id'], 1);
						$release_status = 1;
						$json['msg'] = $this->language->get('text_release_ok_order');

						$this->load->model('sale/order');

						$history = array();
						$history['order_status_id'] = $this->config->get('securetrading_ws_order_status_success_settled_id');
						$history['comment'] = '';
						$history['notify'] = '';

						$this->model_sale_order->addOrderHistory($this->request->post['order_id'], $history);
					} else {
						$release_status = 0;
						$json['msg'] = $this->language->get('text_release_ok');
					}

					$json['data'] = array();
					$json['data']['created'] = date("Y-m-d H:i:s");
					$json['data']['amount'] = $amount;
					$json['data']['release_status'] = $release_status;
					$json['data']['total'] = (double)$total_released;
					$json['error'] = false;
				}
			} else {
				$json['error'] = true;
				$json['msg'] = $this->language->get('error_connection');
			}
		} else {
			$json['error'] = true;
			$json['msg'] = $this->language->get('error_data_missing');
		}

		$this->response->setOutput(json_encode($json));
	}

	public function rebate() {
		$this->load->language('extension/payment/securetrading_ws');
		$json = array();

		if (isset($this->request->post['order_id']) && !empty($this->request->post['order_id'])) {
			$this->load->model('extension/payment/securetrading_ws');

			$securetrading_ws_order = $this->model_extension_payment_securetrading_ws->getOrder($this->request->post['order_id']);

			$amount = number_format($this->request->post['amount'], 2);

			$rebate_response = $this->model_extension_payment_securetrading_ws->rebate($this->request->post['order_id'], $amount);

			$this->model_extension_payment_securetrading_ws->logger('Rebate result:\r\n' . print_r($rebate_response, 1));

			if ($rebate_response !== false) {
				$response_xml = simplexml_load_string($rebate_response);

				$error_code = (string)$response_xml->response->error->code;

				if ($error_code == '0') {

					$this->model_extension_payment_securetrading_ws->addTransaction($securetrading_ws_order['securetrading_ws_order_id'], 'rebate', $amount * -1);

					$total_rebated = $this->model_extension_payment_securetrading_ws->getTotalRebated($securetrading_ws_order['securetrading_ws_order_id']);
					$total_released = $this->model_extension_payment_securetrading_ws->getTotalReleased($securetrading_ws_order['securetrading_ws_order_id']);

					if ($total_released <= 0 && $securetrading_ws_order['release_status'] == 1) {
						$json['status'] = 1;
						$json['message'] = $this->language->get('text_refund_issued');

						$this->model_extension_payment_securetrading_ws->updateRebateStatus($securetrading_ws_order['securetrading_ws_order_id'], 1);
						$rebate_status = 1;
						$json['msg'] = $this->language->get('text_rebate_ok_order');

						$this->load->model('sale/order');

						$history = array();
						$history['order_status_id'] = $this->config->get('payment_securetrading_ws_refunded_order_status_id');
						$history['comment'] = '';
						$history['notify'] = '';

						$this->model_sale_order->addOrderHistory($this->request->post['order_id'], $history);
					} else {
						$rebate_status = 0;
						$json['msg'] = $this->language->get('text_rebate_ok');
					}

					$json['data'] = array();
					$json['data']['created'] = date("Y-m-d H:i:s");
					$json['data']['amount'] = $amount * -1;
					$json['data']['total_released'] = (double)$total_released;
					$json['data']['total_rebated'] = (double)$total_rebated;
					$json['data']['rebate_status'] = $rebate_status;
					$json['error'] = false;
				} else {
					$json['error'] = true;
					$json['msg'] = (string)$response_xml->response->error->message;
				}
			} else {
				$json['status'] = 0;
				$json['message'] = $this->language->get('error_connection');
			}
		} else {
			$json['error'] = true;
			$json['msg'] = 'Missing data';
		}

		$this->response->setOutput(json_encode($json));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/payment/securetrading_pp')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->request->post['payment_securetrading_ws_site_reference']) {
			$this->error['site_reference'] = $this->language->get('error_site_reference');
		}

		if (!$this->request->post['payment_securetrading_ws_username']) {
			$this->error['username'] = $this->language->get('error_username');
		}

		if (!$this->request->post['payment_securetrading_ws_password']) {
			$this->error['password'] = $this->language->get('error_password');
		}

		if (empty($this->request->post['payment_securetrading_ws_cards_accepted'])) {
			$this->error['cards_accepted'] = $this->language->get('error_cards_accepted');
		}

		return !$this->error;
	}
}