Skip to content

Develop#595

Open
proteusbr1 wants to merge 5 commits intopagarme:developfrom
proteusbr1:develop
Open

Develop#595
proteusbr1 wants to merge 5 commits intopagarme:developfrom
proteusbr1:develop

Conversation

@proteusbr1
Copy link

Git Merge

Qual o tipo de PR é esse? (marque todos os aplicáveis)

  • Refatoração
  • Adição de funcionalidade
  • Correção de bug
  • Otimização
  • Atualização de documentação

Descrição

Este PR agrupa correções críticas para a estabilidade do módulo no checkout e no processamento de webhooks.

1. Correções no Checkout (Installments Ajax):

  • Sintoma: Ocorria erro SyntaxError ao carregar as parcelas do cartão ("Unexpected token <" ou "[object Object] is not valid JSON").
  • Backend (Checkout.php): Substituído echo json_encode(...) + exit() por ob_clean() + wp_send_json(...). Isso garante que o buffer de saída esteja limpo antes de enviar a resposta, evitando que notices PHP ou HTML alheio quebrem o JSON, além de enviar o header Content-Type: application/json correto.
  • Frontend (card.js): Removido o JSON.parse(response) na callback do jQuery. Como o backend agora retorna o header JSON correto, o jQuery já entrega o payload parseado (como Objeto). O JSON.parse redundante forçava a conversão para string "[object Object]" causando o erro.

[BÔNUS] Correção no Webhook (Webhooks.php):

  • Sintoma: Erro "Attempt to read property metadata on null" ao receber webhooks.
  • Correção: Adicionada validação de null coalescing (?? null) ao acessar $body->data->code e $body->data->order->metadata, prevenindo erros fatais quando o payload do webhook não traz esses dados.

Copilot AI review requested due to automatic review settings January 16, 2026 16:17
@backstage-catalog-validator
Copy link

⚠️ Este repositório ainda não está catalogado no Backstage. ⚠️

Por favor, catalogue-o seguindo as instruções nesta documentação. [Via VPN].

💁 Qualquer problema ou dúvida, estamos no Slack, basta abrir um ticket no canal #help-foundation-platform.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses critical stability issues in the WooCommerce Pagar.me payment module, specifically fixing JSON response handling in the checkout installments Ajax endpoint and adding null safety to webhook processing.

Changes:

  • Backend checkout endpoint now uses wp_send_json() with output buffer cleaning instead of manual JSON encoding
  • Frontend Ajax handler no longer double-parses JSON responses
  • Webhook handler adds null coalescing for potentially missing payload properties

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/Controller/Webhooks.php Adds null coalescing operators to prevent null pointer errors when accessing webhook payload properties
src/Controller/Checkout.php Replaces manual JSON encoding with WordPress wp_send_json() function and adds output buffer cleaning
assets/javascripts/front/checkout/model/payment/card.js Removes redundant JSON.parse() call and applies code style formatting improvements

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return;
}
if (!$this->orderByWoocommerce($body->data->code, $body->data->order->metadata, $body->id) ) {
$code = $body->data->code ?? null;
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The null coalescing operator ?? does not protect against accessing properties on null objects. If $body->data is null, this line will throw "Attempt to read property 'code' on null" error before the ?? operator can apply.

Consider adding an explicit check for $body->data first, or use PHP 8.0+ null-safe operator: $code = $body?->data?->code ?? null; or $code = isset($body->data->code) ? $body->data->code : null;

Copilot uses AI. Check for mistakes.
}
if (!$this->orderByWoocommerce($body->data->code, $body->data->order->metadata, $body->id) ) {
$code = $body->data->code ?? null;
$metadata = $body->data->order->metadata ?? null;
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The null coalescing operator on line 65 does not fully protect against null pointer errors. If $body->data is null, accessing $body->data->order will still throw "Attempt to read property 'order' on null" error before the ?? operator can apply.

Consider adding an explicit check: $metadata = isset($body->data->order->metadata) ? $body->data->order->metadata : null; or use the null-safe operator if PHP 8.0+ is available: $metadata = $body?->data?->order?->metadata ?? null;

Suggested change
$metadata = $body->data->order->metadata ?? null;
$metadata = isset($body->data->order->metadata) ? $body->data->order->metadata : null;

Copilot uses AI. Check for mistakes.
}
};
pagarmeCard.start();
pagarmeCard.start();
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

There is a trailing whitespace at the end of this line. Trailing whitespaces should be removed to maintain code cleanliness and avoid unnecessary git diff noise.

Suggested change
pagarmeCard.start();
pagarmeCard.start();

Copilot uses AI. Check for mistakes.
echo json_encode([

// Ensure no previous output contaminates the JSON response
if (ob_get_length()) {
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The ob_get_length() function returns false if output buffering is not active, which will cause the condition to be falsy and skip the ob_clean() call. However, calling ob_clean() when no buffer is active will generate a notice. The current implementation is safe, but consider using if (ob_get_level() > 0) instead to be more explicit about checking if output buffering is active.

Suggested change
if (ob_get_length()) {
if (ob_get_level() > 0) {

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant