Skip to content

Instantly share code, notes, and snippets.

@whitej031788
Created February 13, 2020 10:50
Show Gist options
  • Save whitej031788/783b0ee8059e1c4ba0e77bd6346dc4fe to your computer and use it in GitHub Desktop.
Save whitej031788/783b0ee8059e1c4ba0e77bd6346dc4fe to your computer and use it in GitHub Desktop.
Paddle Mailchimp Integration
// We use this for the Mailchimp API calls
const https = require('https');
// Input a Mailchimp API Key here; we just use Basic Auth for Server to Server communication
// https://mailchimp.com/help/about-api-keys/
const mailchimpApiKey = process.env.MAILCHIMP_API_KEY;
// Input your Mailchimp domain controller here
// When logged into Mailchimp, first part of URL in your browser, eg us20.admin.mailchimp.com is us20
const domainController = process.env.MAILCHIMP_DC;
// This maps the purchased Paddle Product IDs to Mailchimp audiences/lists
// https://mailchimp.com/help/find-audience-id/
const productToListMapping = {
540922: '0f8ffd1063', // Paddle product ID 540922 mapped to the Mailchimp List/Audience ID
540923: '0f8ffd1063' // Paddle product ID 540923 mapped to the Mailchimp List/Audience ID
};
// This code is written as an AWS Node Lambda, so that the webhook is sent to AWS API Gateway and "ingested" by this Lambda script,
// extracting the webhook data and then calling the Mailchimp API
exports.handler = async (event) => {
// The data sent from Paddle; Paddle sends it as content type application/x-www-form-urlencoded
// This is for an AWS Lambda, but getting this data from the HTTP POST will differ based on environment
const webhookBody = event.data;
return new Promise((resolve, reject) => {
// See if we have a Mailchimp audience/list for that product
let listId = productToListMapping[webhookBody.product_id];
if (!listId) {
reject('Product ID not supported');
}
// Now we can perform different actions based on certain Paddle alerts
// For now, just add a Mailchimp contact for a payment success
switch (webhookBody.alert_name) {
case 'payment_succeeded':
processMailchimp(webhookBody, listId).then(data => {
console.log(data);
resolve(JSON.parse(data));
}, function(error) {
reject(JSON.parse(error.message))
});
break;
default:
reject('Webhook type not supported');
break;
}
});
};
function processMailchimp(webhookBody, listId) {
return new Promise((resolve, reject) => {
const options = {
host: domainController + '.api.mailchimp.com',
port: 443,
path: '/3.0/lists/' + listId + '/members',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + new Buffer.from('anystring:' + mailchimpApiKey).toString('base64')
}
};
const req = https.request(options, (res) => {
res.setEncoding('utf8');
let returnBody = '';
res.on('data', function (chunk) {
returnBody += chunk;
});
res.on('end', function() {
resolve(returnBody);
});
});
req.on('error', (e) => {
reject(e.message);
});
let data = JSON.stringify({"email_address": webhookBody.email, "status": "subscribed"});
req.write(data);
req.end();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment