<?php

if (!defined('INCLUDED_AMEMBER_CONFIG')) 
    die("Direct access to this location is not allowed");

/*
*
*
*     Author: Alex Scott
*      Email: alex@cgi-central.net
*        Web: http://www.cgi-central.net
*    FileName $RCSfile$
*    Release: 3.1.8PRO ($Revision: 2256 $)
*
* Please direct bug reports,suggestions or feedback to the cgi-central forums.
* http://www.cgi-central.net/forum/
                                                                     *
*/


add_paysystem_to_list(
array(
            'paysys_id'   => 'migs_r',
            'title'       => $config['payment']['migs_r']['title'] ? $config['payment']['migs_r']['title'] : _PLUG_PAY_24eKOUT_TITLE,
            'description' => $config['payment']['migs_r']['description'] ? $config['payment']['migs_r']['description'] : _PLUG_PAY_24eKOUT_DESC,
            'public'    => 1
        )
);

add_payment_field('transaction_no', 'A unique number generated by the Payment Server for the transaction', 'readonly', 'internal');

class payment_migs_r extends payment {

    function do_payment($payment_id, $member_id, $product_id,
            $price, $begin_date, $expire_date, &$vars){

        global $config;
        global $db;
        $product = & get_product($product_id);

        $varsx = array(
            'vpc_Version' => '1',
            'vpc_Command' => 'pay',
            'vpc_MerchTxnRef' => $payment_id,
            'vpc_AccessCode' => $this->config['access_code'],
            'vpc_Merchant' => $this->config['merchant_id'],
            'vpc_OrderInfo' => $payment_id,
            'vpc_Amount' => intval($price * 100),
            'vpc_Locale' => 'en',
            'vpc_ReturnURL' => $config['root_url'] . "/plugins/payment/migs_r/thanks.php"
            );
        
        $securehash = $this->config['secure_secret'];
        ksort($varsx);
        foreach ($varsx as $k=>$v){
            $securehash .= $v;
        }
        $securehash = strtoupper(md5($securehash));

        $varsx['vpc_SecureHash'] = $securehash;

        $vars1 = array();
        foreach ($varsx as $kk=>$vv){
            $v = urlencode($vv);
            $k = urlencode($kk);
            $vars1[] = "$kk=$vv";
        }
        $varsx = join('&', $vars1);
        html_redirect("https://migs.mastercard.com.au/vpcpay?" . $varsx, '', 'Please wait', 'Please wait');
        //header("Location: https://migs.mastercard.com.au/vpcpay?".$varsx);
        exit;
    }
    
    function log_debug($vars){
        global $db;
        $s = "MIGS DEBUG:<br />\n";
        foreach ($vars as $k=>$v)
            $s .= "[$k] => '$v'<br />\n";
        $db->log_error($s);
    }

    function validate_thanks(&$vars){
        global $db;
        $this->log_debug($vars);
        
        //if ($vars['vpc_Message'])
        //    $db->log_error("MIGS ERROR: " . $vars['vpc_Message']);
            
        if ($vars['vpc_Merchant'] != $this->config['merchant_id']){
            $db->log_error("
                MIGS ERROR: Merchant ID received [".$vars['vpc_Merchant']."]
                doesn't match aMember configuration [".$this->config['merchant_id']."]
            ");
            return "Error. Please contact web site administrator";
        }

        $securehash = $this->config['secure_secret'];
        ksort($vars);
        foreach ($vars as $k=>$v){
            if ($k != 'vpc_SecureHash') $securehash .= $v;
        }
        $securehash = strtoupper(md5($securehash));

        if ($vars['vpc_SecureHash'] == $securehash)
            return '';
        else {
            $db->log_error("Validate postback error");
            return "Error. Please contact web site administrator";
        }
    }

    function process_thanks(&$vars){
            global $db;

            if ($vars['vpc_TxnResponseCode'] != '0'){
                switch ($vars['vpc_TxnResponseCode']){
                    case 1:
                        $msg = "Transaction could not be processed";
                        break;
                    case 2:
                        $msg = "Transaction declined - contact issuing bank";
                        break;
                    case 3:
                        $msg = "No reply from Processing Host";
                        break;
                    case 4:
                        $msg = "Card has expired";
                        break;
                    case 5:
                        $msg = "Insufficient credit";
                        break;
                    case 6:
                        $msg = "Error Communicating with Bank";
                        break;
                    case 7:
                        $msg = "Message Detail Error: " . $vars['vpc_Message'];
                        break;
                    case 8:
                        $msg = "Transaction declined – transaction type not supported";
                        break;
                    case 9:
                        $msg = "Bank Declined Transaction – Do Not Contact Bank";
                        break;
                }
                $db->log_error("MIGS ERROR: " . $msg);
                return "Error: Transaction hasn't been completed [RespCode: ".$vars['vpc_TxnResponseCode']."]";
            }
            
            $payment_id = intval($vars['vpc_OrderInfo']);
            if ($vars['vpc_TransactionNo']){
                $p = $db->get_payment ($payment_id);
                $p['data']['transaction_no'] = $vars['vpc_TransactionNo'];
                $db->update_payment ($payment_id, $p);
            }
            $price = $vars['vpc_Amount'] / 100;
            $err = $db->finish_waiting_payment($payment_id, 'migs_r', $vars['vpc_ReceiptNo'], $price, $vars);
            if ($err) 
                return "finish_waiting_payment error: $err [$payment_id]";
    }

}

?>