You are here

function uc_recurring_hosted_paypal_ipn in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 modules/uc_recurring_hosted/uc_recurring_hosted.paypal_ipn.inc \uc_recurring_hosted_paypal_ipn()

Handle IPN callbacks for PayPal recurring payments

1 string reference to 'uc_recurring_hosted_paypal_ipn'
uc_recurring_hosted_menu in modules/uc_recurring_hosted/uc_recurring_hosted.module
Implements hook_menu().

File

modules/uc_recurring_hosted/uc_recurring_hosted.paypal_ipn.inc, line 11
Handle paypal IPN callbacks for recurring payments

Code

function uc_recurring_hosted_paypal_ipn($order_id = NULL) {

  // Assign posted variables to local variables
  $ipn = (object) $_POST;
  $ipn->received = REQUEST_TIME;
  if (!$order_id) {
    $order_id = $ipn->rp_invoice_id;
  }
  watchdog('uc_recurring_hosted', 'Receiving recurring IPN at URL for order @order_id. !debug', array(
    '@order_id' => $order_id,
    '!debug' => variable_get('uc_paypal_wps_debug_ipn', FALSE) ? '<pre>' . check_plain(print_r($_POST, TRUE)) . '</pre>' : '',
  ));
  $order = _uc_recurring_hosted_paypal_ipn_order($ipn);
  if ($order == FALSE) {
    watchdog('uc_recurring_hosted', 'IPN attempted for non-existent order.', array(), WATCHDOG_ERROR);
    return;
  }
  $ipn->order_id = $order->order_id;

  // Log the IPN against the actual order if debug enabled.
  if (variable_get('uc_paypal_wps_debug_ipn', FALSE)) {
    uc_order_log_changes($order->order_id, array(
      '<pre>' . print_r($ipn, TRUE) . '</pre>',
    ));
  }
  if (_uc_recurring_hosted_paypal_ipn_validate($ipn)) {
    watchdog('uc_recurring_hosted', 'Paypal IPN transaction type %txn_type for order id @order_id', array(
      '%txn_type' => $ipn->txn_type,
      '@order_id' => $order->order_id,
    ));
    switch ($ipn->txn_type) {
      case 'subscr_signup':
      case 'recurring_payment_profile_created':

        // First we need to create the recurring fee as paypal_wps overrides the
        // submit order function where this normally happens.
        uc_recurring_product_process_order($order);

        // Subscriptions do not have a txn_id, so we will use the subscr_id.
        $ipn->txn_id = $ipn->subscr_id;
        break;
      case 'subscr_payment':
      case 'recurring_payment':

        // If the order already has been paid for then this must be a renewal.
        if (uc_payment_balance($order) <= 0) {

          // Fetch fee from database.
          $fees = uc_recurring_get_fees($order);
          foreach ($fees as $fee) {
            $fee->ipn = $ipn;
            uc_recurring_renew($fee);
          }
        }
        else {

          // Process the first subscription payment.
          _uc_recurring_hosted_paypal_ipn_payment($ipn);
        }
        break;
      case 'subscr_failed':

        // Calculate when the next retry will be and then extend.
        $retry_at = strtotime(check_plain($_POST['retry_at']));
        $fees = uc_recurring_get_fees($order);
        foreach ($fees as $fee) {
          $extend_seconds = $retry_at - REQUEST_TIME;
          uc_recurring_extend_fee($fee, $extend_seconds);
        }
        return;
      case 'recurring_payment_failed':

        // Calculate when the next retry will be and then extend.
        $next_payment_date = strtotime(check_plain($_POST['NEXTBILLINGDATE']));
        $fees = uc_recurring_get_fees($order);
        foreach ($fees as $fee) {
          $extend_seconds = $next_payment_date - REQUEST_TIME;
          uc_recurring_extend_fee($fee, $extend_seconds);
        }
        return;
      case 'subscr_eot':
      case 'subscr_cancel':
        $fees = uc_recurring_get_fees($order);
        if ($fee = $fees[0]) {
          uc_recurring_fee_cancel($fee->rfid, $fee);
        }
        $ipn->txn_id = check_plain($_POST['subscr_id']);

        // subscriptions do not have a txn_id, so we will record the subscr_id instead
        break;
      case 'recurring_payment_skipped':
        $ipn->txn_id = $ipn->recurring_payment_id;
        $fees = uc_recurring_get_fees($order);
        foreach ($fees as $fee) {
          $fee->ipn = $ipn;
          uc_recurring_renew($fee);
        }
        break;
      case 'recurring_payment_suspended_due_to_max_failed_payment':
        $ipn->txn_id = $ipn->recurring_payment_id;
        $fees = uc_recurring_get_fees($order);
        foreach ($fees as $fee) {
          uc_recurring_hosted_paypal_wpp_suspension_log($fee, $ipn);
        }
        break;
      case 'recurring_payment_profile_cancel':
        $fees = uc_recurring_get_fees($order);
        if ($fee = $fees[0]) {
          $ipn->txn_id = $ipn->recurring_payment_id;
          if ($fee->remaining_intervals == -1) {
            uc_recurring_fee_cancel($fee->rfid, $fee);
          }
          uc_order_comment_save($fee->order_id, NULL, t('PayPal confirmed cancellation of billing agreement.'));
        }
        break;
    }

    // For some reason paypal sets this as the new order id, which is a fail
    $ipn->order_id = $order->order_id;

    //save our ipn transaction
    _uc_recurring_hosted_paypal_ipn_save($ipn);
  }
  return;
}