I have enabled webhook on Shopify. Shopify sends raw JSON in webhook response. Somehow it is converted to map and now I want to convert it back to raw JSON that Shopify sends for HMAC verification. Can someone help how it can be done in JAVA? What encoding is applied to data at Shopify end?
Shopify Webhook JSON:
{"id":4700248899822,"admin_graphql_api_id":"gid:\/\/shopify\/Order\/4700248899822","app_id":1758145,"browser_ip":null,"buyer_accepts_marketing":false,"cancel_reason":null,"cancelled_at":null,"cart_token":null,"checkout_id":null,"checkout_token":null,"closed_at":null,"confirmed":true,"contact_email":"[email protected]","created_at":"2022-03-18T22:44:54 00:00","currency":"GBP","current_subtotal_price":"4924.99","current_subtotal_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"current_total_discounts":"0.00","current_total_discounts_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"current_total_duties_set":null,"current_total_price":"4929.98","current_total_price_set":{"shop_money":{"amount":"4929.98","currency_code":"GBP"},"presentment_money":{"amount":"4929.98","currency_code":"GBP"}},"current_total_tax":"0.00","current_total_tax_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"customer_locale":null,"device_id":null,"discount_codes":[],"email":"[email protected]","estimated_taxes":false,"financial_status":"paid","fulfillment_status":null,"gateway":"manual","landing_site":null,"landing_site_ref":null,"location_id":null,"name":"27776","note":null,"note_attributes":[],"number":19545,"order_number":20545,"order_status_url":"https:\/\/www.shop.co.uk\/60948381934\/orders\/fa7e0285eed82f571ad456ded44f697e\/authenticate?key=dcff94c973659a1be74fb3d0f59424fd","original_total_duties_set":null,"payment_gateway_names":["manual"],"phone":null,"presentment_currency":"GBP","processed_at":"2022-03-18T22:44:54 00:00","processing_method":"","reference":null,"referring_site":null,"source_identifier":null,"source_name":"Matrixify App","source_url":null,"subtotal_price":"4924.99","subtotal_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"tags":"payment:fully-paid","tax_lines":[],"taxes_included":false,"test":false,"token":"fa7e0285eed82f571ad456ded44f697e","total_discounts":"0.00","total_discounts_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"total_line_items_price":"4924.99","total_line_items_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"total_outstanding":"0.00","total_price":"4929.98","total_price_set":{"shop_money":{"amount":"4929.98","currency_code":"GBP"},"presentment_money":{"amount":"4929.98","currency_code":"GBP"}},"total_price_usd":"6487.16","total_shipping_price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"total_tax":"0.00","total_tax_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"total_tip_received":"0.00","total_weight":0,"updated_at":"2022-07-12T10:41:17 01:00","user_id":null,"billing_address":{"first_name":"Mark","address1":"Captains Row","phone":"07989408757","city":"Lymington","zip":"SO41 9RR","province":"England","country":"United Kingdom","last_name":"Pidsley","address2":null,"company":null,"latitude":50.7584704,"longitude":-1.537493,"name":"Mark Pidsley","country_code":"GB","province_code":"ENG"},"customer":{"id":6102973153518,"email":"[email protected]","accepts_marketing":false,"created_at":"2022-03-18T03:18:48 00:00","updated_at":"2022-07-12T10:41:17 01:00","first_name":"Mark","last_name":"Pidsley","orders_count":3,"state":"invited","total_spent":"9896.94","last_order_id":4700248899822,"note":null,"verified_email":true,"multipass_identifier":null,"tax_exempt":false,"phone":null,"tags":"first-order:fully-paid, Guest","last_order_name":"27776","currency":"GBP","accepts_marketing_updated_at":"2022-03-18T03:18:48 00:00","marketing_opt_in_level":null,"tax_exemptions":[],"sms_marketing_consent":null,"admin_graphql_api_id":"gid:\/\/shopify\/Customer\/6102973153518","default_address":{"id":7602051678446,"customer_id":6102973153518,"first_name":"Mark","last_name":"Pidsley","company":"","address1":"Captains Row","address2":"","city":"Lymington","province":"England","country":"United Kingdom","zip":"SO41 9RR","phone":"07989408757","name":"Mark Pidsley","province_code":"ENG","country_code":"GB","country_name":"United Kingdom","default":true}},"discount_applications":[],"fulfillments":[],"line_items":[{"id":12045659570414,"admin_graphql_api_id":"gid:\/\/shopify\/LineItem\/12045659570414","fulfillable_quantity":1,"fulfillment_service":"manual","fulfillment_status":null,"gift_card":false,"grams":0,"name":"Cube Stereo Hybrid 160 HPC SL 2022 Bosch - XL 55cm","price":"4899.00","price_set":{"shop_money":{"amount":"4899.00","currency_code":"GBP"},"presentment_money":{"amount":"4899.00","currency_code":"GBP"}},"product_exists":true,"product_id":7570044977390,"properties":[{"name":"Pre-Order","value":""},{"name":"Frame Size (Check stock below)","value":"55cm"}],"quantity":1,"requires_shipping":true,"sku":"53710322","taxable":false,"title":"Cube Stereo Hybrid 160 HPC SL 2022 Bosch","total_discount":"0.00","total_discount_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"variant_id":42482856460526,"variant_inventory_management":"shopify","variant_title":"XL 55cm","vendor":"","tax_lines":[],"duties":[],"discount_allocations":[]},{"id":12045659603182,"admin_graphql_api_id":"gid:\/\/shopify\/LineItem\/12045659603182","fulfillable_quantity":1,"fulfillment_service":"manual","fulfillment_status":null,"gift_card":false,"grams":0,"name":"KMC Bosch Gen 4 38t Chainring (9mm Offset)","price":"25.99","price_set":{"shop_money":{"amount":"25.99","currency_code":"GBP"},"presentment_money":{"amount":"25.99","currency_code":"GBP"}},"product_exists":true,"product_id":7569830576366,"properties":[],"quantity":1,"requires_shipping":true,"sku":"BFCB45238","taxable":false,"title":"KMC Bosch Gen 4 38t Chainring (9mm Offset)","total_discount":"0.00","total_discount_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"variant_id":42435842703598,"variant_inventory_management":"shopify","variant_title":null,"vendor":"","tax_lines":[],"duties":[],"discount_allocations":[]}],"payment_terms":null,"refunds":[],"shipping_address":{"first_name":"Mark","address1":"Captains Row","phone":"07989408757","city":"Lymington","zip":"SO41 9RR","province":"England","country":"United Kingdom","last_name":"Pidsley","address2":"","company":"","latitude":50.7584704,"longitude":-1.537493,"name":"Mark Pidsley","country_code":"GB","province_code":"ENG"},"shipping_lines":[{"id":3913660694766,"carrier_identifier":null,"code":"UK E\u0026amp;W Zone A (Weight: 24.75kg)","delivery_category":null,"discounted_price":"4.99","discounted_price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"phone":null,"price":"4.99","price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"requested_fulfillment_service_id":null,"source":"shopify","title":"UK E\u0026amp;W Zone A (Weight: 24.75kg)","tax_lines":[],"discount_allocations":[]}]}
JSON converted to map:
{"id":4700248899822,"admin_graphql_api_id":"gid://shopify/Order/4700248899822","app_id":1758145,"browser_ip":null,"buyer_accepts_marketing":false,"cancel_reason":null,"cancelled_at":null,"cart_token":null,"checkout_id":null,"checkout_token":null,"closed_at":null,"confirmed":true,"contact_email":"[email protected]","created_at":"2022-03-18T22:44:54 00:00","currency":"GBP","current_subtotal_price":"4924.99","current_subtotal_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"current_total_discounts":"0.00","current_total_discounts_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"current_total_duties_set":null,"current_total_price":"4929.98","current_total_price_set":{"shop_money":{"amount":"4929.98","currency_code":"GBP"},"presentment_money":{"amount":"4929.98","currency_code":"GBP"}},"current_total_tax":"0.00","current_total_tax_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"customer_locale":null,"device_id":null,"discount_codes":[],"email":"[email protected]","estimated_taxes":false,"financial_status":"paid","fulfillment_status":null,"gateway":"manual","landing_site":null,"landing_site_ref":null,"location_id":null,"name":"27776","note":null,"note_attributes":[],"number":19545,"order_number":20545,"order_status_url":"https://www.shop.co.uk/60948381934/orders/fa7e0285eed82f571ad456ded44f697e/authenticate?key=dcff94c973659a1be74fb3d0f59424fd","original_total_duties_set":null,"payment_gateway_names":["manual"],"phone":null,"presentment_currency":"GBP","processed_at":"2022-03-18T22:44:54 00:00","processing_method":"","reference":null,"referring_site":null,"source_identifier":null,"source_name":"Matrixify App","source_url":null,"subtotal_price":"4924.99","subtotal_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"tags":"payment:fully-paid","tax_lines":[],"taxes_included":false,"test":false,"token":"fa7e0285eed82f571ad456ded44f697e","total_discounts":"0.00","total_discounts_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"total_line_items_price":"4924.99","total_line_items_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"total_outstanding":"0.00","total_price":"4929.98","total_price_set":{"shop_money":{"amount":"4929.98","currency_code":"GBP"},"presentment_money":{"amount":"4929.98","currency_code":"GBP"}},"total_price_usd":"6487.16","total_shipping_price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"total_tax":"0.00","total_tax_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"total_tip_received":"0.00","total_weight":0,"updated_at":"2022-07-12T10:41:17 01:00","user_id":null,"billing_address":{"first_name":"Mark","address1":"Captains Row","phone":"07989408757","city":"Lymington","zip":"SO41 9RR","province":"England","country":"United Kingdom","last_name":"Pidsley","address2":null,"company":null,"latitude":50.7584704,"longitude":-1.537493,"name":"Mark Pidsley","country_code":"GB","province_code":"ENG"},"customer":{"id":6102973153518,"email":"[email protected]","accepts_marketing":false,"created_at":"2022-03-18T03:18:48 00:00","updated_at":"2022-07-12T10:41:17 01:00","first_name":"Mark","last_name":"Pidsley","orders_count":3,"state":"invited","total_spent":"9896.94","last_order_id":4700248899822,"note":null,"verified_email":true,"multipass_identifier":null,"tax_exempt":false,"phone":null,"tags":"first-order:fully-paid, Guest","last_order_name":"27776","currency":"GBP","accepts_marketing_updated_at":"2022-03-18T03:18:48 00:00","marketing_opt_in_level":null,"tax_exemptions":[],"sms_marketing_consent":null,"admin_graphql_api_id":"gid://shopify/Customer/6102973153518","default_address":{"id":7602051678446,"customer_id":6102973153518,"first_name":"Mark","last_name":"Pidsley","company":"","address1":"Captains Row","address2":"","city":"Lymington","province":"England","country":"United Kingdom","zip":"SO41 9RR","phone":"07989408757","name":"Mark Pidsley","province_code":"ENG","country_code":"GB","country_name":"United Kingdom","default":true}},"discount_applications":[],"fulfillments":[],"line_items":[{"id":12045659570414,"admin_graphql_api_id":"gid://shopify/LineItem/12045659570414","fulfillable_quantity":1,"fulfillment_service":"manual","fulfillment_status":null,"gift_card":false,"grams":0,"name":"Cube Stereo Hybrid 160 HPC SL 2022 Bosch - XL 55cm","price":"4899.00","price_set":{"shop_money":{"amount":"4899.00","currency_code":"GBP"},"presentment_money":{"amount":"4899.00","currency_code":"GBP"}},"product_exists":true,"product_id":7570044977390,"properties":[{"name":"Pre-Order","value":""},{"name":"Frame Size (Check stock below)","value":"55cm"}],"quantity":1,"requires_shipping":true,"sku":"53710322","taxable":false,"title":"Cube Stereo Hybrid 160 HPC SL 2022 Bosch","total_discount":"0.00","total_discount_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"variant_id":42482856460526,"variant_inventory_management":"shopify","variant_title":"XL 55cm","vendor":"","tax_lines":[],"duties":[],"discount_allocations":[]},{"id":12045659603182,"admin_graphql_api_id":"gid://shopify/LineItem/12045659603182","fulfillable_quantity":1,"fulfillment_service":"manual","fulfillment_status":null,"gift_card":false,"grams":0,"name":"KMC Bosch Gen 4 38t Chainring (9mm Offset)","price":"25.99","price_set":{"shop_money":{"amount":"25.99","currency_code":"GBP"},"presentment_money":{"amount":"25.99","currency_code":"GBP"}},"product_exists":true,"product_id":7569830576366,"properties":[],"quantity":1,"requires_shipping":true,"sku":"BFCB45238","taxable":false,"title":"KMC Bosch Gen 4 38t Chainring (9mm Offset)","total_discount":"0.00","total_discount_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"variant_id":42435842703598,"variant_inventory_management":"shopify","variant_title":null,"vendor":"","tax_lines":[],"duties":[],"discount_allocations":[]}],"payment_terms":null,"refunds":[],"shipping_address":{"first_name":"Mark","address1":"Captains Row","phone":"07989408757","city":"Lymington","zip":"SO41 9RR","province":"England","country":"United Kingdom","last_name":"Pidsley","address2":"","company":"","latitude":50.7584704,"longitude":-1.537493,"name":"Mark Pidsley","country_code":"GB","province_code":"ENG"},"shipping_lines":[{"id":3913660694766,"carrier_identifier":null,"code":"UK E&W Zone A (Weight: 24.75kg)","delivery_category":null,"discounted_price":"4.99","discounted_price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"phone":null,"price":"4.99","price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"requested_fulfillment_service_id":null,"source":"shopify","title":"UK E&W Zone A (Weight: 24.75kg)","tax_lines":[],"discount_allocations":[]}]}
HMAC verification code is correct. The problem is with the webhook body. I want to convert JSON to its original format that Shopify sends in the webhook
CodePudding user response:
Serializing Map to JSON is a very simple task in Java. You may use any of JSON libraries, the most popular are Json Jackson (also known asFaster XML) or Gson of Google. But if you want even simpler solution, I wrote my own thin wrapper over Jackson Json that allows you to serialize an object to JSON or deserialize it from Json to Object. For your case the code could be as simple as this
Map<String, Object) map = ... //here is your map that you got from your source String jsonString;
try {
jsonString = JsonUtils.writeObjectToJsonString(map);
}catch(IOException ioe) {
//Handle exception
}
Here is the Javadoc for JsonUtils class. It is part of MgntUtils open source library written and maintained by me. You can get the library as maven artifact here or from Github, including source code and Javadoc here
CodePudding user response:
Map<String, String> elements = new HashMap();
elements.put("Key1", "Value1");
elements.put("Key2", "Value2");
ObjectMapper objectMapper = new ObjectMapper();
try {
String json = objectMapper.writeValueAsString(elements);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}