<? 
session_start();
include $_SERVER[DOCUMENT_ROOT] . "/common/conf/config.inc.php";
include $_SERVER[DOCUMENT_ROOT] . "/module/category/category.lib.php";
include $_SERVER[DOCUMENT_ROOT] . "/module/shop/shop.lib.php";
include $_SERVER[DOCUMENT_ROOT] . "/module/mail/mail.lib.php";
include $_SERVER[DOCUMENT_ROOT] . "/module/point/point.lib.php";

   if (function_exists("mb_http_input")) mb_http_input("utf-8"); 
   if (function_exists("mb_http_output")) mb_http_output("utf-8");
?>
<? include "./KSPayWebHost.inc.php"; ?>
<?
    $rcid       = $_POST["reWHCid"];
    $rctype     = $_POST["reWHCtype"];
    $rhash      = $_POST["reWHHash"];

	$ipg = new KSPayWebHost($rcid, null);

	$authyn		= "";
	$trno		= "";
	$trddt		= "";
	$trdtm		= "";
	$amt		= "";
	$authno		= "";
	$msg1		= "";
	$msg2		= "";
	$ordno		= "";
	$isscd		= "";
	$aqucd		= "";
	$temp_v		= "";
	$result		= "";
	$halbu		= "";
	$cbtrno		= "";
	$cbauthno		= "";

	$resultcd =  "";

	//업체에서 추가하신 인자값을 받는 부분입니다
	$a =  $_POST["a"]; 
	$b =  $_POST["b"]; 
	$c =  $_POST["c"]; 
	$d =  $_POST["d"];

	if ($ipg->kspay_send_msg("1"))
	{
		$authyn	 = $ipg->kspay_get_value("authyn");
		$trno	 = $ipg->kspay_get_value("trno"  );
		$trddt	 = $ipg->kspay_get_value("trddt" );
		$trdtm	 = $ipg->kspay_get_value("trdtm" );
		$amt	 = $ipg->kspay_get_value("amt"   );
		$authno	 = $ipg->kspay_get_value("authno");
		$msg1	 = $ipg->kspay_get_value("msg1"  );
		$msg2	 = $ipg->kspay_get_value("msg2"  );
		$ordno	 = $ipg->kspay_get_value("ordno" );
		$isscd	 = $ipg->kspay_get_value("isscd" );
		$aqucd	 = $ipg->kspay_get_value("aqucd" );
		$temp_v	 = "";
		$result	 = $ipg->kspay_get_value("result");
		$halbu	 = $ipg->kspay_get_value("halbu");
		$cbtrno	 = $ipg->kspay_get_value("cbtrno");
		$cbauthno	 = $ipg->kspay_get_value("cbauthno");
		
		if (!empty($msg1)) $msg1 = iconv("EUC-KR","UTF-8", $msg1);
		if (!empty($msg2)) $msg2 = iconv("EUC-KR","UTF-8", $msg2);

		if (!empty($authyn) && 1 == strlen($authyn))
		{
			if ($authyn == "O")
			{
				$resultcd = "0000";
			}else
			{
				$resultcd = trim($authno);
			}

			//$ipg->kspay_send_msg("3"); // 정상처리가 완료되었을 경우 호출합니다.(이 과정이 없으면 일시적으로 kspay_send_msg("1")을 호출하여 거래내역 조회가 가능합니다.)

			//DB연결
			$dblink = SetConn($_conf_db["main_db"]);

			//회원의 경우 회원아이디로 로그인 전이라면 세션 아이디로
			if($_SESSION[$_SITE["DOMAIN"]]["MEMBER"]["ID"]){
				$tp = "1";
			}else{
				$tp = "2";
			}	
			$arrList = getPreOrderList($_SESSION[$_SITE["DOMAIN"]]["SESSIONID"], $_SESSION[$_SITE["DOMAIN"]]["MEMBER"]["ID"],$tp);


			//_POST 로 받는 주문번호가 기존에 주문된 주문번호인지 확인
			if(checkVaildOrderNo(mysql_escape_string($ordno))==true){
				jsMsg("이미 주문이 완료되었습니다.");
			}

			//_POST 로 받은 주문번호가 구매직전 장바구니에 있는지 확인
			if($ordno != $arrList["list"][0]["order_no"]){
				jsMsg("잘못된 주문 정보 입니다. 주문 장바구니에 해당 주문건이 없습니다.");
			}

			if(substr($result,0,1) == "6") {
				$ordergb = "1";
			} else {
				$ordergb = "6";
			
			}
			
			//주문정보 입력
			$blnRS = setOrderInfo($_SESSION[$_SITE["DOMAIN"]]["SESSIONID"], $_SESSION[$_SITE["DOMAIN"]]["MEMBER"]["ID"], $tp, $arrList["list"][0]["order_no"], $ordergb);
			if($blnRS==true){
				//KCP 결제후 DB 입력처리 완료 변수 설정
				$bSucc = "true";

				//주문자에게 메일발송
				$arrInfo = getOrderInfo($_SESSION[$_SITE["DOMAIN"]]["MEMBER"]["ID"], $tp, mysql_escape_string($_REQUEST["order_no"]));
				$arrMailInfo = getMailConfig($ordergb);
				//sendMailShopInfo($arrInfo, $arrMailInfo);
				//주문자에게 메일발송
			}


			//DB해제
			SetDisConn($dblink);
		}
	}
?>
<html>
<head>
</head>
<body onload="javascript:frmAGS_pay_ing.submit();">
<form name=frmAGS_pay_ing method=post action="/shop.php">
<input type=hidden name=goPage value="Thanks">
<input type=hidden name=order_no value="<?=$ordno?>">

<!-- 각 결제 공통 사용 변수 -->
<input type=hidden name=authyn value="<?=iconv("EUC-KR","UTF-8",$authyn)?>">	
<input type=hidden name=trno value="<?=iconv("EUC-KR","UTF-8",$trno)?>">			
<input type=hidden name=trddt value="<?=iconv("EUC-KR","UTF-8",$trddt)?>">		
<input type=hidden name=trdtm value="<?=iconv("EUC-KR","UTF-8",$trdtm)?>">		
<input type=hidden name=authno value="<?=iconv("EUC-KR","UTF-8",$authno)?>">
<input type=hidden name=amt value="<?=iconv("EUC-KR","UTF-8",$amt)?>">		
<input type=hidden name=ordno value="<?=iconv("EUC-KR","UTF-8",$ordno)?>">

<input type=hidden name=isscd value="<?=iconv("EUC-KR","UTF-8",$isscd)?>">
<input type=hidden name=aqucd value="<?=iconv("EUC-KR","UTF-8",$aqucd)?>">
<input type=hidden name=result value="<?=iconv("EUC-KR","UTF-8",$result)?>">
<input type=hidden name=halbu value="<?=iconv("EUC-KR","UTF-8",$halbu)?>">		
<input type=hidden name=cbtrno value="<?=iconv("EUC-KR","UTF-8",$cbtrno)?>">		
<input type=hidden name=cbauthno value="<?=iconv("EUC-KR","UTF-8",$cbauthno)?>">
</form>
</body> 
</html>
<!-- <html>
<head>
<meta http-equiv="Cache-Control" content="no-cache"> 
<meta http-equiv="Pragma" content="no-cache"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>*** KSNET WebHost 결과 [PHP] ***</title>
<link href="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/css/pgstyle.css" rel="stylesheet" type="text/css" charset="euc-kr">
</head>
<script language="javascript">
// 현금영수증 출력 스크립트
function CashreceiptView(tr_no)
{
    receiptWin = "http://nims.ksnet.co.kr/pg_infoc/src/bill/ps2.jsp?s_pg_deal_numb="+tr_no;
    window.open(receiptWin , "" , "scrollbars=no,width=434,height=580");
}
// 신용카드 영수증 출력 스크립트
function receiptView(tr_no)
{
	receiptWin = "http://nims.ksnet.co.kr/pg_infoc/src/bill/credit_view.jsp?tr_no="+tr_no;
    window.open(receiptWin , "" , "scrollbars=no,width=434,height=700");
}
</script>

