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: allow installments for subscription #379

Merged
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
33 changes: 22 additions & 11 deletions assets/javascripts/front/checkout/model/payment/card.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* globals wc_pagarme_checkout */

/*jshint esversion: 8 */
let pagarmeCard = {
limitTokenize: 10,
tokenExpirationAttribute: 'data-pagarmecheckout-expiration',
cardNumberTarget: 'input[data-element="pagarme-card-number"]',
brandTarget: 'input[data-pagarmecheckout-element="brand-input"]',
valueTarget: 'input[data-pagarmecheckout-element="order-value"]',
installmentsTarget: '[data-pagarme-component="installments"]',
installmentsInfoTarget: '[data-pagarme-component="installments-info"]',
mundiCdn: 'https://cdn.mundipagg.com/assets/images/logos/brands/png/',
tokenElement: '[data-pagarmecheckout-element="token"]',
fieldsetCardElements: 'fieldset[data-pagarmecheckout="card"]',
Expand Down Expand Up @@ -255,22 +256,26 @@ let pagarmeCard = {
if (!elem) {
return false;
}
let brand = elem.closest('fieldset').find(this.brandTarget).val();
const brand = elem.closest('fieldset').find(this.brandTarget).val();
let total = elem.closest('fieldset').find(this.valueTarget).val();
if (total) {
total = pagarmeCard.formatValue(total);
}
let cardForm = elem.closest("fieldset");
let select = cardForm.find(this.installmentsTarget);
if (!total)
const cardForm = elem.closest("fieldset");
const select = cardForm.find(this.installmentsTarget);
const info = cardForm.find(this.installmentsInfoTarget);
if (!total) {
total = cartTotal;
}
if ((!total) ||
(select.data("type") === 2 && !brand) ||
(select.data("type") === 1 && elem.data('element') !== "order-value"))
(select.data("type") === 1 && elem.data('element') !== "order-value")
) {
return false;
let storageName = btoa(brand + total);
}
const storageName = btoa(brand + total);
sessionStorage.removeItem(storageName);
let storage = sessionStorage.getItem(storageName);
const storage = sessionStorage.getItem(storageName);
if (storage) {
select.html(storage);
} else {
Expand All @@ -283,7 +288,7 @@ let pagarmeCard = {
}
});
ajax.done(function (response) {
pagarmeCard._done(select, storageName, cardForm, response);
pagarmeCard._done(select, info, storageName, cardForm, JSON.parse(response));
});
ajax.fail(function () {
pagarmeCard._fail(cardForm);
Expand All @@ -293,8 +298,14 @@ let pagarmeCard = {
return true;
},

_done: function (select, storageName, event, response) {
select.html(response);
_done: function (select, info, storageName, event, response) {
if (info.length) {
info.addClass('pagarme-hidden');
if(response.installmentsConfig > 1) {
info.removeClass('pagarme-hidden');
}
}
select.html(response.optionsHtml);
sessionStorage.setItem(storageName, response);
this.removeLoader(event);
},
Expand Down
17 changes: 14 additions & 3 deletions assets/stylesheets/front/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.pagarme-hidden {
display: none!important;
}

#wcmp-checkout-errors {
display: none;
}
Expand Down Expand Up @@ -67,6 +71,13 @@
pointer-events: none;
}

#payment .payment_methods li[class*="pagarme"] .payment_box .pagarme-installments-info {
display: block;
margin: 0 .75em .25em;
font-size: .9em;
font-style: italic;
}

.woocommerce-order .woocommerce-message .pagarme-response p:last-child {
margin-bottom: 0;
}
Expand Down Expand Up @@ -132,7 +143,7 @@

/* sm */
/* @media (min-width: 576px) {

} */

/* md */
Expand All @@ -151,10 +162,10 @@

/* xl */
/* @media (min-width: 1200px) {

} */

