Last active
August 15, 2018 18:59
-
-
Save tristansokol/6ec7ee4ea2152cc5bde3c2ef46a19918 to your computer and use it in GitHub Desktop.
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
exports.handler = function(context, event, callback) { | |
var SquareConnect = require('square-connect'); | |
var Twilio = require('twilio'); | |
var accessToken = context.accessToken; | |
var locationId = context.locationId; | |
var messageBody = event.message; | |
var number = event.number; | |
// Configure authorization for the SDK | |
var defaultClient = SquareConnect.ApiClient.instance; | |
var oauth2 = defaultClient.authentications['oauth2']; | |
oauth2.accessToken = accessToken; | |
var checkoutApi = new SquareConnect.CheckoutApi(); | |
var orderRequest ={ | |
idempotency_key: new Date().getTime().toString(), | |
line_items: [{ | |
quantity: '1', | |
base_price_money:{ | |
amount: 1500, | |
currency: 'USD' | |
}, | |
note:"Ordered via SMS: " + number, | |
}] | |
} ; | |
var itemName | |
if (messageBody.toLowerCase().includes("small")) { | |
itemName = "Awesome Hat (small)"; | |
} else if (messageBody.toLowerCase().includes("medium")) { | |
itemName = "Awesome Hat (medium)"; | |
} else if (messageBody.toLowerCase().includes("large")) { | |
itemName = "Awesome Hat (large)"; | |
} else { | |
console.error("Something has gone wrong!"); | |
callback(null, twiml); | |
} | |
orderRequest.line_items[0].name = itemName | |
var checkoutRequest = { | |
idempotency_key: new Date().getTime().toString(), | |
order: orderRequest, | |
ask_for_shipping_address: true | |
} | |
checkoutApi.createCheckout(locationId, checkoutRequest).then(function(data) { | |
let twiml = new Twilio.twiml.MessagingResponse(); | |
twiml.message('Awe-some! finish up checking out here: '+ data.checkout.checkout_page_url); | |
callback(null, twiml); | |
}, function(error) { | |
console.error(error.response); | |
callback(null, error.response); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment