Skip to content

Instantly share code, notes, and snippets.

@maxwellimpact
Last active March 22, 2016 17:23
Show Gist options
  • Save maxwellimpact/06ed4c056b6e1379423e to your computer and use it in GitHub Desktop.
Save maxwellimpact/06ed4c056b6e1379423e to your computer and use it in GitHub Desktop.
Simple Lambda Node script for passing new Stripe customer to Mailchimp list
/**
* Stripe to Mailchimp passthrough
*
* 1. Setup a Lambda in AWS (128mb) with an API endpoint and paste in this script.
* 2. Add a map to the API for type application/json
* // we need this to add some BASIC security
* {
* "secret" : "$input.params('secret')",
* "body" : $input.json('$')
* }
* 3. Add a webhook to Stripe with the API endpoint and add ?secret=SOMETHING_RANDOM to the end.
* 4. Get an API Key from Mailchimp
* 5. The Mailchimp API prefix is at the end of the API Key (ex. us5, us6, etc.)
* 6. Get the Mailchimp list id
* 7. Send a test event for customer.created from Stripe
**/
var https = require('https');
exports.handler = function(event, context) {
if (event.body.type != "customer.created") context.fail("Invalid Request");
if (event.secret != 'YOUR_KEY_HERE') context.fail("Unauthorized");
var list_id = 'MAILCHIMP_LIST_ID_HERE'; // mailchimp list id
var data = {
"email_address": event.body.data.object.email,
"status": "subscribed"
};
data = new Buffer(JSON.stringify(data), 'utf8');
var options = {
'auth': 'apikey:MAILCHIMP_API_KEY_HERE',
'headers': {
'Content-Type':'application/json',
'User-Agent': 'MailChimp-Node/3.0.0',
'Content-Length': data.length
},
'method': 'POST',
'hostname': 'MAILCHIMP_API_PREFIX_HERE.api.mailchimp.com',
'path': '/3.0/lists/'+list_id+'/members'
};
var req = https.request(options, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
context.succeed(body);
});
});
req.on('error', context.fail);
req.write(data);
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment