<?
// ###################################################################################
//
//   cancel.php
//
//   Copyright (c) 1997-2000 INTERBANK(Angelpay) Co. LTD.
//   All rights reserved.
//  ------------------------------------------------------------------------------
//   최종 수정일 : 2002.08.29
//
// ###################################################################################
//
//  ANGELPAY PG에서 승인취소 결과를 받아 DB에 수정하는 모듈
//
// ###################################################################################
//
// [ cancel.php를 호출할때 인자로 넘어오는 구성요소 ]
//
//  1. ReplyCode	: 응답코드로 '0000'이면 정상 그외는 에러처리한다.
//  2. ScrMessage	: 응답메시지
//  3. OrderDate	: 거래요청일자(YYYYMMDD)
//  4. SequenceNo	: 거래요청번호
//  5. OrderNo		: 주문번호
//  6. cancel_date	: 거래취소일자(YYYYMMDD)
//  7. cancel_time	: 거래취소시간(HHMMSS)
//  8. Reserved1	: FILLER
//  9. Reserved1	: FILLER
// 10. UserID		: 해당 쇼핑몰 번호(Angelpay에서 부여받은 코드)
//
// * Mall에서는 해당 페이지를 수정하여 결과를 DB에 수정할 수 있도록 한다.
//
//
//
// ###################################################################################
include "./config.php";
include "./log.php";
include "./mysql_function.php";

//*************************************************************************
//  ANGELPAY로부터 전달된 인자를 입력받는 루틴
//*************************************************************************
Function RecvFromAngelPay()
{
	global	$g_ReplyCode,		$ReplyCode,  
		$g_ScrMessage,      	$ScrMessage, 
		$g_OrderDate,       	$OrderDate,  
		$g_SequenceNo,      	$SequenceNo, 
		$g_OrderNo,         	$OrderNo,    
		$g_Cancel_Date,     	$cancel_date,
		$g_Cancel_Time,     	$cancel_time,
		$g_Reserved1,       	$Reserved1,  
		$g_Reserved2,       	$Reserved2,  
		$g_UserID,          	$UserID,
		$g_OrgApvNo,		$OrgApvNo;
			     


	$g_ReplyCode	= Trim($ReplyCode);
	$g_ScrMessage	= Trim($ScrMessage);
	$g_OrderDate	= Trim($OrderDate);
	$g_SequenceNo	= Trim($SequenceNo);
	$g_OrderNo	= Trim($OrderNo);
	$g_Cancel_Date	= Trim($cancel_date);
	$g_Cancel_Time	= Trim($cancel_time);
	$g_Reserved1	= Trim($Reserved1);
	$g_Reserved2	= Trim($Reserved2);
	$g_UserID	= Trim($UserID);
	$g_OrgApvNo	= Trim($OrgApvNo);


	TraceLog	("Cancel", "RecvFromAngelPay",
			 "ReplyCode="	.	$g_ReplyCode	.	", "	.
			 "ScrMessage="	.	$g_ScrMessage	.	", "	.
			 "OrderDate="	.	$g_OrderDate	.	", "	.
			 "SequenceNo="	.	$g_SequenceNo	.	", "	.
			 "Cancel_Date="	.	$g_Cancel_Date	.	", "	.
			 "Cancel_Time="	.	$g_Cancel_Time	.	", "	.
			 "Reserved1="	.	$g_Reserved1	.	", "	.
			 "Reserved2="	.	$g_Reserved2	.	", "	.
			 "UserID="	.	$g_UserID 	.	", "	.
			 "OrgApvNo="	.	$g_OrgApvNo );

	return True;

}

//*************************************************************************
//  승인 결과정보를 DB에 Update 하는 루틴
//*************************************************************************
Function UpdateDB()
{
	global	$g_ReplyCode,
		$g_ScrMessage,
		$g_OrderDate,
		$g_SequenceNo,
		$g_OrderNo,
		$g_Cancel_Date,
		$g_Cancel_Time,
		$g_Reserved1,
		$g_Reserved2,
		$g_UserID,
		$g_OrgApvNo,
		$TABLE_ANGELPAY;


	$strExistQuery	 = " SELECT 	sequence_no ";
	$strExistQuery	.= "   FROM 	$TABLE_ANGELPAY ";
	$strExistQuery	.= "  WHERE 	order_date ='$g_OrderDate' ";
	$strExistQuery	.= "    AND 	sequence_no ='$g_SequenceNo' ";
	$strExistQuery	.= "    AND 	approval_no ='$g_OrgApvNo' ";
	$strExistQuery	.= "    AND	userid='$g_UserID'";
				
	$aRet = FncResultQuery( $strExistQuery );
	if ( !$aRet or strcmp($aRet[0],$g_SequenceNo))
	{
		TraceLog ("Cancel", "UpdateDB", "[ERROR] Update 할 자료 없음.($strExistQuery)" );
		return false;
	}

	$CancelDateTime = $g_Cancel_Date . $g_Cancel_Time;
	
	$strQuery  = "";
	$strQuery .= " UPDATE	$TABLE_ANGELPAY SET ";
	$strQuery .= "		result_code	='$g_ReplyCode', ";
	$strQuery .= "		result_msg	='$g_ScrMessage', ";
	$strQuery .= "		cancel_date	='$CancelDateTime', ";
	$strQuery .= "		status_flag	='D2' ";
	$strQuery .= "  WHERE	order_date	='$g_OrderDate' ";
	$strQuery .= "    AND	sequence_no	='$g_SequenceNo' ";
	$strQuery .= "    AND	userid		='$g_UserID' ";
	//강제승인취소처리로 주석처리
	//$strQuery .= "    AND	approval_no	='$g_OrgApvNo' ";
	

	TraceLog ("Cancel", "Query Builder", $strQuery);

	if ( ! FncExecQuery( $strQuery ) )
	{
		TraceLog ("Cancel", "UpdateDB", "[ERROR] DB 작업 오류.-- (" . mysql_errno() . ")" . mysql_error() );
		return false;
	}

	return true;

}

//*************************************************************************
//  MAIN
//*************************************************************************

	TraceLog ("Cancel", "==================", "===> Start");

	RecvFromAngelPay();

	$bRetVal = false;

	If ( strlen(Trim($g_OrderDate)) > 0 And strlen(Trim($g_SequenceNo)) > 0 )
	{
		$bRetVal = UpdateDB();
	}

	echo ( $bRetVal ) ? "0000" : "9999";
?>
