Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add option 0 for quantity interest free installments #445

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Concrete/WoocommerceCoreSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
31 changes: 15 additions & 16 deletions src/Model/CardInstallments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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
Expand Down
110 changes: 2 additions & 108 deletions src/Model/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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(
'<option value="1">%1$s</option>',
__('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('<option value="%1$s">%2$s</option>', $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('<option value="">%s</option>', __('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)
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Payment/PostFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ 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!",
"A installment times should be set between 0 and 24!",
$times
);
}
Expand Down Expand Up @@ -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;
}

Expand Down
Loading