<?php

/**
 * @file
 * Contains hook implementations and global functions.
 */

/**
 * The URL path to receive IPN requests at.
 */
define('PAYPAL_IPN_LISTENER_PATH', 'paypal_payment_ipn');

/**
 * Implements hook_menu().
 */
function paypal_payment_ipn_menu() {
  $items[PAYPAL_IPN_LISTENER_PATH] = array(
    'load arguments' => array('payment_method'),
    'page callback' => 'paypal_payment_ipn_post',
    'page arguments' => array(1),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Processes an IPN request based on POST data.
 */
function paypal_payment_ipn_post() {
  $ipn_variables = $_POST;
  foreach ($_POST as $ipn_variable => $value) {
    $ipn_variables[$ipn_variable] = rawurldecode($value);
  }
  if (PayPalPaymentIPNController::acknowledge($ipn_variables) && PayPalPaymentIPNController::validate($ipn_variables)) {
    $ipn = new PayPalPaymentIPN($ipn_variables);
    PayPalPaymentIPNController::save($ipn);
    PayPalPaymentIPNController::process($ipn_variables);
  }
}
