initTpayGateway

The tpay.com Woocommerce payment module initTpayGateway function.

Description

initTpayGateway(); 

Usage

  1. if ( !function_exists( 'initTpayGateway' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'tpay-com-woocommerce-payment-module/tpayWoocommerceGateway.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = initTpayGateway(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/tpayWoocommerceGateway.php  
  1. function initTpayGateway() 
  2.  
  3. if (!class_exists('WC_Payment_Gateway')) { 
  4. add_action('admin_init', 'childPluginHasParentPlugin'); 
  5. function childPluginHasParentPlugin() 
  6. if (is_admin() && current_user_can('activate_plugins') 
  7. && !is_plugin_active('woocommerce/woocommerce.php') 
  8. ) { 
  9. add_action('admin_notices', 'childPluginNotice'); 
  10.  
  11. deactivate_plugins(plugin_basename(__FILE__)); 
  12.  
  13. if (filter_input(INPUT_GET, 'activate')) { 
  14. unset($_GET['activate']); 
  15.  
  16. function childPluginNotice() 
  17. echo ' 
  18. <div class="error"><p>Modu* p*atno*ci tpay.com wymaga zainstalowanej wtyczki Woocommerce, ktr* mo*na pobra* 
  19. <a target="blank" href="https://wordpress.org/plugins/woocommerce/">tutaj</a></p></div>'; 
  20.  
  21. return; 
  22.  
  23.  
  24. class WC_Gateway_Transferuj extends WC_Payment_Gateway 
  25. const REGULATIONS = 'regulamin'; 
  26. const BLIKCODE = 'blikcode'; 
  27. const ORDER_ID = 'orderId'; 
  28. const KANAL = 'kanal'; 
  29. const RESULT = 'result'; 
  30. const REDIRECT = 'redirect'; 
  31. const SUCCESS = 'success'; 
  32. const CAUGHT_EXCEPTION = 'Caught exception: '; 
  33. const TR_CRC = 'tr_crc'; 
  34. const TR_ERROR = 'tr_error'; 
  35. const KWOTA_DOPLATY = 'kwota_doplaty'; 
  36. const BANK_LIST = 'bank_list'; 
  37. const DOPLATA = 'doplata'; 
  38. const GATEWAY_NAME = 'WC_Gateway_Transferuj'; 
  39. const WOOCOMMERCE = 'woocommerce'; 
  40. //MUST BE OLD NAME! 
  41. const GATEWAY_ID = 'transferuj'; 
  42. const JEZYK = 'jezyk'; 
  43. const BANK_VIEW = 'bank_view'; 
  44. const FAILURE = 'failure'; 
  45. const HTTP = 'http://'; 
  46. const HTTPS = 'https://'; 
  47. const WC_API = 'wc-api'; 
  48. private $pluginUrl; 
  49.  
  50. /** 
  51. * Constructor for the gateway. 
  52. * @access public 
  53. * @global type $woocommerce 
  54. */ 
  55.  
  56. public function __construct() 
  57. $this->id = __(static::GATEWAY_ID, static::WOOCOMMERCE); 
  58.  
  59. $this->has_fields = true; 
  60.  
  61. $this->method_title = __('tpay.com', static::WOOCOMMERCE); 
  62. if ((isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') 
  63. || ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')) 
  64. ) { 
  65. $this->pluginUrl = str_replace(static::HTTP, static::HTTPS, plugins_url('', __FILE__)); 
  66. $this->notify_link = str_replace(static::HTTP, static::HTTPS,  
  67. add_query_arg(static::WC_API, static::GATEWAY_NAME, home_url('/'))); 
  68. } else { 
  69. $this->pluginUrl = plugins_url('', __FILE__); 
  70. $this->notify_link = str_replace(static::HTTPS, static::HTTP,  
  71. add_query_arg(static::WC_API, static::GATEWAY_NAME, home_url('/'))); 
  72.  
  73. $this->icon = apply_filters('woocommerce_transferuj_icon',  
  74. $this->pluginUrl . '/images/tpayLogo.png'); 
  75. // Add tpay.com as payment gateway 
  76. add_filter('woocommerce_payment_gateways', array($this, 'add_transferuj_gateway')); 
  77.  
  78. // Load the settings. 
  79. $this->init_form_fields(); 
  80. $this->init_settings(); 
  81. if (!$this->is_valid_for_use()) { 
  82. $this->enabled = 'no'; 
  83. // Define user set variables 
  84. $this->title = $this->get_option('title'); 
  85. $this->opis = $this->get_option('opis'); 
  86. $this->kwota_doplaty = $this->get_option(static::KWOTA_DOPLATY); 
  87. $this->description = $this->get_option('description'); 
  88. $this->seller_id = $this->get_option('seller_id'); 
  89. $this->security_code = $this->get_option('security_code'); 
  90. $this->bank_list = $this->get_option(static::BANK_LIST); 
  91. $this->doplata = $this->get_option(static::DOPLATA); 
  92. $this->status = $this->get_option('status'); 
  93. $this->blik_on = $this->get_option('blik_on'); 
  94. $this->api_key = $this->get_option('api_key'); 
  95. $this->api_pass = $this->get_option('api_pass'); 
  96. // Actions 
  97. add_action('woocommerce_update_options_payment_gateways_' 
  98. . $this->id, array($this, 'process_admin_options')); 
  99.  
  100. //obliczanie koszyka na nowo jesli jest doplata za tpay.com 
  101. if ($this->doplata != 0) { 
  102. add_action('woocommerce_cart_calculate_fees', array($this, 'addFeeTpay'), 99); 
  103. add_action('woocommerce_review_order_after_submit', array($this, 'basketReload')); 
  104. // Payment listener/API hook 
  105. add_action('woocommerce_api_wc_gateway_transferuj', array($this, 'gateway_communication')); 
  106. add_filter('payment_fields', array($this, 'payment_fields')); 
  107.  
  108. include_once 'includes/lib/src/_class_tpay/validate.php'; 
  109. include_once 'includes/lib/src/_class_tpay/util.php'; 
  110. include_once 'includes/lib/src/_class_tpay/exception.php'; 
  111. include_once 'includes/lib/src/_class_tpay/paymentBasic.php'; 
  112. include_once 'includes/lib/src/_class_tpay/curl.php'; 
  113. include_once 'includes/lib/src/_class_tpay/transactionApi.php'; 
  114. include_once 'includes/lib/src/_class_tpay/lang.php'; 
  115.  
  116. /** 
  117. * Initialise Gateway Settings Form Fields 
  118. * @access public 
  119. * @return void 
  120. */ 
  121. public function init_form_fields() 
  122. include_once 'includes/settingsTpay.php'; 
  123. $charge = $this->get_option(static::DOPLATA); 
  124. $list = $this->get_option('bank_list'); 
  125. $tiles = $this->get_option(static::BANK_VIEW); 
  126.  
  127. $settingsTpay = new SettingsTpay(); 
  128. $this->form_fields = $settingsTpay->getSettings($charge, $list, $tiles); 
  129.  
  130. /** 
  131. * Check if this gateway is enabled and available in the user's country. 
  132. * @return bool 
  133. */ 
  134. public function is_valid_for_use() 
  135. return get_woocommerce_currency() === "PLN"; 
  136.  
  137. public function addFeeTpay() 
  138. //dodawanie do zamowienia oplaty za tpay.com 
  139. include 'includes/addFee.php'; 
  140.  
  141. public function basketReload() 
  142. //przeladowanie koszyka zamowieniapowybraniu platnosci tpay.com 
  143. include_once 'includes/_tpl/basketReload.html'; 
  144.  
  145. /** 
  146. * Generates box with gateway name and description, terms acceptance checkbox and channel list 
  147. */ 
  148. public function payment_fields() 
  149.  
  150. if (strcmp(get_locale(), "pl_PL") == 0) { 
  151. tpay\Lang::setLang('pl'); 
  152. } else { 
  153. tpay\Lang::setLang('en'); 
  154. if ($this->get_option(static::DOPLATA) == 1) { 
  155.  
  156. \tpay\Lang::l('fee_info'); 
  157. echo "<b>" . $this->get_option(static::KWOTA_DOPLATY) . " z*</b><br/><br/>"; 
  158.  
  159. if ($this->get_option(static::DOPLATA) == 2) { 
  160. $kwota = WC()->cart->cart_contents_total + WC()->cart->shipping_total; 
  161. $kwota = $kwota * ($this->get_option(static::KWOTA_DOPLATY)) / 100; 
  162. $kwota = doubleval($kwota); 
  163. $kwota = number_format($kwota, 2); 
  164. \tpay\Lang::l('fee_info'); 
  165. echo "<b>" . $kwota . " z*</b><br/><br/>"; 
  166.  
  167. $kwota = WC()->cart->cart_contents_total + WC()->cart->shipping_total; 
  168.  
  169. $data['merchant_id'] = $this->seller_id; 
  170. $data['show_regulations_checkbox'] = true; 
  171. $data['regulation_url'] = 'https://secure.tpay.com/regulamin.pdf'; 
  172. $data['form'] = ''; 
  173. echo '<style>'; 
  174. include_once 'includes/lib/src/common/_css/style.css'; 
  175. echo '</style>'; 
  176. echo $this->description; 
  177. if ($this->blikOn() && $kwota >= 1) { 
  178. $data['static_files_url'] = $this->pluginUrl . '/includes/lib/src/'; 
  179.  
  180. include_once 'includes/lib/src/common/_tpl/blikForm.phtml'; 
  181. $paymentArray = array(1, 2); 
  182. if (in_array($this->paymentType(), $paymentArray)) { 
  183. echo '<script type=text/javascript>'; 
  184. include_once 'includes/lib/src/common/_js/showHide.js'; 
  185. echo '</script>'; 
  186. echo '<input type="hidden" name="regulamin" id="tpay-regulations-input" value="0">'; 
  187. echo '<input type="hidden" id="tpay-payment-submit">'; 
  188.  
  189. if ($this->paymentType() == 1) { 
  190.  
  191. echo '<input type="hidden" name="kanal" id="tpay-channel-input" value=" ">'; 
  192.  
  193. include_once 'includes/lib/src/common/_tpl/bankSelection.phtml'; 
  194.  
  195. } elseif ($this->paymentType() == 2) { 
  196.  
  197. echo '<input type="hidden" name="kanal" id="tpay-channel-input" value=" ">'; 
  198.  
  199. include_once 'includes/lib/src/common/_tpl/bankSelectionList.phtml'; 
  200.  
  201. } else { 
  202. include_once 'includes/_tpl/displayNone.phtml'; 
  203.  
  204. public function blikOn() 
  205. return ($this->blik_on == '1'); 
  206.  
  207. public function paymentType() 
  208.  
  209. if ($this->get_option(static::BANK_LIST) == "0" && $this->get_option(static::BANK_VIEW) == "0") { 
  210. $type = 1; 
  211. } elseif ($this->get_option(static::BANK_LIST) == "0" && $this->get_option(static::BANK_VIEW) == "1") { 
  212. $type = 2; 
  213. } elseif ($this->get_option(static::BANK_LIST) == "1") { 
  214. $type = 3; 
  215. } else { 
  216. $type = 0; 
  217. return $type; 
  218.  
  219. /** 
  220. * Adds tpay.com payment gateway to the list of installed gateways 
  221. * @param $methods 
  222. * @return array 
  223. */ 
  224. public function add_transferuj_gateway($methods) 
  225. $methods[] = static::GATEWAY_NAME; 
  226. return $methods; 
  227.  
  228. /** 
  229. * Generates admin options 
  230. */ 
  231. public function admin_options() 
  232. include_once 'includes/_tpl/settingsAdmin.phtml'; 
  233.  
  234. /** 
  235. * Sends and receives data to/from tpay.com server 
  236. */ 
  237. //woocommerce required function 
  238.  
  239. public function gateway_communication() 
  240. if (filter_input(INPUT_GET, static::ORDER_ID)) { 
  241. $data = $this->collectData(filter_input(INPUT_GET, static::ORDER_ID)); 
  242. $this->sendPaymentData($data); 
  243.  
  244. } else { 
  245. $this->verifyPaymentResponse(); 
  246.  
  247. //exit must be present in this function! 
  248. exit; 
  249.  
  250. public function collectData($orderId) 
  251. // get order data 
  252. $order = new WC_Order($orderId); 
  253. // populate data array to be posted 
  254. $data['kwota'] = $order->get_total(); 
  255. $data['opis'] = trim($this->opis . " Zamwienie nr " . $order->get_order_number()); 
  256. $data['crc'] = $orderId; 
  257. $data['email'] = $order->billing_email; 
  258. $data['nazwisko'] = $order->billing_first_name . ' ' . $order->billing_last_name; 
  259. $data['adres'] = $order->billing_address_1 . ' ' . $order->billing_address_2; 
  260. $data['miasto'] = $order->billing_city; 
  261. $data['kraj'] = $order->billing_country; 
  262. $data['kod'] = $order->billing_postcode; 
  263. $data['telefon'] = str_replace(' ', '', $order->billing_phone); 
  264. $data['pow_url'] = $this->get_return_url($order) . '&utm_nooverride=1'; 
  265. $data['pow_url_blad'] = $order->get_cancel_order_url() . '&utm_nooverride=1'; 
  266. $data['wyn_url'] = $this->notify_link; 
  267. if (filter_input(INPUT_GET, 'regulamin') === "1") { 
  268. $data['akceptuje_regulamin'] = 1; 
  269. if (strcmp(get_locale(), "pl_PL") == 0) { 
  270. $data[static::JEZYK] = "PL"; 
  271. } elseif (strcmp(get_locale(), "de_DE") == 0) { 
  272. $data[static::JEZYK] = "DE"; 
  273. } else { 
  274. $data[static::JEZYK] = "EN"; 
  275.  
  276. if (filter_input(INPUT_GET, static::KANAL)) { 
  277. $data[static::KANAL] = (int)filter_input(INPUT_GET, static::KANAL); 
  278. return $data; 
  279.  
  280. public function sendPaymentData($data) 
  281. if (filter_input(INPUT_GET, static::BLIKCODE)) { 
  282. $kodblik = filter_input(INPUT_GET, static::BLIKCODE); 
  283. $data2 = $data; 
  284. $data2[static::KANAL] = 64; 
  285. $data2['akceptuje_regulamin'] = 1; 
  286. $transactionAPI = new tpay\TransactionAPI( 
  287. (string)$this->api_key,  
  288. (string)$this->api_pass,  
  289. (int)$this->seller_id,  
  290. (string)$this->security_code 
  291. ); 
  292. try { 
  293.  
  294. $resp = $transactionAPI->create($data2); 
  295. } catch (tpay\TException $exception) { 
  296. echo static::CAUGHT_EXCEPTION, $exception->getMessage(), "\n"; 
  297. return false; 
  298. $title = (string)$resp['title']; 
  299.  
  300. try { 
  301. $resp = $transactionAPI->blik($kodblik, $title); 
  302. } catch (tpay\TException $exception) { 
  303. echo static::CAUGHT_EXCEPTION, $exception->getMessage(), "\n"; 
  304. return false; 
  305.  
  306. if ($resp['result'] === 1) { 
  307. $powUrl = $data['pow_url']; 
  308. wp_redirect(esc_url($powUrl)); 
  309. return true; 
  310. } else { 
  311. $powUrl = $data['pow_url_blad']; 
  312. wp_redirect(esc_url($powUrl)); 
  313. return false; 
  314. } else { 
  315.  
  316. try { 
  317. $paymentBasic = new tpay\PaymentBasic( 
  318. (int)$this->seller_id,  
  319. (string)$this->security_code 
  320. ); 
  321. $form = $paymentBasic->getTransactionForm($data); 
  322. } catch (tpay\TException $exception) { 
  323. echo static::CAUGHT_EXCEPTION, $exception->getMessage(), "\n"; 
  324. return false; 
  325. echo $form; 
  326.  
  327. return true; 
  328.  
  329. /** 
  330. * Verifies that no errors have occured during transaction 
  331. */ 
  332. public function verifyPaymentResponse() 
  333. try { 
  334. $paymentBasic = new tpay\PaymentBasic( 
  335. (int)$this->seller_id,  
  336. (string)$this->security_code 
  337. ); 
  338. $res = $paymentBasic->checkPayment(); 
  339. } catch (tpay\TException $exception) { 
  340. echo static::CAUGHT_EXCEPTION, $exception->getMessage(), "\n"; 
  341. return; 
  342.  
  343. if (($res['tr_status'] == 'TRUE')) { 
  344. if ($res[static::TR_ERROR] == 'none') { 
  345. // transaction successful 
  346. $this->completePayment($res[static::TR_CRC], static::SUCCESS, false); 
  347. } elseif ($res[static::TR_ERROR] == 'overpay') { 
  348. // payment was bigger than required 
  349. $this->completePayment($res[static::TR_CRC], static::SUCCESS, true); 
  350. } else { 
  351. // transaction failed 
  352. $this->completePayment($res[static::TR_CRC], static::FAILURE, false); 
  353. } else { 
  354. // transaction failed 
  355. $this->completePayment($res[static::TR_CRC], static::FAILURE, false); 
  356.  
  357.  
  358. /** 
  359. * Sets proper transaction status for order based on $status 
  360. * @param $orderId ; id of an order 
  361. * @param $status ; status of a transaction, enum : {success, failure} 
  362. * @param $overpay ; whether there was an overpay during payment 
  363. * @throws ; 
  364. */ 
  365. public function completePayment($orderId, $status, $overpay) 
  366. $order = new WC_Order($orderId); 
  367. if ($this->get_option('status') == 0) { 
  368. $orderStatus = 'processing'; 
  369. } else { 
  370. $orderStatus = 'completed'; 
  371.  
  372. if ($status == static::SUCCESS) { 
  373. if ($overpay) { 
  374. $order->update_status($orderStatus, __('Zap*acono z nadp*at*.')); 
  375. } else { 
  376.  
  377. $order->update_status($orderStatus, __('Zap*acono')); 
  378. } elseif ($status == static::FAILURE) { 
  379. $order->update_status('failed', __('Zap*ata nie powiod*a si*.')); 
  380. } else { 
  381. throw new exception ('Invalid payment type for tpay.com gateway'); 
  382.  
  383.  
  384. public function process_payment($orderId) 
  385. global $woocommerce; 
  386. $order = new WC_Order($orderId); 
  387. // Mark as on-hold (we will be awaiting the tpay.com payment) 
  388. $order->update_status('on-hold', __('Oczekiwanie na p*atno** tpay.com', static::WOOCOMMERCE)); 
  389. // Reduce stock levels 
  390. $order->reduce_order_stock(); 
  391. // Clear cart 
  392. $woocommerce->cart->empty_cart(); 
  393. // Post data and redirect to tpay.com 
  394. if (!filter_input(INPUT_POST, static::BLIKCODE) || (filter_input(INPUT_POST, static::BLIKCODE) === null)) { 
  395. return array( 
  396. static::RESULT => static::SUCCESS,  
  397. static::REDIRECT => add_query_arg(array( 
  398. static::REGULATIONS => filter_input(INPUT_POST, static::REGULATIONS),  
  399. static::ORDER_ID => $orderId,  
  400. static::BLIKCODE => filter_input(INPUT_POST, static::BLIKCODE),  
  401. static::KANAL => filter_input(INPUT_POST, static::KANAL),  
  402. ), $this->notify_link) 
  403. ); 
  404. } else { 
  405. return array( 
  406. static::RESULT => static::SUCCESS,  
  407. static::REDIRECT => add_query_arg(array( 
  408. static::REGULATIONS => filter_input(INPUT_POST, static::REGULATIONS),  
  409. static::ORDER_ID => $orderId,  
  410. static::BLIKCODE => filter_input(INPUT_POST, static::BLIKCODE),  
  411. ), $this->notify_link) 
  412. ); 
  413.  
  414. new WC_Gateway_Transferuj(); 
  415. include_once 'includes/tpayCards.php';