我想在我的 woocommerce 插件中为 paypal 的网络钩子处理程序添加验证。反应总是失败

问题描述 投票:0回答:1

我可以成功捕获来自 paypal 的 webhook 触发器。但我无法验证签名。

enter image description here 这是图片,我实际上如何将数据传输到 paypal url 进行验证。我还提供了代码。

 $raw_data = file_get_contents("php://input");
        
        // Get the headers
        $headers = getallheaders();
    
        // Decode the raw data
        $decoded_data = json_decode($raw_data, true);


this is code i use to get data from the event

$headers=> paypal 通知中的标题

$decoded_data=> 正文

$webhook_id=> id 表单 paypal 仪表板


and this is the code to send the request 

public function eh_verify_webhook_signature_paypal($headers, $decoded_data, $webhook_id) {
        // Extract necessary data for signature verification
        $transmission_id = isset($headers['Paypal-Transmission-Id']) ? $headers['Paypal-Transmission-Id'] : null;
        $transmission_time = isset($headers['Paypal-Transmission-Time']) ? $headers['Paypal-Transmission-Time'] : null;
        $cert_url = isset($headers['Paypal-Cert-Url']) ? $headers['Paypal-Cert-Url'] : null;
        $auth_algo = isset($headers['Paypal-Auth-Algo']) ? $headers['Paypal-Auth-Algo'] : null;
        $transmission_sig = isset($headers['Paypal-Transmission-Sig']) ? $headers['Paypal-Transmission-Sig'] : null;
    
        // Additional data from decoded_data
        $event_details = isset($decoded_data['resource']) ? $decoded_data['resource'] : null;
    
        $webhook_event = array(
            'id' => $decoded_data['id'],
            'create_time' => $decoded_data['create_time'],
            'resource_type' => $decoded_data['resource_type'],
            'event_type' => $decoded_data['event_type'],
            'summary' => $decoded_data['summary'],
            'event_version' => $decoded_data['event_version'], // Add event version
            'resource_version' => $decoded_data['resource_version'], // Add resource version
            'resource' => array(
                'id' => $event_details['id'],
                'create_time' => $event_details['create_time'],
                //'state' => $event_details['state'],
                'amount' => $event_details['amount'],
            ),
        );
    
        // Perform the verification and return the response
        $verification_response = $this->perform_webhook_signature_verification(
            $transmission_id,
            $transmission_sig,
            $transmission_time,
            $webhook_event,
            $cert_url,
            $auth_algo,
            $webhook_id
        );
    
        return $verification_response;
    }
    
    
    
    public function perform_webhook_signature_verification($transmission_id, $transmission_sig, $transmission_time, $webhook_event, $cert_url, $auth_algo, $webhook_id) {
        // Get the access token
        $request_process = new Eh_PE_Process_Request();
        $request_build = $this->new_rest_request();
        $this->access_token = $this->get_access_token($request_process, $request_build);
    
        if (!$this->access_token) {
            wc_add_notice(__('An error occurred, We were unable to process your order, please try again.', 'eh-paypal-express'), 'error');
            return false;
        }
    
        // URL for verification
        $api_endpoint = 'https://api-m.sandbox.paypal.com/v1/notifications/verify-webhook-signature';
    
        $args = array(
            'method' => 'POST', // Change to 'POST' if necessary
            'headers' => array(
                'Authorization' => 'Bearer ' . $this->access_token,
                'Content-Type' => 'application/json',
            ),
            'body' => wp_json_encode(
                array(
                    'transmission_id' => $transmission_id,
                    'transmission_time' => $transmission_time,
                    'cert_url' => $cert_url,
                    'auth_algo' => $auth_algo,
                    'transmission_sig' => $transmission_sig,
                    'webhook_id' => $webhook_id,
                    'webhook_event' => $webhook_event,
                )
            ),
        );
    
        $response = wp_remote_post($api_endpoint, $args);
    

    
        // Check if the request was successful
        if (!is_wp_error($response)) {
            // Get the response body
            $body = wp_remote_retrieve_body($response);
    
            // Get the response headers
            $response_headers = wp_remote_retrieve_headers($response);
    
            // Decode the JSON response
            $response_data = json_decode($body, true);
    
            // Log the request data, headers, response data, and access token
            wc_get_logger()->debug('Request Data: ' . print_r($args, true), array('source' => 'AAA_verification_subscription_eh_stripe_express_log'));
           // wc_get_logger()->debug('Request Headers: ' . print_r($headers, true), array('source' => 'AAA_verification_subscription_eh_stripe_express_log'));
            wc_get_logger()->debug('Response Headers: ' . print_r($response_headers, true), array('source' => 'AAA_verification_subscription_eh_stripe_express_log'));
            wc_get_logger()->debug('Response Data: ' . print_r($response_data, true), array('source' => 'AAA_verification_subscription_eh_stripe_express_log'));
            wc_get_logger()->debug('Access Token: ' . $this->access_token, array('source' => 'AAA_verification_subscription_eh_stripe_express_log'));
    
            // Check if the verification was successful
            if (isset($response_data['verification_status']) && $response_data['verification_status'] === 'SUCCESS') {
                return true;
            } else {
                // Log the error details
                if (isset($response_data['details'])) {
                    wc_get_logger()->debug('Error Details: ' . print_r($response_data['details'], true), array('source' => 'AAA_verification_subscription_eh_stripe_express_log'));
                }
    
                // Handle verification failure
                return false;
            }
        } else {
            // Handle request error
            $error_message = $response->get_error_message();
            wc_get_logger()->debug('Request Error: ' . $error_message, array('source' => 'AAA_verification_subscription_eh_stripe_express_log'));
            return false;
        }
    }
    
php woocommerce plugins paypal webhooks
1个回答
0
投票

webhook_event
不应由您建造或组装。它应该是您收到的
raw_data
字符串,按位相同,没有任何改变。

© www.soinside.com 2019 - 2024. All rights reserved.