Forked from princesslea/Shopify Webhooks: How to Prep Your App This Black Friday Cyber Monday.js
Last active
January 1, 2022 17:40
-
-
Save shopifypartners/54e3827247069e5376336ac1332dd40a to your computer and use it in GitHub Desktop.
lambda.function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var AWS = require('aws-sdk'); | |
| var sqs = new AWS.SQS(); | |
| var crypto = require('crypto'); | |
| exports.handler = (event, context, callback) => { | |
| var client_secret = event.client_secret; | |
| delete event.client_secret; | |
| //calculate the hash | |
| var calculated_hash = crypto.createHmac("sha256", client_secret).update(new Buffer(event.body, "base64")).digest("base64"); | |
| //reject the message if the hash doesn't match | |
| if (event["X-Shopify-Hmac-SHA256"] != calculated_hash) { | |
| console.log("calculated_hash: (" + calculated_hash + ") != X-Shopify-Hmac-SHA256: (" + event["X-Shopify-Hmac-SHA256"] + ")"); | |
| return; | |
| } | |
| sqs.getQueueUrl({ QueueName: event.queue }, function(err, data) { | |
| if (err) { | |
| console.log('ERR', err); | |
| return; | |
| } | |
| var sqsMessageParams = { | |
| MessageBody: JSON.stringify(event), | |
| QueueUrl: data.QueueUrl | |
| }; | |
| sqs.sendMessage(sqsMessageParams, function(err, data) { | |
| if (err) { | |
| console.log('ERR', err); | |
| } | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment