Advertisement
ess_sebastian

payment

Mar 6th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.88 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /*
  5. Plugin Name: Plataforma de pago PagosOnline - Colombia
  6. Plugin URI: https://github.com/8manos/woocommerce-gateway-pagosonline
  7. Description: Plugin desarrollado para procesar pagos con WooCommerce utilizando la plataforma de pago seguro de PagosOnline.
  8. Version: 1.2
  9. Author: ess_sebastian
  10. */
  11.  
  12.  
  13. /* ##########################################################################################################################
  14. Copyright 2012 8manos S.A.S (email : plugins@8manos.com)
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License, version 2, as
  18. published by the Free Software Foundation.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program; if not, write to the Free Software
  27. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28.  
  29. ##########################################################################################################################
  30. */
  31.  
  32. add_action('plugins_loaded', 'inicia_plataforma_pagosonline', 0);
  33.  
  34. function inicia_plataforma_pagosonline() {
  35. if ( !class_exists( 'woocommerce_payment_gateway' ) ) return;
  36. class woocommerce_pagos extends woocommerce_payment_gateway {
  37. public function __construct() {
  38. global $woocommerce;
  39. $this->id = 'pagosonline';
  40. $this->icon = plugins_url(basename(dirname(__FILE__))."/images/pagos.png");
  41. $this->has_fields = false;
  42.  
  43. // Si se va a realizar pagos reales activar la siguiente dirección para el action
  44.  
  45. if ($this->testmode == '0' ) {
  46.  
  47. $this->purchaseurl = "https://gateway.pagosonline.net/apps/gateway/index.html";
  48.  
  49. // Si se van a hacer pruebas, activar la siguiente dirección
  50. }
  51. else
  52. {
  53. $this->purchaseurl = "https://gateway.pagosonline.net/apps/gateway/index.html";
  54.  
  55. }
  56.  
  57.  
  58. $this->init_form_fields();
  59. $this->init_settings();
  60.  
  61. $this->enabled = $this->settings['enabled'];
  62. $this->title = $this->settings['title'];
  63. $this->descripcion = $this->settings['Description'];
  64. $this->usuarioId = $this->settings['usuarioId'];
  65. $this->firma =$this->settings['firma'];
  66. $this->iva = $this->settings['iva'];
  67. $this->debugmode = $this->settings['debugmode'];
  68. $this->debugmode_email = $this->settings['debugmode_email'];
  69. $this->testmode = $this->settings['testmode'];
  70.  
  71. add_action( 'init', array(&$this, 'check_pagos_response') );
  72. add_action( 'valid-pagosonline-request', array(&$this, 'successful_request') );
  73. add_action( 'woocommerce_receipt_pagosonline', array(&$this, 'receipt_page') );
  74. add_action( 'woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options') );
  75. }
  76. //Información de los campos de configuración
  77. function init_form_fields() {
  78. $this->form_fields = array(
  79. 'enabled' => array(
  80. 'title' => __( 'Habilitar/Deshabilitar', 'woothemes' ),
  81. 'type' => 'checkbox',
  82. 'label' => __( 'Habilita el pago con PagosOnline', 'woothemes' ),
  83. 'default' => 'yes'
  84. ),
  85. 'title' => array(
  86. 'title' => __( 'Titulo', 'woothemes' ),
  87. 'type' => 'text',
  88. 'description' => __( 'Este sera el titulo que se mostrara a tus clientes al momento del pago.', 'woothemes' ),
  89. 'default' => __( 'Pagos', 'woothemes' )
  90. ),
  91. 'Description'=>array(
  92. 'title' => __( 'Descripcion', 'woothemes' ),
  93. 'type' => 'text',
  94. 'description' => __( 'Ingresa la descripcion del pago que va a realizar el cliente;', 'woothemes' ),
  95. 'default' => 'Pruebas'
  96. ),
  97. 'usuarioId'=>array(
  98. 'title' => __( 'Numero de cuenta en PagosOnline', 'woothemes' ),
  99. 'type' => 'text',
  100. 'description' => __( 'Ingresa tu numero de cuenta de PagosOnline; este numero te lo entregaran una vez creada tu cuenta en PagosOnline', 'woothemes' ),
  101. 'default' => ''
  102. ),
  103. 'testmode'=>array(
  104. 'title' => __( 'Estado del pago', 'woothemes' ),
  105. 'type' => 'select',
  106. 'options' => array('1'=>'Pruebas', '0'=>'Funcionamiento normal'),
  107. 'description' => __( '<br/>Utiliza la opcion <b>Pruebas</b>, cuando estes realizando pagos ficticios para comprobar el funcionamiento del sitio. Una vez listo, utiliza <b>Funcionamiento normal</b> para comenzar a recibir pagos.', 'woothemes' ),
  108. 'default' => '1'
  109. ),
  110.  
  111. 'firma' => array(
  112. 'title' => __( 'Firma', 'woothemes' ),
  113. 'type' => 'text',
  114. 'description' => __( 'Ingresa la firma secreta que se te entregara una vez creada tu cuenta de PagosOnline.', 'woothemes' ),
  115. 'default' => ''
  116. ),
  117. 'iva' => array(
  118. 'title' => __( 'Iva', 'woothemes' ),
  119. 'type' => 'text',
  120. 'description' => __( 'La tasa de IVA que utilizan tus productos', 'woothemes' ),
  121. 'default' => ''
  122. ),
  123. 'debugmode_email' => array(
  124. 'title' => __( 'Email para errores', 'woothemes' ),
  125. 'type' => 'text',
  126. 'description' => __( 'Si hay algun problema, a que correo te enviamos la notificacion?', 'woothemes' ),
  127. 'default' => get_bloginfo('admin_email')
  128. )
  129. );
  130.  
  131. } // Terminamos de editar los campos
  132.  
  133.  
  134. public function admin_options() {
  135.  
  136. ?>
  137. <h3><?php _e('Plataforma de pagos con PagosOnline', 'woothemes'); ?></h3>
  138.  
  139. <?php _e('<p>Esta plataforma de pago funciona enviando los datos del cliente a la plataforma de pago seguro de PagosOnline, una vez alli, se genera una respuesta y esta vuelve nuevamente al carrito de compra original alojado en woocommerce.</p>', 'woothemes'); ?>
  140.  
  141.  
  142. <table class="form-table">
  143. <?php
  144. $this->generate_settings_html();
  145. ?>
  146. </table><!--/.form-table-->
  147. <?php
  148. }
  149. function payment_fields() {
  150. if ($this->description) echo wpautop(wptexturize($this->description));
  151. }
  152.  
  153. /**
  154. * Generamos el boton de pago para integrarlo en el checkout
  155. **/
  156. public function generate_pagos_form( $order_id ) {
  157. global $woocommerce;
  158. $order = &new woocommerce_order( $order_id );
  159. $oitems = unserialize($order->order_custom_fields['_order_items'][0]);
  160. $moneda = "COP";
  161.  
  162. $order_currency = $order->order_custom_fields[0];
  163. $shipping_name = explode(' ', $order->shipping_method);
  164. $llave_encripcion = "$this->firma";
  165. $refVenta = "$order_id";
  166. $baseDevolucionIva= $oitems[0]['line_total'];
  167. $iva= $oitems[0]['line_subtotal_tax'];
  168. $valor= $order->order_total;
  169. $firma_cadena = "$llave_encripcion~$this->usuarioId~$refVenta~$valor~$moneda";
  170. $pagos_args = array_merge(
  171.  
  172.  
  173.  
  174. array(
  175.  
  176. $iva1 = $valor/1.16,
  177. $iva2 = $iva1*0.16,
  178. $ivafinal = $iva2,
  179.  
  180. 'usuarioId' => $this->usuarioId,
  181. 'prueba' => $this->testmode,
  182. 'refVenta' => $refVenta,
  183. 'descripcion' => $this->descripcion,
  184. 'iva' => $ivafinal,
  185. 'baseDevolucionIva' => $valor/1.16,
  186. 'emailComprador' => $this->debugmode_email,
  187. 'valor' => $valor,
  188. 'moneda' => $moneda,
  189. 'firma' => md5("$firma_cadena"),
  190. 'url_respuesta' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('woocommerce_thanks_page_id')))),
  191. //'url_confirmacion' => get_site_url()."/confirm-pagos/",
  192. 'nombreComprador' => $order->billing_first_name ." ".$order->billing_last_name,
  193. 'telefonoMovil' => $order->billing_phone,
  194.  
  195.  
  196. 'direccionCobro' => $order->billing_address_1,
  197. 'ciudadCobro' => $order->billing_city,
  198.  
  199.  
  200. )
  201. );
  202. $item_loop = 0;
  203. if (sizeof($order->get_items())>0) : foreach ($order->get_items() as $item) :
  204. $_product = $order->get_product_from_item( $item );
  205. if ($_product->exists() && $item['qty']) :
  206.  
  207. if ( $_product->is_virtual() ) :
  208. $tangible = "N";
  209. else :
  210. $tangible = "Y";
  211. endif;
  212.  
  213.  
  214. if ($order->prices_include_tax) :
  215. else :
  216. endif;
  217.  
  218.  
  219.  
  220. $item_loop++;
  221.  
  222. endif;
  223. endforeach; endif;
  224.  
  225.  
  226. if ($order->get_shipping()>0) :
  227.  
  228. if ($order->prices_include_tax) :
  229. else :
  230. endif;
  231.  
  232.  
  233. endif;
  234.  
  235. if (!$order->prices_include_tax && $order->get_total_tax()>0) :
  236.  
  237. endif;
  238.  
  239. if ($order->get_order_discount()>0) :
  240.  
  241. endif;
  242.  
  243.  
  244. if ( $this->debugmode == 'yes' && isset($this->debugmode_email) ) :
  245.  
  246. foreach ( $pagos_args as $key => $value ) {
  247. $message .= $key . '=' . $value . "\r\n";
  248. }
  249.  
  250. $message = 'Order ID: ' . $order_id . "\r\n" . "\r\n" . $message;
  251. pagos_debug_email( $this->debugmode_email, 'Pagos Debug. Sent Values Order ID: ' . $order_id, $message );
  252.  
  253. endif;
  254.  
  255. $pagos_adr = $this->purchaseurl;
  256. $pagos_args_array = array();
  257. foreach ($pagos_args as $key => $value) {
  258. $pagos_args_array[] = '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
  259. }
  260. return '<form action="'.$pagos_adr.'" method="post" id="Formulario_pago_pagosonline">
  261. ' . implode('', $pagos_args_array) . '
  262. <input type="submit" class="button-alt" id="submit_Formulario_pago_pagosonline" value="'.__('Paga seguro con Pagosonline', 'woothemes').'" /> <a class="button cancel" href="'.$order->get_cancel_order_url().'">'.__('Cancela la orden', 'woothemes').'</a>
  263. <script type="text/javascript">
  264. jQuery(function(){
  265. jQuery("body").block(
  266. {
  267. message: "<img src=\"'.$woocommerce->plugin_url().'/assets/images/ajax-loader.gif\" alt=\"Redirecting...\" />'.__('Gracias por tu orden. Ahora te estamos redireccionando a la plataforma segura de PagosOnline.', 'woothemes').'",
  268. overlayCSS:
  269. {
  270. background: "#fff",
  271. opacity: 1
  272. },
  273. css: {
  274. padding: 20,
  275. textAlign: "center",
  276. color: "#555",
  277. border: "3px solid #aaa",
  278. backgroundColor:"#fff",
  279. cursor: "wait",
  280. lineHeight: "32px"
  281. }
  282. });
  283. jQuery("#submit_Formulario_pago_pagosonline").click();
  284. });
  285. </script>
  286. </form>';
  287.  
  288. }
  289.  
  290. function process_payment( $order_id ) {
  291.  
  292. $order = &new woocommerce_order( $order_id );
  293. return array(
  294. 'result' => 'success',
  295. 'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))))
  296. );
  297.  
  298. }
  299.  
  300. function receipt_page( $order ) {
  301. echo '<p>'.__('Thank you for your order, please click the button below to pay with pagosonline.', 'woothemes').'</p>';
  302.  
  303. echo $this->generate_pagos_form( $order );
  304.  
  305. }
  306.  
  307. function check_pagos_response() {
  308.  
  309. $llave = $llave_encripcion;
  310. $usuario_id=$_REQUEST['usuario_id'];
  311. $ref_venta=$_REQUEST['ref_venta'];
  312. $ref_pol = $_REQUEST['ref_pol'];
  313. $valor=$_REQUEST['valor'];
  314. $moneda=$_REQUEST['moneda'];
  315. $estado_pol=$_REQUEST['estado_pol'];
  316. $firma_cadena= "$llave~$usuario_id~$ref_venta~$valor~$moneda~$estado_pol";$firmacreada = md5($firma_cadena);
  317. $firma =$_REQUEST['firma'];
  318. $ref_venta=$_REQUEST['ref_venta'];
  319. $fecha_procesamiento=$_REQUEST['fecha_procesamiento'];
  320. $ref_pol=$_REQUEST['ref_pol'];
  321. $cus=$_REQUEST['cus'];
  322. $extra1=$_REQUEST['extra1'];
  323. $banco_pse=$_REQUEST['banco_pse'];
  324. if($_REQUEST['estado_pol'] == 6 && $_REQUEST['codigo_respuesta_pol'] == 5)
  325. {$estadoTx = "Transacci&oacute;n fallida";}
  326. else if($_REQUEST['estado_pol'] == 6 && $_REQUEST['codigo_respuesta_pol'] == 4)
  327. {$estadoTx = "Transacci&oacute;n rechazada";}
  328. else if($_REQUEST['estado_pol'] == 12 && $_REQUEST['codigo_respuesta_pol'] == 9994)
  329. {$estadoTx = "Pendiente, Por favor revisar si el d&eacute;bito fue realizado en el Banco";}
  330. else if($_REQUEST['estado_pol'] == 4 && $_REQUEST['codigo_respuesta_pol'] == 1)
  331. {
  332. $firma = "$llave_encripcion~$usuario_id~$ref_venta~$valor~$moneda~$estado_pol";
  333. $estadoTx = "Transacci&oacute;n aprobada";
  334.  
  335.  
  336. }
  337.  
  338. else
  339. {$estadoTx=$_REQUEST['mensaje'];}
  340.  
  341. if ( isset($_REQUEST['ref_pol']) ) :
  342.  
  343. $firma = $firma_cadena;
  344. $usuarioId = $usuario_id;
  345.  
  346. if ( isset($ref_pol) ) :
  347.  
  348. $RefNr = $ref_pol;
  349. $order_number = $_REQUEST["order"];
  350. $total = $valor;
  351. $pagosMD5 = $_REQUEST["key"];
  352.  
  353. if ( $this->testmode == 'yes' ):
  354. $string_to_hash = $usuarioId . $sid . "1" . $valor;
  355. else :
  356. $string_to_hash = $usuarioId . $sid . $_REQUEST["order"] . $valor;
  357. endif;
  358.  
  359. $check_key = strtoupper(md5($string_to_hash));
  360.  
  361. $pagos_return_values = array(
  362. "check_key" => $check_key,
  363. "RefNr" => $RefNr,
  364. "sale_id" => $ref_venta,
  365. "total" => $total,
  366. "twocheckoutMD5" => $pagosMD5
  367. );
  368.  
  369.  
  370. elseif ( isset($ref_pol) ) :
  371.  
  372. $RefNr = $ref_pol;
  373. $sale_id = $refVenta;
  374. $invoice_id = $_REQUEST["invoice_id"];
  375. $pagoMD5 = $_REQUEST["md5_hash"];
  376. $vendor_id = $ref_pol;
  377.  
  378. $string_to_hash = $sale_id . $sid . $invoice_id . $secret_word;
  379. $check_key = strtoupper(md5($string_to_hash));
  380.  
  381.  
  382. $pagos_return_values = array(
  383. "check_key" => $check_key,
  384. "RefNr" => $RefNr,
  385. "sale_id" => $sale_id,
  386. "vendor_id" => $vendor_id,
  387. "twocheckoutMD5" => $pagoMD5
  388. );
  389.  
  390. endif;
  391.  
  392. if ( $this->debugmode == 'yes' && isset($this->debugmode_email) ) :
  393.  
  394. $order_id = $RefNr;
  395.  
  396. foreach ( $twocheckout_return_values as $key => $value ) {
  397. $message .= $key . '=' . $value . "\r\n";
  398. }
  399.  
  400. $message = 'Order ID: ' . $order_id . "\r\n" . "\r\n" . $message;
  401. pagos_debug_email( $this->debugmode_email, 'Pagos Debug. Return Values Order ID: ' . $order_id, $message );
  402.  
  403. endif;
  404.  
  405. if ( isset($pagos_return_values['check_key']) && $check_key == $twocheckoutMD5 ) :
  406. do_action("valid-twocheckout-request", $pagos_return_values);
  407. endif;
  408.  
  409.  
  410. endif;
  411. }
  412.  
  413. }
  414. }
  415. function add_pagosonline_gateway( $methods ) {
  416. $methods[] = 'woocommerce_pagos'; return $methods;
  417. }
  418.  
  419. add_filter('woocommerce_payment_gateways', 'add_pagosonline_gateway' );
  420.  
  421. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement