Conversation
This commit adds safe property access (null coalescing) for webhook payload data, specifically preventing errors when 'data->order' is missing in the webhook body.
|
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. |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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;
| } | ||
| if (!$this->orderByWoocommerce($body->data->code, $body->data->order->metadata, $body->id) ) { | ||
| $code = $body->data->code ?? null; | ||
| $metadata = $body->data->order->metadata ?? null; |
There was a problem hiding this comment.
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;
| $metadata = $body->data->order->metadata ?? null; | |
| $metadata = isset($body->data->order->metadata) ? $body->data->order->metadata : null; |
| } | ||
| }; | ||
| pagarmeCard.start(); | ||
| pagarmeCard.start(); |
There was a problem hiding this comment.
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.
| pagarmeCard.start(); | |
| pagarmeCard.start(); |
| echo json_encode([ | ||
|
|
||
| // Ensure no previous output contaminates the JSON response | ||
| if (ob_get_length()) { |
There was a problem hiding this comment.
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.
| if (ob_get_length()) { | |
| if (ob_get_level() > 0) { |
Qual o tipo de PR é esse? (marque todos os aplicáveis)
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):
SyntaxErrorao carregar as parcelas do cartão ("Unexpected token <" ou "[object Object] is not valid JSON").echo json_encode(...)+exit()porob_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 headerContent-Type: application/jsoncorreto.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). OJSON.parseredundante forçava a conversão para string "[object Object]" causando o erro.[BÔNUS] Correção no Webhook (
Webhooks.php):?? null) ao acessar$body->data->codee$body->data->order->metadata, prevenindo erros fatais quando o payload do webhook não traz esses dados.