PageRenderTime 63ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/payment/g2apay.php

https://gitlab.com/reclamare/mao
PHP | 356 lines | 275 code | 81 blank | 0 comment | 63 complexity | 9df2bb338a9a7561b79b5236d8e79b1e MD5 | raw file
  1. <?php
  2. class ControllerPaymentG2APay extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('payment/g2apay');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('setting/setting');
  8. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  9. $this->model_setting_setting->editSetting('g2apay', $this->request->post);
  10. $this->session->data['complete'] = $this->language->get('text_complete');
  11. $this->response->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
  12. }
  13. $data['heading_title'] = $this->language->get('heading_title');
  14. $data['entry_username'] = $this->language->get('entry_username');
  15. $data['entry_secret'] = $this->language->get('entry_secret');
  16. $data['entry_api_hash'] = $this->language->get('entry_api_hash');
  17. $data['entry_environment'] = $this->language->get('entry_environment');
  18. $data['entry_secret_token'] = $this->language->get('entry_secret_token');
  19. $data['entry_ipn_url'] = $this->language->get('entry_ipn_url');
  20. $data['entry_total'] = $this->language->get('entry_total');
  21. $data['entry_order_status'] = $this->language->get('entry_order_status');
  22. $data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
  23. $data['entry_status'] = $this->language->get('entry_status');
  24. $data['entry_sort_order'] = $this->language->get('entry_sort_order');
  25. $data['entry_debug'] = $this->language->get('entry_debug');
  26. $data['entry_complete_status'] = $this->language->get('entry_complete_status');
  27. $data['entry_rejected_status'] = $this->language->get('entry_rejected_status');
  28. $data['entry_cancelled_status'] = $this->language->get('entry_cancelled_status');
  29. $data['entry_refunded_status'] = $this->language->get('entry_refunded_status');
  30. $data['entry_partially_refunded_status'] = $this->language->get('entry_partially_refunded_status');
  31. $data['g2apay_environment_live'] = $this->language->get('g2apay_environment_live');
  32. $data['g2apay_environment_test'] = $this->language->get('g2apay_environment_test');
  33. $data['text_edit'] = $this->language->get('text_edit');
  34. $data['text_enabled'] = $this->language->get('text_enabled');
  35. $data['text_disabled'] = $this->language->get('text_disabled');
  36. $data['text_all_zones'] = $this->language->get('text_all_zones');
  37. $data['help_username'] = $this->language->get('help_username');
  38. $data['help_secret_token'] = $this->language->get('help_secret_token');
  39. $data['help_ipn_url'] = $this->language->get('help_ipn_url');
  40. $data['help_total'] = $this->language->get('help_total');
  41. $data['help_debug'] = $this->language->get('help_debug');
  42. $data['tab_settings'] = $this->language->get('tab_settings');
  43. $data['tab_order_status'] = $this->language->get('tab_order_status');
  44. $data['button_save'] = $this->language->get('button_save');
  45. $data['button_cancel'] = $this->language->get('button_cancel');
  46. if (isset($this->error['warning'])) {
  47. $data['error_warning'] = $this->error['warning'];
  48. } else {
  49. $data['error_warning'] = '';
  50. }
  51. if (isset($this->error['username'])) {
  52. $data['error_username'] = $this->error['username'];
  53. } else {
  54. $data['error_username'] = '';
  55. }
  56. if (isset($this->error['secret'])) {
  57. $data['error_secret'] = $this->error['secret'];
  58. } else {
  59. $data['error_secret'] = '';
  60. }
  61. if (isset($this->error['api_hash'])) {
  62. $data['error_api_hash'] = $this->error['api_hash'];
  63. } else {
  64. $data['error_api_hash'] = '';
  65. }
  66. if (isset($this->request->post['g2apay_order_status_id'])) {
  67. $data['g2apay_order_status_id'] = $this->request->post['g2apay_order_status_id'];
  68. } else {
  69. $data['g2apay_order_status_id'] = $this->config->get('g2apay_order_status_id');
  70. }
  71. if (isset($this->request->post['g2apay_complete_status_id'])) {
  72. $data['g2apay_complete_status_id'] = $this->request->post['g2apay_complete_status_id'];
  73. } else {
  74. $data['g2apay_complete_status_id'] = $this->config->get('g2apay_complete_status_id');
  75. }
  76. if (isset($this->request->post['g2apay_rejected_status_id'])) {
  77. $data['g2apay_rejected_status_id'] = $this->request->post['g2apay_rejected_status_id'];
  78. } else {
  79. $data['g2apay_rejected_status_id'] = $this->config->get('g2apay_rejected_status_id');
  80. }
  81. if (isset($this->request->post['g2apay_cancelled_status_id'])) {
  82. $data['g2apay_cancelled_status_id'] = $this->request->post['g2apay_cancelled_status_id'];
  83. } else {
  84. $data['g2apay_cancelled_status_id'] = $this->config->get('g2apay_cancelled_status_id');
  85. }
  86. if (isset($this->request->post['g2apay_refunded_status_id'])) {
  87. $data['g2apay_refunded_status_id'] = $this->request->post['g2apay_refunded_status_id'];
  88. } else {
  89. $data['g2apay_refunded_status_id'] = $this->config->get('g2apay_refunded_status_id');
  90. }
  91. if (isset($this->request->post['g2apay_partially_refunded_status_id'])) {
  92. $data['g2apay_partially_refunded_status_id'] = $this->request->post['g2apay_partially_refunded_status_id'];
  93. } else {
  94. $data['g2apay_partially_refunded_status_id'] = $this->config->get('g2apay_partially_refunded_status_id');
  95. }
  96. $data['breadcrumbs'] = array();
  97. $data['breadcrumbs'][] = array(
  98. 'text' => $this->language->get('text_home'),
  99. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
  100. );
  101. $data['breadcrumbs'][] = array(
  102. 'text' => $this->language->get('text_payment'),
  103. 'href' => $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL')
  104. );
  105. $data['breadcrumbs'][] = array(
  106. 'text' => $this->language->get('heading_title'),
  107. 'href' => $this->url->link('payment/g2apay', 'token=' . $this->session->data['token'], 'SSL')
  108. );
  109. $this->load->model('localisation/order_status');
  110. $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
  111. $data['action'] = $this->url->link('payment/g2apay', 'token=' . $this->session->data['token'], 'SSL');
  112. $data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
  113. if (isset($this->request->post['g2apay_username'])) {
  114. $data['g2apay_username'] = $this->request->post['g2apay_username'];
  115. } else {
  116. $data['g2apay_username'] = $this->config->get('g2apay_username');
  117. }
  118. if (isset($this->request->post['g2apay_secret'])) {
  119. $data['g2apay_secret'] = $this->request->post['g2apay_secret'];
  120. } else {
  121. $data['g2apay_secret'] = $this->config->get('g2apay_secret');
  122. }
  123. if (isset($this->request->post['g2apay_api_hash'])) {
  124. $data['g2apay_api_hash'] = $this->request->post['g2apay_api_hash'];
  125. } else {
  126. $data['g2apay_api_hash'] = $this->config->get('g2apay_api_hash');
  127. }
  128. if (isset($this->request->post['g2apay_environment'])) {
  129. $data['g2apay_environment'] = $this->request->post['g2apay_environment'];
  130. } else {
  131. $data['g2apay_environment'] = $this->config->get('g2apay_environment');
  132. }
  133. if (isset($this->request->post['g2apay_total'])) {
  134. $data['g2apay_total'] = $this->request->post['g2apay_total'];
  135. } else {
  136. $data['g2apay_total'] = $this->config->get('g2apay_total');
  137. }
  138. if (isset($this->request->post['g2apay_secret_token'])) {
  139. $data['g2apay_secret_token'] = $this->request->post['g2apay_secret_token'];
  140. } elseif ($this->config->get('g2apay_secret_token')) {
  141. $data['g2apay_secret_token'] = $this->config->get('g2apay_secret_token');
  142. } else {
  143. $data['g2apay_secret_token'] = sha1(uniqid(mt_rand(), 1));
  144. }
  145. $data['g2apay_ipn_url'] = HTTPS_CATALOG . 'index.php?route=payment/g2apay/ipn&token=' . $data['g2apay_secret_token'];
  146. if (isset($this->request->post['g2apay_ipn_uri'])) {
  147. $data['g2apay_ipn_uri'] = $this->request->post['g2apay_ipn_uri'];
  148. } else {
  149. $data['g2apay_ipn_uri'] = $this->config->get('g2apay_ipn_uri');
  150. }
  151. if (isset($this->request->post['g2apay_order_status_id'])) {
  152. $data['g2apay_order_status_id'] = $this->request->post['g2apay_order_status_id'];
  153. } else {
  154. $data['g2apay_order_status_id'] = $this->config->get('g2apay_order_status_id');
  155. }
  156. $this->load->model('localisation/order_status');
  157. $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
  158. if (isset($this->request->post['g2apay_geo_zone_id'])) {
  159. $data['g2apay_geo_zone_id'] = $this->request->post['g2apay_geo_zone_id'];
  160. } else {
  161. $data['g2apay_geo_zone_id'] = $this->config->get('g2apay_geo_zone_id');
  162. }
  163. $this->load->model('localisation/geo_zone');
  164. $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
  165. if (isset($this->request->post['g2apay_status'])) {
  166. $data['g2apay_status'] = $this->request->post['g2apay_status'];
  167. } else {
  168. $data['g2apay_status'] = $this->config->get('g2apay_status');
  169. }
  170. if (isset($this->request->post['g2apay_debug'])) {
  171. $data['g2apay_debug'] = $this->request->post['g2apay_debug'];
  172. } else {
  173. $data['g2apay_debug'] = $this->config->get('g2apay_debug');
  174. }
  175. if (isset($this->request->post['g2apay_sort_order'])) {
  176. $data['g2apay_sort_order'] = $this->request->post['g2apay_sort_order'];
  177. } else {
  178. $data['g2apay_sort_order'] = $this->config->get('g2apay_sort_order');
  179. }
  180. $data['header'] = $this->load->controller('common/header');
  181. $data['column_left'] = $this->load->controller('common/column_left');
  182. $data['footer'] = $this->load->controller('common/footer');
  183. $this->response->setOutput($this->load->view('payment/g2apay.tpl', $data));
  184. }
  185. public function order() {
  186. if ($this->config->get('g2apay_status')) {
  187. $this->load->model('payment/g2apay');
  188. $g2apay_order = $this->model_payment_g2apay->getOrder($this->request->get['order_id']);
  189. if (!empty($g2apay_order)) {
  190. $this->load->language('payment/g2apay');
  191. $g2apay_order['total_released'] = $this->model_payment_g2apay->getTotalReleased($g2apay_order['g2apay_order_id']);
  192. $g2apay_order['total_formatted'] = $this->currency->format($g2apay_order['total'], $g2apay_order['currency_code'], false);
  193. $g2apay_order['total_released_formatted'] = $this->currency->format($g2apay_order['total_released'], $g2apay_order['currency_code'], false);
  194. $data['g2apay_order'] = $g2apay_order;
  195. $data['text_payment_info'] = $this->language->get('text_payment_info');
  196. $data['text_order_ref'] = $this->language->get('text_order_ref');
  197. $data['text_order_total'] = $this->language->get('text_order_total');
  198. $data['text_total_released'] = $this->language->get('text_total_released');
  199. $data['text_refund_status'] = $this->language->get('text_refund_status');
  200. $data['text_transactions'] = $this->language->get('text_transactions');
  201. $data['text_yes'] = $this->language->get('text_yes');
  202. $data['text_no'] = $this->language->get('text_no');
  203. $data['text_column_amount'] = $this->language->get('text_column_amount');
  204. $data['text_column_type'] = $this->language->get('text_column_type');
  205. $data['text_column_date_added'] = $this->language->get('text_column_date_added');
  206. $data['btn_refund'] = $this->language->get('btn_refund');
  207. $data['text_confirm_refund'] = $this->language->get('text_confirm_refund');
  208. $data['order_id'] = $this->request->get['order_id'];
  209. $data['token'] = $this->request->get['token'];
  210. return $this->load->view('payment/g2apay_order.tpl', $data);
  211. }
  212. }
  213. }
  214. public function refund() {
  215. $this->load->language('payment/g2apay');
  216. $json = array();
  217. if (isset($this->request->post['order_id']) && !empty($this->request->post['order_id'])) {
  218. $this->load->model('payment/g2apay');
  219. $g2apay_order = $this->model_payment_g2apay->getOrder($this->request->post['order_id']);
  220. $refund_response = $this->model_payment_g2apay->refund($g2apay_order, $this->request->post['amount']);
  221. $this->model_payment_g2apay->logger($refund_response);
  222. if ($refund_response == 'ok') {
  223. $this->model_payment_g2apay->addTransaction($g2apay_order['g2apay_order_id'], 'refund', $this->request->post['amount'] * -1);
  224. $total_refunded = $this->model_payment_g2apay->getTotalRefunded($g2apay_order['g2apay_order_id']);
  225. $total_released = $this->model_payment_g2apay->getTotalReleased($g2apay_order['g2apay_order_id']);
  226. if ($total_released <= 0 && $g2apay_order['release_status'] == 1) {
  227. $this->model_payment_g2apay->updateRefundStatus($g2apay_order['g2apay_order_id'], 1);
  228. $refund_status = 1;
  229. $json['msg'] = $this->language->get('text_refund_ok_order');
  230. } else {
  231. $refund_status = 0;
  232. $json['msg'] = $this->language->get('text_refund_ok');
  233. }
  234. $json['data'] = array();
  235. $json['data']['date_added'] = date("Y-m-d H:i:s");
  236. $json['data']['amount'] = $this->currency->format(($this->request->post['amount'] * -1), $g2apay_order['currency_code'], false);
  237. $json['data']['total_released'] = (float)$total_released;
  238. $json['data']['total_refunded'] = (float)$total_refunded;
  239. $json['data']['refund_status'] = $refund_status;
  240. $json['error'] = false;
  241. } else {
  242. $json['error'] = true;
  243. $json['msg'] = 'Unable to refund: ' . $refund_response;
  244. }
  245. } else {
  246. $json['error'] = true;
  247. $json['msg'] = 'Missing data';
  248. }
  249. $this->response->addHeader('Content-Type: application/json');
  250. $this->response->setOutput(json_encode($json));
  251. }
  252. public function install() {
  253. $this->load->model('payment/g2apay');
  254. $this->model_payment_g2apay->install();
  255. }
  256. public function uninstall() {
  257. $this->load->model('payment/g2apay');
  258. $this->model_payment_g2apay->uninstall();
  259. }
  260. protected function validate() {
  261. if (!$this->user->hasPermission('modify', 'payment/g2apay')) {
  262. $this->error['warning'] = $this->language->get('error_permission');
  263. }
  264. if (!$this->request->post['g2apay_username']) {
  265. $this->error['username'] = $this->language->get('error_username');
  266. }
  267. if (!$this->request->post['g2apay_secret']) {
  268. $this->error['secret'] = $this->language->get('error_secret');
  269. }
  270. if (!$this->request->post['g2apay_api_hash']) {
  271. $this->error['api_hash'] = $this->language->get('error_api_hash');
  272. }
  273. return !$this->error;
  274. }
  275. }