You are here

function uc_cybersource_hop_post in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 payment/uc_cybersource/uc_cybersource.module \uc_cybersource_hop_post()
1 string reference to 'uc_cybersource_hop_post'
uc_cybersource_menu in payment/uc_cybersource/uc_cybersource.module
Implements hook_menu().

File

payment/uc_cybersource/uc_cybersource.module, line 113
A module used for CyberSource's Silent Order POST and Hosted Order Page methods of payment.

Code

function uc_cybersource_hop_post() {
  if (!uc_cybersource_hop_include()) {
    watchdog('uc_cybersource_hop', 'Unable to receive HOP POST due to missing or unreadable HOP.php file.', array(), 'error');
    drupal_set_header('HTTP/1.1 503 Service unavailable');
    drupal_set_title(t('Unable to receive HOP POST.'));
    print t('The site was unable to receive a HOP post because of a missing or unreadble HOP.php');
  }
  $verify = VerifyTransactionSignature($_POST);
  watchdog('uc_cybersource_hop', 'Receiving payment notification at URL for order @orderNumber', array(
    '@orderNumber' => $_POST['orderNumber'],
  ));
  if (!isset($_POST['orderNumber'])) {
    watchdog('uc_cybersource_hop', 'CS HOP attempted with invalid order number.', array(), WATCHDOG_ERROR);
    return;
  }
  if (!$verify) {
    watchdog('uc_cybersource_hop', 'Receiving invalid payment notification at URL for order @orderNumber. <pre>@debug</pre>', array(
      '@orderNumber' => $_POST['orderNumber'],
      '@debug' => print_r($_POST, TRUE),
    ));
    return;
  }

  // Assign posted variables to local variables
  $decision = check_plain($_POST['decision']);
  $reason_code = check_plain($_POST['reasonCode']);
  $reason = _parse_cs_reason_code($reason_code);
  $payment_amount = check_plain($_POST['orderAmount']);
  $payment_currency = check_plain($_POST['paymentCurrency']);
  $request_id = check_plain($_POST['requestID']);
  $request_token = check_plain($_POST['orderPage_requestToken']);
  $reconciliation_id = check_plain($_POST['reconciliationID']);
  $order_id = check_plain($_POST['orderNumber']);
  $payer_email = check_plain($_POST['billTo_email']);
  $order = uc_order_load($_POST['orderNumber']);
  switch ($decision) {
    case 'ACCEPT':
      watchdog('uc_cybersource_hop', 'CyberSource verified successful payment.');
      $duplicate = db_result(db_query("SELECT COUNT(*) FROM {uc_payment_cybersource_hop_post} WHERE order_id = '%s' AND decision = 'ACCEPT'", $order_id));
      if ($duplicate > 0) {
        watchdog('uc_cybersource_hop', 'CS HOP transaction for order @order-id has been processed before.', array(
          '@order_id' => $order_id,
        ), WATCHDOG_NOTICE);
        return;
      }
      $sql = "INSERT INTO {uc_payment_cybersource_hop_post} (order_id, request_id, request_token, reconciliation_id, gross, decision, reason_code, payer_email, received) VALUES (%d, '%s', '%s', '%s', %f, '%s', '%s', '%s', %d)";
      db_query($sql, $order_id, $request_id, $request_token, $reconciliation_id, $payment_amount, $decision, $reason_code, $payer_email, time());
      $context = array(
        'revision' => 'formatted-original',
        'type' => 'amount',
      );
      $options = array(
        'sign' => FALSE,
      );
      $comment = t('CyberSource request ID: @txn_id', array(
        '@txn_id' => $request_id,
      ));
      uc_payment_enter($order_id, 'cybersource_hop', $payment_amount, $order->uid, NULL, $comment);
      uc_cart_complete_sale($order);
      uc_order_comment_save($order_id, 0, t('Payment of @amount @currency submitted through CyberSource with request ID @rid.', array(
        '@amount' => uc_price($payment_amount, $context, $options),
        '@currency' => $payment_currency,
        '@rid' => $request_id,
      )), 'order', 'payment_received');
      break;
    case 'ERROR':
      uc_order_comment_save($order_id, 0, t("Payment error:@reason with request ID @rid", array(
        '@reason' => $reason,
        '@rid' => '@request_id',
      )), 'admin');
      break;
    case 'REJECT':
      uc_order_comment_save($order_id, 0, t("Payment is rejected:@reason with request ID @rid", array(
        '@reason' => $reason,
        '@rid' => '@request_id',
      )), 'admin');
      break;
    case 'REVIEW':
      uc_order_update_status($order_id, 'review');
      uc_order_comment_save($order_id, 0, t('Payment is in review & not complete: @reason. Request ID @rid', array(
        '@reason' => $reason,
        '@rid' => '@request_id',
      )), 'admin');
      break;
  }
}