From ad37240d44705738ca566992fa5f7b577f7e142a Mon Sep 17 00:00:00 2001 From: Emilio Davis Date: Mon, 17 Aug 2015 11:02:06 -0300 Subject: [PATCH] fix chequeo de wc --- class-wc-khipu.php | 762 +++++++++++++++++++++-------------------- class-wc-khipubacs.php | 633 +++++++++++++++++----------------- readme.txt | 2 +- 3 files changed, 714 insertions(+), 683 deletions(-) diff --git a/class-wc-khipu.php b/class-wc-khipu.php index bdb56c6..e7aa353 100644 --- a/class-wc-khipu.php +++ b/class-wc-khipu.php @@ -8,218 +8,229 @@ * Plugin Name: WooCommerce khipu * Plugin URI: https://khipu.com * Description: khipu payment gateway for woocommerce - * Version: 2.2 + * Version: 2.3 * Author: khipu * Author URI: https://khipu.com */ add_action('plugins_loaded', 'woocommerce_khipu_init', 0); - function woocommerce_khipu_showWooCommerceNeeded() { - woocommerce_khipu_showMessage("Debes instalar y activar el plugin WooCommerce. El plugin de khipu se deshabilitará hasta que esto este corregido.", true); + woocommerce_khipu_showMessage("Debes instalar y activar el plugin WooCommerce. El plugin de khipu se deshabilitará hasta que esto este corregido.", + true); } function woocommerce_khipu_orderReceivedHasSpaces() { - woocommerce_khipu_showMessage("El 'endpoint' de Pedido recibido tiene espacios, debe ser una palabra sin espacios, para corregirlo anda a WooCommerce->Ajustes->Finalizar compra y corrige el valor en el campo 'Pedido recibido'. El plugin de khipu se deshabilitará hasta que esto este corregido.", true); + woocommerce_khipu_showMessage("El 'endpoint' de Pedido recibido tiene espacios, debe ser una palabra sin espacios, para corregirlo anda a WooCommerce->Ajustes->Finalizar compra y corrige el valor en el campo 'Pedido recibido'. El plugin de khipu se deshabilitará hasta que esto este corregido.", + true); } - function woocommerce_khipu_showMessage($message, $errormsg = false) { if ($errormsg) { echo '
'; - } - else { + } else { echo '
'; } echo "

$message

"; } - function woocommerce_khipu_init() { require_once "lib/lib-khipu/src/Khipu.php"; - $orderReceived = isset( WC()->query->query_vars[ 'order-received' ] ) ? WC()->query->query_vars[ 'order-received' ] : 'order-received'; - if (!class_exists('WC_Payment_Gateway')) { add_action('admin_notices', 'woocommerce_khipu_showWooCommerceNeeded'); - } else if (strpos($orderReceived, ' ') !== false){ + return; + } + + $orderReceived = + isset(WC()->query->query_vars['order-received']) ? WC()->query->query_vars['order-received'] : 'order-received'; + + if (strpos($orderReceived, ' ') !== false) { add_action('admin_notices', 'woocommerce_khipu_orderReceivedHasSpaces'); - } else { + return; + } - class WC_Gateway_khipu extends WC_Payment_Gateway - { + class WC_Gateway_khipu extends WC_Payment_Gateway + { - var $notify_url; - - /** - * Constructor for the gateway. - * - */ - public function __construct() - { - $this->id = 'khipu'; - $this->icon = plugins_url('images/buttons/110x25-transparent.png', __FILE__); - $this->has_fields = false; - $this->method_title = __('khipu - Transferencia simplificada', 'woocommerce'); - $this->notify_url = add_query_arg('wc-api', 'WC_Gateway_' . $this->id, home_url('/')); - - // Load the settings and init variables. - $this->init_form_fields(); - $this->init_settings(); - $this->title = $this->get_option('title'); - $this->description = $this->get_option('description'); - $this->receiver_id = $this->get_option('receiver_id'); - $this->secret = $this->get_option('secret'); - - // Actions - add_action('valid-' . $this->id . '-ipn-request', array($this, 'successful_request')); - add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); - add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); - - // Payment listener/API hook - add_action('woocommerce_api_wc_gateway_' . $this->id, array($this, 'check_ipn_response')); - - if (!$this->is_valid_for_use()) { - $this->enabled = false; - } + var $notify_url; + + /** + * Constructor for the gateway. + * + */ + public function __construct() + { + $this->id = 'khipu'; + $this->icon = plugins_url('images/buttons/110x25-transparent.png', __FILE__); + $this->has_fields = false; + $this->method_title = __('khipu - Transferencia simplificada', 'woocommerce'); + $this->notify_url = add_query_arg('wc-api', 'WC_Gateway_' . $this->id, home_url('/')); + + // Load the settings and init variables. + $this->init_form_fields(); + $this->init_settings(); + $this->title = $this->get_option('title'); + $this->description = $this->get_option('description'); + $this->receiver_id = $this->get_option('receiver_id'); + $this->secret = $this->get_option('secret'); + + // Actions + add_action('valid-' . $this->id . '-ipn-request', array($this, 'successful_request')); + add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, + array($this, 'process_admin_options')); + + // Payment listener/API hook + add_action('woocommerce_api_wc_gateway_' . $this->id, array($this, 'check_ipn_response')); + + if (!$this->is_valid_for_use()) { + $this->enabled = false; } + } - /** - * Check if this gateway is enabled and available in the user's country - */ - function is_valid_for_use() - { - if (!in_array(get_woocommerce_currency(), apply_filters('woocommerce_' . $this->id . '_supported_currencies', array('CLP')))) { - return false; - } - return true; + /** + * Check if this gateway is enabled and available in the user's country + */ + function is_valid_for_use() + { + if (!in_array(get_woocommerce_currency(), + apply_filters('woocommerce_' . $this->id . '_supported_currencies', array('CLP'))) + ) { + return false; } + return true; + } - /** - * Admin Panel Options - */ - public function admin_options() - { + /** + * Admin Panel Options + */ + public function admin_options() + { + ?> +

+

+ + is_valid_for_use()) : ?> + + generate_settings_html(); ?> -

