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 fixes a critical bug where AJAX responses in the checkout process were returning malformed JSON due to PHP warnings or HTML output being mixed with JSON data, causing SyntaxError: Unexpected token '<' errors in the frontend.
Changes:
- Replaced
echo json_encode()with WordPress's nativewp_send_json()function for proper JSON responses - Added output buffer cleaning before sending JSON to prevent contamination from previous output
- Added defensive null coalescing operators to prevent potential errors when accessing webhook data properties
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Controller/Checkout.php | Fixed AJAX JSON response handling by using wp_send_json() instead of manual echo json_encode(), added output buffer cleaning, and removed redundant exit() call |
| src/Controller/Webhooks.php | Added null coalescing operators to safely access potentially undefined webhook data properties |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $code = $body->data->code ?? null; | ||
| $metadata = $body->data->order->metadata ?? null; | ||
|
|
||
| if (!$this->orderByWoocommerce($code, $metadata, $body->id) ) { |
There was a problem hiding this comment.
Extra space before closing parenthesis. Remove the space to maintain consistent code formatting throughout the codebase.
| if (!$this->orderByWoocommerce($code, $metadata, $body->id) ) { | |
| if (!$this->orderByWoocommerce($code, $metadata, $body->id)) { |
Qual o tipo de PR é esse? (marque todos os aplicáveis)
Descrição
Este PR corrige um erro crítico (
SyntaxError: Unexpected token '<') que ocorria no frontend durante o checkout.A action AJAX
xqRhBHJ5sW(responsável por buscar as configurações de parcelamento) estava retornando HTML ou avisos PHP misturados com o JSON, o que quebrava oJSON.parseno JavaScript card.js.Alterações realizadas:
echo json_encode(...)pela função nativa do WordPresswp_send_json(...), que garante os headers corretos e encerra a execução adequadamente.ob_clean()antes do envio da resposta para limpar qualquer "sujeira" (notices, espaços em branco, hooks de outros plugins) que possa ter sido impressa no buffer de saída antes do JSON.