WC_Gateway_Tpay_Cards

Tpay.com Woocommerce payment module.

Defined (1)

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

/includes/tpayCards.php  
  1. class WC_Gateway_Tpay_Cards extends WC_Payment_Gateway 
  2. const REGULATIONS = 'regulations'; 
  3. const BLIKCODE = 'blikcode'; 
  4. const ORDER_ID = 'orderId'; 
  5. const KANAL = 'kanal'; 
  6. const RESULT = self::RESULT1; 
  7. const REDIRECT = 'redirect'; 
  8. const SUCCESS = 'success'; 
  9. const CAUGHT_EXCEPTION = 'Caught exception: '; 
  10. const TR_CRC = 'tr_crc'; 
  11. const TR_ERROR = 'tr_error'; 
  12. const KWOTA_DOPLATY = 'kwota_doplaty'; 
  13. const BANK_LIST = 'bank_list'; 
  14. const DOPLATA = 'doplata'; 
  15. const GATEWAY_NAME = 'WC_Gateway_Tpay_Cards'; 
  16. const WOOCOMMERCE = 'woocommerce'; 
  17. //MUST BE OLD NAME! 
  18. const GATEWAY_ID = 'tpaycards'; 
  19. const JEZYK = 'jezyk'; 
  20. const FAILURE = 'failure'; 
  21. const HTTP = 'http://'; 
  22. const HTTPS = 'https://'; 
  23. const WC_API = 'wc-api'; 
  24. const RESULT_0_1_RESULT = '/<result>([0-1]*)<\/result>/'; 
  25.  
  26. const CARDDATA = 'carddata'; 
  27.  
  28. const ORDERID = 'orderid'; 
  29.  
  30. const CLIENTNAME = 'client_name'; 
  31.  
  32. const CLIENTEMAIL = 'client_email'; 
  33.  
  34. const HTTP_X_FORWARDED_PROTO = 'HTTP_X_FORWARDED_PROTO'; 
  35.  
  36. const RESULT1 = 'result'; 
  37. const CURRENCY = 'currency'; 
  38. const TPAY_ID = 'tpayID'; 
  39. const ORDER_ID1 = 'order_id'; 
  40. public $midId = 11; 
  41. public $siteDomain; 
  42. private $pluginUrl; 
  43.  
  44. public function __construct() 
  45. $this->id = __(static::GATEWAY_ID, static::WOOCOMMERCE); 
  46.  
  47. $this->has_fields = true; 
  48.  
  49. $this->method_title = __('tpay.com credit cards', static::WOOCOMMERCE); 
  50. if ((isset($_SERVER[static::HTTP_X_FORWARDED_PROTO]) && $_SERVER[static::HTTP_X_FORWARDED_PROTO] === 'https') 
  51. || ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')) 
  52. ) { 
  53. $this->pluginUrl = str_replace(static::HTTP, static::HTTPS, plugins_url('', __FILE__)); 
  54. $this->siteDomain = preg_replace('/\?.*/', '', str_replace(static::HTTP, static::HTTPS, home_url('/'))); 
  55. } else { 
  56. $this->pluginUrl = plugins_url('', __FILE__); 
  57. $this->siteDomain = preg_replace('/\?.*/', '', str_replace(static::HTTPS, static::HTTP, home_url('/'))); 
  58. $this->notify_link = add_query_arg('wc-api', static::GATEWAY_NAME, $this->siteDomain); 
  59. $this->icon = apply_filters('woocommerce_transferuj_icon',  
  60. $this->pluginUrl . '/../images/tpayLogo.png'); 
  61. add_action('woocommerce_update_options_payment_gateways_' 
  62. . $this->id, array($this, 'process_admin_options')); 
  63. add_action('woocommerce_api_wc_gateway_tpay_cards', array($this, 'gateway_communication')); 
  64.  
  65. add_filter('payment_fields', array($this, 'payment_fields')); 
  66. add_filter('form', array($this, 'form')); 
  67. add_filter('woocommerce_payment_gateways', array($this, 'add_transferuj_gateway')); 
  68.  
  69. $this->init_form_fields(); 
  70. $this->init_settings(); 
  71. if (!$this->is_valid_for_use()) { 
  72. $this->enabled = 'no'; 
  73. // Define user set variables 
  74. $this->title = $this->get_option('title'); 
  75. $this->debugMode = $this->get_option('debugMode'); 
  76. $this->domain = $this->get_option('midDomain' . $this->midId); 
  77. $this->opis = $this->get_option('opis' . $this->midId); 
  78. $this->doplata = $this->get_option(static::DOPLATA . $this->midId); 
  79. $this->kwota_doplaty = $this->get_option(static::KWOTA_DOPLATY . $this->midId); 
  80. $this->description = $this->get_option('description' . $this->midId); 
  81. $this->seller_id = $this->get_option('seller_id' . $this->midId); 
  82. $this->security_code = $this->get_option('security_code' . $this->midId); 
  83. $this->cardApiKey = $this->get_option('cardApiKey' . $this->midId); 
  84. $this->cardApiPassword = $this->get_option('cardApiPassword' . $this->midId); 
  85. $this->verificationCode = $this->get_option('verificationCode' . $this->midId); 
  86. $this->hashAlg = $this->get_option('hashAlg' . $this->midId); 
  87. $this->keyRSA = $this->get_option('keyRSA' . $this->midId); 
  88. $this->midType = $this->get_option('midType' . $this->midId); 
  89. $this->midCurrency = $this->get_option('midCurrency' . $this->midId); 
  90. $this->midOn = $this->get_option('midOn' . $this->midId); 
  91. //obliczanie koszyka na nowo jesli jest doplata za tpay.com 
  92. if ((int)$this->doplata !== 0) { 
  93. add_action('woocommerce_cart_calculate_fees', array($this, 'addFeeTpay'), 99); 
  94. add_action('woocommerce_review_order_after_submit', array($this, 'basketReload')); 
  95. $path = dirname(__FILE__); 
  96. include_once $path . '/lib/src/_class_tpay/lang.php'; 
  97. include_once $path . '/lib/src/_class_tpay/cardApi.php'; 
  98. include_once $path . '/lib/src/_class_tpay/paymentCard.php'; 
  99. include_once $path . '/lib/src/_class_tpay/util.php'; 
  100. include_once $path . '/lib/src/_class_tpay/exception.php'; 
  101. include_once $path . '/lib/src/_class_tpay/validate.php'; 
  102.  
  103.  
  104. public function init_form_fields() 
  105. include_once 'settingsTpayCards.php'; 
  106. $settingsTpay = new SettingsTpayCards(); 
  107. $this->form_fields = $settingsTpay->getSettings(); 
  108.  
  109. public function is_valid_for_use() 
  110. if ((int)filter_input(INPUT_GET, static::TPAY_ID)) { 
  111. $this->midId = (int)filter_input(INPUT_GET, static::TPAY_ID); 
  112. return true; 
  113. } elseif (filter_input(INPUT_POST, static::ORDER_ID1)) { 
  114. $id = explode('|', filter_input(INPUT_POST, static::ORDER_ID1)); 
  115. $this->midId = $id[1]; 
  116.  
  117. } else { 
  118. $counter = 10; 
  119. $validMidId = array(); 
  120. $saleCurrency = get_woocommerce_currency(); 
  121. $midForCurrency = ''; 
  122. $midPLN = ''; 
  123. for ($i = 1; $i <= $counter; $i++) { 
  124. if ($this->get_option('midDomain' . $i) === $this->siteDomain) { 
  125. $validMidId[] = $i; 
  126. for ($i = 0; $i < count($validMidId); $i++) { 
  127.  
  128. $midCurrency = explode(', ', $this->get_option('midCurrency' . $validMidId[$i])); 
  129. $midType = $this->get_option('midType' . $validMidId[$i]); 
  130. $midOn = $this->get_option('midOn' . $validMidId[$i]); 
  131.  
  132. if ((int)$midType === 0 && $saleCurrency === 'PLN' && $midOn !== 'no') { 
  133. $this->midId = $validMidId[$i]; 
  134. $midPLN = $validMidId[$i]; 
  135. break; 
  136. foreach ($midCurrency as $key => $value) { 
  137. if ((strcasecmp($midCurrency[$key], $saleCurrency) === 0 
  138. || strcasecmp($midCurrency[$key], filter_input(INPUT_POST, static::CURRENCY)) === 0) 
  139. && $midOn !== 'no' && (int)$midType === 1 
  140. ) { 
  141. $this->midId = $validMidId[$i]; 
  142. $midForCurrency = $validMidId[$i]; 
  143.  
  144. } elseif ($midCurrency[$key] === '' && $midOn !== 'no') { 
  145. $this->midId = $validMidId[$i]; 
  146. if ($this->midId === 11) { 
  147. return false; 
  148. } elseif (!empty($midForCurrency) && empty($midPLN)) { 
  149. $this->midId = $midForCurrency; 
  150.  
  151. try { 
  152. \tpay\Validate::validateCardCurrency($saleCurrency); 
  153. return true; 
  154. } catch (tpay\TException $exception) { 
  155. return false; 
  156.  
  157. public function admin_options() 
  158. include_once '_tpl/settingsAdminCards.phtml'; 
  159.  
  160. public function basketReload() 
  161. //przeladowanie koszyka zamowienia po wybraniu platnosci tpay.com 
  162. include_once '_tpl/basketReload.html'; 
  163.  
  164. public function addFeeTpay() 
  165. //dodawanie do zamowienia oplaty za tpay.com 
  166. include 'addFee.php'; 
  167.  
  168. public function gateway_communication() 
  169. if (filter_input(INPUT_GET, static::ORDER_ID) && (filter_input(INPUT_GET, static::CARDDATA))) { 
  170.  
  171. $basicClass = new WC_Gateway_Transferuj(); 
  172. $transactionData = $basicClass->collectData(filter_input(INPUT_GET, static::ORDER_ID)); 
  173. $orderId = filter_input(INPUT_GET, static::ORDER_ID); 
  174. $transactionData[static::ORDERID] = $orderId . '|' . $this->midId; 
  175. $transactionData[static::CARDDATA] = str_replace(' ', '+', filter_input(INPUT_GET, static::CARDDATA)); 
  176.  
  177. if (filter_input(INPUT_GET, static::CLIENTNAME)) { 
  178. $transactionData['nazwisko'] = filter_input(INPUT_GET, static::CLIENTNAME); 
  179. if (filter_input(INPUT_GET, static::CLIENTEMAIL)) { 
  180. $transactionData['email'] = filter_input(INPUT_GET, static::CLIENTEMAIL); 
  181. $order = new WC_Order($orderId); 
  182. $transactionData[static::CURRENCY] = $order->get_order_currency(); 
  183. $transactionData['opis'] = $this->get_option('opis' . $this->midId) 
  184. . " Zamwienie nr " . $order->get_order_number(); 
  185. $response = $this->processCardSale($transactionData); 
  186. if (isset($response[static::RESULT1]) && (int)$response[static::RESULT1] === 1) { 
  187. $paymentCard = new tpay\PaymentCard( 
  188. (int)$this->seller_id, (string)$this->security_code, (string)$this->cardApiKey,  
  189. (string)$this->cardApiPassword, (string)$this->verificationCode,  
  190. (string)$this->hashAlg, (string)$this->keyRSA 
  191. ); 
  192. $paymentCard->validateSign($response['sign'], $response['sale_auth'], $response['card'],  
  193. $transactionData['kwota'], $response['date'], 'correct',  
  194. \tpay\Validate::validateCardCurrency($transactionData[static::CURRENCY]),  
  195. isset($response['test_mode']) ? '1' : '', '', ''); 
  196. $basicClass->completePayment($orderId, static::SUCCESS, false); 
  197.  
  198. $powUrl = $transactionData['pow_url']; 
  199. wp_redirect(esc_url($powUrl)); 
  200.  
  201. } elseif (isset($response['3ds_url'])) { 
  202. wp_redirect($response['3ds_url']); 
  203. } else { 
  204. $basicClass->completePayment($orderId, static::FAILURE, false); 
  205. $powUrl = $transactionData['pow_url_blad']; 
  206. if ($this->debugMode !== 'no') { 
  207. var_dump($response); 
  208. } else { 
  209. wp_redirect(esc_url($powUrl)); 
  210.  
  211. } elseif (filter_input(INPUT_GET, 'type') === 'sale') { 
  212.  
  213. $this->verifyPaymentResponse(); 
  214. } else { 
  215. echo 'INVALID DATA'; 
  216.  
  217. //exit must be present in this function! 
  218. exit; 
  219.  
  220. public function processCardSale($transactionData) 
  221. if (strcmp(get_locale(), "pl_PL") == 0) { 
  222. tpay\Lang::setLang('pl'); 
  223. } else { 
  224. tpay\Lang::setLang('en'); 
  225. if ($this->debugMode !== 'no') { 
  226. var_dump($transactionData); 
  227. $paymentCard = new tpay\PaymentCard( 
  228. (int)$this->seller_id, (string)$this->security_code, (string)$this->cardApiKey,  
  229. (string)$this->cardApiPassword, (string)$this->verificationCode,  
  230. (string)$this->hashAlg, (string)$this->keyRSA 
  231. ); 
  232. $_POST[static::CARDDATA] = $transactionData[static::CARDDATA]; 
  233. $_POST['client_name'] = $transactionData['nazwisko']; 
  234. $_POST['client_email'] = $transactionData['email']; 
  235. $_POST['card_save'] = 'no'; 
  236.  
  237. return $paymentCard->secureSale( 
  238. $transactionData['kwota'],  
  239. $transactionData[static::ORDERID],  
  240. $transactionData['opis'],  
  241. $transactionData[static::CURRENCY],  
  242. true,  
  243. get_locale(),  
  244. $transactionData['pow_url'],  
  245. $transactionData['pow_url_blad'] 
  246. ); 
  247.  
  248. public function verifyPaymentResponse() 
  249. $paymentCard = new tpay\PaymentCard( 
  250. (int)$this->seller_id, (string)$this->security_code, (string)$this->cardApiKey,  
  251. (string)$this->cardApiPassword, (string)$this->verificationCode,  
  252. (string)$this->hashAlg, (string)$this->keyRSA 
  253. ); 
  254. $resp = $paymentCard->handleNotification(); 
  255. if (isset($resp[static::ORDER_ID1])) { 
  256. $basicClass = new WC_Gateway_Transferuj(); 
  257. $orderId = explode('|', $resp[static::ORDER_ID1]); 
  258. $order = new WC_Order($orderId[0]); 
  259. $orderCurrency = $order->get_order_currency(); 
  260. $orderCurrency = tpay\Validate::validateCardCurrency($orderCurrency); 
  261.  
  262. $paymentCard->validateSign($resp['sign'],  
  263. $resp['sale_auth'], $resp['card'], (float)$order->get_total(),  
  264. $resp['date'], 'correct', $orderCurrency, isset($resp['test_mode']) ? '1' : '', $resp['order_id']); 
  265. $basicClass->completePayment($orderId[0], static::SUCCESS, false); 
  266.  
  267. public function payment_fields() 
  268. $fee = (int)$this->doplata; 
  269. $feeAmount = (float)$this->kwota_doplaty; 
  270. $currency = get_woocommerce_currency(); 
  271.  
  272. switch ($fee) { 
  273. case 1: 
  274. \tpay\Lang::l('fee_info'); 
  275. echo " <b> " . $feeAmount . $currency . " </b><br/><br/>"; 
  276. break; 
  277. case 2: 
  278. $kwota = WC()->cart->cart_contents_total + WC()->cart->shipping_total; 
  279. $kwota = $kwota * $feeAmount / 100; 
  280. $kwota = doubleval($kwota); 
  281. $kwota = number_format($kwota, 2); 
  282. \tpay\Lang::l('fee_info'); 
  283. echo "<b>" . $kwota . $currency . "</b><br/><br/>"; 
  284. break; 
  285. default: 
  286. break; 
  287.  
  288.  
  289. include_once "_tpl/cardForm.phtml"; 
  290.  
  291. public function add_transferuj_gateway($methods) 
  292. $methods[] = static::GATEWAY_NAME; 
  293. return $methods; 
  294.  
  295. public function process_payment($orderId) 
  296. global $woocommerce; 
  297. $this->order = new WC_Order($orderId); 
  298. // Mark as on-hold (we will be awaiting the tpay.com payment) 
  299. $this->order->update_status('on-hold', __('Oczekiwanie na p*atno** kart* tpay.com', static::WOOCOMMERCE)); 
  300. // Reduce stock levels 
  301. $this->order->reduce_order_stock(); 
  302. // Clear cart 
  303. $woocommerce->cart->empty_cart(); 
  304. return array( 
  305. static::RESULT => static::SUCCESS,  
  306. static::REDIRECT => add_query_arg(array( 
  307. static::CARDDATA => filter_input(INPUT_POST, static::CARDDATA),  
  308. static::TPAY_ID => filter_input(INPUT_POST, static::TPAY_ID),  
  309. static::CLIENTNAME => filter_input(INPUT_POST, static::CLIENTNAME),  
  310. static::CLIENTEMAIL => filter_input(INPUT_POST, static::CLIENTEMAIL),  
  311. static::ORDER_ID => $orderId,  
  312. ), $this->notify_link) 
  313. ); 
  314.