Skip to content

Instantly share code, notes, and snippets.

@opencubicles
Created February 20, 2018 19:34
Show Gist options
  • Save opencubicles/5dc3ca3427fc4dfde6ad2bad4f7852ad to your computer and use it in GitHub Desktop.
Save opencubicles/5dc3ca3427fc4dfde6ad2bad4f7852ad to your computer and use it in GitHub Desktop.
<?php
define('SHOPIFY_APP_SECRET', 'your_secret_here');
define('MailChimp_API_KEY', 'your_mailchimp_api_key here');
$mailchimp_list_id = 'mailchimp list id here';
function verify_webhook($data, $hmac_header)
{
$calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
return hash_equals($hmac_header, $calculated_hmac);
}
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
if($verified) {
$customer_data = json_decode($data);
$customer_email = $customer_data->email;
mc_subscribe($customer_email, '', MailChimp_API_KEY, $mailchimp_list_id, 'us7');
}
# credit for function below goes to https://github.com/actuallymentor/MailChimp-API-v3.0-PHP-cURL-example
function mc_subscribe($email, $fname, $apikey, $listid, $server) {
$auth = base64_encode( 'user:'.$apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $fname
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$listid.'/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
die();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment