Skip to content

Commit d55f281

Browse files
committed
feat: allow installments for subscription
1 parent e70027d commit d55f281

File tree

11 files changed

+218
-30
lines changed

11 files changed

+218
-30
lines changed

assets/javascripts/front/checkout/model/payment/card.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* globals wc_pagarme_checkout */
2-
2+
/*jshint esversion: 8 */
33
let pagarmeCard = {
44
limitTokenize: 10,
55
tokenExpirationAttribute: 'data-pagarmecheckout-expiration',
66
cardNumberTarget: 'input[data-element="pagarme-card-number"]',
77
brandTarget: 'input[data-pagarmecheckout-element="brand-input"]',
88
valueTarget: 'input[data-pagarmecheckout-element="order-value"]',
99
installmentsTarget: '[data-pagarme-component="installments"]',
10+
installmentsInfoTarget: '[data-pagarme-component="installments-info"]',
1011
mundiCdn: 'https://cdn.mundipagg.com/assets/images/logos/brands/png/',
1112
tokenElement: '[data-pagarmecheckout-element="token"]',
1213
fieldsetCardElements: 'fieldset[data-pagarmecheckout="card"]',
@@ -262,12 +263,16 @@ let pagarmeCard = {
262263
}
263264
let cardForm = elem.closest("fieldset");
264265
let select = cardForm.find(this.installmentsTarget);
265-
if (!total)
266+
let info = cardForm.find(this.installmentsInfoTarget);
267+
if (!total) {
266268
total = cartTotal;
269+
}
267270
if ((!total) ||
268271
(select.data("type") === 2 && !brand) ||
269-
(select.data("type") === 1 && elem.data('element') !== "order-value"))
272+
(select.data("type") === 1 && elem.data('element') !== "order-value")
273+
) {
270274
return false;
275+
}
271276
let storageName = btoa(brand + total);
272277
sessionStorage.removeItem(storageName);
273278
let storage = sessionStorage.getItem(storageName);
@@ -283,7 +288,7 @@ let pagarmeCard = {
283288
}
284289
});
285290
ajax.done(function (response) {
286-
pagarmeCard._done(select, storageName, cardForm, response);
291+
pagarmeCard._done(select, info, storageName, cardForm, JSON.parse(response));
287292
});
288293
ajax.fail(function () {
289294
pagarmeCard._fail(cardForm);
@@ -293,8 +298,12 @@ let pagarmeCard = {
293298
return true;
294299
},
295300

296-
_done: function (select, storageName, event, response) {
297-
select.html(response);
301+
_done: function (select, info, storageName, event, response) {
302+
info.addClass('pagarme-hidden');
303+
if(response.installmentsConfig > 1) {
304+
info.removeClass('pagarme-hidden');
305+
}
306+
select.html(response.optionsHtml);
298307
sessionStorage.setItem(storageName, response);
299308
this.removeLoader(event);
300309
},

assets/stylesheets/front/style.css

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.pagarme-hidden {
2+
display: none!important;
3+
}
4+
15
#wcmp-checkout-errors {
26
display: none;
37
}
@@ -67,6 +71,13 @@
6771
pointer-events: none;
6872
}
6973

74+
#payment .payment_methods li[class*="pagarme"] .payment_box .pagarme-installments-info {
75+
display: block;
76+
margin: 0 .75em .25em;
77+
font-size: .9em;
78+
font-style: italic;
79+
}
80+
7081
.woocommerce-order .woocommerce-message .pagarme-response p:last-child {
7182
margin-bottom: 0;
7283
}
@@ -132,7 +143,7 @@
132143

133144
/* sm */
134145
/* @media (min-width: 576px) {
135-
146+
136147
} */
137148

138149
/* md */
@@ -151,10 +162,10 @@
151162

152163
/* xl */
153164
/* @media (min-width: 1200px) {
154-
165+
155166
} */
156167

157168
/* xxl */
158169
/* @media (min-width: 1400px) {
159-
170+
160171
} */
664 Bytes
Binary file not shown.

languages/woo-pagarme-payments-pt_BR.po

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ msgstr ""
55
"Project-Id-Version: WooCommerce Pagar.me Payments 1.0\n"
66
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/woo-pagarme-payments\n"
77
"POT-Creation-Date: 2018-06-22 13:58-0300\n"
8-
"PO-Revision-Date: 2024-01-17 10:24-0300\n"
8+
"PO-Revision-Date: 2024-01-19 11:54-0300\n"
99
"Last-Translator: Pagar.me\n"
1010
"Language-Team: \n"
1111
"Language: pt_BR\n"
@@ -1287,3 +1287,18 @@ msgstr "Habilita boleto para assinatura"
12871287

12881288
msgid "Activates billet payment method for subscriptions."
12891289
msgstr "Ativa o método de pagamento de boleto para assinaturas."
1290+
1291+
msgid "Allow installments for subscription"
1292+
msgstr "Permitir parcelamento para assinatura"
1293+
1294+
msgid "Enable installments for subscription"
1295+
msgstr "Habilita parcelamento para assinatura"
1296+
1297+
msgid "Activates credit card installments for subscriptions."
1298+
msgstr "Ativa parcelamento de cartão de crédito para assinaturas."
1299+
1300+
msgid "Works only for monthly and yearly subscriptions."
1301+
msgstr "Funciona apenas para assinaturas mensais e anuais."
1302+
1303+
msgid "Your cart has one or more daily or weekly subscription products, which do not allow installments."
1304+
msgstr "Seu carrinho possui um ou mais produtos de assinatura diário ou semanal, que não permitem parcelamentos."

src/Block/Checkout/Form/Installments.php

+77-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
use Woocommerce\Pagarme\Block\Checkout\Gateway;
1515
use Woocommerce\Pagarme\Helper\Utils;
1616
use Woocommerce\Pagarme\Model\CardInstallments;
17+
use Woocommerce\Pagarme\Model\Subscription;
1718
use Woocommerce\Pagarme\View\Checkouts;
1819

19-
global $woocommerce;
20-
21-
defined( 'ABSPATH' ) || exit;
20+
defined('ABSPATH') || exit;
2221

2322
/**
2423
* Class Installments
@@ -34,6 +33,19 @@ class Installments extends Gateway
3433
/** @var int */
3534
protected $sequence = 1;
3635

36+
/** @var int */
37+
protected $cardInstallments;
38+
39+
/** @var Subscription */
40+
protected $subscription;
41+
42+
public function __construct()
43+
{
44+
parent::__construct();
45+
$this->cardInstallments = new CardInstallments();
46+
$this->subscription = new Subscription();
47+
}
48+
3749
/**
3850
* @param int $sequence
3951
* @return $this
@@ -89,8 +101,7 @@ public function getInstallmentsComponent()
89101

90102
public function render_installments($total)
91103
{
92-
$cardInstallments = new CardInstallments();
93-
return $cardInstallments->getInstallmentsByType($total);
104+
return $this->cardInstallments->getInstallmentsByType($total);
94105
}
95106

96107
/**
@@ -100,4 +111,65 @@ public function render()
100111
{
101112
return $this->render_installments($this->getCartTotals());
102113
}
114+
115+
/**
116+
* @return bool
117+
*/
118+
public function hasSubscriptionProductInCart()
119+
{
120+
return Subscription::hasSubscriptionProductInCart();
121+
}
122+
123+
/**
124+
* @return bool
125+
*/
126+
public function subscriptionAllowInstallments() {
127+
return $this->subscription->allowInstallments();
128+
}
129+
130+
/**
131+
* @return bool
132+
*/
133+
public function hasOneInstallmentPeriodInCart()
134+
{
135+
return $this->subscription->hasOneInstallmentPeriodInCart();
136+
}
137+
138+
/**
139+
* @return bool
140+
*/
141+
public function isCcInstallmentTypeByFlag() {
142+
$type = intval($this->cardInstallments->config->getCcInstallmentType()) ?? 1;
143+
return $type === CardInstallments::INSTALLMENTS_BY_FLAG;
144+
}
145+
146+
/**
147+
* @return int
148+
*/
149+
public function getConfiguredMaxCcInstallments(): int {
150+
if ($this->isCcInstallmentTypeByFlag()) {
151+
$flag = Utils::get('flag', false, 'esc_html');
152+
$configByFlags = $this->cardInstallments->config->getCcInstallmentsByFlag();
153+
return intval($configByFlags['max_installment'][$flag]);
154+
}
155+
return intval($this->cardInstallments->config->getCcInstallmentsMaximum());
156+
}
157+
158+
/**
159+
* @return bool
160+
*/
161+
public function showOneInstallmentInfo()
162+
{
163+
if (!$this->hasSubscriptionProductInCart()) {
164+
return false;
165+
}
166+
if (
167+
$this->subscriptionAllowInstallments()
168+
&& $this->hasOneInstallmentPeriodInCart()
169+
&& ($this->getConfiguredMaxCcInstallments() > 1 || $this->isCcInstallmentTypeByFlag())
170+
) {
171+
return true;
172+
}
173+
return false;
174+
}
103175
}

src/Block/Checkout/Gateway.php

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public function getConfigDataProvider()
8888
*/
8989
public function getElementId(string $id)
9090
{
91+
if (!$this->getPaymentInstance()){
92+
return;
93+
}
9194
return WCMP_PREFIX . '[' . $this->getPaymentInstance()->getMethodCode() . ']' . $id;
9295
}
9396

src/Controller/Checkout.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Pagarme\Core\Payment\Repositories\CustomerRepository;
1010
use Pagarme\Core\Payment\Repositories\SavedCardRepository;
11+
use Woocommerce\Pagarme\Block\Checkout\Form\Installments;
1112
use Woocommerce\Pagarme\Controller\Gateways\AbstractGateway;
1213
use Woocommerce\Pagarme\Model\CardInstallments;
1314
use Woocommerce\Pagarme\Model\Config;
@@ -27,13 +28,17 @@ class Checkout
2728
/** @var CardInstallments */
2829
protected $cardInstallments;
2930

31+
/** @var Installments */
32+
protected $installments;
33+
3034
/**
3135
* @var Orders
3236
*/
3337
protected $ordersController;
3438

3539
public function __construct(
36-
CardInstallments $cardInstallments = null
40+
CardInstallments $cardInstallments = null,
41+
Installments $installments = null
3742
) {
3843
$this->ordersController = new Orders();
3944
add_action('woocommerce_api_' . Model\Checkout::API_REQUEST, array($this, 'process_checkout_transparent'));
@@ -51,7 +56,11 @@ public function __construct(
5156
];
5257
$this->cardInstallments = $cardInstallments;
5358
if (!$this->cardInstallments) {
54-
$this->cardInstallments = new CardInstallments;
59+
$this->cardInstallments = new CardInstallments();
60+
}
61+
$this->installments = $installments;
62+
if (!$this->installments) {
63+
$this->installments = new Installments();
5564
}
5665
}
5766

@@ -109,13 +118,18 @@ public function build_installments()
109118
exit(0);
110119
}
111120

112-
$html = $this->cardInstallments->renderOptions(
121+
$installmentsConfig = $this->installments->getConfiguredMaxCcInstallments();
122+
123+
$optionsHtml = $this->cardInstallments->renderOptions(
113124
$this->cardInstallments->getInstallmentsByType(
114125
Utils::get('total', false),
115126
Utils::get('flag', false, 'esc_html')
116127

117128
));
118-
echo wp_kses_no_null($html);
129+
echo json_encode([
130+
'installmentsConfig' => $installmentsConfig,
131+
'optionsHtml' => wp_kses_no_null($optionsHtml)
132+
]);
119133
exit();
120134
}
121135

src/Controller/Gateways/CreditCard.php

+23
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function append_form_fields()
7272
'cc_installments_by_flag' => $this->field_cc_installment_fields('flags'),
7373
'cc_allow_save' => $this->field_cc_allow_save(),
7474
'cc_allowed_in_subscription' => $this->field_cc_allowed_for_subscription(),
75+
'cc_subscription_installments' => $this->field_cc_subscription_installments(),
7576
];
7677
}
7778

@@ -187,6 +188,28 @@ private function field_cc_allowed_for_subscription()
187188
];
188189
}
189190

191+
/**
192+
* @return array
193+
*/
194+
private function field_cc_subscription_installments()
195+
{
196+
if (!Subscription::hasSubscriptionPlugin()){
197+
return [];
198+
}
199+
return [
200+
'title' => __('Allow installments for subscription', 'woo-pagarme-payments'),
201+
'type' => 'select',
202+
'options' => $this->yesnoOptions->toLabelsArray(true),
203+
'label' => __('Enable installments for subscription', 'woo-pagarme-payments'),
204+
'default' => $this->config->getData('cc_subscription_installments') ?? strtolower(Yesno::NO),
205+
'desc_tip' => __('Activates credit card installments for subscriptions.', 'woo-pagarme-payments'),
206+
'description' => __('Works only for monthly and yearly subscriptions.', 'woo-pagarme-payments'),
207+
'custom_attributes' => array(
208+
'data-field' => 'cc-subscription-installments',
209+
),
210+
];
211+
}
212+
190213
/**
191214
* @return array
192215
*/

0 commit comments

Comments
 (0)