<?php
/**
* Script para agregar suscriptores a la lista de pago de phpbyex
*/
namespace Listener;
require_once __DIR__.'/paypal/PaypalIPN.php';
require_once __DIR__.'/MailChimp.php';
use PaypalIPN;
use DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('XXX-us11');
$logFile = __DIR__.DIRECTORY_SEPARATOR.'ref.log';
$ipn = new PaypalIPN();
error_log( '['.date('Y-m-d H:i:s').']: Got a request from PayPal! (Aparently)'.PHP_EOL, 3, $logFile );
try {
$verified = $ipn->verifyIPN();
if ($verified) {
error_log( '['.date('Y-m-d H:i:s').']: The request was verified!'.PHP_EOL, 3, $logFile );
$subscriber_hash = $MailChimp->subscriberHash($_POST['payer_email']);
if (!$subscribe_hash) {
error_log( '['.date('Y-m-d H:i:s').']: The email "'.$_POST['payer_email'].'" doesn\'t belong to the list'.PHP_EOL, 3, $logFile );
die;
}
$list_id = 'XXXX';
$postData = [
'tags' => [
[
'name' => 'Paid subscriber',
'status' => 'active'
],
],
];
error_log( '['.date('Y-m-d H:i:s').']: Posting to MailChimp (lists/'.$list_id.'/members/'.$subscriber_hash.'/tags): '.( print_r( $postData, 1 ) ).PHP_EOL, 3, $logFile );
$result = $MailChimp->post("lists/$list_id/members/$subscriber_hash/tags", $postData );
if ( !$MailChimp->success() ) {
error_log( '['.date('Y-m-d H:i:s').']: Post failed!: '.$MailChimp->getLastError().PHP_EOL, 3, $logFile );
} else {
error_log( '['.date('Y-m-d H:i:s').']: Post was succesful. Response from MailChimp: '.print_r( $result, 1 ).PHP_EOL, 3, $logFile );
}
} else {
error_log( '['.date('Y-m-d H:i:s').']: Request was not verified'.PHP_EOL, 3, $logFile );
}
error_log( '['.date('Y-m-d H:i:s').']: All done, responding with 200 OK'.PHP_EOL, 3, $logFile );
header("HTTP/1.1 200 OK");
} catch ( Exception $e ) {
error_log( '['.date('Y-m-d H:i:s').']: Exception found: '.$e->getMessage().PHP_EOL, 3, $logFile );
}