/sisow/base.php

  1. <?php 
  2. /** 
  3. error_reporting(E_ALL); 
  4. ini_set("display_errors", 1); 
  5. */ 
  6. class SisowBase extends WC_Payment_Gateway { 
  7.  
  8. function __construct() { 
  9. global $woocommerce; 
  10.  
  11. $this->id = 'sisow' . $this->paymentcode; 
  12. $this->method_title = __($this->paymentname, 'sisow'); 
  13. $this->has_fields = true; 
  14.  
  15. // Load the form fields 
  16. $this->init_form_fields(); 
  17.  
  18. // Load the settings. 
  19. $this->init_settings(); 
  20.  
  21. // Get setting values 
  22. $this->title = $this->settings['title']; 
  23. $this->merchantId = $this->settings['merchantid']; 
  24. $this->merchantKey = $this->settings['merchantkey']; 
  25. $this->shopId = (isset($this->settings['shopid'])) ? $this->settings['shopid'] : ''; 
  26. $this->klarnaId = (isset($this->settings['klarnaid'])) ? $this->settings['klarnaid'] : ''; 
  27. $this->omschrijving = $this->settings['omschrijving']; 
  28. $this->testmode = $this->settings['testmode']; 
  29. $this->displaylogo = (isset($this->settings['displaylogo'])) ? $this->settings['displaylogo'] : "yes"; 
  30. $this->description = (isset($this->settings['description'])) ? $this->settings['description'] : "yes"; 
  31.  
  32. if(isset($this->settings['completed'])) 
  33. $this->_completed = $this->settings['completed'] == "yes" ? true : false; 
  34.  
  35. if(isset($this->settings['paymentfee']) && $this->settings['paymentfee'] != '') 
  36. $this->paymentfee = $this->settings['paymentfee']; 
  37. $this->paymentfeetax = $this->settings['paymentfeetax']; 
  38. $this->paymentfeelabel =(isset($this->settings['paymentfeelabel']) && $this->settings['paymentfeelabel'] != '') ? $this->settings['paymentfeelabel'] : 'Payment Fee'; 
  39. else 
  40. $this->paymentfee = 0; 
  41. $this->paymentfeetax = 0; 
  42. $this->paymentfeelabel = ''; 
  43.  
  44. $this->notify_url = add_query_arg( array('wc-api' => 'WC_sisow_' . $this->paymentcode, 'utm_nooverride' => '1'), home_url('/')); 
  45.  
  46. if ($this->paymentcode == 'overboeking' || $this->paymentcode == 'ebill') { 
  47. $this->includelink = $this->settings['includelink']; 
  48. $this->days = $this->settings['days']; 
  49. } elseif ($this->paymentcode == 'klarna' || $this->paymentcode == 'klarnaacc') { 
  50. $this->klarnaid = $this->settings['klarnaid']; 
  51.  
  52. if (!isset($woocommerce->version) || $woocommerce->version < 2) { 
  53. add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options')); 
  54. } else { 
  55. add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); 
  56.  
  57. //notify actie 
  58. add_action('woocommerce_api_wc_sisow_' . $this->paymentcode, array($this, 'check_notify')); 
  59.  
  60. //payment fee actions  
  61. add_action('wp_enqueue_scripts', array(&$this, 'add_script')); 
  62.  
  63. public function add_script() { 
  64. wp_enqueue_script('sisow', plugins_url('checkout.js', __FILE__), array('jquery')); 
  65.  
  66. function init_form_fields() { 
  67. $velden = array(); 
  68.  
  69. $velden['enabled'] = array( 
  70. 'title' => __('Enable/Disable', 'sisow'),  
  71. 'type' => 'checkbox',  
  72. 'label' => __('Enable ' . $this->paymentname, 'sisow'),  
  73. 'default' => 'yes' 
  74. ); 
  75.  
  76. $velden['title'] = array( 
  77. 'title' => __('Title', 'sisow'),  
  78. 'type' => 'text',  
  79. 'description' => __('This controls the title which the user sees during checkout.', 'sisow'),  
  80. 'default' => __($this->paymentname, 'sisow') 
  81. ); 
  82.  
  83. $velden['description'] = array( 
  84. 'title' => __('Description', 'sisow'),  
  85. 'type' => 'text',  
  86. 'description' => __('The description which the user sees during checkout.', 'sisow'),  
  87. 'default' => __($this->paymentname, 'sisow') 
  88. );  
  89.  
  90. $velden['displaylogo'] = array( 
  91. 'title' => __('Display logo', 'sisow'),  
  92. 'type' => 'checkbox',  
  93. 'label' => __('Display logo on checkout page', 'sisow'),  
  94. 'default' => 'yes' 
  95. ); 
  96.  
  97. $velden['merchantid'] = array( 
  98. 'title' => __('Sisow Merchant ID', 'sisow'),  
  99. 'type' => 'text',  
  100. 'description' => __('This is your Merchant ID which you can find in your Sisow profile on <a href="http://www.sisow.nl/">www.sisow.nl</a>.', 'sisow'),  
  101. 'default' => __("", 'sisow') 
  102. ); 
  103. $velden['merchantkey'] = array( 
  104. 'title' => __('Sisow Merchant Key', 'sisow'),  
  105. 'type' => 'text',  
  106. 'description' => __('This is your Merchant Key which you can find in your Sisow profile on <a href="http://www.sisow.nl/">www.sisow.nl</a>.', 'sisow'),  
  107. 'default' => __("", 'sisow') 
  108. ); 
  109.  
  110. $velden['shopid'] = array( 
  111. 'title' => __('Sisow ShopId', 'sisow'),  
  112. 'type' => 'text',  
  113. 'description' => __('This is your Shop Id which you can find in your Sisow profile on <a href="http://www.sisow.nl/">www.sisow.nl</a>.', 'sisow'),  
  114. 'default' => __("", 'sisow') 
  115. ); 
  116.  
  117. if ($this->paymentcode == 'klarna' || $this->paymentcode == 'klarnaacc') { 
  118. $velden['klarnaid'] = array( 
  119. 'title' => __('Klarna ID', 'sisow'),  
  120. 'type' => 'text',  
  121. 'description' => __('This is your Klarna ID, you get this ID from Klarna.', 'sisow'),  
  122. 'default' => __("", 'sisow') 
  123. ); 
  124.  
  125. $velden['omschrijving'] = array( 
  126. 'title' => __('Description', 'sisow'),  
  127. 'type' => 'text',  
  128. 'description' => __("This is the description your customer will see on his bank statement ", 'sisow'),  
  129. 'default' => __("", 'sisow') 
  130. ); 
  131.  
  132. if($this->paymentcode != 'focum') 
  133. $velden['testmode'] = array( 
  134. 'title' => __('Testmode', 'sisow'),  
  135. 'type' => 'checkbox',  
  136. 'label' => __('Enable the testmode to test your connection', 'sisow'),  
  137. 'description' => __('Test the connection between Sisow and your Webshop.', 'sisow'),  
  138. 'default' => 'yes' 
  139. ); 
  140.  
  141. if ($this->paymentcode == 'ebill' || $this->paymentcode == 'overboeking') { 
  142. $velden['days'] = array( 
  143. 'title' => __('Days', 'sisow'),  
  144. 'type' => 'text',  
  145. 'description' => __("Number of days payment is valid.", 'sisow'),  
  146. 'default' => __("", 'sisow') 
  147. ); 
  148.  
  149. $velden['includelink'] = array( 
  150. 'title' => __('Include bank info', 'sisow'),  
  151. 'type' => 'checkbox',  
  152. 'label' => ($this->paymentcode == 'ebill') ? __('Add Sisow bank account information, the customer can also pay through a bank transfer.', 'sisow') : __('Add an iDEAL link in the mail, the customer can also pay with iDEAL.', 'sisow'),  
  153. 'default' => 'yes' 
  154. ); 
  155.  
  156. $velden['completed'] = array( 
  157. 'title' => __('Completed', 'sisow'),  
  158. 'type' => 'checkbox',  
  159. 'label' => __('Orderstatus Completed', 'sisow'),  
  160. 'description' => __('If enabled set the order to completed, disabled the order will be processing.', 'sisow'),  
  161. 'default' => 'no' 
  162. ); 
  163.  
  164. if($this->paymentcode != 'klarnaacc') 
  165. $velden['paymentfeelabel'] = array( 
  166. 'title' => __('Payment fee label:', 'sisow'),  
  167. 'type' => 'text',  
  168. 'description' => __('Set the order total text for the payment fee', 'sisow'),  
  169. 'default' => __("", 'sisow') 
  170. ); 
  171.  
  172. $desc = ($this->paymentcode != 'klarna') ? 'Set the payment fee amount (negative amount is %)' : 'Set the payment fee amount.'; 
  173. $velden['paymentfee'] = array( 
  174. 'title' => __('Payment fee:', 'sisow'),  
  175. 'type' => 'text',  
  176. 'description' => __($desc, 'sisow'),  
  177. 'default' => __("", 'sisow') 
  178. ); 
  179.  
  180. $classes = array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes')))); 
  181. $classes_options = array(); 
  182. $classes_options[''] = __('Standard', 'sisow'); 
  183. if ($classes) { 
  184. foreach ($classes as $class) : 
  185. $classes_options[sanitize_title($class)] = $class; 
  186. endforeach; 
  187.  
  188. $velden['paymentfeetax'] = array( 
  189. 'title' => __('Payment fee:', 'sisow'),  
  190. 'type' => 'select',  
  191. 'options' => $classes_options,  
  192. 'description' => __('Tax class for the payment fee.', 'sisow'),  
  193. 'default' => __("", 'sisow') 
  194. ); 
  195.  
  196.  
  197. $this->form_fields = $velden; 
  198.  
  199. // End init_form_fields() 
  200.  
  201. public function getFee() { 
  202. global $woocommerce; 
  203.  
  204. $paymentfee_subtotal = $this->calculate_fee_for($this->settings, $woocommerce->cart->subtotal + $woocommerce->cart->shipping_total); 
  205. $paymentfee_total = $paymentfee_subtotal + $this->calculate_tax($this->settings, $paymentfee_subtotal); 
  206. $this->paymentfeelabel = (isset($this->paymentfeelabel)) ? $this->paymentfeelabel : 'Payment Fee'; 
  207.  
  208. return $paymentfee_total; 
  209.  
  210. public function process_payment($order_id) { 
  211. global $woocommerce; 
  212.  
  213. $order = new WC_Order($order_id); 
  214.  
  215. $order_number = ltrim($order->get_order_number(), '#'); 
  216.  
  217. $sisow = new Sisow($this->settings['merchantid'], $this->settings['merchantkey'], $this->settings['shopid']); 
  218. $sisow->purchaseId = $order_number; 
  219. $sisow->description = (isset($this->settings['omschrijving']) && $this->settings['omschrijving'] != '') ? rtrim($this->settings['omschrijving']) . ' ' . $order_number : get_bloginfo('name') . ' ' . $order_number; 
  220. $sisow->amount = $order->order_total; 
  221. $sisow->payment = $this->paymentcode; 
  222. $sisow->entranceCode = $order_id; 
  223. $sisow->returnUrl = $this->notify_url; //$this->get_return_url($order); 
  224. $sisow->cancelUrl = $order->get_cancel_order_url(); 
  225. $sisow->notifyUrl = $this->notify_url; 
  226. $sisow->callbackUrl = $this->notify_url; 
  227.  
  228. if ($this->paymentcode == 'ideal') { 
  229. $sisow->issuerId = $this->issuerid; 
  230.  
  231. if (($ex = $sisow->TransactionRequest($this->prep($order))) < 0) { 
  232. if (($this->paymentcode == 'klarna' || $this->paymentcode == 'klarnaacc') && $sisow->errorMessage != '') { 
  233. $error = $sisow->errorMessage; 
  234. }  
  235. else if($this->paymentcode == 'focum') 
  236. if($sisow->errorCode == 'IBAN') 
  237. $error = 'Het opgegeven IBAN is onjuist'; 
  238. else 
  239. $error = "Betalen met Achteraf Betalen is op dit moment niet mogelijk, betaal anders."; 
  240. else { 
  241. if($sisow->errorCode == 'TA3410') 
  242. $error = 'Testen op uw Sisow account is niet toegestaan.<br> 
  243. Log in op www.sisow.nl en kies voor "Mijn Profiel" tabblad "Geavanceerd" en schakel de optie "testen met behulp van simulator" in.'; 
  244. else if($sisow->errorCode == 'IBAN') 
  245. $error = 'Het opgegeven IBAN is onjuist'; 
  246. else if($ex == -4) 
  247. $error = 'Bedrag lager dan 45 cent.'; 
  248. else 
  249. $error = __('Betalen met ' . $this->paymentname . ' is nu niet mogelijk (' . $ex . ';' . $sisow->errorCode . '). Kies een andere betaalmethode.', 'sisow'); 
  250.  
  251. wc_add_notice( $error, 'error' ); 
  252. } else { 
  253. update_post_meta($order->id, '_trxid', $sisow->trxId); 
  254.  
  255. if ($this->redirect === false && $sisow->pendingKlarna) { 
  256.  
  257. $order->update_status('on-hold', __($this->paymentname . ' waiting for Klarna', 'sisow')); 
  258. //$woocommerce->add_error(__('Voor uw betaling met Klarna is een extra controle nodig. U ontvangt binnen 24 uur bericht.', 'woothemes')); 
  259.  
  260. return array( 
  261. 'result' => 'success',  
  262. 'redirect' => $this->get_return_url($order)); 
  263. } else if ($this->redirect === false) { 
  264. $this->orderid = $order_id; 
  265. $this->trxid = $sisow->trxId; 
  266. $this->sisow = $sisow; 
  267. return $this->notify(); 
  268. } else { 
  269. return array( 
  270. 'result' => 'success',  
  271. 'redirect' => $sisow->issuerUrl); 
  272.  
  273. private function prep($order) { 
  274. $arg = array(); 
  275. $arg['ipaddress'] = filter_input(INPUT_SERVER, 'REMOTE_ADDR');//$_SERVER['REMOTE_ADDR']; 
  276. $arg['shipping_firstname'] = $order->shipping_first_name; 
  277. $arg['shipping_lastname'] = $order->shipping_last_name; 
  278. //$arg['shipping_mail'] = $order->; 
  279. $arg['shipping_company'] = $order->shipping_company; 
  280. $arg['shipping_address1'] = $order->shipping_address_1; 
  281. $arg['shipping_address2'] = $order->shipping_address_2; 
  282. $arg['shipping_zip'] = $order->shipping_postcode; 
  283. $arg['shipping_city'] = $order->shipping_city; 
  284. //$arg['shipping_country'] = $order->; 
  285. $arg['shipping_countrycode'] = $order->shipping_country; 
  286. $arg['shipping_phone'] = $order->billing_phone; 
  287. $arg['billing_firstname'] = $order->billing_first_name; 
  288. $arg['billing_lastname'] = $order->billing_last_name; 
  289. $arg['billing_mail'] = $order->billing_email; 
  290. $arg['billing_company'] = $order->billing_company; 
  291. $arg['billing_address1'] = $order->billing_address_1; 
  292. $arg['billing_address2'] = $order->billing_address_2; 
  293. $arg['billing_zip'] = $order->billing_postcode; 
  294. $arg['billing_city'] = $order->billing_city; 
  295. //$arg['billing_country'] = $order->billing_country; 
  296. $arg['billing_countrycode'] = $order->billing_country; 
  297. if (isset($this->phone ) ) { 
  298. $arg['billing_phone'] = $this->phone; 
  299. $arg['shipping_phone'] = $this->phone; 
  300. } else { 
  301. $arg['billing_phone'] = $order->billing_phone; 
  302. if (isset($this->dob)) 
  303. $arg['birthdate'] = $this->dob; 
  304.  
  305. if(isset($this->pclass)) 
  306. $arg['pclass'] = $this->pclass; 
  307.  
  308. if (isset($this->gender)) 
  309. $arg['gender'] = $this->gender; 
  310.  
  311. if (isset($this->iban)) 
  312. $arg['iban'] = $this->iban; 
  313.  
  314. $arg['amount'] = round($order->order_total * 100.0, 0); 
  315. $arg['tax'] = round(($order->order_tax + $order->order_shipping_tax) * 100.0, 0); 
  316. $arg['currency'] = get_woocommerce_currency(); 
  317.  
  318. //producten 
  319. $item_loop = 0; 
  320. if (sizeof($order->get_items()) > 0) : foreach ($order->get_items() as $item) : 
  321. if ($item['qty']) : 
  322.  
  323. $item_loop++; 
  324.  
  325. $product = $order->get_product_from_item($item); 
  326.  
  327. $_tax = new WC_Tax(); 
  328.  
  329. foreach ($_tax->get_shop_base_rate($product->tax_class) as $line_tax) { 
  330. $tax = $line_tax['rate']; 
  331.  
  332. $arg['product_id_' . $item_loop] = $item['product_id']; 
  333. $arg['product_description_' . $item_loop] = $item['name']; 
  334. $arg['product_quantity_' . $item_loop] = $item['qty']; 
  335. $arg['product_netprice_' . $item_loop] = round($product->get_price_excluding_tax(), 2) * 100; 
  336. $arg['product_total_' . $item_loop] = round($item['line_total'] + $item['line_tax'], 2) * 100; 
  337. $arg['product_nettotal_' . $item_loop] = round($item['line_total'], 2) * 100; 
  338. $arg['product_tax_' . $item_loop] = round($item['line_tax'], 2) * 100; 
  339. $arg['product_taxrate_' . $item_loop] = (!isset($tax)) ? 0 : round($tax, 2) * 100; 
  340. $arg['product_weight_' . $item_loop] = round($product->weight, 2) * 100; 
  341. endif; 
  342. endforeach; 
  343. endif; 
  344.  
  345. if (isset($order->order_shipping)) { 
  346. if ($order->order_shipping > 0) { 
  347. $item_loop++; 
  348. $arg['product_id_' . $item_loop] = 'shipping'; 
  349. $arg['product_description_' . $item_loop] = 'Verzendkosten'; 
  350. $arg['product_quantity_' . $item_loop] = '1'; 
  351. $arg['product_netprice_' . $item_loop] = round($order->order_shipping, 2) * 100; 
  352. $arg['product_total_' . $item_loop] = round($order->order_shipping + $order->order_shipping_tax, 2) * 100; 
  353. $arg['product_nettotal_' . $item_loop] = round($order->order_shipping, 2) * 100; 
  354. $arg['product_tax_' . $item_loop] = round($order->order_shipping_tax, 2) * 100; 
  355. $arg['product_taxrate_' . $item_loop] = round($tax, 2) * 100; 
  356.  
  357. foreach($order->get_fees() as $fee) 
  358. {  
  359. $item_loop++; 
  360. $arg['product_id_' . $item_loop] = 'fee' . $item_loop; 
  361. $arg['product_description_' . $item_loop] = $fee['name']; 
  362. $arg['product_quantity_' . $item_loop] = '1'; 
  363. $arg['product_netprice_' . $item_loop] = round($fee['line_total'], 2) * 100; 
  364. $arg['product_total_' . $item_loop] = round($fee['line_total'] + $fee['line_tax'], 2) * 100; 
  365. $arg['product_nettotal_' . $item_loop] = round($fee['line_total'], 2) * 100; 
  366. $arg['product_tax_' . $item_loop] = round($fee['line_tax'], 2) * 100; 
  367. $arg['product_taxrate_' . $item_loop] = round((($arg['product_tax_' . $item_loop] * 100.0) / $arg['product_nettotal_' . $item_loop])) * 100; 
  368.  
  369. if ($this->settings['testmode'] == 'yes') { 
  370. $arg['testmode'] = 'true'; 
  371. } else { 
  372. $arg['testmode'] = 'false'; 
  373.  
  374. if (isset($this->includelink) && $this->includelink == 'yes') { 
  375. $arg['including'] = 'true'; 
  376. if (isset($this->days) && $this->days > 0) { 
  377. $arg['days'] = $this->days; 
  378.  
  379. if(isset($this->bic) && !empty($this->bic)) { 
  380. $arg['bic'] = $this->bic; 
  381.  
  382. return $arg; 
  383.  
  384. public function check_notify() { 
  385. $this->orderid = filter_input(INPUT_GET, 'ec'); // $_GET['ec']; 
  386. $this->trxid = filter_input(INPUT_GET, 'trxid'); //$_GET['trxid']; 
  387.  
  388. if (!$this->orderid || !$this->trxid ) { 
  389. echo 'URL not correct!'; 
  390. exit; 
  391.  
  392. $this->notify(); 
  393.  
  394. private function notify() { 
  395. $order = new WC_Order($this->orderid); 
  396. $sisow = new Sisow($this->settings['merchantid'], $this->settings['merchantkey'], $this->settings['shopid']); 
  397.  
  398. $trxid = get_post_meta($order->id, '_trxid', true); 
  399.  
  400. if(empty($trxid)) 
  401. $trxid = $this->trxid; 
  402.  
  403. if (($order->status != 'processing' && $order->status != 'completed') || $sisow->status == Sisow::statusReversed || $sisow->status == Sisow::statusRefunded) { 
  404. if (($ex = $sisow->StatusRequest($trxid)) < 0) { 
  405. echo 'fail' . $ex; 
  406. exit; 
  407. } else { 
  408. if(($sisow->status == 'Pending' || $sisow->status == 'Open') && ($this->paymentcode != 'overboeking' && $this->paymentcode != 'ebill')) 
  409. exit('Order still pending'); 
  410.  
  411. switch ($sisow->status) { 
  412. case 'Success': 
  413. $order->add_order_note(__($this->paymentname . ' transaction Success', 'sisow')); 
  414. $order->payment_complete(); 
  415.  
  416. if(isset($this->_completed) && $this->_completed) 
  417. $order->update_status('completed', 'Transaction ' . $_GET['trxid'] . ':Sisow set status to Completed'); 
  418. break; 
  419. case 'Reservation': 
  420. $order->add_order_note(__('Transaction ' . $_GET['trxid'] . ': Reservation made for ' . $this->paymentname, 'sisow')); 
  421. $order->payment_complete(); 
  422. break; 
  423. case 'Cancelled': 
  424. $order->add_order_note($this->paymentname . __(': transaction(' . $_GET['trxid'] . ') was cancelled.', 'sisow')); 
  425. break; 
  426. case 'Denied': 
  427. $order->cancel_order($this->paymentname . __(': transaction(' . $_GET['trxid'] . ') was denied by Klarna.', 'sisow')); 
  428. break; 
  429. case 'Expired': 
  430. $order->cancel_order($this->paymentname . __(': transaction(' . $_GET['trxid'] . ') was expired.', 'sisow')); 
  431. break; 
  432. case 'Failure': 
  433. $order->cancel_order($this->paymentname . __(': transaction(' . $_GET['trxid'] . ') was failed.', 'sisow')); 
  434. break; 
  435. case Sisow::statusRefunded: 
  436. $order->cancel_order($this->paymentname . __(': transaction(' . $_GET['trxid'] . ') was '.Sisow::statusRefunded.'.', 'sisow')); 
  437. break; 
  438. case Sisow::statusReversed: 
  439. $order->cancel_order($this->paymentname . __(': transaction(' . $_GET['trxid'] . ') was '.Sisow::statusReversed.'.', 'sisow')); 
  440. break; 
  441. case 'Pending': 
  442. $order->update_status('on-hold', __($this->paymentname . ': transaction Pending', 'sisow')); 
  443. break; 
  444. case 'Open': 
  445. $order->update_status('on-hold', __($this->paymentname . ': transaction Pending', 'sisow')); 
  446. break; 
  447. add_post_meta($this->orderid, 'status', $sisow->status); 
  448. add_post_meta($this->orderid, 'trxid', $sisow->trxId); 
  449.  
  450. if (isset($_GET['notify']) || isset($_GET['callback'])) { 
  451. exit; 
  452.  
  453. if($this->redirect == false) 
  454. return array( 
  455. 'result' => 'success',  
  456. 'redirect' => $this->get_return_url($order) ); 
  457. else 
  458. header('Location: ' . $this->get_return_url($order)); 
  459. exit; 
  460.  
  461. private function calculate_fee_for($settings, $total) { 
  462. global $woocommerce; 
  463. $charge = 0; 
  464.  
  465. if(strpos($settings['paymentfee'], ';') > 0) 
  466. $taxes = explode(";", $settings['paymentfee']); 
  467. $charge = 0; 
  468. if($taxes[0] > 0) 
  469. $charge += $taxes[0]; 
  470. else 
  471. $charge += $total * (($taxes[0] * -1) / 100.0); 
  472.  
  473. if($taxes[1] > 0) 
  474. $charge += $taxes[1]; 
  475. else 
  476. $charge += $total * (($taxes[1] * -1) / 100.0); 
  477. else if ($settings['paymentfee'] > 0) { 
  478. $charge = $settings['paymentfee']; 
  479. } else { 
  480. $charge = $total * (($settings['paymentfee'] * -1) / 100.0); 
  481.  
  482. return $charge; 
  483.  
  484. private function calculate_tax($settings, $charge) { 
  485. $amount = 0; 
  486.  
  487. if(isset($settings['paymentfeetax']) && $settings['paymentfeetax'] > 0) 
  488. $prices_include_tax = get_option('woocommerce_prices_include_tax') == 'yes' ? true : false; 
  489. $_tax = new WC_Tax(); 
  490. $tax_rates = $_tax->get_shop_base_rate($settings['paymentfeetax']); 
  491. $taxes = $_tax->calc_tax($charge, $tax_rates, $prices_include_tax); 
  492. $amount = $_tax->get_tax_total($taxes); 
  493.  
  494. return $amount; 
  495.  
  496.  
  497. //payment fee 
  498. //sisow 
  499. //added 06-08-2013 
  500. add_action('woocommerce_checkout_update_order_meta', 'process_checkout', 10, 2); 
  501. add_action('woocommerce_calculate_totals', 'sisow_payment_fee'); 
  502. add_action('woocommerce_admin_order_totals_after_shipping', 'admin_order_totals'); 
  503. add_action('woocommerce_process_shop_order_meta', 'process_shop_order', 100, 2); 
  504.  
  505. function sisow_payment_fee($cart) { 
  506. global $woocommerce; 
  507.  
  508. if (isset($_POST['payment_method'])) { 
  509. $gw = $_POST['payment_method']; 
  510. } elseif (isset($_POST['post_data'])) { 
  511. $parsed = array(); 
  512. parse_str($_POST['post_data'], $parsed); 
  513.  
  514. if (isset($parsed['payment_method'])) { 
  515. $gw = $parsed['payment_method']; 
  516. } elseif (isset($woocommerce->checkout->posted) && !empty($woocommerce->checkout->posted)) { 
  517. $gw = $woocommerce->checkout->posted['payment_method']; 
  518.  
  519. if (isset($gw) && strpos($gw, 'sisow') !== false) { 
  520. $class = 'WC_Sisow_' . str_replace('sisow', '', $gw); 
  521.  
  522. if (!class_exists($class)) 
  523. return; 
  524.  
  525. $payment = new $class; 
  526.  
  527. $settings = $payment->settings; 
  528. $paymentFee = 0; 
  529. $cart_total = $cart->subtotal + $cart->shipping_total; 
  530. $add_note = false; 
  531.  
  532. $paymentFee = calculate_fee_for($settings, $cart_total); 
  533.  
  534. if ($paymentFee > 0) { 
  535. $tax_amount = calculate_tax($settings, $paymentFee); 
  536.  
  537. if (get_option('woocommerce_tax_display_cart') == 'incl') { 
  538. $paymentFee += $tax_amount; 
  539.  
  540. if ($tax_amount > 0) { 
  541.  
  542. if (get_option('woocommerce_prices_include_tax') == 'yes') { 
  543. $paymentFee -= $tax_amount; 
  544.  
  545. if (get_option('woocommerce_tax_display_cart') == 'excl') { 
  546. $add_note = true; 
  547.  
  548. if (get_option('woocommerce_tax_display_cart') == 'excl') { 
  549. $cart_taxes = $cart->taxes; 
  550. $keys = array_keys($cart_taxes); 
  551.  
  552. if (isset($keys[0])) { 
  553. $cart_taxes[$keys[0]] += $tax_amount; 
  554. } else { 
  555. $cart_taxes[0] = $tax_amount; 
  556.  
  557. $cart->taxes = $cart_taxes; 
  558. $cart->tax_total += $tax_amount; 
  559.  
  560. if (function_exists('get_product')) { 
  561. $cart->fee_total = $cart->fee_total + $paymentFee; 
  562.  
  563. if (!function_exists('get_product')) { 
  564. $cart->cart_contents_total = $cart->cart_contents_total + $paymentFee; 
  565.  
  566. $paymentfeelabel = (isset($settings['paymentfeelabel']) && $settings['paymentfeelabel'] != '') ? $settings['paymentfeelabel'] : 'Payment Fee'; 
  567.  
  568. if (method_exists($woocommerce->cart, 'add_fee')) { 
  569. $cart->add_fee($paymentfeelabel, $paymentFee, true, $settings['paymentfeetax']); 
  570.  
  571. if (isset($woocommerce->checkout->posted) && !empty($woocommerce->checkout->posted)) { 
  572. $woocommerce->checkout->posted['processing_fee_label'] = $paymentfeelabel; 
  573. $woocommerce->checkout->posted['processing_fee'] = $paymentFee; 
  574. $woocommerce->checkout->posted['processing_fee_tax'] = $tax_amount; 
  575.  
  576. function process_checkout($order_id, $posted) { 
  577. global $woocommerce; 
  578.  
  579. if (isset($posted['processing_fee'])) { 
  580. update_post_meta($order_id, '_processing_fee', woocommerce_format_total($posted['processing_fee'])); 
  581.  
  582. if (isset($posted['processing_fee_label'])) { 
  583. update_post_meta($order_id, '_processing_fee_label', $posted['processing_fee_label']); 
  584.  
  585. if (isset($posted['processing_fee_tax'])) { 
  586. update_post_meta($order_id, '_processing_fee_tax', woocommerce_format_total($posted['processing_fee_tax'])); 
  587.  
  588. $new_taxes = array(); 
  589. $order_taxes = get_post_meta($order_id, '_order_taxes', true); 
  590.  
  591. if ($order_taxes && is_array($order_taxes)) { 
  592. foreach ($order_taxes as $tax) { 
  593. $label = $tax['label']; 
  594. if (isset($new_taxes[$label])) { 
  595. $new_taxes[$label]['cart_tax'] += $tax['cart_tax']; 
  596. $new_taxes[$label]['shipping_tax'] += $tax['shipping_tax']; 
  597. } else { 
  598. $new_taxes[$label] = $tax; 
  599.  
  600. $i = 0; 
  601. foreach ($new_taxes as $key => $tax) { 
  602. $new_taxes[$i] = $tax; 
  603. unset($new_taxes[$key]); 
  604.  
  605. update_post_meta($order_id, '_order_taxes', $new_taxes); 
  606. update_post_meta($order_id, '_old_order_taxes', $order_taxes); 
  607.  
  608. function calculate_fee_for($settings, $total) { 
  609. global $woocommerce; 
  610. $charge = 0; 
  611.  
  612. if(isset($settings['paymentfee']) && $settings['paymentfee'] != '') 
  613. if(strpos($settings['paymentfee'], ';') > 0) 
  614. $taxes = explode(";", $settings['paymentfee']); 
  615. $charge = 0; 
  616. if($taxes[0] > 0) 
  617. $charge += $taxes[0]; 
  618. else 
  619. $charge += $total * (($taxes[0] * -1) / 100.0); 
  620.  
  621. if($taxes[1] > 0) 
  622. $charge += $taxes[1]; 
  623. else 
  624. $charge += $total * (($taxes[1] * -1) / 100.0); 
  625. else if ($settings['paymentfee'] > 0) { 
  626. $charge = $settings['paymentfee']; 
  627. } else { 
  628. $charge = $total * (($settings['paymentfee'] * -1) / 100.0); 
  629. else 
  630. $charge = 0; 
  631.  
  632. return $charge; 
  633.  
  634. function calculate_tax($settings, $charge) { 
  635. $amount = 0; 
  636.  
  637. $prices_include_tax = get_option('woocommerce_prices_include_tax') == 'yes' ? true : false; 
  638.  
  639. $_tax = new WC_Tax(); 
  640. $tax_rates = $_tax->get_shop_base_rate($settings['paymentfeetax']); 
  641. $taxes = $_tax->calc_tax($charge, $tax_rates, $prices_include_tax); 
  642. $amount = $_tax->get_tax_total($taxes); 
  643.  
  644. return $amount; 
  645.  
  646. function process_shop_order($post_id, $post) { 
  647. $old_fee = get_post_meta($post_id, '_processing_fee', true); 
  648. $new_fee = stripslashes($_POST['_processing_fee']); 
  649. $order_total = get_post_meta($post_id, '_order_total', true); 
  650.  
  651. if ($old_fee != '') { 
  652. if ($new_fee == 0 || $new_fee == '') { 
  653. // if there is a processing fee which is now being removed 
  654. $order_total -= $old_fee; 
  655. } else { 
  656. // adding or subtracting of fee 
  657. if ($old_fee > $new_fee) { 
  658. // fee lessened 
  659. $diff = $old_fee - $new_fee; 
  660. $order_total -= $diff; 
  661. } elseif ($old_fee < $new_fee) { 
  662. // fee added 
  663. $diff = $new_fee - $old_fee; 
  664. $order_total += $diff; 
  665.  
  666. update_post_meta($post_id, '_order_total', $order_total); 
  667. update_post_meta($post_id, '_processing_fee', $new_fee); 
  668.  
  669. function admin_order_totals($order_id) { 
  670. global $woocommerce; 
  671.  
  672. $label = get_post_meta($order_id, '_processing_fee_label', true); 
  673. $processing_fee = get_post_meta($order_id, '_processing_fee', true); 
  674.  
  675. if (!$processing_fee) 
  676. return; 
  677. ?> 
  678. <div class="clear"></div> 
  679. </div> 
  680. <div class="totals_group"> 
  681. <h4><?php echo $label; ?></h4> 
  682. <ul class="totals"> 
  683.  
  684. <li class="left"> 
  685. <label><?php _e('Amount:', 'woocommerce'); ?></label> 
  686. <input type="text" id="_processing_fee" name="_processing_fee" value="<?php echo $processing_fee; ?>" class="first" /> 
  687. </li> 
  688. </ul> 
  689. <?php 
  690. ?> 
.