Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active May 18, 2023 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save damiencarbery/7290049bc799d6a98f96abe0cdc8a553 to your computer and use it in GitHub Desktop.
Save damiencarbery/7290049bc799d6a98f96abe0cdc8a553 to your computer and use it in GitHub Desktop.
WooCommerce REST API authentication: Use Oauth 1 and the WooCommerce REST API to retrieve order details. https://www.damiencarbery.com/2019/06/woocommerce-rest-api-authentication/
{
"id": 7755,
"parent_id": 0,
"number": "7755",
"order_key": "wc_order_YjITxJBWgJIFu",
"created_via": "programatically",
"version": "3.6.4",
"status": "completed",
"currency": "EUR",
"date_created": "2019-06-11T08:58:35",
"date_created_gmt": "2019-06-11T08:58:35",
"date_modified": "2019-06-11T08:58:36",
"date_modified_gmt": "2019-06-11T08:58:36",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "42.00",
"total_tax": "0.00",
"prices_include_tax": false,
"customer_id": 8,
"customer_ip_address": "::1",
"customer_user_agent": "PostmanRuntime/7.13.0",
"customer_note": "",
"billing": {
"first_name": "John",
"last_name": "Smith",
"company": "",
"address_1": "1234 Main Street",
"address_2": "",
"city": "Auckland",
"state": "NA",
"postcode": "3345",
"country": "NZ",
"email": "jsmith@example.com",
"phone": ""
},
"shipping": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"payment_method": "bacs",
"payment_method_title": "Direct bank transfer",
"transaction_id": "",
"date_paid": "2019-06-11T08:58:36",
"date_paid_gmt": "2019-06-11T08:58:36",
"date_completed": "2019-06-11T08:58:36",
"date_completed_gmt": "2019-06-11T08:58:36",
"cart_hash": "",
"meta_data": [],
"line_items": [
{
"id": 20,
"name": "Hoodie",
"product_id": 33,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"subtotal": "42.00",
"subtotal_tax": "0.00",
"total": "42.00",
"total_tax": "0.00",
"taxes": [],
"meta_data": [],
"sku": "",
"price": 42
}
],
"tax_lines": [],
"shipping_lines": [],
"fee_lines": [],
"coupon_lines": [],
"refunds": [],
"_links": {
"self": [
{
"href": "http://localhost/storefront/wp-json/wc/v3/orders/7755"
}
],
"collection": [
{
"href": "http://localhost/storefront/wp-json/wc/v3/orders"
}
],
"customer": [
{
"href": "http://localhost/storefront/wp-json/wc/v3/customers/8"
}
]
}
}
<?php
function join_params( $params ) {
$query_params = array();
foreach ( $params as $param_key => $param_value ) {
$string = $param_key . '=' . $param_value;
$query_params[] = str_replace( array( '+', '%7E' ), array( ' ', '~' ), rawurlencode( $string ) );
}
return implode( '%26', $query_params );
}
// WooCommerce REST API keys. Update these with your keys.
$consumer_key = 'ck_8d8b5e0ee1d65081f1270736db585dc62d3a3e88';
$consumer_secret = 'cs_c483bc1b79a0e81c15763cc990bdf8f2cb7f0020';
// Request URI.
$request_uri = 'http://localhost/storefront/wp-json/wc/v3/orders/7753';
// Unique once-off parameters.
$nonce = uniqid();
$timestamp = time();
$oauth_signature_method = 'HMAC-SHA1';
$hash_algorithm = strtolower( str_replace( 'HMAC-', '', $oauth_signature_method ) ); // sha1
$secret = $consumer_secret . '&';
$http_method = 'GET';
$base_request_uri = rawurlencode( $request_uri );
$params = array( 'oauth_consumer_key' => $consumer_key, 'oauth_nonce' => $nonce, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => $timestamp );
$query_string = join_params( $params );
$string_to_sign = $http_method . '&' . $base_request_uri . '&' . $query_string;
$oauth_signature = base64_encode( hash_hmac( $hash_algorithm, $string_to_sign, $secret, true ) );
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $request_uri,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $http_method,
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: OAuth oauth_consumer_key=\"".$consumer_key."\",oauth_signature_method=\"".$oauth_signature_method."\",oauth_timestamp=\"".$timestamp."\",oauth_nonce=\"".$nonce."\",oauth_signature=\"".$oauth_signature."\"",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: localhost",
"User-Agent: PostmanRuntime/7.13.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment