<?php
#include_once("HTTP/Request.php");
# 携帯判別
require_once 'Net/UserAgent/Mobile.php';

define('REMISEID',               $REMISEID);
define('HOSTID',                 $HOSTID);
define('RetURL',                 $RetURL);
define('CvsRetURL',              $CvsRetURL);
define('Action_URL',             $Action_URL);
define('Action_Mobile_URL',      $Action_Mobile_URL);
define('Action_url_cvs',         $Action_url_cvs);
define('Action_Mobile_url_cvs',  $Action_Mobile_url_cvs);
define("ItemCo",                 $ItemCo);
define("MethodID",               $MethodID);
define("DirectMode",             $DirectMode);
define("PayQuick",               $PayQuick);
define("R_Result_flg",           $R_Result_flg);

define('ENCODE',                 $ENCODE);
define('R_REMISE_DIR',           $R_REMISE_DIR);

class Remise
{
    var $req;

    var $dt;

    var $is_mobile;

    var $postdata;
    var $cvspostdata;

    var $remiseid;
    var $hostid;
    var $returl;
    var $cvsreturl;
    var $action_url;
    var $actionurl_cvs;
    var $itemco;
    var $methodid;
    var $directmode;
    var $payquick;
    var $r_result_flg;

    var $encode;

    var $card_number;
    var $card_name;
    var $card_exp;

    function Remise()
    {
        $this->is_mobile = &Net_UserAgent_Mobile::isMobile();

        $this->postdata = array(
            "SHOPCO" => REMISEID,
            "HOSTID" => HOSTID,
            "ITEM" => ItemCo,
            "METHOD" => MethodID,
            "DIRECT" => DirectMode,
            "PAYQUICK" => PayQuick
        );

        $this->cvspostdata = array(
            "SHOPCO" => REMISEID,
            "HOSTID" => HOSTID
        );
        if ($this->is_mobile) {
            $this->postdata["TMPURL"] = RetURL;
            $this->cvspostdata["TMPURL"] = CvsRetURL;
        } else {
            $this->postdata["RETURL"] = RetURL;
            $this->cvspostdata["TMPURL"] = CvsRetURL;
        }

        $this->encode         = ENCODE;
    }

    function getCardCompany ($cd) {
        $cardcompany = array("137"     => "AMEX",
                             "2a99661" => "JCB",
                             "2a99663" => "スミトモ", 
                             "2a90100" => "BC", 
                             "2a99662" => "DC", 
                             "2a99660" => "DINERS", 
                             "2a99664" => "UFJ",
                             "15250"   => "UC", 
                             "2S63046" => "AEON", 
                             "2S10035" => "SAISON", 
                             "2s63141" => "OMC", 
                             "2s83000" => "NC", 
                             "2s51206" => "PERSONA", 
                             "2s59681" => "APLUS", 
                             "2a80001" => "NISSEN", 
                             "2S83000" => "LAWSON", 
                             "2s77710" => "SF", 
                             "2s59880" => "ORICO", 
                             "2a59875" => "KC", 
                             "2P50002" => "GC", 
                             "2s59110" => "JACCS", 
                             "2s58588" => "CF", 
                             "2s50001" => "NS", 
                             "2959876" => "LIFE", 
                             "2s60020" => "POCKET", 
                             "2s62234" => "UCS", 
                             "2s50109" => "TOKYU", 
                             "2J32131" => "JAL", 
                             "2s58650" => "AJ", 
                             "2s50057" => "QUOQ", 
                             "2s77334" => "TOYOTA", 
                             "2s61203" => "タカシマヤ", 
                             "2F61100" => "ISETAN"
                             );
        return $cardcompany[$cd];
    }
    function getCvsCompany ($cd) {
        $cvscompany = array("D001" => "セブンイレブン",
                            "D002" => "ローソン",
                            "D003" => "サンクス", 
                            "D004" => "サークルK", 
                            "D005" => "ミニストップ", 
                            "D010" => "デイリーヤマザキ", 
                            "D011" => "ヤマザキデイリーストア",
                            "D015" => "セイコーマート",
                            "D030" => "ファミリーマート", 
                            "D401" => "CyberEdy", 
                            "D404" => "イーバンク", 
                            "D405" => "ペイジー" 
                             );
        return $cvscompany[$cd];
    }

    function getOrderid($dt) {
        $this->dt = date('YmdHis');
        if (!empty($dt)) $this->dt = $dt;
        return 'oneshop'. $this->dt;
    }

    function getMi($price, $dt="") {
        return $this->dt;
    }


    function getMiSig($MI="")
    {
        return "";
    }

    function checkSignature($price="") {
        $this->card_number = $_POST['card_number'];
        $this->card_name = $_POST['card_name'];
        $this->card_exp = sprintf("%02d/%02d", $_POST['exp_month'], substr($_POST['exp_year'], -2));
        return true;
    }


    function getCVSSig ($amount, $dt="") {
        return "";
    }
    function checkCVSSig () {
        return true;
    }

    function jCode ($str) {
        return $str;
    }

    function setCardinfo(&$number, &$name, &$exp) {
        $number = $this->card_number;
        $name = $this->card_name;
        $exp = $this->card_exp;
    }

    function doCardTransaction(&$r, $txn_type, $request, $info) {
        $log =& LogFactory::getLog();

        $log->debug($txn_type, "job");
        if ($txn_type=="authonly" || $txn_type=="authcapture") {
            $p = array(
                       "JOB" => "AUTH",
                       "S_TORIHIKI_NO" => $request["order-id"],
                       "AMOUNT" => $request["amount"],
                       "TOTAL" => $request["amount"],
                       "MAIL" => $request["MAIL"],
                       "OPT" => $request["OPT"]
                      );
         } elseif ($txn_type=="re-authonly" || $txn_type=="re-authcapture") {
            $remiseinfo = $info["remiseinfo"];
            $d = explode("\n", $remiseinfo);
            foreach ($d as $v) {
                $log->debug($v, "info");
                list ($alias, $class) = preg_split("/:/", $v);
                $log->debug($alias." ". $class, "info");
                $data[$alias] = $class;
            }
            $p = array(
                       "JOB" => "AUTH",
                       "PAYQUICKID" => $data["payquickid"],
                       "S_TORIHIKI_NO" => $request["order-id"],
                       "AMOUNT" => $request["amount"],
                       "TOTAL" => $request["amount"],
                       "MAIL" => $request["MAIL"],
                       "OPT" => $request["OPT"]
                      );
        }
        $hid = '';
        $postdata = array_merge($this->postdata, $p);
        foreach ($postdata as $n => $v) {
            $hid .= '<input type="hidden" name="'. $n .'" value="'. $v .'">';
        }
        $html = '<html><head>';
        $html .= '<meta http-equiv="Content-Type" content="text/html; charset=SHIFT_JIS" />';
        $html .= '<title>通信中・・・</title>';
        $html .= '</head>';
        if ($this->is_mobile) { // 携帯
            $html .= '<body>';
            $html .= '<form action="'. Action_Mobile_URL .'" method="POST" name="frm">';
            $html .= $hid;
            $html .= 'カード情報入力画面へ遷移します。以下のボタンを押してください。<br />';
            $html .= '<input type="submit" value="カード情報入力画面へ"><br />';
        } else {                // PC
            $html .= '<body onload="document.frm.submit();">';
            $html .= '<form action="'. Action_URL .'" method="POST" name="frm">';
            $html .= $hid;
            $html .= '時間が経っても画面が切り替わらない場合は、以下のボタンを押してください。<br />';
            $html .= '<input type="submit" value="カード情報入力画面へ"><br />';
        }
        $html .= '</form>';
        $html .= '</body>';
        $html .= '</html>';
        header("Content-Type: text/html; charset='SHIFT_JIS'");
        $log->debug($html, "remise card");
        echo mb_convert_encoding($html, "SJIS", "UTF-8");
        exit;
    }

    function doCvsTransaction(&$r, $request, $info) {
        $log =& LogFactory::getLog();

        $p = array(
                   "S_TORIHIKI_NO" => $request["ORDER_ID"],
                   "AMOUNT" => $request["TOTAL"],
                   "TOTAL" => $request["TOTAL"],
                   "NAME1" => $request["NAME1"],
                   "NAME2" => $request["NAME2"],
                   "KANA1" => $request["KANA1"],
                   "TEL" => str_replace("-", "", $request["TEL"]),
                   "MAIL" => $request["MAIL"],
                   "S_PAYDATE" => $request["PAY_LIMIT"]
                  );
        $hid = '';
        $postdata = array_merge($this->cvspostdata, $p);
        foreach ($postdata as $n => $v) {
            $hid .= '<input type="hidden" name="'. $n .'" value="'. $v .'">';
        }
        $html = '<html><head>';
        $html .= '<meta http-equiv="Content-Type" content="text/html; charset=SHIFT_JIS" />';
        $html .= '<title>通信中・・・</title>';
        $html .= '</head>';
        if ($this->is_mobile) { // 携帯
            $html .= '<body>';
            $html .= '<form action="'. Action_Mobile_url_cvs .'" method="POST" name="frm">';
            $html .= $hid;
            $html .= '支払い情報入力画面へ遷移します。以下のボタンを押してください。<br />';
            $html .= '<input type="submit" value="支払い情報入力画面へ"><br />';
        } else {                // PC
            $html .= '<body onload="document.frm.submit();">';
            $html .= '<form action="'. Action_url_cvs .'" method="POST" name="frm">';
            $html .= $hid;
            $html .= '時間が経っても画面が切り替わらない場合は、以下のボタンを押してください。<br />';
            $html .= '<input type="submit" value="支払い情報入力画面へ"><br />';
        }
        $html .= '</form>';
        $html .= '</body>';
        $html .= '</html>';
        header("Content-Type: text/html; charset='SHIFT_JIS'");
        $log->debug($html, "remise cvs");
        echo mb_convert_encoding($html, "SJIS", "UTF-8");
        exit;
    }

 }
?>