Skip to content

Commit 788b785

Browse files
feat: add option 0 for quantity interest free installments
Feat: add option 0 for quantity interest free installments
2 parents d33a891 + aa03176 commit 788b785

File tree

7 files changed

+25
-139
lines changed

7 files changed

+25
-139
lines changed

src/Concrete/WoocommerceCoreSetup.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ private static function getBrandConfig($storeConfig)
337337
true,
338338
CardBrand::$brandMethod(),
339339
(!empty($max) ? $max : 1),
340-
(!empty($maxWithout) ? $maxWithout : 1),
340+
(!empty($maxWithout) ? $maxWithout : 0),
341341
$initial,
342342
$incremental,
343343
(!empty($minValue) ? $minValue : 0) * 100

src/Controller/Gateways/AbstractGateway.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function process_payment($orderId): array
201201
public function payment_fields()
202202
{
203203
$this->model->payment = $this->method;
204-
echo $this->gatewayBlock->setPaymentInstance($this->model->getPaymentInstace($this->method))->toHtml();
204+
echo $this->gatewayBlock->setPaymentInstance($this->model->getPaymentInstance($this->method))->toHtml();
205205
}
206206

207207
/**

src/Model/CardInstallments.php

+15-16
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,8 @@ public function getInstallmentsByType($total, $flag = false)
8585
*/
8686
public function getOptions($total, $maxInstallments, $minAmount, $interest, $interestIncrease, $noInterest)
8787
{
88-
$firstOptionLabel = __('1x', 'woo-pagarme-payments');
89-
$firstOptionContent = __('1x', 'woo-pagarme-payments') . ' (' . wc_price($total) . ')';
90-
$options[] = [
91-
'value' => 1,
92-
'content' => $firstOptionContent,
93-
'installmentPrice' => $this->formatPrice($total),
94-
'optionLabel' => $firstOptionLabel,
95-
'finalPrice' => $this->formatPrice($total),
96-
];
9788
$interestBase = $interest;
98-
for ($times = 2; $times <= $maxInstallments; $times++) {
89+
for ($times = 1; $times <= $maxInstallments; $times++) {
9990
$interest = $interestBase;
10091
$amount = $total;
10192
if ($interest || $interestIncrease) {
@@ -112,13 +103,8 @@ public function getOptions($total, $maxInstallments, $minAmount, $interest, $int
112103
if ($price < $minAmount) {
113104
break;
114105
}
115-
$text = sprintf(
116-
__('%dx of %s (%s)', 'woo-pagarme-payments'),
117-
$times,
118-
wc_price($price),
119-
wc_price($value)
120-
);
121106

107+
$text = $this->getInstallmentText($times, $price, $value);
122108
$extraText = $this->verifyInterest($times, $noInterest, $interest);
123109

124110
$text .= $extraText;
@@ -135,6 +121,19 @@ public function getOptions($total, $maxInstallments, $minAmount, $interest, $int
135121
return $options;
136122
}
137123

124+
private function getInstallmentText($times, $priceInstallment, $priceWithInterest)
125+
{
126+
if($times === 1) {
127+
return __('1x', 'woo-pagarme-payments') . ' (' . wc_price($priceWithInterest) . ')';
128+
}
129+
return sprintf(
130+
__('%dx of %s (%s)', 'woo-pagarme-payments'),
131+
$times,
132+
wc_price($priceInstallment),
133+
wc_price($priceWithInterest)
134+
);
135+
}
136+
138137
/**
139138
* @param int $times
140139
* @param mixed $noInterest

src/Model/Gateway.php

+2-108
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getInstallmentOptions($isGatewayType = false)
7474
$installments = [];
7575
$installmentsAmount = $this->getInstallmentsMaximumQuantity($isGatewayType);
7676

77-
for ($i = 1; $i <= $installmentsAmount; ++$i) {
77+
for ($i = 0; $i <= $installmentsAmount; ++$i) {
7878
$installments[$i] = $i;
7979
}
8080

@@ -95,112 +95,6 @@ public function getSoftDescriptorMaxLength($isGatewayType)
9595
return $isGatewayType ? 22 : 13;
9696
}
9797

98-
public function get_installments_by_type($total, $flag = false)
99-
{
100-
$flags = $this->settings->flags;
101-
$type = $this->settings->cc_installment_type;
102-
$max_installments = intval($this->settings->cc_installments_maximum);
103-
$min_amount = Utils::str_to_float($this->settings->cc_installments_min_amount);
104-
$no_interest = intval($this->settings->cc_installments_without_interest);
105-
$interest = Utils::str_to_float($this->settings->cc_installments_interest);
106-
$interest_increase = Utils::str_to_float($this->settings->cc_installments_interest_increase);
107-
108-
$method = '_calc_installments_' . $type;
109-
110-
return $this->{$method}(
111-
compact('max_installments', 'min_amount', 'no_interest', 'interest', 'interest_increase', 'total', 'flag')
112-
);
113-
}
114-
115-
/** phpcs:disable */
116-
public function render_installments_options($total, $max_installments, $min_amount, $interest, $interest_increase, $no_interest)
117-
{
118-
$output = sprintf(
119-
'<option value="1">%1$s</option>',
120-
__('1x', 'woo-pagarme-payments') . ' (' . wc_price($total) . ')'
121-
);
122-
123-
$interestBase = $interest;
124-
125-
for ($times = 2; $times <= $max_installments; $times++) {
126-
$interest = $interestBase;
127-
$amount = $total;
128-
129-
if ($interest || $interest_increase) {
130-
131-
if ($interest_increase && $times > $no_interest + 1) {
132-
$interest += ($interest_increase * ($times - ($no_interest + 1)));
133-
}
134-
135-
$amount += Utils::calc_percentage($interest, $total);
136-
}
137-
138-
$value = $amount;
139-
140-
if ($times <= $no_interest) {
141-
$value = $total;
142-
}
143-
144-
$price = ceil($value / $times * 100) / 100;
145-
if ($price < $min_amount) {
146-
break;
147-
}
148-
$text = sprintf(
149-
__('%dx of %s (%s)', 'woo-pagarme-payments'),
150-
$times,
151-
wc_price($price),
152-
wc_price($value)
153-
);
154-
155-
$text .= $this->verifyInterest($times, $no_interest, $interest);
156-
157-
$output .= sprintf('<option value="%1$s">%2$s</option>', $times, $text);
158-
}
159-
160-
return $output;
161-
}
162-
163-
/**
164-
* @param int $times
165-
* @param mixed $no_interest
166-
* @param mixed $interest
167-
* @return string
168-
*/
169-
public function verifyInterest(int $times, $no_interest, $interest): string
170-
{
171-
if ($times > $no_interest && $interest) {
172-
return " c/juros";
173-
}
174-
175-
return " s/juros";
176-
}
177-
178-
private function _calc_installments_1(array $params)
179-
{
180-
extract($params, EXTR_SKIP);
181-
182-
return $this->render_installments_options($total, $max_installments, $min_amount, $interest, $interest_increase, $no_interest);
183-
}
184-
185-
private function _calc_installments_2(array $params)
186-
{
187-
$settings_by_flag = $this->settings->cc_installments_by_flag;
188-
189-
extract($params, EXTR_SKIP);
190-
191-
if (!$flag || !isset($settings_by_flag['max_installment'][$flag])) {
192-
return sprintf('<option value="">%s</option>', __('This card brand not is allowed on checkout.', Core::SLUG));
193-
}
194-
195-
$max_installments = intval($settings_by_flag['max_installment'][$flag]);
196-
$min_amount = Utils::str_to_float($settings_by_flag['installment_min_amount'][$flag]);
197-
$no_interest = intval($settings_by_flag['no_interest'][$flag]);
198-
$interest = Utils::str_to_float($settings_by_flag['interest'][$flag]);
199-
$interest_increase = Utils::str_to_float($settings_by_flag['interest_increase'][$flag]);
200-
201-
return $this->render_installments_options($total, $max_installments, $min_amount, $interest, $interest_increase, $no_interest);
202-
}
203-
20498
public function get_hub_button_text($hub_install_id)
20599
{
206100
return !empty($hub_install_id)
@@ -271,7 +165,7 @@ public function is_sandbox_mode(): bool
271165
* @return PaymentInterface
272166
* @throws Exception
273167
*/
274-
public function getPaymentInstace($paymentCode)
168+
public function getPaymentInstance($paymentCode)
275169
{
276170
foreach ($this->getPayments() as $class) {
277171
/** @var PaymentInterface $payment */

src/Model/Payment.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
$gateway = new Gateway;
4444
}
4545
$this->gateway = $gateway;
46-
$this->paymentInstance = $this->gateway->getPaymentInstace($paymentMethod);
46+
$this->paymentInstance = $this->gateway->getPaymentInstance($paymentMethod);
4747
}
4848

4949
/**

src/Model/Payment/PostFormatter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function formatReactCheckout()
108108
private function dataToFilterFromPost($paymentMethod)
109109
{
110110
if ($paymentMethod) {
111-
return $this->gateway->getPaymentInstace($paymentMethod)->getRequirementsData();
111+
return $this->gateway->getPaymentInstance($paymentMethod)->getRequirementsData();
112112
}
113113
return $_POST;
114114
}
@@ -144,7 +144,7 @@ private function renameFieldsFromFormattedPost($formattedPost, $paymentMethod)
144144
$arrayFieldKey
145145
);
146146
if ($paymentMethod) {
147-
$formattedPost = $this->gateway->getPaymentInstace($paymentMethod)->renameFieldsPost($field, $formattedPost, $arrayFieldKey);
147+
$formattedPost = $this->gateway->getPaymentInstance($paymentMethod)->renameFieldsPost($field, $formattedPost, $arrayFieldKey);
148148
}
149149
}
150150
return $formattedPost;

vendor/pagarme/ecommerce-module-core/src/Kernel/ValueObjects/Installment.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function __construct($times, $baseTotal, $interest)
4848
private function setTimes($times)
4949
{
5050
$newTimes = intval($times);
51-
if ($newTimes < 1 || $newTimes > 24) {
51+
if ($newTimes < 0 || $newTimes > 24) {
5252
throw new InvalidParamException(
53-
"A installment times should be set between 1 and 24!",
53+
"A installment times should be set between 0 and 24!",
5454
$times
5555
);
5656
}
@@ -85,14 +85,7 @@ private function setBaseTotal($baseTotal)
8585
*/
8686
private function setInterest($interest)
8787
{
88-
$newInterest = floatval($interest);
89-
if ($newInterest < 0) {
90-
throw new InvalidParamException(
91-
"A installment interest should be greater or equal to 0!",
92-
$interest
93-
);
94-
}
95-
$this->interest = $newInterest;
88+
$this->interest = floatval($interest);
9689
return $this;
9790
}
9891

0 commit comments

Comments
 (0)