Skip to content

Commit

Permalink
Merge pull request #18 from LinkNacional/dev
Browse files Browse the repository at this point in the history
1.4.0 Armazenamento de parcelas, modo de compatibilidade e correções
  • Loading branch information
emanuellopess authored Feb 7, 2023
2 parents 6c3217b + 316e31c commit a76466e
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 164 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.4.0
* Implementação de modo de compatibilidade de validação;
* Ajustes nas descrições de configurações;
* Atualizar links de notices;
* Armazenamento de quantidade de parcelas nos metadados do pedido;
* Adicionado quantidade de parcelas na tela de obrigado e no e-mail de novo pedido.

# 1.3.2
* Correção de mensagens de validação duplicadas;
* Ajustes de carregamento de configurações;
Expand Down
13 changes: 10 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
=== Payment Gateway for Cielo API on WooCommerce ===
Contributors: linknacional
Donate link: https://www.linknacional.com.br/wordpress/plugins/
Donate link: https://www.linknacional.com.br/wordpress/woocommerce/cielo/
Tags: woocommerce, invoice, payment, paymethod, card, credit, debit
Requires at least: 5.7
Tested up to: 6.0
Stable tag: 1.3.2
Tested up to: 6.1
Stable tag: 1.4.0
Requires PHP: 7.2
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -72,6 +72,13 @@ Payment Gateway for Cielo API on WooCommerce plugin is dependent on WooCommerce

== Changelog ==

# 1.4.0
* Validation compatibility mode implementation;
* Tweaks to settings descriptions;
* Update notice links;
* Storage of the number of installments in the order metadata;
* Added amount of installments in the thank you page and in the new order email.

