php - Need to forward Data from WooCommerce Webhook sent to same site Wordpress REST API custom endpoint

admin2025-01-07  4

I created a Wordpress REST API custom endpoint to be used as a Webhook URL in WooCommerce in order to convert the received data and then send it to a third party system, but the endpoint apparently is not receiving any data. I tested the code by sending some JSON data to my custom endpoint using Postman and it works only after installing another P;ugin to enable Basic Auth. I wonder if the problem is because probably the webhook needs authentication in order to be able to POST the data to the same domain? If that would be the case I have no idea where to setup basic authentication in the Webhook setting in WooCommerce.

this is my code:

    function woocbz_CallAPI($method, $url, $data = false)
    {
        $curl = curl_init();

        switch ($method) {
            case "POST":
                curl_setopt($curl, CURLOPT_POST, 1);

                if ($data) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                }
                break;
            case "PUT":
                curl_setopt($curl, CURLOPT_PUT, 1);
                break;
            default:
                if ($data) {
                    $url = sprintf("%s?%s", $url, http_build_query($data));
                }
        }

        // Optional Authentication:
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, "username:password");

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($curl);

        curl_close($curl);

        return $result;
    }

    function woocbz_order_created(WP_REST_Request $request)
    {
        $method = 'POST';
        $url = '/2e5e23db-68ef-4d03-b9ec-687309d35166';

        $data = $request->get_json_params();

        $user_data = array(
            'order_id' => $data['id'],
            'memo'     => $data['order_key'],
            'status'   => $data['status']
        );

        $resultado = woocbz_CallAPI($method, $url, $user_data);

        return $data;
    }

    add_action(
        'rest_api_init',
        function () {
            register_rest_route(
                'woocbz/v1',
                '/new-order',
                array(
                    'methods'  => 'POST',
                    'callback' => 'woocbz_order_created',
                    'permission_callback' => function () {
                        return true;
                    }
                )
            );
        }
    );

Any help will be greatly appreciated.

I created a Wordpress REST API custom endpoint to be used as a Webhook URL in WooCommerce in order to convert the received data and then send it to a third party system, but the endpoint apparently is not receiving any data. I tested the code by sending some JSON data to my custom endpoint using Postman and it works only after installing another P;ugin to enable Basic Auth. I wonder if the problem is because probably the webhook needs authentication in order to be able to POST the data to the same domain? If that would be the case I have no idea where to setup basic authentication in the Webhook setting in WooCommerce.

this is my code:

    function woocbz_CallAPI($method, $url, $data = false)
    {
        $curl = curl_init();

        switch ($method) {
            case "POST":
                curl_setopt($curl, CURLOPT_POST, 1);

                if ($data) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                }
                break;
            case "PUT":
                curl_setopt($curl, CURLOPT_PUT, 1);
                break;
            default:
                if ($data) {
                    $url = sprintf("%s?%s", $url, http_build_query($data));
                }
        }

        // Optional Authentication:
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, "username:password");

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($curl);

        curl_close($curl);

        return $result;
    }

    function woocbz_order_created(WP_REST_Request $request)
    {
        $method = 'POST';
        $url = 'https://webhook.site/2e5e23db-68ef-4d03-b9ec-687309d35166';

        $data = $request->get_json_params();

        $user_data = array(
            'order_id' => $data['id'],
            'memo'     => $data['order_key'],
            'status'   => $data['status']
        );

        $resultado = woocbz_CallAPI($method, $url, $user_data);

        return $data;
    }

    add_action(
        'rest_api_init',
        function () {
            register_rest_route(
                'woocbz/v1',
                '/new-order',
                array(
                    'methods'  => 'POST',
                    'callback' => 'woocbz_order_created',
                    'permission_callback' => function () {
                        return true;
                    }
                )
            );
        }
    );

Any help will be greatly appreciated.

Share Improve this question asked Jun 21, 2019 at 19:38 Adalberto Hernandez VegaAdalberto Hernandez Vega 12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I figured out it could be an Authorization problem, then I looked the WooCommerce webhook functions and fount where the delivery is executed: /wp-content/plugins/woocommerce/includes/class-wc-webhook.php I hard coded my Authorization header after line # 303 in the deliver() function:

'headers'     => array(
    'Content-Type'  => 'application/json',
    'Authorization' => 'Basic bmNkaWgdrGFsOiNIb25kdXJhczIwMThTUFMh',
),

Now I need to calculate the Authorization Basic token based on the id of the user who created the Webhook who is authorized in order to make this solution flexible.

Rather than modifying the plugin, you can use the woocommerce_webhook_http_args filter to add the Authorization header:

add_filter( 'woocommerce_webhook_http_args', 'add_auth_header' );

function add_auth_header($http_args) {
  $http_args['headers']['Authorization'] = 'Basic bmNkaWgdrGFsOiNIb25kdXJhczIwMThTUFMh';
  return $http_args;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256440a360.html

最新回复(0)