woocommerce_payulatam_init

The WooCommerce - PayU Latam Gateway woocommerce payulatam init function.

Description

woocommerce_payulatam_init(); 

Usage

  1. if ( !function_exists( 'woocommerce_payulatam_init' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-payu-latam-gateway/woocommerce-gateway-payulatam.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = woocommerce_payulatam_init(); 
  7.  

Defined (1)

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

/woocommerce-gateway-payulatam.php  
  1. function woocommerce_payulatam_init() { 
  2. if(!class_exists('WC_Payment_Gateway')) return; 
  3.  
  4. if( isset($_GET['msg']) && !empty($_GET['msg']) ) { 
  5. add_action('the_content', 'showPayuLatamMessage'); 
  6. function showPayuLatamMessage($content) { 
  7. return '<div class="'.htmlentities($_GET['type']).'">'.htmlentities(urldecode($_GET['msg'])).'</div>'.$content; 
  8.  
  9. /** 
  10. * PayU Gateway Class 
  11. * @access public 
  12. * @param  
  13. * @return  
  14. */ 
  15. class WC_payulatam extends WC_Payment_Gateway{ 
  16.  
  17. public function __construct() { 
  18. global $woocommerce; 
  19. $this->load_plugin_textdomain(); 
  20. //add_action('init', array($this, 'load_plugin_textdomain')); 
  21.  
  22. $this->id = 'payulatam'; 
  23. $this->icon_default = $this->get_country_icon(false); 
  24. $this->method_title = __('PayU Latam', 'payu-latam-woocommerce'); 
  25. $this->method_description = __("The easiest way to sell and recive payments online in latinamerica", 'payu-latam-woocommerce'); 
  26. $this->has_fields = false; 
  27.  
  28. $this->init_form_fields(); 
  29. $this->init_settings(); 
  30. $this->language = get_bloginfo('language'); 
  31.  
  32. $this->testmode = $this->settings['testmode']; 
  33. $this->testmerchant_id = '508029'; 
  34. $this->testaccount_id = '512321'; 
  35. $this->testapikey = '4Vj8eK4rloUd272L48hsrarnUA'; 
  36. $this->debug = "no"; 
  37.  
  38. $this->show_methods = $this->settings['show_methods']; 
  39. $this->icon_checkout = $this->settings['icon_checkout']; 
  40.  
  41. if($this->show_methods=='yes'&&trim($this->settings['icon_checkout'])=='') { 
  42. $this->icon = $this->icon_default; 
  43. }elseif(trim($this->settings['icon_checkout'])!='') { 
  44. $this->icon = $this->settings['icon_checkout']; 
  45. }else{ 
  46. $this->icon = $this->get_country_icon(); 
  47.  
  48. $this->title = $this->settings['title']; 
  49. $this->description = $this->settings['description']; 
  50. $this->merchant_id = ($this->testmode=='yes')?$this->testmerchant_id:$this->settings['merchant_id']; 
  51. $this->account_id = ($this->testmode=='yes')?$this->testaccount_id:$this->settings['account_id']; 
  52. $this->apikey = ($this->testmode=='yes')?$this->testapikey:$this->settings['apikey']; 
  53. $this->redirect_page_id = $this->settings['redirect_page_id']; 
  54. $this->endpoint = $this->settings['endpoint']; 
  55. $this->payu_language = $this->settings['payu_language']; 
  56. $this->taxes = $this->settings['taxes']; 
  57. $this->tax_return_base = $this->settings['tax_return_base']; 
  58. $this->currency = ($this->is_valid_currency())?get_woocommerce_currency():'USD'; 
  59. $this->textactive = 0; 
  60. $this->form_method = $this->settings['form_method']; 
  61. $this->liveurl = 'https://gateway.payulatam.com/ppp-web-gateway/'; 
  62. $this->testurl = 'https://sandbox.gateway.payulatam.com/ppp-web-gateway/'; 
  63.  
  64. /** mesagges */ 
  65. $this->msg_approved = $this->settings['msg_approved']; 
  66. $this->msg_declined = $this->settings['msg_declined']; 
  67. $this->msg_cancel = $this->settings['msg_cancel']; 
  68. $this->msg_pending = $this->settings['msg_pending']; 
  69.  
  70. if ($this->testmode == "yes") 
  71. $this->debug = "yes"; 
  72.  
  73. add_filter( 'woocommerce_currencies', 'add_all_currency' ); 
  74. add_filter( 'woocommerce_currency_symbol', 'add_all_symbol', 10, 2); 
  75.  
  76. $this->msg['message'] = ""; 
  77. $this->msg['class'] = ""; 
  78. // Logs 
  79.  
  80. if(version_compare( WOOCOMMERCE_VERSION, '2.1', '>=')) { 
  81. $this->log = new WC_Logger(); 
  82. }else{ 
  83. $this->log = $woocommerce->logger(); 
  84.  
  85.  
  86.  
  87. add_action('payulatam_init', array( $this, 'pauylatam_successful_request')); 
  88. add_action( 'woocommerce_receipt_payulatam', array( $this, 'receipt_page' ) ); 
  89. //update for woocommerce >2.0 
  90. add_action( 'woocommerce_api_' . strtolower( get_class( $this ) ), array( $this, 'check_payulatam_response' ) ); 
  91.  
  92. if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) { 
  93. /** 2.0.0 */ 
  94. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) ); 
  95. } else { 
  96. /** 1.6.6 */ 
  97. add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) ); 
  98.  
  99.  
  100. public function load_plugin_textdomain() 
  101. load_plugin_textdomain( 'payu-latam-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . "/languages" ); 
  102.  
  103. /** 
  104. * Show payment metods by country 
  105. */ 
  106.  
  107. public function get_country_icon($default=true) { 
  108. $country = ''; 
  109. if(!$default) 
  110. $country = WC()->countries->get_base_country(); 
  111.  
  112. $icon = PAYU_ASSETS.'/img/payulogo'.$country.'.png'; 
  113. return $icon; 
  114. /** 
  115. * Check if Gateway can be display  
  116. * @access public 
  117. * @return void 
  118. */ 
  119. function is_available() { 
  120. global $woocommerce; 
  121.  
  122. if ( $this->enabled=="yes" ) : 
  123.  
  124. if ( !$this->is_valid_currency()) return false; 
  125.  
  126. if ( $woocommerce->version < '1.5.8' ) return false; 
  127.  
  128. if ($this->testmode!='yes'&&(!$this->merchant_id || !$this->account_id || !$this->apikey )) return false; 
  129.  
  130. return true; 
  131. endif; 
  132.  
  133. return false; 
  134.  
  135. /** 
  136. * Settings Options 
  137. * @access public 
  138. * @return void 
  139. */ 
  140. function init_form_fields() { 
  141. $this->form_fields = array( 
  142. 'enabled' => array( 
  143. 'title' => __('Enable/Disable', 'payu-latam-woocommerce'),  
  144. 'type' => 'checkbox',  
  145. 'label' => __('Enable PayU Latam Payment Module.', 'payu-latam-woocommerce'),  
  146. 'default' => 'no',  
  147. 'description' => __('Show in the Payment List as a payment option', 'payu-latam-woocommerce') 
  148. ),  
  149. 'show_methods' => array( 
  150. 'title' => __('Mostrar Metodos', 'payu-latam-woocommerce'),  
  151. 'type' => 'checkbox',  
  152. 'label' => __('Mostrar metodos de pago por Pais.', 'payu-latam-woocommerce'),  
  153. 'default' => 'no',  
  154. 'description' => __('Mostrar imagen de los metodos de pago soportados por Pais.', 'payu-latam-woocommerce') 
  155. ),  
  156. 'icon_checkout' => array( 
  157. 'title' => __('Logo en el checkout:', 'payu-latam-woocommerce'),  
  158. 'type' => 'text',  
  159. 'default' => $this->get_country_icon(),  
  160. 'description' => __('URL de la Imagen para mostrar en el carrro de compra.', 'payu-latam-woocommerce'),  
  161. 'desc_tip' => true 
  162. ),  
  163. 'title' => array( 
  164. 'title' => __('Title:', 'payu-latam-woocommerce'),  
  165. 'type' => 'text',  
  166. 'default' => __('PayU Latam Online Payments', 'payu-latam-woocommerce'),  
  167. 'description' => __('This controls the title which the user sees during checkout.', 'payu-latam-woocommerce'),  
  168. 'desc_tip' => true 
  169. ),  
  170. 'description' => array( 
  171. 'title' => __('Description:', 'payu-latam-woocommerce'),  
  172. 'type' => 'textarea',  
  173. 'default' => __('Pay securely by Credit or Debit Card or Internet Banking through PayU Latam Secure Servers.', 'payu-latam-woocommerce'),  
  174. 'description' => __('This controls the description which the user sees during checkout.', 'payu-latam-woocommerce'),  
  175. 'desc_tip' => true 
  176. ),  
  177. 'merchant_id' => array( 
  178. 'title' => __('Merchant ID', 'payu-latam-woocommerce'),  
  179. 'type' => 'text',  
  180. 'description' => __('Given to Merchant by PayU Latam', 'payu-latam-woocommerce'),  
  181. 'desc_tip' => true 
  182. ),  
  183. 'account_id' => array( 
  184. 'title' => __('Account ID', 'payu-latam-woocommerce'),  
  185. 'type' => 'text',  
  186. 'description' => __('Some Countrys (Brasil, Mexico) require this ID, Gived to you by PayU Latam on regitration.', 'payu-latam-woocommerce'),  
  187. 'desc_tip' => true 
  188. ),  
  189. 'apikey' => array( 
  190. 'title' => __('ApiKey', 'payu-latam-woocommerce'),  
  191. 'type' => 'text',  
  192. 'description' => __('Given to Merchant by PayU Latam', 'payu-latam-woocommerce'),  
  193. 'desc_tip' => true 
  194. ),  
  195. 'testmode' => array( 
  196. 'title' => __('TEST Mode', 'payu-latam-woocommerce'),  
  197. 'type' => 'checkbox',  
  198. 'label' => __('Enable PayU Latam TEST Transactions.', 'payu-latam-woocommerce'),  
  199. 'default' => 'no',  
  200. 'description' => __('Tick to run TEST Transaction on the PayU Latam platform', 'payu-latam-woocommerce'),  
  201. 'desc_tip' => true 
  202. ),  
  203. 'taxes' => array( 
  204. 'title' => __('Tax Rate - Read', 'payu-latam-woocommerce').' <a target="_blank" href="http://docs.payulatam.com/manual-integracion-web-checkout/informacion-adicional/tablas-de-variables-complementarias/">PayU Documentacion</a>',  
  205. 'type' => 'text',  
  206. 'default' => '0',  
  207. 'description' => __('Tax rates for Transactions (IVA).', 'payu-latam-woocommerce'),  
  208. 'desc_tip' => true 
  209. ),  
  210. 'tax_return_base' => array( 
  211. 'title' => __('Tax Return Base', 'payu-latam-woocommerce'),  
  212. 'type' => 'text',  
  213. //'options' => array('0' => 'None', '2' => '2% Credit Cards Payments Return (Colombia)'),  
  214. 'default' => '0',  
  215. 'description' => __('Tax base to calculate IVA ', 'payu-latam-woocommerce'),  
  216. 'desc_tip' => true 
  217. ),  
  218. 'payu_language' => array( 
  219. 'title' => __('Gateway Language', 'payu-latam-woocommerce'),  
  220. 'type' => 'select',  
  221. 'options' => array('ES' => 'ES', 'EN' => 'EN', 'PT' => 'PT'),  
  222. 'description' => __('PayU Latam Gateway Language ', 'payu-latam-woocommerce'),  
  223. 'desc_tip' => true 
  224. ),  
  225. 'form_method' => array( 
  226. 'title' => __('Form Method', 'payu-latam-woocommerce'),  
  227. 'type' => 'select',  
  228. 'default' => 'POST',  
  229. 'options' => array('POST' => 'POST', 'GET' => 'GET'),  
  230. 'description' => __('Checkout form submition method ', 'payu-latam-woocommerce'),  
  231. 'desc_tip' => true 
  232. ),  
  233. 'redirect_page_id' => array( 
  234. 'title' => __('Return Page', 'payu-latam-woocommerce'),  
  235. 'type' => 'select',  
  236. 'options' => $this->get_pages(__('Select Page', 'payu-latam-woocommerce')),  
  237. 'description' => __('URL of success page', 'payu-latam-woocommerce'),  
  238. 'desc_tip' => true 
  239. ),  
  240. 'endpoint' => array( 
  241. 'title' => __('Page End Point (Woo > 2.1)', 'payu-latam-woocommerce'),  
  242. 'type' => 'text',  
  243. 'default' => '',  
  244. 'description' => __('Return Page End Point.', 'payu-latam-woocommerce'),  
  245. 'desc_tip' => true 
  246. ),  
  247. 'msg_approved' => array( 
  248. 'title' => __('Message for approved transaction', 'payu-latam-woocommerce'),  
  249. 'type' => 'text',  
  250. 'default' => __('PayU Latam Payment Approved', 'payu-latam-woocommerce'),  
  251. 'description' => __('Message for approved transaction', 'payu-latam-woocommerce'),  
  252. 'desc_tip' => true 
  253. ),  
  254. 'msg_pending' => array( 
  255. 'title' => __('Message for pending transaction', 'payu-latam-woocommerce'),  
  256. 'type' => 'text',  
  257. 'default' => __('Payment pending', 'payu-latam-woocommerce'),  
  258. 'description' => __('Message for pending transaction', 'payu-latam-woocommerce'),  
  259. 'desc_tip' => true 
  260. ),  
  261. 'msg_cancel' => array( 
  262. 'title' => __('Message for cancel transaction', 'payu-latam-woocommerce'),  
  263. 'type' => 'text',  
  264. 'default' => __('Transaction Canceled.', 'payu-latam-woocommerce'),  
  265. 'description' => __('Message for cancel transaction', 'payu-latam-woocommerce'),  
  266. 'desc_tip' => true 
  267. ),  
  268. 'msg_declined' => array( 
  269. 'title' => __('Message for declined transaction', 'payu-latam-woocommerce'),  
  270. 'type' => 'text',  
  271. 'default' => __('Payment rejected via PayU Latam.', 'payu-latam-woocommerce'),  
  272. 'description' => __('Message for declined transaction ', 'payu-latam-woocommerce'),  
  273. 'desc_tip' => true 
  274. ),  
  275. ); 
  276.  
  277. }  
  278.  
  279. /** 
  280. * Generate Admin Panel Options 
  281. * @access public 
  282. * @return string 
  283. **/ 
  284. public function admin_options() { 
  285. echo '<img src="'.$this->get_country_icon().'" alt="PayU" width="80"><h3>'.__('PayU Latam', 'payu-latam-woocommerce').'</h3>'; 
  286. echo '<p>'.__('The easiest way to sell and recive payments online in latinamerica', 'payu-latam-woocommerce').'</p>'; 
  287. echo '<table class="form-table">'; 
  288. // Generate the HTML For the settings form. 
  289. $this->generate_settings_html(); 
  290. echo '</table>'; 
  291. /** 
  292. * Generate the PayU Latam Payment Fields 
  293. * @access public 
  294. * @return string 
  295. */ 
  296. function payment_fields() { 
  297. if($this->description) echo wpautop(wptexturize($this->description)); 
  298. /** 
  299. * Generate the PayU Latam Form for checkout 
  300. * @access public 
  301. * @param mixed $order 
  302. * @return string 
  303. **/ 
  304. function receipt_page($order) { 
  305. echo '<p>'.__('Thank you for your order, please click the button below to pay with PayU Latam.', 'payu-latam-woocommerce').'</p>'; 
  306. echo $this->generate_payulatam_form($order); 
  307. /** 
  308. * Generate PayU POST arguments 
  309. * @access public 
  310. * @param mixed $order_id 
  311. * @return string 
  312. **/ 
  313. function get_payulatam_args($order_id) { 
  314. global $woocommerce; 
  315. $order = new WC_Order( $order_id ); 
  316. $txnid = $order->order_key.'-'.time(); 
  317.  
  318. $redirect_url = ($this->redirect_page_id=="" || $this->redirect_page_id==0)?get_site_url() . "/":get_permalink($this->redirect_page_id); 
  319. //For wooCoomerce 2.0 
  320. $redirect_url = add_query_arg( 'wc-api', get_class( $this ), $redirect_url ); 
  321. $redirect_url = add_query_arg( 'order_id', $order_id, $redirect_url ); 
  322. $redirect_url = add_query_arg( '', $this->endpoint, $redirect_url ); 
  323.  
  324. $productinfo = "Order $order_id"; 
  325.  
  326. $str = "$this->apikey~$this->merchant_id~$txnid~$order->order_total~$this->currency"; 
  327. $hash = strtolower(md5($str)); 
  328.  
  329.  
  330. $payulatam_args = array( 
  331. 'merchantId' => $this->merchant_id,  
  332. 'accountId' => $this->account_id,  
  333. 'signature' => $hash,  
  334. 'referenceCode' => $txnid,  
  335. 'amount' => $order->order_total,  
  336. 'currency' => $this->currency,  
  337. 'payerFullName' => $order->billing_first_name .' '.$order->billing_last_name,  
  338. 'buyerEmail' => $order->billing_email,  
  339. 'telephone' => $order->billing_phone,  
  340. 'billingAddress' => $order->billing_address_1.' '.$order->billing_address_2,  
  341. 'shippingAddress' => $order->billing_address_1.' '.$order->billing_address_2,  
  342. 'billingCity' => $order->billing_city,  
  343. 'shippingCity' => $order->billing_city,  
  344. 'billingCountry' => $order->billing_country,  
  345. 'shippingCountry' => $order->billing_country,  
  346. 'zipCode' => $order->billing_postcode,  
  347. 'lng' => $this->payu_language,  
  348. 'description' => $productinfo,  
  349. 'responseUrl' => $redirect_url,  
  350. 'confirmationUrl' => $redirect_url,  
  351. 'tax' => $this->taxes,  
  352. 'taxReturnBase' => $this->tax_return_base,  
  353. 'extra1' => $order->order_id,  
  354. 'discount' => '0' 
  355. ); 
  356.  
  357. if ( $this->testmode == 'yes' ) { 
  358. $payulatam_args['ApiKey'] = $this->testapikey; 
  359. $payulatam_args['test'] = '1'; 
  360. }else{ 
  361. $payulatam_args['ApiKey'] = $this->apikey; 
  362.  
  363. return $payulatam_args; 
  364.  
  365. /** 
  366. * Generate the PayU Latam button link 
  367. * @access public 
  368. * @param mixed $order_id 
  369. * @return string 
  370. */ 
  371. function generate_payulatam_form( $order_id ) { 
  372. global $woocommerce; 
  373.  
  374. $order = new WC_Order( $order_id ); 
  375.  
  376. if ( $this->testmode == 'yes' ) 
  377. $payulatam_adr = $this->testurl; 
  378. else  
  379. $payulatam_adr = $this->liveurl; 
  380.  
  381.  
  382. $payulatam_args = $this->get_payulatam_args( $order_id ); 
  383. $payulatam_args_array = array(); 
  384.  
  385. foreach ($payulatam_args as $key => $value) { 
  386. $payulatam_args_array[] = '<input type="hidden" name="'.esc_attr( $key ).'" value="'.esc_attr( $value ).'" />'; 
  387. $code='jQuery("body").block({ 
  388. message: "' . esc_js( __( 'Thank you for your order. We are now redirecting you to PayU Latam to make payment.', 'payu-latam-woocommerce' ) ) . '",  
  389. baseZ: 99999,  
  390. overlayCSS: 
  391. background: "#fff",  
  392. opacity: 0.6 
  393. },  
  394. css: { 
  395. padding: "20px",  
  396. zindex: "9999999",  
  397. textAlign: "center",  
  398. color: "#555",  
  399. border: "3px solid #aaa",  
  400. backgroundColor:"#fff",  
  401. cursor: "wait",  
  402. lineHeight: "24px",  
  403. }); 
  404. jQuery("#submit_payulatam_payment_form").click();'; 
  405.  
  406. if (version_compare( WOOCOMMERCE_VERSION, '2.1', '>=')) { 
  407. wc_enqueue_js($code); 
  408. } else { 
  409. $woocommerce->add_inline_js($code); 
  410.  
  411. return '<form action="'.$payulatam_adr.'" method="POST" id="payulatam_payment_form" target="_top"> 
  412. ' . implode( '', $payulatam_args_array) . ' 
  413. <input type="submit" class="button alt" id="submit_payulatam_payment_form" value="' . __( 'Pay via PayU Latam', 'payu-latam-woocommerce' ) . '" /> <a class="button cancel" href="'.esc_url( $order->get_cancel_order_url() ).'">'.__( 'Cancel order & restore cart', 'woocommerce' ).'</a> 
  414. </form>'; 
  415.  
  416. /** 
  417. * Process the payment and return the result 
  418. * @access public 
  419. * @param int $order_id 
  420. * @return array 
  421. */ 
  422. function process_payment( $order_id ) { 
  423. $order = new WC_Order( $order_id ); 
  424. if ( $this->form_method == 'GET' ) { 
  425. $payulatam_args = $this->get_payulatam_args( $order_id ); 
  426. $payulatam_args = http_build_query( $payulatam_args, '', '&' ); 
  427. if ( $this->testmode == 'yes' ): 
  428. $payulatam_adr = $this->testurl . '&'; 
  429. else : 
  430. $payulatam_adr = $this->liveurl . '?'; 
  431. endif; 
  432.  
  433. return array( 
  434. 'result' => 'success',  
  435. 'redirect' => $payulatam_adr . $payulatam_args 
  436. ); 
  437. } else { 
  438. if (version_compare( WOOCOMMERCE_VERSION, '2.1', '>=')) { 
  439. return array( 
  440. 'result' => 'success',  
  441. 'redirect' => add_query_arg('order-pay', $order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay' )))) 
  442. ); 
  443. } else { 
  444. return array( 
  445. 'result' => 'success',  
  446. 'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay' )))) 
  447. ); 
  448.  
  449.  
  450. /** 
  451. * Check for valid payu server callback 
  452. * @access public 
  453. * @return void 
  454. **/ 
  455. function check_payulatam_response() { 
  456. @ob_clean(); 
  457. if ( ! empty( $_REQUEST ) ) { 
  458. header( 'HTTP/1.1 200 OK' ); 
  459. do_action( "payulatam_init", $_REQUEST ); 
  460. } else { 
  461. wp_die( __("PayU Latam Request Failure", 'payu-latam-woocommerce') ); 
  462.  
  463.  
  464. /** 
  465. * Process Payu Response and update the order information 
  466. * @access public 
  467. * @param array $posted 
  468. * @return void 
  469. */ 
  470. function pauylatam_successful_request( $posted ) { 
  471. global $woocommerce; 
  472.  
  473. if ( ! empty( $posted['transactionState'] ) && ! empty( $posted['referenceCode'] ) ) { 
  474. $this->payulatam_return_process($posted); 
  475. if ( ! empty( $posted['state_pol'] ) && ! empty( $posted['reference_sale'] ) ) { 
  476. $this->payulatam_confirmation_process($posted); 
  477.  
  478. $redirect_url = $woocommerce->cart->get_checkout_url(); 
  479. //For wooCoomerce 2.0 
  480. $redirect_url = add_query_arg( array('msg'=> urlencode(__( 'There was anerroron the request. please contact the website administrator.', 'payulatam' )), 'type'=>$this->msg['class']), $redirect_url ); 
  481.  
  482. wp_redirect( $redirect_url ); 
  483. exit; 
  484.  
  485. /** 
  486. * Procesar pagina de respuesta 
  487. *  
  488. */ 
  489. function payulatam_return_process($posted) 
  490. global $woocommerce; 
  491.  
  492. $order = $this->get_payulatam_order( $posted ); 
  493.  
  494. $codes=array('4' => 'APPROVED' , '6' => 'DECLINED' , '104' => ERROR , '5' => 'EXPIRED', '7' => 'PENDING' ); 
  495.  
  496. if ( 'yes' == $this->debug ) 
  497. $this->log->add( 'payulatam', 'PAYULATAM Found order #' . $order->id ); 
  498.  
  499.  
  500. if ( 'yes' == $this->debug ) 
  501. $this->log->add( 'payulatam', 'PAYULATAM Transaction state: ' . $posted['transactionState'] ); 
  502.  
  503.  
  504. $state=$posted['transactionState']; 
  505. // We are here so lets check status and do actions 
  506. switch ( $codes[$state] ) { 
  507. case 'APPROVED' : 
  508. case 'PENDING' : 
  509.  
  510. // Check order not already completed 
  511. if ( $order->status == 'completed' ) { 
  512. if ( 'yes' == $this->debug ) 
  513. $this->log->add( 'payulatam', __('Aborting, Order #' . $order->id . ' is already complete.', 'payu-latam-woocommerce') ); 
  514. exit; 
  515.  
  516. // Validate Amount 
  517. if ( $order->get_total() != $posted['TX_VALUE'] ) { 
  518. $order->update_status( 'on-hold', sprintf( __( 'Validationerror PayU Latam amounts do not match (gross %s).', 'payu-latam-woocommerce'), $posted['TX_VALUE'] ) ); 
  519.  
  520. $this->msg['message'] = sprintf( __( 'Validationerror PayU Latam amounts do not match (gross %s).', 'payu-latam-woocommerce'), $posted['TX_VALUE'] ); 
  521. $this->msg['class'] = 'woocommerce-error';  
  522.  
  523.  
  524. // Validate Merchand id  
  525. if ( strcasecmp( trim( $posted['merchantId'] ), trim( $this->merchant_id ) ) != 0 ) { 
  526. $order->update_status( 'on-hold', sprintf( __( 'Validationerror Payment in PayU Latam comes from another id (%s).', 'payu-latam-woocommerce'), $posted['merchantId'] ) ); 
  527. $this->msg['message'] = sprintf( __( 'Validationerror Payment in PayU Latam comes from another id (%s).', 'payu-latam-woocommerce'), $posted['merchantId'] ); 
  528. $this->msg['class'] = 'woocommerce-error'; 
  529.  
  530.  
  531. // Payment Details 
  532. if ( ! empty( $posted['buyerEmail'] ) ) 
  533. update_post_meta( $order->id, __('Payer PayU Latam email', 'payu-latam-woocommerce'), $posted['buyerEmail'] ); 
  534. if ( ! empty( $posted['transactionId'] ) ) 
  535. update_post_meta( $order->id, __('Transaction ID', 'payu-latam-woocommerce'), $posted['transactionId'] ); 
  536. if ( ! empty( $posted['trazabilityCode'] ) ) 
  537. update_post_meta( $order->id, __('Trasability Code', 'payu-latam-woocommerce'), $posted['trazabilityCode'] ); 
  538. /**if ( ! empty( $posted['last_name'] ) ) 
  539. update_post_meta( $order->id, 'Payer last name', $posted['last_name'] );*/ 
  540. if ( ! empty( $posted['lapPaymentMethodType'] ) ) 
  541. update_post_meta( $order->id, __('Payment type', 'payu-latam-woocommerce'), $posted['lapPaymentMethodType'] ); 
  542.  
  543. if ( $codes[$state] == 'APPROVED' ) { 
  544. $order->add_order_note( __( 'PayU Latam payment approved', 'payu-latam-woocommerce') ); 
  545. $this->msg['message'] = $this->msg_approved; 
  546. $this->msg['class'] = 'woocommerce-message'; 
  547. $order->payment_complete(); 
  548. } else { 
  549. $order->update_status( 'on-hold', sprintf( __( 'Payment pending: %s', 'payu-latam-woocommerce'), $codes[$state] ) ); 
  550. $this->msg['message'] = $this->msg_pending; 
  551. $this->msg['class'] = 'woocommerce-info'; 
  552.  
  553. break; 
  554. case 'DECLINED' : 
  555. case 'EXPIRED' : 
  556. case ERROR : 
  557. // Order failed 
  558. $order->update_status( 'failed', sprintf( __( 'Payment rejected via PayU Latam. Error type: %s', 'payu-latam-woocommerce'), ( $codes[$state] ) ) ); 
  559. $this->msg['message'] = $this->msg_declined ; 
  560. $this->msg['class'] = 'woocommerce-error'; 
  561. break; 
  562. default : 
  563. $order->update_status( 'failed', sprintf( __( 'Payment rejected via PayU Latam.', 'payu-latam-woocommerce'), ( $codes[$state] ) ) ); 
  564. $this->msg['message'] = $this->msg_cancel ; 
  565. $this->msg['class'] = 'woocommerce-error'; 
  566. break; 
  567.  
  568. $redirect_url = ($this->redirect_page_id=='default' || $this->redirect_page_id=="" || $this->redirect_page_id==0)?$order->get_checkout_order_received_url():get_permalink($this->redirect_page_id); 
  569. //For wooCoomerce 2.0 
  570. $redirect_url = add_query_arg( array('msg'=> urlencode($this->msg['message']), 'type'=>$this->msg['class']), $redirect_url ); 
  571.  
  572. wp_redirect( $redirect_url ); 
  573. exit; 
  574.  
  575.  
  576. /** 
  577. * Procesar pagina de confirmacion 
  578. *  
  579. */ 
  580. function payulatam_confirmation_process($posted) { 
  581. global $woocommerce; 
  582. $order = $this->get_payulatam_order( $posted ); 
  583.  
  584. $codes=array( 
  585. '1' => 'CAPTURING_DATA' ,  
  586. '2' => 'NEW' ,  
  587. '101' => 'FX_CONVERTED' ,  
  588. '102' => 'VERIFIED' ,  
  589. '103' => 'SUBMITTED' ,  
  590. '4' => 'APPROVED' ,  
  591. '6' => 'DECLINED' ,  
  592. '104' => ERROR ,  
  593. '7' => 'PENDING' ,  
  594. '5' => 'EXPIRED'  
  595. ); 
  596.  
  597. if ( 'yes' == $this->debug ) 
  598. $this->log->add( 'payulatam', 'Found order #' . $order->id ); 
  599.  
  600. $state=$posted['state_pol']; 
  601.  
  602. if ( 'yes' == $this->debug ) 
  603. $this->log->add( 'payulatam', 'Payment status: ' . $codes[$state] ); 
  604.  
  605. // We are here so lets check status and do actions 
  606. switch ( $codes[$state] ) { 
  607. case 'APPROVED' : 
  608. case 'PENDING' : 
  609.  
  610. // Check order not already completed 
  611. if ( $order->status == 'completed' ) { 
  612. if ( 'yes' == $this->debug ) 
  613. $this->log->add( 'payulatam', __('Aborting, Order #' . $order->id . ' is already complete.', 'payu-latam-woocommerce') ); 
  614. exit; 
  615.  
  616. // Validate Amount 
  617. if ( $order->get_total() != $posted['value'] ) { 
  618. $order->update_status( 'on-hold', sprintf( __( 'Validationerror PayU Latam amounts do not match (gross %s).', 'payu-latam-woocommerce'), $posted['value'] ) ); 
  619.  
  620. $this->msg['message'] = sprintf( __( 'Validationerror PayU Latam amounts do not match (gross %s).', 'payu-latam-woocommerce'), $posted['value'] ); 
  621. $this->msg['class'] = 'woocommerce-error';  
  622.  
  623. // Validate Merchand id  
  624. if ( strcasecmp( trim( $posted['merchant_id'] ), trim( $this->merchant_id ) ) != 0 ) { 
  625.  
  626. $order->update_status( 'on-hold', sprintf( __( 'Validationerror Payment in PayU Latam comes from another id (%s).', 'payu-latam-woocommerce'), $posted['merchant_id'] ) ); 
  627. $this->msg['message'] = sprintf( __( 'Validationerror Payment in PayU Latam comes from another id (%s).', 'payu-latam-woocommerce'), $posted['merchant_id'] ); 
  628. $this->msg['class'] = 'woocommerce-error'; 
  629.  
  630. // Payment details 
  631. if ( ! empty( $posted['email_buyer'] ) ) 
  632. update_post_meta( $order->id, __('PayU Latam Client email', 'payu-latam-woocommerce'), $posted['email_buyer'] ); 
  633. if ( ! empty( $posted['transaction_id'] ) ) 
  634. update_post_meta( $order->id, __('Transaction ID', 'payu-latam-woocommerce'), $posted['transaction_id'] ); 
  635. if ( ! empty( $posted['reference_pol'] ) ) 
  636. update_post_meta( $order->id, __('Trasability Code', 'payu-latam-woocommerce'), $posted['reference_pol'] ); 
  637. if ( ! empty( $posted['sign'] ) ) 
  638. update_post_meta( $order->id, __('Tash Code', 'payu-latam-woocommerce'), $posted['sign'] ); 
  639. if ( ! empty( $posted['ip'] ) ) 
  640. update_post_meta( $order->id, __('Transaction IP', 'payu-latam-woocommerce'), $posted['ip'] ); 
  641.  
  642. update_post_meta( $order->id, __('Extra Data', 'payu-latam-woocommerce'), 'response_code_pol: '.$posted['response_code_pol'].' - '.'state_pol: '.$posted['state_pol'].' - '.'payment_method: '.$posted['payment_method'].' - '.'transaction_date: '.$posted['transaction_date'].' - '.'currency: '.$posted['currency'] ); 
  643.  
  644.  
  645. if ( ! empty( $posted['payment_method_type'] ) ) 
  646. update_post_meta( $order->id, __('Payment type', 'payu-latam-woocommerce'), $posted['payment_method_type'] ); 
  647.  
  648. if ( $codes[$state] == 'APPROVED' ) { 
  649. $order->add_order_note( __( 'PayU Latam payment approved', 'payu-latam-woocommerce') ); 
  650. $this->msg['message'] = $this->msg_approved; 
  651. $this->msg['class'] = 'woocommerce-message'; 
  652. $order->payment_complete(); 
  653.  
  654. if ( 'yes' == $this->debug ) { $this->log->add( 'payulatam', __('Payment complete.', 'payu-latam-woocommerce'));} 
  655.  
  656. } else { 
  657. $order->update_status( 'on-hold', sprintf( __( 'Payment pending: %s', 'payu-latam-woocommerce'), $codes[$state] ) ); 
  658. $this->msg['message'] = $this->msg_pending; 
  659. $this->msg['class'] = 'woocommerce-info'; 
  660.  
  661.  
  662. break; 
  663. case 'DECLINED' : 
  664. case 'EXPIRED' : 
  665. case ERROR : 
  666. case 'ABANDONED_TRANSACTION': 
  667. // Order failed 
  668. $order->update_status( 'failed', sprintf( __( 'Payment rejected via PayU Latam. Error type: %s', 'payu-latam-woocommerce'), ( $codes[$state] ) ) ); 
  669. $this->msg['message'] = $this->msg_declined ; 
  670. $this->msg['class'] = 'woocommerce-error'; 
  671. break; 
  672. default : 
  673. $order->update_status( 'failed', sprintf( __( 'Payment rejected via PayU Latam.', 'payu-latam-woocommerce'), ( $codes[$state] ) ) ); 
  674. $this->msg['message'] = $this->msg_cancel ; 
  675. $this->msg['class'] = 'woocommerce-error'; 
  676. break; 
  677.  
  678.  
  679. /** 
  680. * Get order information 
  681. * @access public 
  682. * @param mixed $posted 
  683. * @return void 
  684. */ 
  685. function get_payulatam_order( $posted ) { 
  686. $custom = $posted['order_id']; 
  687.  
  688. // Backwards comp for IPN requests 
  689.  
  690. $order_id = (int) $custom; 
  691. $reference_code = ($posted['referenceCode'])?$posted['referenceCode']:$posted['reference_sale']; 
  692. $order_key = explode('-', $reference_code); 
  693. $order_key = ($order_key[0])?$order_key[0]:$order_key; 
  694.  
  695. $order = new WC_Order( $order_id ); 
  696.  
  697. if ( ! isset( $order->id ) ) { 
  698. $order_id = woocommerce_get_order_id_by_order_key( $order_key ); 
  699. $order = new WC_Order( $order_id ); 
  700.  
  701. // Validate key 
  702. if ( $order->order_key !== $order_key ) { 
  703. if ( $this->debug=='yes' ) 
  704. $this->log->add( 'payulatam', __('Error: Order Key does not match invoice.', 'payu-latam-woocommerce') ); 
  705. exit; 
  706.  
  707. return $order; 
  708.  
  709.  
  710. /** 
  711. * Check if current currency is valid for PayU Latam 
  712. * @access public 
  713. * @return bool 
  714. */ 
  715. function is_valid_currency() { 
  716. if ( ! in_array( get_woocommerce_currency(), apply_filters( 'woocommerce_payulatam_supported_currencies', array( 'CLP', 'ARS', 'BRL', 'COP', 'MXN', 'PEN', 'USD' ) ) ) ) return false; 
  717.  
  718. return true; 
  719.  
  720. /** 
  721. * Get pages for return page setting 
  722. * @access public 
  723. * @return bool 
  724. */ 
  725. function get_pages($title = false, $indent = true) { 
  726. $wp_pages = get_pages('sort_column=menu_order'); 
  727. $page_list = array('default'=>__('Default Page', 'payu-latam-woocommerce')); 
  728. if ($title) $page_list[] = $title; 
  729. foreach ($wp_pages as $page) { 
  730. $prefix = ''; 
  731. // show indented child pages? 
  732. if ($indent) { 
  733. $has_parent = $page->post_parent; 
  734. while($has_parent) { 
  735. $prefix .= ' - '; 
  736. $next_page = get_page($has_parent); 
  737. $has_parent = $next_page->post_parent; 
  738. // add to page list array array 
  739. $page_list[$page->ID] = $prefix . $page->post_title; 
  740. return $page_list; 
  741.  
  742.  
  743. /** 
  744. * Add all currencys supported by PayU Latem so it can be display  
  745. * in the woocommerce settings 
  746. * @access public 
  747. * @return bool 
  748. */ 
  749. function add_all_currency( $currencies ) { 
  750. $currencies['ARS'] = __( 'Argentine Peso', 'payu-latam-woocommerce'); 
  751. $currencies['BRL'] = __( 'Brasilian Real', 'payu-latam-woocommerce'); 
  752. $currencies['COP'] = __( 'Colombian Peso', 'payu-latam-woocommerce'); 
  753. $currencies['MXN'] = __( 'Mexican Peso', 'payu-latam-woocommerce'); 
  754. $currencies['CLP'] = __( 'Chile Peso', 'payu-latam-woocommerce'); 
  755. $currencies['PEN'] = __( 'Perubian New Sol', 'payu-latam-woocommerce'); 
  756. return $currencies; 
  757. /** 
  758. * Add simbols for all currencys in payu latam so it can be display  
  759. * in the woocommerce settings 
  760. * @access public 
  761. * @return bool 
  762. */ 
  763. function add_all_symbol( $currency_symbol, $currency ) { 
  764. switch( $currency ) { 
  765. case 'ARS': $currency_symbol = '$'; break; 
  766. case 'CLP': $currency_symbol = '$'; break; 
  767. case 'BRL': $currency_symbol = 'R$'; break; 
  768. case 'COP': $currency_symbol = '$'; break; 
  769. case 'MXN': $currency_symbol = '$'; break; 
  770. case 'PEN': $currency_symbol = 'S/.'; break; 
  771. return $currency_symbol; 
  772. /** 
  773. * Add the Gateway to WooCommerce 
  774. **/ 
  775. function woocommerce_add_payulatam_gateway($methods) { 
  776. $methods[] = 'WC_payulatam'; 
  777. return $methods; 
  778.  
  779. add_filter('woocommerce_payment_gateways', 'woocommerce_add_payulatam_gateway' );