From f24358f6e42edf761bb7cd84f9f65319d0b097c0 Mon Sep 17 00:00:00 2001
From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com>
Date: Fri, 14 Jun 2024 10:45:56 -0300
Subject: [PATCH 1/3] improve: add 0 installment with interest
---
src/Controller/Gateways/AbstractGateway.php | 2 +-
src/Model/CardInstallments.php | 31 +++--
src/Model/Gateway.php | 110 +-----------------
src/Model/Payment.php | 2 +-
src/Model/Payment/PostFormatter.php | 4 +-
.../src/Kernel/ValueObjects/Installment.php | 9 +-
6 files changed, 22 insertions(+), 136 deletions(-)
diff --git a/src/Controller/Gateways/AbstractGateway.php b/src/Controller/Gateways/AbstractGateway.php
index 37dc8ff9..da75d58e 100644
--- a/src/Controller/Gateways/AbstractGateway.php
+++ b/src/Controller/Gateways/AbstractGateway.php
@@ -201,7 +201,7 @@ public function process_payment($orderId): array
public function payment_fields()
{
$this->model->payment = $this->method;
- echo $this->gatewayBlock->setPaymentInstance($this->model->getPaymentInstace($this->method))->toHtml();
+ echo $this->gatewayBlock->setPaymentInstance($this->model->getPaymentInstance($this->method))->toHtml();
}
/**
diff --git a/src/Model/CardInstallments.php b/src/Model/CardInstallments.php
index e7745973..dc24eb75 100644
--- a/src/Model/CardInstallments.php
+++ b/src/Model/CardInstallments.php
@@ -78,17 +78,8 @@ public function getInstallmentsByType($total, $flag = false)
*/
public function getOptions($total, $maxInstallments, $minAmount, $interest, $interestIncrease, $noInterest)
{
- $firstOptionLabel = __('1x', 'woo-pagarme-payments');
- $firstOptionContent = __('1x', 'woo-pagarme-payments') . ' (' . wc_price($total) . ')';
- $options[] = [
- 'value' => 1,
- 'content' => $firstOptionContent,
- 'installmentPrice' => $this->formatPrice($total),
- 'optionLabel' => $firstOptionLabel,
- 'finalPrice' => $this->formatPrice($total),
- ];
$interestBase = $interest;
- for ($times = 2; $times <= $maxInstallments; $times++) {
+ for ($times = 1; $times <= $maxInstallments; $times++) {
$interest = $interestBase;
$amount = $total;
if ($interest || $interestIncrease) {
@@ -105,13 +96,8 @@ public function getOptions($total, $maxInstallments, $minAmount, $interest, $int
if ($price < $minAmount) {
break;
}
- $text = sprintf(
- __('%dx of %s (%s)', 'woo-pagarme-payments'),
- $times,
- wc_price($price),
- wc_price($value)
- );
+ $text = $this->getInstallmentText($times, $price, $value);
$extraText = $this->verifyInterest($times, $noInterest, $interest);
$text .= $extraText;
@@ -128,6 +114,19 @@ public function getOptions($total, $maxInstallments, $minAmount, $interest, $int
return $options;
}
+ private function getInstallmentText($times, $priceInstallment, $priceWithInterest)
+ {
+ if($times === 1) {
+ return __('1x', 'woo-pagarme-payments') . ' (' . wc_price($priceWithInterest) . ')';
+ }
+ return sprintf(
+ __('%dx of %s (%s)', 'woo-pagarme-payments'),
+ $times,
+ wc_price($priceInstallment),
+ wc_price($priceWithInterest)
+ );
+ }
+
/**
* @param int $times
* @param mixed $noInterest
diff --git a/src/Model/Gateway.php b/src/Model/Gateway.php
index b0dd4226..a74b7a37 100644
--- a/src/Model/Gateway.php
+++ b/src/Model/Gateway.php
@@ -74,7 +74,7 @@ public function getInstallmentOptions($isGatewayType = false)
$installments = [];
$installmentsAmount = $this->getInstallmentsMaximumQuantity($isGatewayType);
- for ($i = 1; $i <= $installmentsAmount; ++$i) {
+ for ($i = 0; $i <= $installmentsAmount; ++$i) {
$installments[$i] = $i;
}
@@ -95,112 +95,6 @@ public function getSoftDescriptorMaxLength($isGatewayType)
return $isGatewayType ? 22 : 13;
}
- public function get_installments_by_type($total, $flag = false)
- {
- $flags = $this->settings->flags;
- $type = $this->settings->cc_installment_type;
- $max_installments = intval($this->settings->cc_installments_maximum);
- $min_amount = Utils::str_to_float($this->settings->cc_installments_min_amount);
- $no_interest = intval($this->settings->cc_installments_without_interest);
- $interest = Utils::str_to_float($this->settings->cc_installments_interest);
- $interest_increase = Utils::str_to_float($this->settings->cc_installments_interest_increase);
-
- $method = '_calc_installments_' . $type;
-
- return $this->{$method}(
- compact('max_installments', 'min_amount', 'no_interest', 'interest', 'interest_increase', 'total', 'flag')
- );
- }
-
- /** phpcs:disable */
- public function render_installments_options($total, $max_installments, $min_amount, $interest, $interest_increase, $no_interest)
- {
- $output = sprintf(
- '',
- __('1x', 'woo-pagarme-payments') . ' (' . wc_price($total) . ')'
- );
-
- $interestBase = $interest;
-
- for ($times = 2; $times <= $max_installments; $times++) {
- $interest = $interestBase;
- $amount = $total;
-
- if ($interest || $interest_increase) {
-
- if ($interest_increase && $times > $no_interest + 1) {
- $interest += ($interest_increase * ($times - ($no_interest + 1)));
- }
-
- $amount += Utils::calc_percentage($interest, $total);
- }
-
- $value = $amount;
-
- if ($times <= $no_interest) {
- $value = $total;
- }
-
- $price = ceil($value / $times * 100) / 100;
- if ($price < $min_amount) {
- break;
- }
- $text = sprintf(
- __('%dx of %s (%s)', 'woo-pagarme-payments'),
- $times,
- wc_price($price),
- wc_price($value)
- );
-
- $text .= $this->verifyInterest($times, $no_interest, $interest);
-
- $output .= sprintf('', $times, $text);
- }
-
- return $output;
- }
-
- /**
- * @param int $times
- * @param mixed $no_interest
- * @param mixed $interest
- * @return string
- */
- public function verifyInterest(int $times, $no_interest, $interest): string
- {
- if ($times > $no_interest && $interest) {
- return " c/juros";
- }
-
- return " s/juros";
- }
-
- private function _calc_installments_1(array $params)
- {
- extract($params, EXTR_SKIP);
-
- return $this->render_installments_options($total, $max_installments, $min_amount, $interest, $interest_increase, $no_interest);
- }
-
- private function _calc_installments_2(array $params)
- {
- $settings_by_flag = $this->settings->cc_installments_by_flag;
-
- extract($params, EXTR_SKIP);
-
- if (!$flag || !isset($settings_by_flag['max_installment'][$flag])) {
- return sprintf('', __('This card brand not is allowed on checkout.', Core::SLUG));
- }
-
- $max_installments = intval($settings_by_flag['max_installment'][$flag]);
- $min_amount = Utils::str_to_float($settings_by_flag['installment_min_amount'][$flag]);
- $no_interest = intval($settings_by_flag['no_interest'][$flag]);
- $interest = Utils::str_to_float($settings_by_flag['interest'][$flag]);
- $interest_increase = Utils::str_to_float($settings_by_flag['interest_increase'][$flag]);
-
- return $this->render_installments_options($total, $max_installments, $min_amount, $interest, $interest_increase, $no_interest);
- }
-
public function get_hub_button_text($hub_install_id)
{
return !empty($hub_install_id)
@@ -271,7 +165,7 @@ public function is_sandbox_mode(): bool
* @return PaymentInterface
* @throws Exception
*/
- public function getPaymentInstace($paymentCode)
+ public function getPaymentInstance($paymentCode)
{
foreach ($this->getPayments() as $class) {
/** @var PaymentInterface $payment */
diff --git a/src/Model/Payment.php b/src/Model/Payment.php
index a40319c9..87dda85c 100644
--- a/src/Model/Payment.php
+++ b/src/Model/Payment.php
@@ -43,7 +43,7 @@ public function __construct(
$gateway = new Gateway;
}
$this->gateway = $gateway;
- $this->paymentInstance = $this->gateway->getPaymentInstace($paymentMethod);
+ $this->paymentInstance = $this->gateway->getPaymentInstance($paymentMethod);
}
/**
diff --git a/src/Model/Payment/PostFormatter.php b/src/Model/Payment/PostFormatter.php
index 60bd9c4e..a914fff8 100644
--- a/src/Model/Payment/PostFormatter.php
+++ b/src/Model/Payment/PostFormatter.php
@@ -108,7 +108,7 @@ public function formatReactCheckout()
private function dataToFilterFromPost($paymentMethod)
{
if ($paymentMethod) {
- return $this->gateway->getPaymentInstace($paymentMethod)->getRequirementsData();
+ return $this->gateway->getPaymentInstance($paymentMethod)->getRequirementsData();
}
return $_POST;
}
@@ -144,7 +144,7 @@ private function renameFieldsFromFormattedPost($formattedPost, $paymentMethod)
$arrayFieldKey
);
if ($paymentMethod) {
- $formattedPost = $this->gateway->getPaymentInstace($paymentMethod)->renameFieldsPost($field, $formattedPost, $arrayFieldKey);
+ $formattedPost = $this->gateway->getPaymentInstance($paymentMethod)->renameFieldsPost($field, $formattedPost, $arrayFieldKey);
}
}
return $formattedPost;
diff --git a/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php b/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
index c9203ca0..6b295ac0 100644
--- a/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
+++ b/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
@@ -85,14 +85,7 @@ private function setBaseTotal($baseTotal)
*/
private function setInterest($interest)
{
- $newInterest = floatval($interest);
- if ($newInterest < 0) {
- throw new InvalidParamException(
- "A installment interest should be greater or equal to 0!",
- $interest
- );
- }
- $this->interest = $newInterest;
+ $this->interest = floatval($interest);
return $this;
}
From c673f6ddcaf6f20e1737b024fe2c201045cfce12 Mon Sep 17 00:00:00 2001
From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com>
Date: Fri, 14 Jun 2024 17:40:16 -0300
Subject: [PATCH 2/3] fix: module core error
---
src/Concrete/WoocommerceCoreSetup.php | 2 +-
.../src/Kernel/ValueObjects/Installment.php | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Concrete/WoocommerceCoreSetup.php b/src/Concrete/WoocommerceCoreSetup.php
index 41518628..31467d73 100644
--- a/src/Concrete/WoocommerceCoreSetup.php
+++ b/src/Concrete/WoocommerceCoreSetup.php
@@ -323,7 +323,7 @@ private static function getBrandConfig($storeConfig)
true,
CardBrand::$brandMethod(),
(!empty($max) ? $max : 1),
- (!empty($maxWithout) ? $maxWithout : 1),
+ (!empty($maxWithout) ? $maxWithout : 0),
$initial,
$incremental,
(!empty($minValue) ? $minValue : 0) * 100
diff --git a/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php b/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
index 6b295ac0..9815e17c 100644
--- a/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
+++ b/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
@@ -48,7 +48,7 @@ public function __construct($times, $baseTotal, $interest)
private function setTimes($times)
{
$newTimes = intval($times);
- if ($newTimes < 1 || $newTimes > 24) {
+ if ($newTimes < 0 || $newTimes > 24) {
throw new InvalidParamException(
"A installment times should be set between 1 and 24!",
$times
From aa03176b7fba2ae9165368a1645c7db22b754a85 Mon Sep 17 00:00:00 2001
From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com>
Date: Mon, 24 Jun 2024 15:24:49 -0300
Subject: [PATCH 3/3] fix: incorrect message
---
.../src/Kernel/ValueObjects/Installment.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php b/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
index 9815e17c..2e9d4f6e 100644
--- a/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
+++ b/vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php
@@ -50,7 +50,7 @@ private function setTimes($times)
$newTimes = intval($times);
if ($newTimes < 0 || $newTimes > 24) {
throw new InvalidParamException(
- "A installment times should be set between 1 and 24!",
+ "A installment times should be set between 0 and 24!",
$times
);
}