-

- - is_valid_for_use()) : ?> -
- generate_settings_html(); - ?> -
- - -
-

- : -

-
+ + + +
+

+ : +

+
form_fields = array( - 'enabled' => array( - 'title' => __('Enable/Disable', 'woocommerce'), - 'type' => 'checkbox', - 'label' => __('Enable khipu', 'woocommerce'), - 'default' => 'yes' - ), - 'title' => array( - 'title' => __('Title', 'woocommerce'), - 'type' => 'text', - 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'), - 'default' => __('Transferencia simplificada', 'woocommerce'), - 'desc_tip' => true - ), - 'description' => array( - 'title' => __('Description', 'woocommerce'), - 'type' => 'textarea', - 'description' => __('Payment method description that the customer will see on your checkout.', 'woocommerce'), - - 'default' => __('khipu es una aplicación simple y segura para pagar con' - .'tu banco a través de una transferencia, evita errores' - .'al escribir datos y brinda protección adicional contra' - .'algunos tipos de ataque, como lo son el Phishing y la' - .'clonación de datos. Si no has instalado la aplicación,' - .'la página de pago te ayudará a instalarla. ESTA ES LA' - .'OPCIÓN RECOMENDADA.', 'woocommerce') - ), - 'receiver_id' => array( - 'title' => __('Id de cobrador', 'woocommerce'), - 'type' => 'text', - 'description' => __('Ingrese su Id de cobrador. Se obtiene en https://khipu.com/merchant/profile', 'woocommerce'), - 'default' => '', - 'desc_tip' => true - ), - 'secret' => array( - 'title' => __('Llave', 'woocommerce'), - 'type' => 'text', - 'description' => __('Ingrese su llave secreta. Se obtiene en https://khipu.com/merchant/profile', 'woocommerce'), - 'default' => '', - 'desc_tip' => true - ) - ); + /** + * Initialise Gateway Settings Form Fields + */ + function init_form_fields() + { + $this->form_fields = array( + 'enabled' => array( + 'title' => __('Enable/Disable', 'woocommerce'), + 'type' => 'checkbox', + 'label' => __('Enable khipu', 'woocommerce'), + 'default' => 'yes' + ), + 'title' => array( + 'title' => __('Title', 'woocommerce'), + 'type' => 'text', + 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'), + 'default' => __('Transferencia simplificada', 'woocommerce'), + 'desc_tip' => true + ), + 'description' => array( + 'title' => __('Description', 'woocommerce'), + 'type' => 'textarea', + 'description' => __('Payment method description that the customer will see on your checkout.', + 'woocommerce'), + 'default' => __('khipu es una aplicación simple y segura para pagar con' + . 'tu banco a través de una transferencia, evita errores' + . 'al escribir datos y brinda protección adicional contra' + . 'algunos tipos de ataque, como lo son el Phishing y la' + . 'clonación de datos. Si no has instalado la aplicación,' + . 'la página de pago te ayudará a instalarla. ESTA ES LA' + . 'OPCIÓN RECOMENDADA.', 'woocommerce') + ), + 'receiver_id' => array( + 'title' => __('Id de cobrador', 'woocommerce'), + 'type' => 'text', + 'description' => __('Ingrese su Id de cobrador. Se obtiene en https://khipu.com/merchant/profile', + 'woocommerce'), + 'default' => '', + 'desc_tip' => true + ), + 'secret' => array( + 'title' => __('Llave', 'woocommerce'), + 'type' => 'text', + 'description' => __('Ingrese su llave secreta. Se obtiene en https://khipu.com/merchant/profile', + 'woocommerce'), + 'default' => '', + 'desc_tip' => true + ) + ); - } + } - /** - * Get banks for this receiver_id - */ - function get_available_banks() - { - $Khipu = new Khipu(); - $Khipu->authenticate($this->receiver_id, $this->secret); - $Khipu->setAgent('woocommerce-khipu-2.2;;'.site_url().';;'.bloginfo('name')); - $service = $Khipu->loadService('ReceiverBanks'); - return $service->consult(); - } + /** + * Get banks for this receiver_id + */ + function get_available_banks() + { + $Khipu = new Khipu(); + $Khipu->authenticate($this->receiver_id, $this->secret); + $Khipu->setAgent('woocommerce-khipu-2.3;;' . site_url() . ';;' . bloginfo('name')); + $service = $Khipu->loadService('ReceiverBanks'); + return $service->consult(); + } - function comm_error() - { - $msg = __('Error de comunicación con khipu, por favor intente nuevamente más tarde.'); - return "
$msg
"; - } + function comm_error() + { + $msg = __('Error de comunicación con khipu, por favor intente nuevamente más tarde.'); + return "
$msg
"; + } - /** - * Create the combos to select bank and bank type. - */ - function generate_khipu_bankselect() - { + /** + * Create the combos to select bank and bank type. + */ + function generate_khipu_bankselect() + { - $banks = json_decode($this->get_available_banks()); + $banks = json_decode($this->get_available_banks()); - if (!$banks) { - return $this->comm_error(); - } + if (!$banks) { + return $this->comm_error(); + } - $bankSelector = "
\n"; + $bankSelector = "\n"; - foreach ($_REQUEST as $key => $value) { - $bankSelector .= "\n"; - } + foreach ($_REQUEST as $key => $value) { + $bankSelector .= "\n"; + } - $send_label = __('Pagar'); - $bank_selector_label = __('Selecciona tu banco:'); - $bankSelector .= << @@ -241,16 +252,16 @@ function generate_khipu_bankselect() bankRootSelect.attr("disabled", "disabled"); EOD; - foreach ($banks->banks as $bank) { - if (!$bank->parent) { - $bankSelector .= "bankRootSelect.append('');\n"; - $bankSelector .= "bankOptions['$bank->id'] = [];\n"; - $bankSelector .= "bankOptions['$bank->id'].push('')\n"; - } else { - $bankSelector .= "bankOptions['$bank->parent'].push('');\n"; - } + foreach ($banks->banks as $bank) { + if (!$bank->parent) { + $bankSelector .= "bankRootSelect.append('');\n"; + $bankSelector .= "bankOptions['$bank->id'] = [];\n"; + $bankSelector .= "bankOptions['$bank->id'].push('')\n"; + } else { + $bankSelector .= "bankOptions['$bank->parent'].push('');\n"; } - $bankSelector .= << EOD; - return $bankSelector; - } + return $bankSelector; + } - function generate_khipu_terminal_page() - { + function generate_khipu_terminal_page() + { - $json_string = $this->base64url_decode_uncompress($_REQUEST['payment-data']); + $json_string = $this->base64url_decode_uncompress($_REQUEST['payment-data']); - $response = json_decode($json_string); + $response = json_decode($json_string); - $readyForTerminal = 'ready-for-terminal'; + $readyForTerminal = 'ready-for-terminal'; - if (!$response->$readyForTerminal) { - wp_redirect($response->url); - return; - } + if (!$response->$readyForTerminal) { + wp_redirect($response->url); + return; + } - // Add the external libraries - wp_enqueue_script('atmosphere', '//cdnjs.cloudflare.com/ajax/libs/atmosphere/2.1.2/atmosphere.min.js'); - wp_enqueue_script('khipu-js', '//storage.googleapis.com/installer/khipu-1.1.js', array('jquery')); + // Add the external libraries + wp_enqueue_script('atmosphere', '//cdnjs.cloudflare.com/ajax/libs/atmosphere/2.1.2/atmosphere.min.js'); + wp_enqueue_script('khipu-js', '//storage.googleapis.com/installer/khipu-1.1.js', array('jquery')); - $waitMsg = __('Estamos iniciando el terminal de pagos khipu, por favor espera unos minutos.
No cierres esta página, una vez que completes el pago serás redirigido automáticamente.'); - $out = <<No cierres esta página, una vez que completes el pago serás redirigido automáticamente.'); + $out = <<$waitMsg
EOD; - return $out; - } + return $out; + } - /** - * Create the payment on khipu and try to start the app. - */ - function generate_khipu_generate_payment($order_id) - { + /** + * Create the payment on khipu and try to start the app. + */ + function generate_khipu_generate_payment($order_id) + { - $order = new WC_Order($order_id); + $order = new WC_Order($order_id); - $Khipu = new Khipu(); - $Khipu->authenticate($this->receiver_id, $this->secret); - $Khipu->setAgent('woocommerce-khipu-2.2;;'.site_url().';;'.bloginfo('name')); - $create_page_service = $Khipu->loadService('CreatePaymentURL'); + $Khipu = new Khipu(); + $Khipu->authenticate($this->receiver_id, $this->secret); + $Khipu->setAgent('woocommerce-khipu-2.3;;' . site_url() . ';;' . bloginfo('name')); + $create_page_service = $Khipu->loadService('CreatePaymentURL'); - $item_names = array(); + $item_names = array(); - if (sizeof($order->get_items()) > 0) { - foreach ($order->get_items() as $item) { - if ($item['qty']) { - $item_names[] = $item['name'] . ' x ' . $item['qty']; - } + if (sizeof($order->get_items()) > 0) { + foreach ($order->get_items() as $item) { + if ($item['qty']) { + $item_names[] = $item['name'] . ' x ' . $item['qty']; } } + } - $create_page_service->setParameter('subject', 'Orden ' . $order->get_order_number() . ' - ' . get_bloginfo('name')); - $create_page_service->setParameter('body', implode(', ', $item_names)); - $create_page_service->setParameter('amount', number_format($order->get_total(), 0, ',', '')); - $create_page_service->setParameter('transaction_id', ltrim($order->get_order_number(), '#')); - $create_page_service->setParameter('custom', serialize(array($order_id, $order->order_key))); - $create_page_service->setParameter('payer_email', $order->billing_email); - $create_page_service->setParameter('notify_url', $this->notify_url); - $create_page_service->setParameter('bank_id', $_REQUEST['bank-id']); - $create_page_service->setParameter('return_url', $this->get_return_url($order)); - - // We need the string json to use it with the khipu.js - $json_string = $create_page_service->createUrl(); - - if (!$json_string) { - return $this->comm_error(); - } - + $create_page_service->setParameter('subject', + 'Orden ' . $order->get_order_number() . ' - ' . get_bloginfo('name')); + $create_page_service->setParameter('body', implode(', ', $item_names)); + $create_page_service->setParameter('amount', number_format($order->get_total(), 0, ',', '')); + $create_page_service->setParameter('transaction_id', ltrim($order->get_order_number(), '#')); + $create_page_service->setParameter('custom', serialize(array($order_id, $order->order_key))); + $create_page_service->setParameter('payer_email', $order->billing_email); + $create_page_service->setParameter('notify_url', $this->notify_url); + $create_page_service->setParameter('bank_id', $_REQUEST['bank-id']); + $create_page_service->setParameter('return_url', $this->get_return_url($order)); + + // We need the string json to use it with the khipu.js + $json_string = $create_page_service->createUrl(); + + if (!$json_string) { + return $this->comm_error(); + } - wp_redirect(add_query_arg(array('payment-data' => $this->base64url_encode_compress($json_string)), remove_query_arg(array('bank-id'), wp_get_referer()))); + wp_redirect(add_query_arg(array('payment-data' => $this->base64url_encode_compress($json_string)), + remove_query_arg(array('bank-id'), wp_get_referer()))); - return; - } + return; + } - function base64url_encode_compress($data) { - return rtrim(strtr(base64_encode(gzcompress($data)), '+/', '-_'), '='); - } + function base64url_encode_compress($data) + { + return rtrim(strtr(base64_encode(gzcompress($data)), '+/', '-_'), '='); + } - function base64url_decode_uncompress($data) { - return gzuncompress(base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT))); - } + function base64url_decode_uncompress($data) + { + return gzuncompress(base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', + STR_PAD_RIGHT))); + } - /** - * Process the payment and return the result - */ - function process_payment($order_id){ - $order = new WC_Order($order_id); - return array('result' => 'success', 'redirect' => $order->get_checkout_payment_url( true )); - } + /** + * Process the payment and return the result + */ + function process_payment($order_id) + { + $order = new WC_Order($order_id); + return array('result' => 'success', 'redirect' => $order->get_checkout_payment_url(true)); + } - /** - * Output for the order received page. - */ - function receipt_page($order) - { - if (isset($_REQUEST['payment-data'])) { - echo $this->generate_khipu_terminal_page(); - } else if (isset($_REQUEST['bank-id'])) { + /** + * Output for the order received page. + */ + function receipt_page($order) + { + if (isset($_REQUEST['payment-data'])) { + echo $this->generate_khipu_terminal_page(); + } else { + if (isset($_REQUEST['bank-id'])) { echo $this->generate_khipu_generate_payment($order); } else { echo $this->generate_khipu_bankselect(); } } + } - /** - * Get order from Khipu IPN - **/ - function get_order_from_ipn() - { - - $_POST = array_map('stripslashes', $_POST); + /** + * Get order from Khipu IPN + **/ + function get_order_from_ipn() + { - $api_version = $_POST['api_version']; + $_POST = array_map('stripslashes', $_POST); + $api_version = $_POST['api_version']; - if($api_version == '1.2') { - return $this->get_order_from_ipn_1_2(); - } else if($api_version == '1.3') { + if ($api_version == '1.2') { + return $this->get_order_from_ipn_1_2(); + } else { + if ($api_version == '1.3') { return $this->get_order_from_ipn_1_3(); } - return false; - } + return false; - /** - * Get order from Khipu IPN API 1.2 - **/ - function get_order_from_ipn_1_2() { - $Khipu = new Khipu(); - $Khipu->authenticate($this->receiver_id, $this->secret); - $Khipu->setAgent('woocommerce-khipu-2.2;;'.site_url().';;'.bloginfo('name')); - $service = $Khipu->loadService('VerifyPaymentNotification'); - $service->setDataFromPost(); - if ($_POST['receiver_id'] != $this->receiver_id) { - return false; - } + } - $verify = $service->verify(); - if($verify['response'] == 'VERIFIED'){ - return $this->get_khipu_order($_POST['custom'], $_POST['transaction_id']); - } + /** + * Get order from Khipu IPN API 1.2 + **/ + function get_order_from_ipn_1_2() + { + $Khipu = new Khipu(); + $Khipu->authenticate($this->receiver_id, $this->secret); + $Khipu->setAgent('woocommerce-khipu-2.3;;' . site_url() . ';;' . bloginfo('name')); + $service = $Khipu->loadService('VerifyPaymentNotification'); + $service->setDataFromPost(); + if ($_POST['receiver_id'] != $this->receiver_id) { return false; } - /** - * Get order from Khipu IPN API 1.3 - **/ - function get_order_from_ipn_1_3() { - $Khipu = new Khipu(); - $Khipu->authenticate($this->receiver_id, $this->secret); - $Khipu->setAgent('woocommerce-khipu-2.2;;'.site_url().';;'.bloginfo('name')); - $service = $Khipu->loadService('GetPaymentNotification'); - $service->setDataFromPost(); - $response = json_decode($service->consult()); - if ($response->receiver_id != $this->receiver_id) { - return false; - } - $order = $this->get_khipu_order($response->custom, $response->transaction_id); + $verify = $service->verify(); + if ($verify['response'] == 'VERIFIED') { + return $this->get_khipu_order($_POST['custom'], $_POST['transaction_id']); + } + return false; + } - if($order) { - return $order; - } + /** + * Get order from Khipu IPN API 1.3 + **/ + function get_order_from_ipn_1_3() + { + $Khipu = new Khipu(); + $Khipu->authenticate($this->receiver_id, $this->secret); + $Khipu->setAgent('woocommerce-khipu-2.3;;' . site_url() . ';;' . bloginfo('name')); + $service = $Khipu->loadService('GetPaymentNotification'); + $service->setDataFromPost(); + $response = json_decode($service->consult()); + if ($response->receiver_id != $this->receiver_id) { return false; + } + $order = $this->get_khipu_order($response->custom, $response->transaction_id); + if ($order) { + return $order; } + return false; - /** - * Check for Khipu IPN Response - */ - function check_ipn_response() - { - @ob_clean(); + } - if(empty($_POST) || empty($_POST['api_version'])){ - wp_die("khipu notification validation invalid"); - } + /** + * Check for Khipu IPN Response + */ + function check_ipn_response() + { + @ob_clean(); + if (empty($_POST) || empty($_POST['api_version'])) { + wp_die("khipu notification validation invalid"); + } - $order = $this->get_order_from_ipn(); - - if($order) { - header('HTTP/1.1 200 OK'); - do_action("valid-khipu-ipn-request", $order); - return; - } + $order = $this->get_order_from_ipn(); + if ($order) { + header('HTTP/1.1 200 OK'); + do_action("valid-khipu-ipn-request", $order); + return; } + } - /** - * Successful Payment - */ - function successful_request($order) - { - if ($order->status == 'completed') { - exit; - } - $order->add_order_note(__('Pago con khipu verificado', 'woocommerce')); - $order->payment_complete(); + + /** + * Successful Payment + */ + function successful_request($order) + { + if ($order->status == 'completed') { + exit; } + $order->add_order_note(__('Pago con khipu verificado', 'woocommerce')); + $order->payment_complete(); + } - /** - * get_khipu_order function. - */ - function get_khipu_order($custom, $transaction_id) - { - $custom = maybe_unserialize($custom); - - // Backwards comp for IPN requests - if (is_numeric($custom)) { - $order_id = (int)$custom; - $order_key = $transaction_id; - } elseif (is_string($custom)) { - $order_id = (int)str_replace($this->invoice_prefix, '', $custom); - $order_key = $custom; - } else { - list($order_id, $order_key) = $custom; - } + /** + * get_khipu_order function. + */ + function get_khipu_order($custom, $transaction_id) + { + $custom = maybe_unserialize($custom); + + // Backwards comp for IPN requests + if (is_numeric($custom)) { + $order_id = (int)$custom; + $order_key = $transaction_id; + } elseif (is_string($custom)) { + $order_id = (int)str_replace($this->invoice_prefix, '', $custom); + $order_key = $custom; + } else { + list($order_id, $order_key) = $custom; + } - $order = new WC_Order($order_id); + $order = new WC_Order($order_id); - if (!isset($order->id)) { - $order_id = woocommerce_get_order_id_by_order_key($order_key); - $order = new WC_Order($order_id); - } + if (!isset($order->id)) { + $order_id = woocommerce_get_order_id_by_order_key($order_key); + $order = new WC_Order($order_id); + } - // Validate key - if ($order->order_key !== $order_key) { - if ($this->debug == 'yes') { - $this->log->add('paypal', 'Error: Order Key does not match invoice.'); - } - exit; + // Validate key + if ($order->order_key !== $order_key) { + if ($this->debug == 'yes') { + $this->log->add('paypal', 'Error: Order Key does not match invoice.'); } - - return $order; + exit; } + return $order; } - /** - * Add the Gateway to WooCommerce - **/ - function woocommerce_add_khipu_gateway($methods) - { - $methods[] = 'WC_Gateway_khipu'; - return $methods; - } + } - add_filter('woocommerce_payment_gateways', 'woocommerce_add_khipu_gateway'); + /** + * Add the Gateway to WooCommerce + **/ + function woocommerce_add_khipu_gateway($methods) + { + $methods[] = 'WC_Gateway_khipu'; + return $methods; + } - function woocommerce_khipu_add_clp_currency($currencies) - { - $currencies["CLP"] = __('Pesos Chilenos'); - return $currencies; - } + add_filter('woocommerce_payment_gateways', 'woocommerce_add_khipu_gateway'); - function woocommerce_khipu_add_clp_currency_symbol($currency_symbol, $currency) - { - switch ($currency) { - case 'CLP': - $currency_symbol = '$'; - break; - } - return $currency_symbol; - } + function woocommerce_khipu_add_clp_currency($currencies) + { + $currencies["CLP"] = __('Pesos Chilenos'); + return $currencies; + } - add_filter('woocommerce_currencies', 'woocommerce_khipu_add_clp_currency', 10, 1); - add_filter('woocommerce_currency_symbol', 'woocommerce_khipu_add_clp_currency_symbol', 10, 2); + function woocommerce_khipu_add_clp_currency_symbol($currency_symbol, $currency) + { + switch ($currency) { + case 'CLP': + $currency_symbol = '$'; + break; + } + return $currency_symbol; } + add_filter('woocommerce_currencies', 'woocommerce_khipu_add_clp_currency', 10, 1); + add_filter('woocommerce_currency_symbol', 'woocommerce_khipu_add_clp_currency_symbol', 10, 2); + } diff --git a/class-wc-khipubacs.php b/class-wc-khipubacs.php index e9e5d5f..1db4c67 100644 --- a/class-wc-khipubacs.php +++ b/class-wc-khipubacs.php @@ -8,417 +8,428 @@ * Plugin Name: WooCommerce khipubacs * Plugin URI: https://khipu.com * Description: khipu powered direct transfer payment gateway for woocommerce - * Version: 2.2 + * Version: 2.3 * Author: khipu * Author URI: https://khipu.com */ add_action('plugins_loaded', 'woocommerce_khipubacs_init', 0); - function woocommerce_khipubacs_showWooCommerceNeeded() { - woocommerce_khipubacs_showMessage("Debes instalar y activar el plugin WooCommerce. El plugin de khipu se deshabilitará hasta que esto este corregido.", true); + woocommerce_khipubacs_showMessage("Debes instalar y activar el plugin WooCommerce. El plugin de khipu se deshabilitará hasta que esto este corregido.", + true); } function woocommerce_khipubacs_orderReceivedHasSpaces() { - woocommerce_khipu_showMessage("El 'endpoint' de Pedido recibido tiene espacios, debe ser una palabra sin espacios, para corregirlo anda a WooCommerce->Ajustes->Finalizar compra y corrige el valor en el campo 'Pedido recibido'. El plugin de khipu se deshabilitará hasta que esto este corregido.", true); + woocommerce_khipu_showMessage("El 'endpoint' de Pedido recibido tiene espacios, debe ser una palabra sin espacios, para corregirlo anda a WooCommerce->Ajustes->Finalizar compra y corrige el valor en el campo 'Pedido recibido'. El plugin de khipu se deshabilitará hasta que esto este corregido.", + true); } - function woocommerce_khipubacs_showMessage($message, $errormsg = false) { if ($errormsg) { echo '
'; - } - else { + } else { echo '
'; } echo "

$message

"; } - function woocommerce_khipubacs_init() { require_once "lib/lib-khipu/src/Khipu.php"; - $orderReceived = isset( WC()->query->query_vars[ 'order-received' ] ) ? WC()->query->query_vars[ 'order-received' ] : 'order-received'; - - if (!class_exists('WC_Payment_Gateway')) { - add_action('admin_notices', 'woocommerce_khipubacs_showWooCommerceNeeded'); + return; + } - } else if (strpos($orderReceived, ' ') !== false){ + $orderReceived = + isset(WC()->query->query_vars['order-received']) ? WC()->query->query_vars['order-received'] : 'order-received'; + if (strpos($orderReceived, ' ') !== false) { add_action('admin_notices', 'woocommerce_khipubacs_orderReceivedHasSpaces'); - } else { + return; + } - class WC_Gateway_khipubacs extends WC_Payment_Gateway - { - var $notify_url; - - /** - * Constructor for the gateway. - * - */ - public function __construct() - { - $this->id = 'khipubacs'; - //$this->icon = plugins_url('images/buttons/50x25.png', __FILE__); - $this->has_fields = false; - $this->method_title = __('Trasferencia normal', 'woocommerce'); - $this->notify_url = add_query_arg('wc-api', 'WC_Gateway_' . $this->id, home_url('/')); - - // Load the settings and init variables. - $this->init_form_fields(); - $this->init_settings(); - $this->title = $this->get_option('title'); - $this->description = $this->get_option('description'); - $this->receiver_id = $this->get_option('receiver_id'); - $this->secret = $this->get_option('secret'); - - // Actions - add_action('valid-' . $this->id . '-ipn-request', array($this, 'successful_request')); - add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); - add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); - - // Payment listener/API hook - add_action('woocommerce_api_wc_gateway_' . $this->id, array($this, 'check_ipn_response')); - - if (!$this->is_valid_for_use()) { - $this->enabled = false; - } + class WC_Gateway_khipubacs extends WC_Payment_Gateway + { + + var $notify_url; + + /** + * Constructor for the gateway. + * + */ + public function __construct() + { + $this->id = 'khipubacs'; + //$this->icon = plugins_url('images/buttons/50x25.png', __FILE__); + $this->has_fields = false; + $this->method_title = __('Trasferencia normal', 'woocommerce'); + $this->notify_url = add_query_arg('wc-api', 'WC_Gateway_' . $this->id, home_url('/')); + + // Load the settings and init variables. + $this->init_form_fields(); + $this->init_settings(); + $this->title = $this->get_option('title'); + $this->description = $this->get_option('description'); + $this->receiver_id = $this->get_option('receiver_id'); + $this->secret = $this->get_option('secret'); + + // Actions + add_action('valid-' . $this->id . '-ipn-request', array($this, 'successful_request')); + add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, + array($this, 'process_admin_options')); + + // Payment listener/API hook + add_action('woocommerce_api_wc_gateway_' . $this->id, array($this, 'check_ipn_response')); + + if (!$this->is_valid_for_use()) { + $this->enabled = false; } + } - /** - * Check if this gateway is enabled and available in the user's country - */ - function is_valid_for_use() - { - if (!in_array(get_woocommerce_currency(), apply_filters('woocommerce_' . $this->id . '_supported_currencies', array('CLP')))) { - return false; - } - return true; + /** + * Check if this gateway is enabled and available in the user's country + */ + function is_valid_for_use() + { + if (!in_array(get_woocommerce_currency(), + apply_filters('woocommerce_' . $this->id . '_supported_currencies', array('CLP'))) + ) { + return false; } + return true; + } - /** - * Admin Panel Options - */ - public function admin_options() - { + /** + * Admin Panel Options + */ + public function admin_options() + { + ?> +

+

+ + is_valid_for_use()) : ?> + + generate_settings_html(); ?> -

-

- - is_valid_for_use()) : ?> -
- generate_settings_html(); - ?> -
- - -
-

- : -

-
+ + + +
+

+ : +

+
form_fields = array( - 'enabled' => array( - 'title' => __('Enable/Disable', 'woocommerce'), - 'type' => 'checkbox', - 'label' => __('Enable khipu', 'woocommerce'), - 'default' => 'yes' - ), - 'title' => array( - 'title' => __('Title', 'woocommerce'), - 'type' => 'text', - 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'), - 'default' => __('Transferencia normal', 'woocommerce'), - 'desc_tip' => true - ), - 'description' => array( - 'title' => __('Description', 'woocommerce'), - 'type' => 'textarea', - 'description' => __('Payment method description that the customer will see on your checkout.', 'woocommerce'), - 'default' => __('Debes ingresar el rut de la cuenta corriente o vista con' - .'que pagarás, luego se te entregarán todos los datos' - .'para que puedas realizar la transferencia desde el' - .'portal web o móvil de tu banco. Debes tener cuidado, el' - .'monto a transferir debe ser el que se te ha informado' - .'que realices, así el pago se procesará con éxito.') - ), - 'receiver_id' => array( - 'title' => __('Id de cobrador', 'woocommerce'), - 'type' => 'text', - 'description' => __('Ingrese su Id de cobrador. Se obtiene en https://khipu.com/merchant/profile', 'woocommerce'), - 'default' => '', - 'desc_tip' => true - ), - 'secret' => array( - 'title' => __('Llave', 'woocommerce'), - 'type' => 'text', - 'description' => __('Ingrese su llave secreta. Se obtiene en https://khipu.com/merchant/profile', 'woocommerce'), - 'default' => '', - 'desc_tip' => true - ) - ); + /** + * Initialise Gateway Settings Form Fields + */ + function init_form_fields() + { + $this->form_fields = array( + 'enabled' => array( + 'title' => __('Enable/Disable', 'woocommerce'), + 'type' => 'checkbox', + 'label' => __('Enable khipu', 'woocommerce'), + 'default' => 'yes' + ), + 'title' => array( + 'title' => __('Title', 'woocommerce'), + 'type' => 'text', + 'description' => __('This controls the title which the user sees during checkout.', + 'woocommerce'), + 'default' => __('Transferencia normal', 'woocommerce'), + 'desc_tip' => true + ), + 'description' => array( + 'title' => __('Description', 'woocommerce'), + 'type' => 'textarea', + 'description' => __('Payment method description that the customer will see on your checkout.', + 'woocommerce'), + 'default' => __('Debes ingresar el rut de la cuenta corriente o vista con' + . 'que pagarás, luego se te entregarán todos los datos' + . 'para que puedas realizar la transferencia desde el' + . 'portal web o móvil de tu banco. Debes tener cuidado, el' + . 'monto a transferir debe ser el que se te ha informado' + . 'que realices, así el pago se procesará con éxito.') + ), + 'receiver_id' => array( + 'title' => __('Id de cobrador', 'woocommerce'), + 'type' => 'text', + 'description' => __('Ingrese su Id de cobrador. Se obtiene en https://khipu.com/merchant/profile', + 'woocommerce'), + 'default' => '', + 'desc_tip' => true + ), + 'secret' => array( + 'title' => __('Llave', 'woocommerce'), + 'type' => 'text', + 'description' => __('Ingrese su llave secreta. Se obtiene en https://khipu.com/merchant/profile', + 'woocommerce'), + 'default' => '', + 'desc_tip' => true + ) + ); - } + } - function comm_error() - { - $msg = __('Error de comunicación con khipu, por favor intente nuevamente más tarde.'); - return "
$msg
"; - } + function comm_error() + { + $msg = __('Error de comunicación con khipu, por favor intente nuevamente más tarde.'); + return "
$msg
"; + } - /** - * Create the payment on khipu and try to start the app. - */ - function generate_khipubacs_submit_button($order_id) - { + /** + * Create the payment on khipu and try to start the app. + */ + function generate_khipubacs_submit_button($order_id) + { - $order = new WC_Order($order_id); + $order = new WC_Order($order_id); - $Khipu = new Khipu(); - $Khipu->authenticate($this->receiver_id, $this->secret); - $Khipu->setAgent('woocommerce-khipubacs-2.2;;'.site_url().';;'.bloginfo('name')); - $create_page_service = $Khipu->loadService('CreatePaymentURL'); + $Khipu = new Khipu(); + $Khipu->authenticate($this->receiver_id, $this->secret); + $Khipu->setAgent('woocommerce-khipubacs-2.3;;' . site_url() . ';;' . bloginfo('name')); + $create_page_service = $Khipu->loadService('CreatePaymentURL'); - $item_names = array(); + $item_names = array(); - if (sizeof($order->get_items()) > 0) { - foreach ($order->get_items() as $item) { - if ($item['qty']) { - $item_names[] = $item['name'] . ' x ' . $item['qty']; - } + if (sizeof($order->get_items()) > 0) { + foreach ($order->get_items() as $item) { + if ($item['qty']) { + $item_names[] = $item['name'] . ' x ' . $item['qty']; } } + } - $create_page_service->setParameter('subject', 'Orden ' . $order->get_order_number() . ' - ' . get_bloginfo('name')); - $create_page_service->setParameter('body', implode(', ', $item_names)); - $create_page_service->setParameter('amount', number_format($order->get_total(), 0, ',', '')); - $create_page_service->setParameter('transaction_id', ltrim($order->get_order_number(), '#')); - $create_page_service->setParameter('custom', serialize(array($order_id, $order->order_key))); - $create_page_service->setParameter('payer_email', $order->billing_email); - $create_page_service->setParameter('notify_url', $this->notify_url); - $create_page_service->setParameter('bank_id', ''); - $create_page_service->setParameter('return_url', $this->get_return_url($order)); - - $json_string = $create_page_service->createUrl(); - $response = json_decode($json_string); - - if (!$response) { - return $this->comm_error(); - } - - $manualUrl = 'manual-url'; + $create_page_service->setParameter('subject', + 'Orden ' . $order->get_order_number() . ' - ' . get_bloginfo('name')); + $create_page_service->setParameter('body', implode(', ', $item_names)); + $create_page_service->setParameter('amount', number_format($order->get_total(), 0, ',', '')); + $create_page_service->setParameter('transaction_id', ltrim($order->get_order_number(), '#')); + $create_page_service->setParameter('custom', serialize(array($order_id, $order->order_key))); + $create_page_service->setParameter('payer_email', $order->billing_email); + $create_page_service->setParameter('notify_url', $this->notify_url); + $create_page_service->setParameter('bank_id', ''); + $create_page_service->setParameter('return_url', $this->get_return_url($order)); + + $json_string = $create_page_service->createUrl(); + $response = json_decode($json_string); + + if (!$response) { + return $this->comm_error(); + } - wp_redirect($response->$manualUrl); - return; + $manualUrl = 'manual-url'; - } + wp_redirect($response->$manualUrl); + return; - /** - * Process the payment and return the result - */ - function process_payment($order_id){ - $order = new WC_Order($order_id); - return array('result' => 'success', 'redirect' => $order->get_checkout_payment_url( true )); - } + } - /** - * Output for the order received page. - */ - function receipt_page($order) - { - echo $this->generate_khipubacs_submit_button($order); - } + /** + * Process the payment and return the result + */ + function process_payment($order_id) + { + $order = new WC_Order($order_id); + return array('result' => 'success', 'redirect' => $order->get_checkout_payment_url(true)); + } - /** - * Get order from Khipu IPN - **/ - function get_order_from_ipn() - { + /** + * Output for the order received page. + */ + function receipt_page($order) + { + echo $this->generate_khipubacs_submit_button($order); + } - $_POST = array_map('stripslashes', $_POST); + /** + * Get order from Khipu IPN + **/ + function get_order_from_ipn() + { - $api_version = $_POST['api_version']; + $_POST = array_map('stripslashes', $_POST); + $api_version = $_POST['api_version']; - if($api_version == '1.2') { - return $this->get_order_from_ipn_1_2(); - } else if($api_version == '1.3') { + if ($api_version == '1.2') { + return $this->get_order_from_ipn_1_2(); + } else { + if ($api_version == '1.3') { return $this->get_order_from_ipn_1_3(); } - return false; - } + return false; - /** - * Get order from Khipu IPN API 1.2 - **/ - function get_order_from_ipn_1_2() { - $Khipu = new Khipu(); - $Khipu->authenticate($this->receiver_id, $this->secret); - $Khipu->setAgent('woocommerce-khipubacs-2.2;;'.site_url().';;'.bloginfo('name')); - $service = $Khipu->loadService('VerifyPaymentNotification'); - $service->setDataFromPost(); - if ($_POST['receiver_id'] != $this->receiver_id) { - return false; - } + } - $verify = $service->verify(); - if($verify['response'] == 'VERIFIED'){ - return $this->get_khipubacs_order($_POST['custom'], $_POST['transaction_id']); - } + /** + * Get order from Khipu IPN API 1.2 + **/ + function get_order_from_ipn_1_2() + { + $Khipu = new Khipu(); + $Khipu->authenticate($this->receiver_id, $this->secret); + $Khipu->setAgent('woocommerce-khipubacs-2.3;;' . site_url() . ';;' . bloginfo('name')); + $service = $Khipu->loadService('VerifyPaymentNotification'); + $service->setDataFromPost(); + if ($_POST['receiver_id'] != $this->receiver_id) { return false; } - /** - * Get order from Khipu IPN API 1.3 - **/ - function get_order_from_ipn_1_3() { - $Khipu = new Khipu(); - $Khipu->authenticate($this->receiver_id, $this->secret); - $Khipu->setAgent('woocommerce-khipubacs-2.2;;'.site_url().';;'.bloginfo('name')); - $service = $Khipu->loadService('GetPaymentNotification'); - $service->setDataFromPost(); - $response = json_decode($service->consult()); - if ($response->receiver_id != $this->receiver_id) { - return false; - } - $order = $this->get_khipubacs_order($response->custom, $response->transaction_id); + $verify = $service->verify(); + if ($verify['response'] == 'VERIFIED') { + return $this->get_khipubacs_order($_POST['custom'], $_POST['transaction_id']); + } + return false; + } - if($order) { - return $order; - } + /** + * Get order from Khipu IPN API 1.3 + **/ + function get_order_from_ipn_1_3() + { + $Khipu = new Khipu(); + $Khipu->authenticate($this->receiver_id, $this->secret); + $Khipu->setAgent('woocommerce-khipubacs-2.3;;' . site_url() . ';;' . bloginfo('name')); + $service = $Khipu->loadService('GetPaymentNotification'); + $service->setDataFromPost(); + $response = json_decode($service->consult()); + if ($response->receiver_id != $this->receiver_id) { return false; - } + $order = $this->get_khipubacs_order($response->custom, $response->transaction_id); - /** - * Check for Khipu IPN Response - */ - function check_ipn_response() - { - @ob_clean(); - - if(empty($_POST) || empty($_POST['api_version'])){ - wp_die("khipu notification validation invalid"); - } - + if ($order) { + return $order; + } + return false; - $order = $this->get_order_from_ipn(); + } - if($order) { - header('HTTP/1.1 200 OK'); - do_action("valid-khipubacs-ipn-request", $order); - return; - } + /** + * Check for Khipu IPN Response + */ + function check_ipn_response() + { + @ob_clean(); + if (empty($_POST) || empty($_POST['api_version'])) { + wp_die("khipu notification validation invalid"); } - /** - * Successful Payment - */ - function successful_request($order) - { - if ($order->status == 'completed') { - exit; - } - $order->add_order_note(__('Pago con khipubacs verificado', 'woocommerce')); - $order->payment_complete(); + $order = $this->get_order_from_ipn(); + + if ($order) { + header('HTTP/1.1 200 OK'); + do_action("valid-khipubacs-ipn-request", $order); + return; } + } + /** + * Successful Payment + */ + function successful_request($order) + { + if ($order->status == 'completed') { + exit; + } + $order->add_order_note(__('Pago con khipubacs verificado', 'woocommerce')); + $order->payment_complete(); + } - /** - * get_khipu_order function. - */ - function get_khipubacs_order($custom, $transaction_id) - { - $custom = maybe_unserialize($custom); + /** + * get_khipu_order function. + */ + function get_khipubacs_order($custom, $transaction_id) + { + $custom = maybe_unserialize($custom); + + // Backwards comp for IPN requests + if (is_numeric($custom)) { + $order_id = (int)$custom; + $order_key = $transaction_id; + } elseif (is_string($custom)) { + $order_id = (int)str_replace($this->invoice_prefix, '', $custom); + $order_key = $custom; + } else { + list($order_id, $order_key) = $custom; + } - // Backwards comp for IPN requests - if (is_numeric($custom)) { - $order_id = (int)$custom; - $order_key = $transaction_id; - } elseif (is_string($custom)) { - $order_id = (int)str_replace($this->invoice_prefix, '', $custom); - $order_key = $custom; - } else { - list($order_id, $order_key) = $custom; - } + $order = new WC_Order($order_id); + if (!isset($order->id)) { + $order_id = woocommerce_get_order_id_by_order_key($order_key); $order = new WC_Order($order_id); + } - if (!isset($order->id)) { - $order_id = woocommerce_get_order_id_by_order_key($order_key); - $order = new WC_Order($order_id); + // Validate key + if ($order->order_key !== $order_key) { + if ($this->debug == 'yes') { + $this->log->add('paypal', 'Error: Order Key does not match invoice.'); } - - // Validate key - if ($order->order_key !== $order_key) { - if ($this->debug == 'yes') { - $this->log->add('paypal', 'Error: Order Key does not match invoice.'); - } - exit; - } - - return $order; + exit; } + return $order; } - /** - * Add the Gateway to WooCommerce - **/ - function woocommerce_add_khipubacs_gateway($methods) - { - $methods[] = 'WC_Gateway_khipubacs'; - return $methods; - } + } - add_filter('woocommerce_payment_gateways', 'woocommerce_add_khipubacs_gateway'); + /** + * Add the Gateway to WooCommerce + **/ + function woocommerce_add_khipubacs_gateway($methods) + { + $methods[] = 'WC_Gateway_khipubacs'; + return $methods; + } - function woocommerce_khipubacs_add_clp_currency($currencies) - { - $currencies["CLP"] = __('Pesos Chilenos'); - return $currencies; - } + add_filter('woocommerce_payment_gateways', 'woocommerce_add_khipubacs_gateway'); - function woocommerce_khipubacs_add_clp_currency_symbol($currency_symbol, $currency) - { - switch ($currency) { - case 'CLP': - $currency_symbol = '$'; - break; - } - return $currency_symbol; + function woocommerce_khipubacs_add_clp_currency($currencies) + { + $currencies["CLP"] = __('Pesos Chilenos'); + return $currencies; + } + + function woocommerce_khipubacs_add_clp_currency_symbol($currency_symbol, $currency) + { + switch ($currency) { + case 'CLP': + $currency_symbol = '$'; + break; } + return $currency_symbol; + } - add_filter('woocommerce_currencies', 'woocommerce_khipubacs_add_clp_currency', 10, 1); - add_filter('woocommerce_currency_symbol', 'woocommerce_khipubacs_add_clp_currency_symbol', 10, 2); + add_filter('woocommerce_currencies', 'woocommerce_khipubacs_add_clp_currency', 10, 1); + add_filter('woocommerce_currency_symbol', 'woocommerce_khipubacs_add_clp_currency_symbol', 10, 2); - } } diff --git a/readme.txt b/readme.txt index 4432a32..dd018be 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: Tags: payment gateway, khipu, woocommerce, chile Requires at least: 3.3 Tested up to: 4.1 -Stable tag: 2.2 +Stable tag: 2.3 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html