<body>
<table width="560" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="50" align="right" background="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/bg_top.gif" class="txt_pd1">KSNET WebHost 결과 [PHP]</td>
  </tr>
  <tr>
    <td height="530" valign="top" background="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/bg_man.gif">	
	<table width="560" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25">&nbsp;</td>
        <td width="505" align="center">
		<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td height="40" style="padding:0px 0px 0px 15px; "><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_tit5.gif" width="30" height="30" align="absmiddle"> <strong>결과항목</strong></td>
      </tr>
      <tr>
        <td align="center"><table width="400" border="0" cellspacing="0" cellpadding="0">
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 결제방법</td>
            <td width="280">
<?
						if (empty($result) || 4 != strlen($result))
						{
							echo("(???)");
						}else
						{
							switch (substr($result,0,1))
							{
								case '1' : echo("신용카드"			); break;
								case 'I' : echo("신용카드"			); break;
								case '2' : echo("실시간계좌이체"	); break;
								case '6' : echo("가상계좌발급"		); break; 
								case 'M' : echo("휴대폰결제"		); break; 
								default  : echo("(????)"			); break; 
							}
						}
?>
            </td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>

          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 성공여부</td>
            <td width="280"><?echo($authyn)?>(<? if(!empty($authyn) && "O" == $authyn) echo("승인성공"); else echo("승인거절"); ?>) <font color=red> :성공여부값은 영어 대문자 O,X입니다. </font></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 응답코드</td>
            <td width="280"><?echo($resultcd)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 주문번호</td>
            <td width="280"><?echo($ordno)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 금액</td>
            <td width="280"><?echo($amt)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 거래번호</td>
            <td width="280"><?echo($trno)?> <font color=red>:KSNET에서 부여한 고유번호입니다. </font></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 거래일자</td>
            <td width="280"><?echo($trddt)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 거래시간</td>
            <td width="280"><?echo($trdtm)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
<? if (!empty($authyn) && "O" == $authyn) { ?>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 카드사 승인번호/은행 코드번호</td>
            <td width="280"><?echo($authno)?><font color=red>:카드사에서 부여한 번호로 고유한값은 아닙니다. </font></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
<? } ?>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 발급사코드/가상계좌번호/계좌이체번호</td>
            <td width="280"><?echo($isscd)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 매입사코드</td>
            <td width="280"><?echo($aqucd)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 메시지1</td>
            <td width="280"><?echo($msg1)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
          <tr bgcolor="#FFFFFF">
            <td width="120"><img src="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/ico_right.gif" width="11" height="11" align="absmiddle"> 메시지2</td>
            <td width="280"><?echo($msg2)?></td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>

		<? if (!empty($authyn) && "O" == $authyn && "1" == substr($trno,0,1)) { //정상승인의 경우만 영수증출력: 신용카드의 경우만 제공?> 
          <tr bgcolor="#FFFFFF">
            <td width="400" colspan="2" align="center"> <input type="button" value="영수증출력" onClick="javascript:receiptView('<?echo($trno)?>')"> </td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
      	<? } ?>
      	<? if (!empty($authyn) && "O" == $authyn && "2" == substr($trno,0,1)) { //정상승인의 경우만 영수증출력: 계좌이체의 경우만 제공 ?>
          <tr bgcolor="#FFFFFF">
            <td width="400" colspan="2" align="center"> <input type="button" value="현금영수증출력" onClick="javascript:CashreceiptView('<?echo($cbtrno)?>')"> </td>
          </tr>
          <tr bgcolor="#E3E3E3"> <td height="1" colspan="2"></td> </tr>
      	<? } ?>
        </table></td>
      </tr>
    </table>
		</td>
        <td width="30">&nbsp;</td>
      </tr>
    </table>
	</td>
  </tr>
  <tr>
    <td height="37" background="http://kspay.ksnet.to/store/KSPayFlashV1.3/mall/imgs/bg_bot.gif">&nbsp;</td>
  </tr>
</table>
</body>
</html> -->
