public function process_payment( $order_id ) { $order = wc_get_order( $order_id ); // 1) Monta array de produtos com subtotal da linha $items = $order->get_items(); $products = array(); foreach ( $items as $item_id => $item ) { $product_id = $item->get_product_id(); $product_name = $item->get_name(); $quantity = intval( $item->get_quantity() ); // Subtotal da linha (quantidade × preço unitário) $line_subtotal = floatval( $order->get_line_subtotal( $item, false ) ); $products[] = array( 'id' => (string) $product_id, 'name' => $product_name, 'quantity' => $quantity, 'price' => $line_subtotal, // Subtotal da linha ); } // 2) Recupera e sanitiza CPF (somente dígitos) $raw_document = isset( $_POST['billing_cpf'] ) ? wc_clean( $_POST['billing_cpf'] ) : ''; $documentDigits = preg_replace( '/\D/', '', $raw_document ); // 3) Calcula valores do pedido $subtotal = floatval( $order->get_subtotal() ); // R$ 319,92 $total = floatval( $order->get_total() ); // R$ 271,93 $shipping_fee = floatval( $order->get_shipping_total() ); // R$ 0,00 $discount = $subtotal - $total + $shipping_fee; // R$ 47,99 // 4) Monta o payload - Testando diferentes abordagens $payload = array( 'identifier' => $this->generate_identifier(), 'amount' => $subtotal, // Subtotal dos produtos 'shippingFee' => $shipping_fee, // Taxa de entrega 'extraFee' => 0.0, 'discount' => $discount, // Desconto aplicado 'client' => array( 'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), 'email' => $order->get_billing_email(), 'phone' => $order->get_billing_phone(), 'document' => $documentDigits, ), 'products' => $products, 'splits' => array(), 'dueDate' => date( 'Y-m-d', strtotime( '+' . apply_filters( 'wc_safirapay_pix_due_days', 1 ) . ' days' ) ), 'metadata' => new stdClass(), 'callbackUrl' => esc_url_raw( $this->get_return_url( $order ) ), ); $json_payload = wp_json_encode( $payload, JSON_UNESCAPED_UNICODE ); $endpoint = 'https://app.safirapag.com/api/v1/gateway/pix/receive'; // 5) Debug detalhado $debug_info = array( 'order_id' => $order_id, 'subtotal_wc' => $subtotal, 'total_wc' => $total, 'shipping_wc' => $shipping_fee, 'discount_calculated' => $discount, 'validation' => array( 'subtotal_minus_discount_plus_shipping' => ($subtotal - $discount + $shipping_fee), 'should_equal_total' => $total, 'matches' => (abs(($subtotal - $discount + $shipping_fee) - $total) < 0.01) ? 'YES' : 'NO' ), 'payload' => $payload ); $debug_path = ABSPATH . 'safirapay_debug.txt'; file_put_contents( $debug_path, "=== TENTATIVA " . date('Y-m-d H:i:s') . " ===\n" . print_r($debug_info, true) . "\n\n", FILE_APPEND ); // 6) Chamada à API $args = array( 'method' => 'POST', 'timeout' => 45, 'headers' => array( 'Content-Type' => 'application/json', 'x-public-key' => $this->public_key, 'x-secret-key' => $this->secret_key, ), 'body' => $json_payload, ); $response = wp_remote_post( $endpoint, $args ); $http_code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $result = json_decode( $body, true ); // 7) Log da resposta da API file_put_contents( ABSPATH . 'safirapay_response.txt', "=== RESPONSE " . date('Y-m-d H:i:s') . " ===\n" . "HTTP Code: " . $http_code . "\n" . "Response: " . $body . "\n\n", FILE_APPEND ); // 8) Verifica erros de conexão if ( is_wp_error( $response ) ) { if ( $this->logging ) { error_log( 'Erro SafiraPay PIX (WP_Error): ' . $response->get_error_message() ); } wc_add_notice( __( 'SafiraPay PIX falhou: Problema de conexão.', 'wc-safirapay' ), 'error' ); return array( 'result' => 'failure' ); } // 9) Aceita HTTP 200 OU 201 como sucesso if ( $http_code !== 200 && $http_code !== 201 ) { if ( $this->logging ) { error_log( sprintf( 'Erro SafiraPay PIX – HTTP Code: %s', $http_code ) ); error_log( sprintf( 'Erro SafiraPay PIX – Response body: %s', $body ) ); } $mensagem = 'SafiraPay PIX falhou: '; if ( isset( $result['errorCode'] ) && isset( $result['message'] ) ) { $mensagem .= $result['errorCode'] . ' – ' . $result['message']; if ( isset( $result['details'] ) ) { file_put_contents( ABSPATH . 'pix_error_details.txt', print_r( $result['details'], true ) ); $mensagem .= ' Detalhes: ' . wp_json_encode( $result['details'] ); } } else { $mensagem .= 'Erro genérico ao criar cobrança PIX.'; } wc_add_notice( $mensagem, 'error' ); return array( 'result' => 'failure' ); } // 10) Sucesso update_post_meta( $order_id, '_safirapay_pix_response', $result ); $order->update_status( 'on-hold', __( 'Aguardando pagamento PIX', 'wc-safirapay' ) ); wc_reduce_stock_levels( $order_id ); WC()->cart->empty_cart(); return array( 'result' => 'success', 'redirect' => $this->get_return_url( $order ), ); } https://kinzancosmeticos.com/wp-sitemap-posts-page-1.xmlhttps://kinzancosmeticos.com/wp-sitemap-posts-product-1.xmlhttps://kinzancosmeticos.com/wp-sitemap-taxonomies-woodmart_slider-1.xmlhttps://kinzancosmeticos.com/wp-sitemap-taxonomies-product_brand-1.xmlhttps://kinzancosmeticos.com/wp-sitemap-taxonomies-product_cat-1.xmlhttps://kinzancosmeticos.com/wp-sitemap-taxonomies-product_tag-1.xmlhttps://kinzancosmeticos.com/wp-sitemap-users-1.xml