Skip to content

Commit 9ad54b2

Browse files
committed
v. 4.53.0
* Melhoria/correção na forma como descontos de PIX e Boleto eram aplicados. Agora o desconto será exibido nos totais em todos os lugares devidos, sem exigir configurações adicionais nem remendos, e ampliando a compatibilidade com plugins de terceiros como Funnel kit. * Removemos a funcionalidade/opção de exibir descontos pix junto aos totais já que isso ocorrerá organicamente em todos os lugares devidos quando houver desconto (PIX ou BOLETO). Merge branch 'release/4.53.0'
2 parents fe9b933 + 0e9d43d commit 9ad54b2

File tree

8 files changed

+115
-548
lines changed

8 files changed

+115
-548
lines changed

admin/views/settings/pix-fields.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,6 @@
8585
'default' => 'no',
8686
'desc_tip' => true,
8787
],
88-
'pix_show_discount_in_totals' => [
89-
'title' => __('Exibir desconto nos totais', 'pagbank-connect'),
90-
'label' => __('Exibir desconto Pix PagBank nos totais do pedido', 'pagbank-connect'),
91-
'type' => 'checkbox',
92-
'description' => __(
93-
'Se marcado, o desconto Pix será exibido nos totais do carrinho e do checkout (linha "Desconto Pix PagBank" e "Total no Pix"), quando houver desconto configurado acima.',
94-
'pagbank-connect'
95-
),
96-
'default' => 'no',
97-
'desc_tip' => false,
98-
],
9988
'pix_send_new_order_email' => [
10089
'title' => __('Enviar e-mail de novo pedido', 'pagbank-connect'),
10190
'label' => __('Enviar e-mail de novo pedido', 'pagbank-connect'),

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ricardomartins/pagbank-woocommerce",
33
"description": "Integração PagBank (PagSeguro) WooCommerce com desconto nas taxas oficiais",
44
"type": "wordpress-plugin",
5-
"version": "4.52.0",
5+
"version": "4.53.0",
66
"license": "GPL-3.0",
77
"autoload": {
88
"psr-4": {

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Donate link: https://github.com/sponsors/r-martins
55
Requires at least: 4.0
66
Tested up to: 6.9
77
Requires PHP: 7.4
8-
Stable tag: 4.52.0
8+
Stable tag: 4.53.0
99
License: GPLv3
1010
License URI: https://www.gnu.org/licenses/gpl-3.0.html
1111
PagBank com PIX, Cartão de Crédito, Boleto, Recorrência + Envio Fácil e com Menos Taxas no PagSeguro.
@@ -239,6 +239,9 @@ A confirmação é exibida ainda na tela de sucesso, e pode opcionalmente dispar
239239
Sim! Você pode [configurar descontos percentuais ou fixos](https://ajuda.pbintegracoes.com/hc/pt-br/articles/19945430928909-Oferecer-Desconto-Pix-e-Boleto) para PIX e Boleto diretamente nas configurações do plugin.
240240

241241
== Changelog ==
242+
= 4.53.0 =
243+
* Melhoria/correção na forma como descontos de PIX e Boleto eram aplicados. Agora o desconto será exibido nos totais em todos os lugares devidos, sem exigir configurações adicionais nem remendos, e ampliando a compatibilidade com plugins de terceiros como Funnel kit.
244+
* Removemos a funcionalidade/opção de exibir descontos pix junto aos totais já que isso ocorrerá organicamente em todos os lugares devidos quando houver desconto (PIX ou BOLETO).
242245

243246
= 4.52.0 =
244247
* Melhoria: Agora é possível esconder os ícones de pagamento (interessante se tiver problemas com css e não quiser ajustar)

rm-pagbank.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Plugin Name: PagBank Connect
1212
* Plugin URI: https://pbintegracoes.com
1313
* Description: Integra seu WooCommerce com as APIs PagSeguro v4 através da aplicação de Ricardo Martins (com descontos nas taxas oficiais), com suporte a PIX transparente e muito mais.
14-
* Version: 4.52.0
14+
* Version: 4.53.0
1515
* Requires at least: 5.2
1616
* Tested up to: 6.9
1717
* Requires PHP: 7.4
@@ -33,7 +33,7 @@
3333
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
3434

3535
// Plugin constants.
36-
define( 'WC_PAGSEGURO_CONNECT_VERSION', '4.52.0' );
36+
define( 'WC_PAGSEGURO_CONNECT_VERSION', '4.53.0' );
3737
define( 'WC_PAGSEGURO_CONNECT_PLUGIN_FILE', __FILE__ );
3838
define( 'WC_PAGSEGURO_CONNECT_BASE_DIR', __DIR__ );
3939
define( 'WC_PAGSEGURO_CONNECT_TEMPLATES_DIR', WC_PAGSEGURO_CONNECT_BASE_DIR . '/src/templates/' );

src/Connect.php

Lines changed: 101 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use RM_PagBank\Connect\MenuPagBank;
99
use RM_PagBank\Connect\OrderMetaBoxes;
1010
use RM_PagBank\Connect\OrderProcessor;
11+
use RM_PagBank\Connect\Payments\Boleto;
1112
use RM_PagBank\Connect\Payments\CreditCard;
1213
use RM_PagBank\Connect\Payments\Pix;
1314
use RM_PagBank\Connect\Standalone\Pix as StandalonePix;
@@ -19,7 +20,6 @@
1920
use RM_PagBank\Connect\Blocks\Redirect as RedirectBlock;
2021
use RM_PagBank\Connect\Blocks\CreditCard as CreditCardBlock;
2122
use RM_PagBank\Connect\Blocks\Pix as PixBlock;
22-
use RM_PagBank\Connect\Blocks\PixDiscountTotals;
2323
use RM_PagBank\Cron\CancelExpiredPix;
2424
use RM_PagBank\Cron\ForceOrderUpdate;
2525
use RM_PagBank\Helpers\Api;
@@ -104,15 +104,10 @@ public static function init()
104104
);
105105
}
106106
//endregion
107-
if (Params::getPixConfig('pix_show_discount_in_totals', 'no') === 'yes' && Params::getPixConfig('pix_discount', 0)) {
108-
add_action('woocommerce_cart_totals_before_order_total', [__CLASS__, 'displayPixDiscountInCartTotals']);
109-
add_action('woocommerce_review_order_before_order_total', [__CLASS__, 'displayPixDiscountInTotals']);
110-
add_filter('woocommerce_cart_totals_order_total_html', [__CLASS__, 'filterOrderTotalHtmlWhenPixSelected'], 10, 1);
111-
add_action('wp_enqueue_scripts', [__CLASS__, 'enqueuePixDiscountCheckoutScript'], 20);
112-
add_action('rest_api_init', [PixDiscountTotals::class, 'registerFilter'], 20);
113-
add_action('wp', [PixDiscountTotals::class, 'registerHydrationFilter'], 5);
114-
add_action('woocommerce_blocks_loaded', [PixDiscountTotals::class, 'init']);
115-
}
107+
}
108+
if ((Params::getPixConfig('enabled') === 'yes' && Params::getPixConfig('pix_discount', 0))
109+
|| (Params::getBoletoConfig('enabled') === 'yes' && Params::getBoletoConfig('boleto_discount', 0))) {
110+
add_action('woocommerce_cart_calculate_fees', [__CLASS__, 'addPaymentMethodDiscountFees'], 20);
116111
}
117112

118113
//if force order update enabled
@@ -827,163 +822,140 @@ public static function addPaymentInfoAdmin($order)
827822
}
828823

829824
/**
830-
* Whether PIX is currently selected as payment (legacy: rm-pagbank + ps_connect_method=pix; blocks: rm-pagbank-pix).
825+
* Whether a given payment method is currently selected (blocks: rm-pagbank-pix / rm-pagbank-boleto;
826+
* legacy: rm-pagbank + ps_connect_method=pix|boleto). Checks POST and post_data first so update_order_review
827+
* AJAX applies the fee before session is updated.
831828
*
829+
* @param string $gatewayId Gateway ID (e.g. 'rm-pagbank-pix', 'rm-pagbank-boleto').
830+
* @param string|null $subMethod Legacy sub-method when gateway is rm-pagbank (e.g. 'pix', 'boleto').
832831
* @return bool
833832
*/
834-
private static function isPixSelectedAsPayment(): bool
833+
public static function isPaymentMethodSelected(string $gatewayId, ?string $subMethod = null): bool
835834
{
835+
$post_method = null;
836+
$post_data_parsed = null;
837+
838+
if (!empty($_POST['payment_method'])) {
839+
$post_method = sanitize_text_field(wp_unslash($_POST['payment_method']));
840+
}
841+
if ($post_method === null && !empty($_POST['post_data']) && is_string($_POST['post_data'])) {
842+
parse_str(wp_unslash($_POST['post_data']), $post_data_parsed);
843+
$post_method = isset($post_data_parsed['payment_method']) ? sanitize_text_field($post_data_parsed['payment_method']) : null;
844+
}
845+
if ($post_data_parsed === null && !empty($_POST['post_data']) && is_string($_POST['post_data'])) {
846+
parse_str(wp_unslash($_POST['post_data']), $post_data_parsed);
847+
}
848+
849+
if ($post_method !== null) {
850+
if ($post_method === $gatewayId) {
851+
return true;
852+
}
853+
if ($post_method === self::DOMAIN && $subMethod !== null) {
854+
$sub = false;
855+
if (!empty($_POST['ps_connect_method']) && sanitize_text_field(wp_unslash($_POST['ps_connect_method'])) === $subMethod) {
856+
$sub = true;
857+
}
858+
if (!$sub && is_array($post_data_parsed) && isset($post_data_parsed['ps_connect_method']) && $post_data_parsed['ps_connect_method'] === $subMethod) {
859+
$sub = true;
860+
}
861+
if ($sub) {
862+
return true;
863+
}
864+
}
865+
if ($post_method !== $gatewayId && $post_method !== self::DOMAIN) {
866+
return false;
867+
}
868+
}
869+
836870
$chosen = WC()->session ? WC()->session->get('chosen_payment_method', '') : '';
837-
if ($chosen === 'rm-pagbank-pix') {
871+
if ($chosen === $gatewayId) {
838872
return true;
839873
}
840874
if ($chosen !== self::DOMAIN) {
841875
return false;
842876
}
843-
// Legacy checkout: gateway is rm-pagbank; sub-method is in post_data (ps_connect_method).
844-
if (!empty($_POST['ps_connect_method'])) {
845-
return sanitize_text_field(wp_unslash($_POST['ps_connect_method'])) === 'pix';
877+
if ($subMethod !== null && !empty($_POST['ps_connect_method']) && sanitize_text_field(wp_unslash($_POST['ps_connect_method'])) === $subMethod) {
878+
return true;
879+
}
880+
if ($subMethod !== null && is_array($post_data_parsed) && isset($post_data_parsed['ps_connect_method']) && $post_data_parsed['ps_connect_method'] === $subMethod) {
881+
return true;
846882
}
847-
if (!empty($_POST['post_data']) && is_string($_POST['post_data'])) {
848-
parse_str(wp_unslash($_POST['post_data']), $post_data);
849-
return isset($post_data['ps_connect_method']) && $post_data['ps_connect_method'] === 'pix';
883+
if ($subMethod !== null && $post_data_parsed === null && !empty($_POST['post_data']) && is_string($_POST['post_data'])) {
884+
parse_str(wp_unslash($_POST['post_data']), $post_data_parsed);
885+
return is_array($post_data_parsed) && isset($post_data_parsed['ps_connect_method']) && $post_data_parsed['ps_connect_method'] === $subMethod;
850886
}
851887
return false;
852888
}
853889

854890
/**
855-
* Outputs PIX discount and "Total no PIX" rows on the cart page (always, when discount is configured).
891+
* Adds PIX/Boleto discount fees when the respective method is selected at checkout.
892+
* Called from woocommerce_cart_calculate_fees; tries each method that has discount configured.
856893
*
894+
* @param \WC_Cart $cart Cart instance.
857895
* @return void
858896
*/
859-
public static function displayPixDiscountInCartTotals(): void
897+
public static function addPaymentMethodDiscountFees($cart): void
860898
{
861-
if (!function_exists('is_cart') || !is_cart() || !WC()->cart || is_wc_endpoint_url('order-pay')) {
862-
return;
863-
}
864-
if (Params::getPixConfig('enabled') !== 'yes') {
865-
return;
866-
}
867-
$discountConfig = Params::getPixConfig('pix_discount', 0);
868-
if (!Params::getDiscountType($discountConfig)) {
869-
return;
870-
}
871-
$excludesShipping = Params::getPixConfig('pix_discount_excludes_shipping', 'no') === 'yes';
872-
$cartTotal = floatval(WC()->cart->get_total('edit'));
873-
$shippingTotal = floatval(WC()->cart->get_shipping_total());
874-
$discount = Params::getDiscountValueForTotal($discountConfig, $cartTotal, $excludesShipping, $shippingTotal);
875-
if ($discount <= 0) {
876-
return;
877-
}
878-
$pixTitle = Params::getPixConfig('title', __('PIX via PagBank', 'pagbank-connect'));
879-
$discountLabel = __('Desconto', 'pagbank-connect') . ' ' . $pixTitle;
880-
$totalNoPix = $cartTotal - $discount;
881-
$totalNoPixLabel = __('Total no PIX', 'pagbank-connect');
882-
?>
883-
<tr class="pagbank-pix-discount fee">
884-
<th><?php echo esc_html($discountLabel); ?></th>
885-
<td data-title="<?php echo esc_attr($discountLabel); ?>"><?php echo wp_kses_post(wc_price(-$discount)); ?></td>
886-
</tr>
887-
<tr class="pagbank-pix-total-no-pix order-total">
888-
<th><?php echo esc_html($totalNoPixLabel); ?></th>
889-
<td data-title="<?php echo esc_attr($totalNoPixLabel); ?>"><?php echo wp_kses_post(wc_price($totalNoPix)); ?></td>
890-
</tr>
891-
<?php
899+
self::addDiscountAsFeeForMethod($cart, 'pix');
900+
self::addDiscountAsFeeForMethod($cart, 'boleto');
892901
}
893902

894903
/**
895-
* Display Pix discount row in checkout totals only when PIX is the selected payment method.
896-
* Relies on update_checkout (triggered on payment method change) so the fragment is re-rendered with session state.
904+
* Adds discount as a cart fee (negative) for one payment method when it is selected.
897905
*
906+
* @param \WC_Cart $cart Cart instance.
907+
* @param string $method 'pix' or 'boleto'.
898908
* @return void
899909
*/
900-
public static function displayPixDiscountInTotals()
910+
protected static function addDiscountAsFeeForMethod($cart, string $method): void
901911
{
902-
if (Params::getPixConfig('enabled') !== 'yes') {
912+
if (function_exists('is_cart') && is_cart()) {
903913
return;
904914
}
905-
if (!WC()->cart || is_wc_endpoint_url('order-pay')) {
906-
return;
907-
}
908-
if (!self::isPixSelectedAsPayment()) {
915+
$config = [
916+
'pix' => [
917+
'enabled' => Params::getPixConfig('enabled') === 'yes',
918+
'discount' => Params::getPixConfig('pix_discount', 0),
919+
'excludes' => Params::getPixConfig('pix_discount_excludes_shipping', 'no') === 'yes',
920+
'title' => Params::getPixConfig('title', __('PIX via PagBank', 'pagbank-connect')),
921+
'gateway_id' => 'rm-pagbank-pix',
922+
'sub_method' => 'pix',
923+
'fee_id' => Pix::DISCOUNT_FEE_ID,
924+
],
925+
'boleto' => [
926+
'enabled' => Params::getBoletoConfig('enabled') === 'yes',
927+
'discount' => Params::getBoletoConfig('boleto_discount', 0),
928+
'excludes' => Params::getBoletoConfig('boleto_discount_excludes_shipping', 'no') === 'yes',
929+
'title' => Params::getBoletoConfig('title', __('Boleto via PagBank', 'pagbank-connect')),
930+
'gateway_id' => 'rm-pagbank-boleto',
931+
'sub_method' => 'boleto',
932+
'fee_id' => Boleto::DISCOUNT_FEE_ID,
933+
],
934+
];
935+
if (!isset($config[$method])) {
909936
return;
910937
}
911-
$discountConfig = Params::getPixConfig('pix_discount', 0);
912-
if (!Params::getDiscountType($discountConfig)) {
938+
$c = $config[$method];
939+
if (!$c['enabled'] || !self::isPaymentMethodSelected($c['gateway_id'], $c['sub_method'])) {
913940
return;
914941
}
915-
$excludesShipping = Params::getPixConfig('pix_discount_excludes_shipping', 'no') === 'yes';
916-
$cartTotal = floatval(WC()->cart->get_total('edit'));
917-
$shippingTotal = floatval(WC()->cart->get_shipping_total());
918-
$discount = Params::getDiscountValueForTotal($discountConfig, $cartTotal, $excludesShipping, $shippingTotal);
919-
if ($discount <= 0) {
942+
if (!Params::getDiscountType($c['discount'])) {
920943
return;
921944
}
922-
$pixTitle = Params::getPixConfig('title', __('PIX via PagBank', 'pagbank-connect'));
923-
$discountLabel = __('Desconto', 'pagbank-connect') . ' ' . $pixTitle;
924-
?>
925-
<tr class="pagbank-pix-discount fee">
926-
<th><?php echo esc_html($discountLabel); ?></th>
927-
<td data-title="<?php echo esc_attr($discountLabel); ?>"><?php echo wp_kses_post(wc_price(-$discount)); ?></td>
928-
</tr>
929-
<?php
930-
}
931-
932-
/**
933-
* When PIX is selected, replace the order total HTML with the discounted total.
934-
*
935-
* @param string $value Default order total HTML.
936-
* @return string
937-
*/
938-
public static function filterOrderTotalHtmlWhenPixSelected(string $value): string
939-
{
940-
if (Params::getPixConfig('enabled') !== 'yes' || !WC()->cart) {
941-
return $value;
942-
}
943-
if (!self::isPixSelectedAsPayment()) {
944-
return $value;
945-
}
946-
$discountConfig = Params::getPixConfig('pix_discount', 0);
947-
if (!Params::getDiscountType($discountConfig)) {
948-
return $value;
949-
}
950-
$excludesShipping = Params::getPixConfig('pix_discount_excludes_shipping', 'no') === 'yes';
951-
$cartTotal = floatval(WC()->cart->get_total('edit'));
952-
$shippingTotal = floatval(WC()->cart->get_shipping_total());
953-
$discount = Params::getDiscountValueForTotal($discountConfig, $cartTotal, $excludesShipping, $shippingTotal);
945+
$cartTotal = (float) $cart->get_cart_contents_total() + (float) $cart->get_shipping_total();
946+
$shippingTotal = (float) $cart->get_shipping_total();
947+
$discount = Params::getDiscountValueForTotal($c['discount'], $cartTotal, $c['excludes'], $shippingTotal);
954948
if ($discount <= 0) {
955-
return $value;
956-
}
957-
$totalWithDiscount = $cartTotal - $discount;
958-
// Preserve tax suffix from original if present (e.g. "includes VAT").
959-
$suffix = '';
960-
if (preg_match('#<small class="includes_tax">(.+?)</small>#s', $value, $m)) {
961-
$suffix = '<small class="includes_tax">' . $m[1] . '</small>';
962-
}
963-
return '<strong>' . wp_kses_post(wc_price($totalWithDiscount)) . '</strong> ' . $suffix;
964-
}
965-
966-
/**
967-
* Enqueue script that triggers update_checkout when payment method changes
968-
* (same approach as Pix por Piggly), so the server re-renders totals with the correct PIX discount.
969-
*
970-
* @return void
971-
*/
972-
public static function enqueuePixDiscountCheckoutScript()
973-
{
974-
if (!is_checkout() || empty(WC()->cart)) {
975-
return;
976-
}
977-
if (Params::getPixConfig('enabled') !== 'yes') {
978-
return;
979-
}
980-
if (Params::getPixConfig('pix_show_discount_in_totals', 'no') !== 'yes' || !Params::getPixConfig('pix_discount', 0)) {
981949
return;
982950
}
983-
$script = "!function(a){\"use strict\";a(function(){a(document.body).on(\"change\",\"input[name=\\\"payment_method\\\"]\",function(){a(\"body\").trigger(\"update_checkout\")})})}(jQuery);";
984-
wp_register_script('pagbank-pix-checkout-update', false, ['jquery'], WC_PAGSEGURO_CONNECT_VERSION, true);
985-
wp_enqueue_script('pagbank-pix-checkout-update');
986-
wp_add_inline_script('pagbank-pix-checkout-update', $script, 'after');
951+
$discountLabel = __('Desconto', 'pagbank-connect') . ' ' . $c['title'];
952+
$cart->fees_api()->add_fee([
953+
'id' => $c['fee_id'],
954+
'name' => $discountLabel,
955+
'amount' => -$discount,
956+
'taxable' => false,
957+
'tax_class' => '',
958+
]);
987959
}
988960

989961
/**

0 commit comments

Comments
 (0)