<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */


//JSONを扱うためのパーサ
//require_once(WEBAPP_DIR."/JSON.php");

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
require_once 'Net/UserAgent/Mobile.php';

/**
 * [[機能説明]]
 *
 * @package     [[package名]]
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Customer_Itembuy_Cashier_Cardresult
{
    var $title;
    var $pagename;

    var $actionChain;
    var $session;
    var $c_member;
    var $member_data;
    var $membercd;
    var $auth;
    var $c_preorder;
    var $c_cart;
    var $c_pref;
    var $c_consumer;
    var $deposit;

    var $payment;

    var $errer_return;

    var $cardmessage;

    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        $this->session->removeParameter("payment");
        $this->session->removeParameter("destination");
        $this->session->removeParameter("cart_items");
        $this->session->removeParameter("takkyubin");

        $mobile = &Net_UserAgent_Mobile::isMobile();
        $re_str = ($mobile) ? "mobile_" : "";

        $errorList =& $this->actionChain->getCurErrorList();
        $log =& LogFactory::getLog();

        $this->membercd = $this->auth->getAuthData("membercd");


        if (empty($_POST['X-S_TORIHIKI_NO'])) {
            trigger_error("remise error", E_USER_ERROR);
            exit;
        }

        $file = R_REMISE_DIR . $_POST['X-S_TORIHIKI_NO'].".txt";

        if ($_POST['X-ERRORLEVEL']!=0) {
            if (is_file($file)) unlink($file);
            trigger_error("remise error", E_USER_ERROR);
            exit;
        }
        if ($_POST['X-ERRORLEVEL']!=0 && $_POST['X-R_CODE']!='0:0000') {
            if (is_file($file)) unlink($file);
            trigger_error("remise error", E_USER_ERROR);
            exit;
        }
        $fh = fopen($file, "r");
        $contents = fread($fh, filesize($file));
        fclose($fh);

        if (empty($contents)) {
             unlink($file);
            exit;
        }

        $txtdata = explode(",", $contents);
        if (empty($txtdata)) {
            unlink($file);
            trigger_error("remise error", E_USER_ERROR);
            exit;
        }

        if (!($txtdata[0] == $_POST['X-TRANID'] &&
              $txtdata[1] == $_POST['X-S_TORIHIKI_NO'] &&
              $txtdata[2] == $_POST['X-AMOUNT'] &&
              $txtdata[3] == $_POST['X-TAX'] &&
              $txtdata[4] == $_POST['X-TOTAL'] &&
              $txtdata[5] == $_POST['X-REFAPPROVED'] &&
              $txtdata[6] == $_POST['X-REFFORWARDED'] &&
              $txtdata[7] == $_POST['X-ERRCODE'] &&
              $txtdata[8] == $_POST['X-ERRLEVEL'] &&
              $txtdata[9] == $_POST['X-R_CODE']
              )) {
            unlink($file);
            trigger_error("remise error", E_USER_ERROR);
            exit;
        }

        unlink($file);
        $opt = $_POST["OPT"];
        $data = array("payquickid"  => $txtdata[10],
                      "cardcompany" => $txtdata[6],
                      "cardnumber"  => $txtdata[11],
                      "cardname"    => $txtdata[13],
                      "cardexp"     => date("Y/m", mktime(0,0,0,substr($txtdata[12],0,2), 1, substr($txtdata[12],2,2))));

        $r = $this->payment->updateRemise($opt, $data, $this->cardmessage);
        //$this->cardmessage .= "お支払額：". $txtdata[4] ."円";

        if (!$r) {
            // 商品情報が不正
            $errorList -> add(1, 
                              mb_convert_encoding($ret["MErrMsg"], 
                              "UTF-8", "UTF-8,EUC-JP")
                              );
            $this->errer_return = "customer_itembuy_cart";
            return $re_str ."input";
        }

        return $re_str .'success';
    }

    function getCardmessage() {
        return $this->cardmessage;
    }
    function getCVSmessage() {
        return "";
    }
    function getDaibikimessage() {
        return "";
    }
    function getBankmessage() {
        return "";
    }
    function getYuubinmessage() {
        return;
    }
    function getDl() { 
        return $this->payment->getDl();
    }
    function getTrackerdata() { 
        return $this->payment->getTrackerdata(); 
    } 
}
?>