/* xxl */
/* @media (min-width: 1400px) {

} */
Binary file modified languages/woo-pagarme-payments-pt_BR.mo
Binary file not shown.
17 changes: 16 additions & 1 deletion 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-01-17 10:24-0300\n"
"PO-Revision-Date: 2024-01-19 11:54-0300\n"
"Last-Translator: Pagar.me\n"
"Language-Team: \n"
"Language: pt_BR\n"
Expand Down Expand Up @@ -1287,3 +1287,18 @@ msgstr "Habilita boleto para assinatura"

msgid "Activates billet payment method for subscriptions."
msgstr "Ativa o método de pagamento de boleto para assinaturas."

msgid "Allow installments for subscription"
msgstr "Permitir parcelamento para assinatura"

msgid "Enable installments for subscription"
msgstr "Habilita parcelamento para assinatura"

msgid "Activates credit card installments for subscriptions."
msgstr "Ativa parcelamento de cartão de crédito para assinaturas."

msgid "Works only for monthly and yearly subscriptions."
msgstr "Funciona apenas para assinaturas mensais e anuais."

msgid "Your cart has one or more daily or weekly subscription products, which do not allow installments."
msgstr "Seu carrinho possui um ou mais produtos de assinatura diário ou semanal, que não permitem parcelamentos."
59 changes: 54 additions & 5 deletions src/Block/Checkout/Form/Installments.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
use Woocommerce\Pagarme\Block\Checkout\Gateway;
use Woocommerce\Pagarme\Helper\Utils;
use Woocommerce\Pagarme\Model\CardInstallments;
use Woocommerce\Pagarme\Model\Subscription;
use Woocommerce\Pagarme\View\Checkouts;

global $woocommerce;

defined( 'ABSPATH' ) || exit;
defined('ABSPATH') || exit;

/**
* Class Installments
Expand All @@ -34,6 +33,19 @@ class Installments extends Gateway
/** @var int */
protected $sequence = 1;

/** @var int */
protected $cardInstallments;

/** @var Subscription */
protected $subscription;

public function __construct()
{
parent::__construct();
$this->cardInstallments = new CardInstallments();
$this->subscription = new Subscription();
}

/**
* @param int $sequence
* @return $this
Expand Down Expand Up @@ -89,8 +101,7 @@ public function getInstallmentsComponent()

public function render_installments($total)
{
$cardInstallments = new CardInstallments();
return $cardInstallments->getInstallmentsByType($total);
return $this->cardInstallments->getInstallmentsByType($total);
}

/**
Expand All @@ -100,4 +111,42 @@ public function render()
{
return $this->render_installments($this->getCartTotals());
}

/**
* @return bool
*/
public function isCcInstallmentTypeByFlag() {
$type = intval($this->cardInstallments->config->getCcInstallmentType()) ?? 1;
return $type === CardInstallments::INSTALLMENTS_BY_FLAG;
}

/**
* @return int
*/
public function getConfiguredMaxCcInstallments(): int {
if ($this->isCcInstallmentTypeByFlag()) {
$flag = Utils::get('flag', false, 'esc_html');
$configByFlags = $this->cardInstallments->config->getCcInstallmentsByFlag();
return intval($configByFlags['max_installment'][$flag]);
}
return intval($this->cardInstallments->config->getCcInstallmentsMaximum());
}

/**
* @return bool
*/
public function showOneInstallmentInfo()
{
if (!Subscription::hasSubscriptionProductInCart()) {
return false;
}
if (
$this->subscription->allowInstallments()
&& $this->subscription->hasOneInstallmentPeriodInCart()
&& ($this->getConfiguredMaxCcInstallments() > 1 || $this->isCcInstallmentTypeByFlag())
) {
return true;
}
return false;
}
}
5 changes: 4 additions & 1 deletion src/Block/Checkout/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ public function getConfigDataProvider()

/**
* @param string $id
* @return string
* @return string | null
*/
public function getElementId(string $id)
{
if (!$this->getPaymentInstance()){
return null;
}
return WCMP_PREFIX . '[' . $this->getPaymentInstance()->getMethodCode() . ']' . $id;
}

Expand Down
22 changes: 18 additions & 4 deletions src/Controller/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Pagarme\Core\Payment\Repositories\CustomerRepository;
use Pagarme\Core\Payment\Repositories\SavedCardRepository;
use Woocommerce\Pagarme\Block\Checkout\Form\Installments;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Será q faz sentido um controller depender de um Block?

