Created
May 1, 2017 21:23
-
-
Save nathanbarry/02f444963f529cfbe8857344de46a9e8 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
<?php | |
// ConvertKit API Key: | |
$convertkitApiKey = 'your-convertkit-api-key'; | |
// Map each of your Clickbank products to a ConvertKit tag ID, or leave this array empty to | |
// use the default tag for all products: | |
$convertkitTags = array( | |
'clickbankproduct1' => 'cktag1', | |
'clickbankproduct2' => 'cktag2', | |
'clickbankproduct3' => 'cktag3', | |
); | |
$defaultConvertKitTag = 'tag-to-use-if-product-is-not-in-list'; | |
// Clickbank username: | |
$vendor = 'your-clickbank-username'; | |
// Clickbank INS secret key: | |
$secretKey = 'your-clickbank-INS-key'; | |
// get JSON from raw body... | |
$message = json_decode(file_get_contents('php://input'), true); | |
// Pull out the encrypted notification and the initialization vector for | |
// AES/CBC/PKCS5Padding decryption | |
$encrypted = $message['notification']; | |
$iv = $message['iv']; | |
// decrypt the body... | |
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, | |
substr(sha1($secretKey), 0, 32), | |
base64_decode($encrypted), | |
MCRYPT_MODE_CBC, | |
base64_decode($iv)), "\0..\32"); | |
// convert the decrypted string to a JSON object... | |
$order = json_decode($decrypted, true); | |
//FROM CB INS | |
$email = $order['customer']['billing']['email']; | |
$fname = $order['customer']['billing']['firstName']; | |
$item = $order['lineItems'][0]['itemNo']; | |
$convertkitTagId = isset($convertkitTags[$item]) ? $convertkitTags[$item] : $defaultConvertKitTag; | |
// Send the API request to ConvertKit to add the customer to the tag: | |
$data = array('api_key' => $convertkitApiKey, 'email' => $email); | |
$json_data = json_encode($data); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, sprintf('https://api.convertkit.com/v3/tags/%s/subscribe', $convertkitTagId)); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
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); | |
$jsonresult = curl_exec($ch); | |
$result = json_decode($jsonresult, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment