<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Authorizepmnt_IpnController extends Core_Controller_Action_Standard {

  public function indexAction() {
    
    $ipnLogFile = APPLICATION_PATH . '/temporary/log/authorizenet-ipn.log';
    
    include_once APPLICATION_PATH . "/application/libraries/Engine/Service/Authorize/autoload.php";
    $params = $this->_getAllParams();
    file_put_contents($ipnLogFile,
        date('c') . ': ' .
        print_r($params, true),
        FILE_APPEND);
        
    if(empty($params["order_id"])){
      echo 'ERR: order id missing';
      exit(1);
    }

    if(empty($params["status"]) || $params["status"] != 100){
      file_put_contents($ipnLogFile,
        date('c') . ': ' .
        'invalid status'.$params["status"],
        FILE_APPEND);
        echo 'invalid status'.$params["status"];
        exit(1);
    }

    $order = Engine_Api::_()->getItem('payment_order',$params['order_id']);
    $gatewayId = $order->gateway_id;
    $gateway = Engine_Api::_()->getItem('payment_gateway', $gatewayId);
    
    $gatewayPlugin = $gateway->getPlugin();
    // Action matches end of plugin
    if( $gatewayType &&
        substr(strtolower($gateway->plugin), - strlen($gatewayType)) == strtolower($gatewayType) ) {
      $activeGateway = $gateway;
    } else if( $gatewayId && $gatewayId == $gateway->gateway_id ) {
      $activeGateway = $gateway;
    } else if( method_exists($gatewayPlugin, 'detectIpn') &&
        $gatewayPlugin->detectIpn($params) ) {
      $activeGateway = $gateway;
    }
    // Gateway could not be detected
    if(!$activeGateway ) {
      file_put_contents($ipnLogFile,
          date('c') . ': ' .
          'Active gateway could not be detected.',
          FILE_APPEND);
      echo 'ERR';
      exit(1);
    }
    
    $secret = $gateway->config["ipn_secret"];

    if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
      file_put_contents($ipnLogFile,
          date('c') . ': ' .
          "No HMAC signature sent",
          FILE_APPEND);
      die("No HMAC signature sent");
    }

    $merchant = isset($_POST['merchant']) ? $_POST['merchant']:'';
    if (empty($merchant)) {
      file_put_contents($ipnLogFile,
          date('c') . ': ' .
          "No Merchant ID passed",
          FILE_APPEND);
      die("No Merchant ID passed");
    }

    $request = file_get_contents('php://input');
    if ($request === FALSE || empty($request)) {
      file_put_contents($ipnLogFile,
          date('c') . ': ' .
          "Error reading POST data",
          FILE_APPEND);
      die("Error reading POST data");
    }

    $hmac = hash_hmac("sha512", $request, $secret);
    if ($hmac != $_SERVER['HTTP_HMAC']) {
      file_put_contents($ipnLogFile,
          date('c') . ': ' .
          "HMAC signature does not match",
          FILE_APPEND);
      die("HMAC signature does not match");
    }
    

    // Process IPN
    try {
      $gatewayPlugin->onTransactionIpn($order,$params);
    } catch( Exception $e ) {
      // IPN validation failed
      file_put_contents($ipnLogFile,
          date('c') . ': ' .
          'IPN processing failed: ' . $e->__toString(),
          FILE_APPEND);
      echo 'ERR';
      exit(1);
    }
    // Exit
    echo 'OK';
    exit(0);
  }
}