use Woocommerce\Pagarme\Controller\Gateways\AbstractGateway;
use Woocommerce\Pagarme\Model\CardInstallments;
use Woocommerce\Pagarme\Model\Config;
Expand All @@ -27,13 +28,17 @@ class Checkout
/** @var CardInstallments */
protected $cardInstallments;

/** @var Installments */
protected $installments;

/**
* @var Orders
*/
protected $ordersController;

public function __construct(
CardInstallments $cardInstallments = null
CardInstallments $cardInstallments = null,
Installments $installments = null
) {
$this->ordersController = new Orders();
add_action('woocommerce_api_' . Model\Checkout::API_REQUEST, array($this, 'process_checkout_transparent'));
Expand All @@ -51,7 +56,11 @@ public function __construct(
];
$this->cardInstallments = $cardInstallments;
if (!$this->cardInstallments) {
$this->cardInstallments = new CardInstallments;
$this->cardInstallments = new CardInstallments();
}
$this->installments = $installments;
if (!$this->installments) {
$this->installments = new Installments();
}
}

Expand Down Expand Up @@ -109,13 +118,18 @@ public function build_installments()
exit(0);
}

$html = $this->cardInstallments->renderOptions(
$installmentsConfig = $this->installments->getConfiguredMaxCcInstallments();

$optionsHtml = $this->cardInstallments->renderOptions(
$this->cardInstallments->getInstallmentsByType(
Utils::get('total', false),
Utils::get('flag', false, 'esc_html')

));
echo wp_kses_no_null($html);
echo json_encode([
'installmentsConfig' => $installmentsConfig,
'optionsHtml' => wp_kses_no_null($optionsHtml)
]);
exit();
}

Expand Down
32 changes: 27 additions & 5 deletions src/Controller/Gateways/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function isSubscriptionActive(): bool
*/
public function append_form_fields()
{
return [
$fields = [
'cc_operation_type' => $this->field_cc_operation_type(),
'cc_soft_descriptor' => $this->field_cc_soft_descriptor(),
'cc_flags' => $this->field_cc_flags(),
Expand All @@ -71,8 +71,14 @@ public function append_form_fields()
'cc_installments_without_interest' => $this->field_cc_installment_fields('without_interest'),
'cc_installments_by_flag' => $this->field_cc_installment_fields('flags'),
'cc_allow_save' => $this->field_cc_allow_save(),
'cc_allowed_in_subscription' => $this->field_cc_allowed_for_subscription(),


];
if (Subscription::hasSubscriptionPlugin()) {
$fields['cc_allowed_in_subscription'] = $this->field_cc_allowed_for_subscription();
$fields['cc_subscription_installments'] = $this->field_cc_subscription_installments();
}
return $fields;
}

/**
Expand Down Expand Up @@ -170,9 +176,6 @@ public function field_cc_allow_save()
*/
private function field_cc_allowed_for_subscription()
{
if (!Subscription::hasSubscriptionPlugin()){
return [];
}
return [
'title' => __('Active for subscription', 'woo-pagarme-payments'),
'type' => 'select',
Expand All @@ -187,6 +190,25 @@ private function field_cc_allowed_for_subscription()
];
}

/**
* @return array
*/
private function field_cc_subscription_installments()
{
return [
'title' => __('Allow installments for subscription', 'woo-pagarme-payments'),
'type' => 'select',
'options' => $this->yesnoOptions->toLabelsArray(true),
'label' => __('Enable installments for subscription', 'woo-pagarme-payments'),
'default' => $this->config->getData('cc_subscription_installments') ?? strtolower(Yesno::NO),
'desc_tip' => __('Activates credit card installments for subscriptions.', 'woo-pagarme-payments'),
'description' => __('Works only for monthly and yearly subscriptions.', 'woo-pagarme-payments'),
'custom_attributes' => array(
'data-field' => 'cc-subscription-installments',
),
];
}

/**
* @return array
*/
Expand Down
Loading
Loading