Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3663

WooCommerce REST Client API - Not able to hit create orders webhook without consumer key and consumer key is hashed?

$
0
0

I am trying to create a webhook for orders create in a plugin settings. But unfortunately i am not able to hit the api without consumer key and consumer key in wp_woocommerce_api_keys table is hashed its not that which i got during creation of rest apis.

function wc_api_generate_api_keys($user_id) {    global $wpdb;    $table_name = $wpdb->prefix . 'woocommerce_api_keys';    $existing_api_key = $wpdb->get_row(        $wpdb->prepare("SELECT consumer_secret FROM $table_name WHERE user_id = %d LIMIT 1", $user_id)    );    // Check if consumer_secret exists for the user    if ($existing_api_key && $existing_api_key->consumer_secret) {        $consumer_secret = $existing_api_key->consumer_secret;    } else {        return new WP_Error('no_consumer_secret', 'No consumer secret found for the user.');    }    // Generate a new consumer_key    $consumer_key = 'ck_' . wc_rand_hash();    // Update only the consumer_key in the WooCommerce API key table    $wpdb->update(        $table_name,        array('consumer_key'  => $consumer_key,'truncated_key' => substr($consumer_key, -7),         ),        array('user_id' => $user_id)    );    return array('consumer_key'    => $consumer_key,'consumer_secret' => $consumer_secret,    );}// Register the webhookfunction register_wc_order_created_webhook($consumer_key, $consumer_secret, $site_url) {    $webhook_url = 'http://127.0.0.1:8000/wordpress/webhook/order-created';    $response = wp_remote_post("$site_url/wp-json/wc/v3/webhooks", ['headers' => ['Content-Type' => 'application/json','Authorization' => 'Basic ' . base64_encode("$consumer_key:$consumer_secret"),        ],'body' => json_encode(['name' => 'Webhook Orders','topic' => 'order.created','delivery_url' => $webhook_url,'status' => 'active'        ]),    ]);    if (is_wp_error($response)) {        error_log('Error registering webhook: ' . $response->get_error_message());    } else {        $response_body = wp_remote_retrieve_body($response);        error_log('Webhook registered successfully: ' . $response_body);        // Decode the response to inspect it further        $decoded_response = json_decode($response_body, true);        error_log('Decoded response: ' . print_r($decoded_response, true));    }}

Viewing all articles
Browse latest Browse all 3663

Trending Articles