Skip to content

Instantly share code, notes, and snippets.

@tnorthcutt
Forked from nathanbarry/clickbank-hook.php
Last active September 9, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tnorthcutt/ddc742deeca35afff162a022fa433650 to your computer and use it in GitHub Desktop.
Save tnorthcutt/ddc742deeca35afff162a022fa433650 to your computer and use it in GitHub Desktop.
<?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'};
error_log("IV: $iv");
// decrypt the body...
$decrypted = trim(
openssl_decrypt(base64_decode($encrypted),
'AES-256-CBC',
substr(sha1($secretKey), 0, 32),
OPENSSL_RAW_DATA,
base64_decode($iv)), "\0..\32");
////UTF8 Encoding, remove escape back slashes, and convert the decrypted string to a JSON object...
$sanitizedData = utf8_encode(stripslashes($decrypted));
$order = json_decode($decrypted);
//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