Skip to content

Commit

Permalink
refactor: checkout blocks document validation and removed dash url va…
Browse files Browse the repository at this point in the history
…lidation
  • Loading branch information
RafaMelazzo committed Sep 30, 2024
1 parent e323e79 commit a50f53f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 80 deletions.
Binary file modified languages/woo-pagarme-payments-pt_BR.mo
Binary file not shown.
24 changes: 7 additions & 17 deletions languages/woo-pagarme-payments-pt_BR.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ msgstr ""
"Project-Id-Version: WooCommerce Pagar.me Payments 1.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/woo-pagarme-payments\n"
"POT-Creation-Date: 2018-06-22 13:58-0300\n"
"PO-Revision-Date: 2024-09-26 13:37-0300\n"
"PO-Revision-Date: 2024-09-30 12:07-0300\n"
"Last-Translator: Pagar.me\n"
"Language-Team: \n"
"Language: pt_BR\n"
Expand Down Expand Up @@ -1237,22 +1237,6 @@ msgstr ""
"Sua conta está desativada na Dash da Pagar.me. Por favor, entre em contato com o nosso time de atendimento para "
"habilitá-la."

#: src/Controller/HubAccounts.php:90 src/Controller/HubAccounts.php:96
msgid ""
"No domain registered on Pagar.me Dash. Please enter your website's domain on the Dash to be able to process payment "
"in your store."
msgstr ""
"Nenhum domínio cadastrado na Dash da Pagar.me. Por favor, insira o domínio do seu site na Dash para poder processar o "
"pagamento em sua loja."

#: src/Controller/HubAccounts.php:90 src/Controller/HubAccounts.php:96
msgid ""
"The registered domain is different from the URL of your website. Please correct the domain configured on the Dash to "
"be able to process payment in your store."
msgstr ""
"O domínio cadastrado é diferente da URL do seu site. Por favor, corrija o domínio configurado na Dash para poder "
"processar o pagamento em sua loja."

msgid ""
"Pix payment method is enabled on your store, but disabled on Pagar.me Dash. Please, access the Dash configurations "
"and enable it to be able to process Pix payment on your store."
Expand Down Expand Up @@ -1523,6 +1507,12 @@ msgctxt "checkout-document-error"
msgid "Shipping"
msgstr "envio"

msgid "Please, enter a valid document number."
msgstr "Por favor, digite um número de documento válido."

msgid "Please, enter a valid %s number."
msgstr "Por favor, digite um número de %s válido."

msgid "Please, enter a valid document number in the <b>%s Document</b>."
msgstr "Por favor, digite um número de documento válido <b>no campo Documento de %s</b>."

Expand Down
23 changes: 18 additions & 5 deletions src/Action/CustomerFieldsActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Woocommerce\Pagarme\Controller\Checkout\CustomerFields;
use Woocommerce\Pagarme\Helper\Utils;
use Woocommerce\Pagarme\Model\Config;
use WP_Error;

defined('ABSPATH') || exit;

Expand All @@ -33,8 +34,9 @@ public function run()
{
add_filter('woocommerce_checkout_init', array($this, 'enqueueScript'));
add_filter('woocommerce_checkout_fields', array($this, 'addDocumentField'));
add_action('woocommerce_init', array($this, 'addDocumentFieldOnCheckoutBlocks'));
add_action('woocommerce_checkout_process', array($this, 'validateDocument'));
add_action('woocommerce_init', array($this, 'addDocumentFieldOnCheckoutBlocks'));
add_action('woocommerce_validate_additional_field', array($this, 'validateCheckoutBlocksDocument'), 10, 3);
add_action(
'woocommerce_admin_order_data_after_billing_address',
array($this, 'displayBillingDocumentOrderMeta')
Expand Down Expand Up @@ -104,10 +106,7 @@ public function addDocumentFieldOnCheckoutBlocks()
'class' => array('form-row-wide'),
'required' => true,
'index' => 25,
'show_in_order_confirmation' => true,
'validate_callback' => function ($documentNumber) {
return $this->customerFields->validateCheckoutBlocksDocument($documentNumber);
},
'show_in_order_confirmation' => true
)
);
}
Expand All @@ -120,6 +119,20 @@ public function validateDocument()
$this->customerFields->validateDocument();
}

/**
* @param WP_Error $errors
* @param $fieldKey
* @param $documentNumber
*
* @return void
*/
public function validateCheckoutBlocksDocument(WP_Error $errors, $fieldKey, $documentNumber)
{
if ($fieldKey == 'address/document') {
$this->customerFields->validateCheckoutBlocksDocument($errors, $documentNumber);
}
}

/**
* @param $order
*
Expand Down
26 changes: 15 additions & 11 deletions src/Controller/Checkout/CustomerFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function validateDocument()

$documentNumber = preg_replace('/\D/', '', $document);

if ($this->isValidDocumentLength($documentNumber)) {
if (!$this->isValidDocumentLength($documentNumber)) {
$errorMessage = sprintf(
__(
'Please, enter a valid document number in the <b>%s Document</b>.',
Expand Down Expand Up @@ -124,7 +124,7 @@ private function isValidDocumentLength($documentNumber): bool
{
$documentLength = strlen($documentNumber);

return $documentLength !== 11 && $documentLength !== 14;
return $documentLength === 11 || $documentLength === 14;
}

/**
Expand All @@ -149,7 +149,7 @@ private function isValidDocument($documentNumber)
*/
private function getDocumentType($documentNumber): string
{
return Utils::getDocumentType($documentNumber);
return Utils::getDocumentTypeByDocumentNumber($documentNumber);
}

/**
Expand All @@ -167,25 +167,28 @@ private function getDocumentValidationFunctionName(string $documentType): string
}

/**
* @param WP_Error $errors
* @param $documentNumber
*
* @return true|WP_Error
* @return WP_Error
*/
public function validateCheckoutBlocksDocument($documentNumber)
public function validateCheckoutBlocksDocument(WP_Error $errors, $documentNumber)
{
$documentNumber = preg_replace('/\D/', '', $documentNumber);
$errorCode = 'pagarme_invalid_document';

if ($this->isValidDocumentLength($documentNumber)) {
if (!$this->isValidDocumentLength($documentNumber)) {
$errorMessage = __(
'Please, enter a valid document number.',
'woo-pagarme-payments'
);

return new WP_Error(
$errors->add(
$errorCode,
$errorMessage
);

return $errors;
}

if (!$this->isValidDocument($documentNumber)) {
Expand All @@ -198,16 +201,17 @@ public function validateCheckoutBlocksDocument($documentNumber)
strtoupper($documentType)
);

return new WP_Error(
$errors->add(
$errorCode,
$errorMessage
);

return $errors;
}

$wpError = new WP_Error();
$wpError->remove($errorCode);
$errors->remove($errorCode);

return $wpError;
return $errors;
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/Controller/HubAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,6 @@ private function getHubAccountErrorsNotices()
$noticesList = [
Account::ACCOUNT_DISABLED => 'Your account is disabled on Pagar.me Dash. '
. 'Please, contact our support team to enable it.',
Account::DOMAIN_EMPTY => [
'message' => 'No domain registered on Pagar.me Dash. Please enter your website\'s domain on the Dash '
. 'to be able to process payment in your store.',
'buttons' => $this->getHubNoticeButtons('account-config')
],
Account::DOMAIN_INCORRECT => [
'message' => 'The registered domain is different from the URL of your website. Please correct the '
. 'domain configured on the Dash to be able to process payment in your store.',
'buttons' => $this->getHubNoticeButtons('account-config')
],
Account::WEBHOOK_INCORRECT => [
'message' => 'The URL for receiving webhook registered in Pagar.me Dash is different from the URL of '
. 'your website. Please, click the button below to access the Hub and click the Delete > Confirm '
Expand Down
6 changes: 3 additions & 3 deletions src/Helper/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public static function build_document_from_order(Order $order)

if (!empty($order->get_meta('billing_document'))) {
$document = $order->get_meta('billing_document');
$documentType = self::getCustomerType($document);
$documentType = self::getCustomerTypeByDocumentNumber($document);
return array(
'type' => $documentType,
'value' => $document,
Expand All @@ -518,13 +518,13 @@ public static function build_document_from_order(Order $order)
);
}

public static function getCustomerType($document): string
public static function getCustomerTypeByDocumentNumber($document): string
{
$documentNumber = preg_replace('/\D/', '', $document ?? '');
return strlen($documentNumber) === 14 ? 'company' : 'individual';
}

public static function getDocumentType($document): string
public static function getDocumentTypeByDocumentNumber($document): string
{
$documentNumber = preg_replace('/\D/', '', $document ?? '');
return strlen($documentNumber) === 14 ? 'cnpj' : 'cpf';
Expand Down
2 changes: 1 addition & 1 deletion src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function extractCustomerDataByWcOrder($wcOrder)
'email' => $billingData['email'],
'name' => $billingData['first_name'] . ' ' . $billingData['last_name'],
'document' => $document,
'document_type' => Utils::getDocumentType($document),
'document_type' => Utils::getDocumentTypeByDocumentNumber($document),
'home_phone' => $billingData['phone'],
'mobile_phone' => $wcOrder->get_meta('_billing_cellphone'),
'street' => $billingData['street'],
Expand Down
33 changes: 0 additions & 33 deletions vendor/pagarme/ecommerce-module-core/src/Middle/Model/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class Account extends ModelWithErrors
{
const ACCOUNT_DISABLED = 'accountDisabled';

const DOMAIN_EMPTY = 'domainEmpty';

const DOMAIN_INCORRECT = 'domainIncorrect';

const WEBHOOK_INCORRECT = 'webhookIncorrect';

const MULTIPAYMENTS_DISABLED = 'multiPaymentsDisabled';
Expand Down Expand Up @@ -242,7 +238,6 @@ public function setDebitCardSettings($debitCardSettings)
public function validate($storeSettings = null)
{
$this->validateAccountEnabled();
$this->validateDomain($storeSettings);
$this->validateWebhooks($storeSettings);
$this->validateMultiBuyer();
$this->validateMultiPayments();
Expand All @@ -265,34 +260,6 @@ private function validateAccountEnabled()
}
}

/**
* @param StoreSettings|null $storeSettings
* @return void
*/
private function validateDomain($storeSettings = null)
{
$domains = $this->getDomains();
if (empty($domains) && (empty($storeSettings) || !$storeSettings->isSandbox())) {
$this->addError(self::DOMAIN_EMPTY);
return;
}

if ($this->canNotValidateUrlSetting($storeSettings)) {
return;
}

$siteUrls = $storeSettings->getStoreUrls();
foreach ($domains as $domain) {
foreach ($siteUrls as $siteUrl) {
if (strpos($siteUrl, $domain) !== false) {
return;
}
}
}

$this->addError(self::DOMAIN_INCORRECT);
}

/**
* @param StoreSettings|null $storeSettings
* @return void
Expand Down

0 comments on commit a50f53f

Please sign in to comment.