<?php
include_once('./_common.php');

check_device('both');

$req = 'cmd=_notify-synch';
$tx_token = $_REQUEST['uid'];
$req .= "&tx=$tx_token&at=$auth_token";

# 수신한 tx 값과 token 값을 paypal 서버로 전송
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://{$pp_hostname}/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: {$pp_hostname}"));
$res = curl_exec($ch);

curl_close($ch);

# 최종 결과값 분석
if( !$res ) {
    $result_msg = "Paypal 서버 연동 오류가 발생했습니다.";
} else {
    // 결과값을 로그로 기록해 보기
    $lines = explode("\n", $res);
    $paypal = array();
    if (strcmp ($lines[0], "SUCCESS") == 0) {
        // 결제가 성공한 경우
        for ($i=1; $i<count($lines);$i++){
            list($key,$val) = explode("=", $lines[$i]);
            if( trim($key) != '' ) {
                $paypal[urldecode($key)] = iconv('euc-kr', 'utf-8', urldecode($val));
            }
        }

        $result_msg = "\"".number_format($paypal['mc_gross'])." 캐시\"가 충전 완료되었습니다!";
    } else if (strcmp ($lines[0], "FAIL") == 0) {
        // 결제가 실패한 경우
        $result_msg = "결제오류가 발생했습니다!";
    }
}

$tmenu_ = "마이로또"; // theme.menu.php 에서 세팅한 이 페이지의 대메뉴명을 입력합니다.

$g5['title'] = "캐시충전결과";

include_once(G5_PATH.'/_head.php');

// add_stylesheet('css 구문', 출력순서); 숫자가 작을 수록 먼저 출력됨
add_stylesheet('<link rel="stylesheet" href="'.LOTTO_CSS_URL.'/charge.css?ver='.G5_TIME_YMDHIS.'">', 0);
?>
<section class="scontents">
    <div style="text-align:right"><a href="<?php echo LOTTO_URL ?>/my_cash.php" class="btn_b011 btn" title="나의 캐시"><i class="fas fa-list"></i> <span class="">나의 캐시</span></a></div>
    <br/>
    <div class="cash_box"><?php echo $result_msg ?></div>
    <div class="button_wrap">
        <ul class="">
            <li><a href="<?php echo LOTTO_URL ?>/number.php" class="btn_b011 btn" title="메가밀리언 구매"><i class="fas fa-arrow-right"></i> <span class="">메가밀리언 구매</span></a></li>
            <li><a href="<?php echo LOTTO_URL ?>/number.php?item=powerball" class="btn_b011 btn" title="파워볼 구매"><i class="fas fa-arrow-right"></i> <span class="">파워볼 구매</span></a></li>
        </ul>	
    </div>
</section>
<?php
include_once(G5_PATH.'/_tail.php');