= 1.3.2 =
* Fixed duplicate validation messages;
* Options loading tweaks;
Expand Down
94 changes: 69 additions & 25 deletions includes/class-lkn-wc-gateway-cielo-credit.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ public function __construct() {
$this->description = $this->get_option('description');
$this->instructions = $this->get_option('instructions', $this->description);

if (function_exists('wc_get_logger')) {
$this->log = wc_get_logger();
} else {
$this->log = new WC_Logger();
}
$this->log = new WC_Logger();

// Actions.
add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
Expand Down Expand Up @@ -293,38 +289,48 @@ public function payment_fields() {
* @return boolean
*/
public function validate_fields() {
$ccnum = sanitize_text_field($_POST['lkn_ccno']);
$expDate = sanitize_text_field($_POST['lkn_cc_expdate']);
$cvv = sanitize_text_field($_POST['lkn_cc_cvc']);
$validateCompatMode = $this->get_option('input_validation_compatibility', 'no');
if ($validateCompatMode === 'no') {
$ccnum = sanitize_text_field($_POST['lkn_ccno']);
$expDate = sanitize_text_field($_POST['lkn_cc_expdate']);
$cvv = sanitize_text_field($_POST['lkn_cc_cvc']);

$validCcNumber = $this->validate_card_number($ccnum);
$validExpDate = $this->validate_exp_date($expDate);
$validCvv = $this->validate_cvv($cvv);
$validCcNumber = $this->validate_card_number($ccnum, true);
$validExpDate = $this->validate_exp_date($expDate, true);
$validCvv = $this->validate_cvv($cvv, true);

if ($validCcNumber === true && $validExpDate === true && $validCvv === true) {
return true;
if ($validCcNumber === true && $validExpDate === true && $validCvv === true) {
return true;
} else {
return false;
}
} else {
return false;
return true;
}
}

/**
* Validate card number
*
* @param string $ccnum
* @param boolean $renderNotice
*
* @return boolean
*/
private function validate_card_number($ccnum) {
private function validate_card_number($ccnum, $renderNotice) {
if (empty($ccnum)) {
$this->add_notice_once(__('Credit Card number is required!', 'lkn-wc-gateway-cielo'), 'error');
if ($renderNotice) {
$this->add_notice_once(__('Credit Card number is required!', 'lkn-wc-gateway-cielo'), 'error');
}

return false;
} else {
$isValid = !preg_match('/[^0-9\s]/', $ccnum);

if ($isValid !== true || strlen($ccnum) < 12) {
$this->add_notice_once(__('Credit Card number is invalid!', 'lkn-wc-gateway-cielo'), 'error');
if ($renderNotice) {
$this->add_notice_once(__('Credit Card number is invalid!', 'lkn-wc-gateway-cielo'), 'error');
}

return false;
} else {
Expand All @@ -337,12 +343,15 @@ private function validate_card_number($ccnum) {
* Validate card expiration date
*
* @param string $expDate
* @param boolean $renderNotice
*
* @return boolean
*/
private function validate_exp_date($expDate) {
private function validate_exp_date($expDate, $renderNotice) {
if (empty($expDate)) {
$this->add_notice_once(__('Expiration date is required!', 'lkn-wc-gateway-cielo'), 'error');
if ($renderNotice) {
$this->add_notice_once(__('Expiration date is required!', 'lkn-wc-gateway-cielo'), 'error');
}

return false;
} else {
Expand All @@ -353,14 +362,18 @@ private function validate_exp_date($expDate) {
$today = new DateTime();

if ($today > $expDate) {
$this->add_notice_once(__('Credit card is expired!', 'lkn-wc-gateway-cielo'), 'error');
if ($renderNotice) {
$this->add_notice_once(__('Credit card is expired!', 'lkn-wc-gateway-cielo'), 'error');
}

return false;
} else {
return true;
}
} catch (Exception $e) {
$this->add_notice_once(__('Expiration date is invalid!', 'lkn-wc-gateway-cielo'), 'error');
if ($renderNotice) {
$this->add_notice_once(__('Expiration date is invalid!', 'lkn-wc-gateway-cielo'), 'error');
}

return false;
}
Expand All @@ -371,19 +384,24 @@ private function validate_exp_date($expDate) {
* Validate card cvv
*
* @param string $cvv
* @param boolean $renderNotice
*
* @return boolean
*/
private function validate_cvv($cvv) {
private function validate_cvv($cvv, $renderNotice) {
if (empty($cvv)) {
$this->add_notice_once(__('CVV is required!', 'lkn-wc-gateway-cielo'), 'error');
if ($renderNotice) {
$this->add_notice_once(__('CVV is required!', 'lkn-wc-gateway-cielo'), 'error');
}

return false;
} else {
$isValid = !preg_match('/\D/', $cvv);

if ($isValid !== true || strlen($cvv) < 3) {
$this->add_notice_once(__('CVV is invalid!', 'lkn-wc-gateway-cielo'), 'error');
if ($renderNotice) {
$this->add_notice_once(__('CVV is invalid!', 'lkn-wc-gateway-cielo'), 'error');
}

return false;
} else {
Expand Down Expand Up @@ -417,6 +435,8 @@ private function get_card_provider($cardNumber) {
$brand = '';
$brand = apply_filters('lkn_wc_cielo_get_card_brand', $brand, $cardNumber);

$this->log->log('error', 'GET BRAND CIELO CARD: ' . var_export($brand, true), ['source' => 'woocommerce-cielo-credit']);

if (empty($brand)) {
// Stores regex for Card Bin Tests
$bin = [
Expand Down Expand Up @@ -504,6 +524,7 @@ public function process_payment($order_id) {
$cardNum = preg_replace('/\s/', '', sanitize_text_field($_POST['lkn_ccno']));
$cardExpSplit = explode('/', preg_replace('/\s/', '', sanitize_text_field($_POST['lkn_cc_expdate'])));
$cardExp = $cardExpSplit[0] . '/20' . $cardExpSplit[1];
$cardExpShort = $cardExpSplit[0] . '/' . $cardExpSplit[1];
$cardCvv = sanitize_text_field($_POST['lkn_cc_cvc']);
$cardName = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$installments = 1;
Expand All @@ -522,6 +543,28 @@ public function process_payment($order_id) {
$currency = $order->get_currency();
$activeInstallment = $this->get_option('installment_payment');

if ($this->validate_card_number($cardNum, false) === false) {
$message = __('Credit Card number is invalid!', 'lkn-wc-gateway-cielo');

throw new Exception($message);
} elseif ($this->validate_exp_date($cardExpShort, false) === false) {
$message = __('Expiration date is invalid!', 'lkn-wc-gateway-cielo');

throw new Exception($message);
} elseif ($this->validate_cvv($cardCvv, false) === false) {
$message = __('CVV is invalid!', 'lkn-wc-gateway-cielo');

throw new Exception($message);
} elseif (empty($merchantId)) {
$message = __('Invalid Cielo API 3.0 credentials.', 'lkn-wc-gateway-cielo');

throw new Exception($message);
} elseif (empty($merchantSecret)) {
$message = __('Invalid Cielo API 3.0 credentials.', 'lkn-wc-gateway-cielo');

throw new Exception($message);
}

// Convert the amount to equivalent in BRL
if ($currency !== 'BRL') {
$amount = apply_filters('lkn_wc_cielo_convert_amount', $amount, $currency);
Expand All @@ -548,6 +591,7 @@ public function process_payment($order_id) {
}

$order->add_order_note(__('Installments quantity', 'lkn-wc-gateway-cielo') . ' ' . $installments);
$order->add_meta_data('installments', $installments, true);

if ($this->get_option('installment_interest') === 'yes') {
$interest = $this->get_option($installments . 'x', 0);
Expand Down Expand Up @@ -590,7 +634,7 @@ public function process_payment($order_id) {
$this->log->log('error', var_export($response->get_error_messages(), true), ['source' => 'woocommerce-cielo-credit']);
}

$message = __('Order payment failed. To make a successful payment using credit card, please review the gateway settings.', 'lkn-wc-gateway-cielo');
$message = __('Order payment failed. Please review the gateway settings.', 'lkn-wc-gateway-cielo');

throw new Exception($message);
} else {
Expand Down
Loading

0 comments on commit a76466e

Please sign in to comment.