<?php
header("Content-type: text/html; charset=utf-8");
ini_set('session.save_path', '../../config/session/');
session_start();
require_once '../../config/functions.php';
require_once '../../lib/ConnectPDO.class.php';
$pdo=new  ConnectPDO();
$id=intval(@$_GET['id']);
if($id<0){exit('account id err');}
$sql="select * from ".$pdo->sys_pre."scanpay_account where `id`='".$id."' limit 0,1";
$account=$pdo->query($sql,2)->fetch(2);
if($account['id']==''){exit('account id not exitst');}
if($account['state']!=1){exit('account state err');}

$account['data']=de_safe_str($account['data']);
$account['data']=str_replace("\n","111",$account['data']);
$account['data']=str_replace("- ","-",$account['data']);
$account['data']=str_replace(" -","-",$account['data']);
$r=json_decode($account['data'],1);
//$r['private_key']=str_replace("111","\n",$r['private_key']);
$ac=$r;
//var_dump($r);



$time=time();
$p_id=intval(@$_GET['p_id']);
if($p_id==0){echo 'p_id err';exit;}
$sql="select * from ".$pdo->sys_pre."scanpay_pay where `id`='".$p_id."'";
$r=$pdo->query($sql,2)->fetch();
if($r['id']==''){exit('p_id not exitst');}
if($r['state']!=0){header('location:../../index.php?monxin=scanpay.pay&id='.$id.'&p_id='.$p_id);exit('state!=0');}
if($r['operator']!=$_SESSION['monxin']['username']){exit('no power this p_id');}
if($r['time']<time()-60){
	$sql="update ".$pdo->sys_pre."scanpay_pay set `state`=2 where `id`=".$r['id']." limit 1";
	$pdo->exec($sql);
	exit('条码过期,<a href="javascript:history.go(-1);">返回</a>');
}
$r=de_safe_str($r);


require_once 'model/builder/AlipayTradePayContentBuilder.php';
require_once 'service/AlipayTradeService.php';


	$outTradeNo = $r["out_id"];
	$authCode = $r["barcode"];
	$totalAmount = $r["money"];
	$subject =$r["out_id"];


//if (!empty($_POST['out_trade_no'])&& trim($_POST['out_trade_no'])!="") {
    // 业务扩展参数，目前可添加由支付宝分配的系统商编号(通过setSysServiceProviderId方法)，详情请咨询支付宝技术支持
    $providerId = ""; //系统商pid,作为系统商返佣数据提取的依据
    $extendParams = new ExtendParams();
    $extendParams->setSysServiceProviderId($providerId);
    $extendParamsArr = $extendParams->getExtendParams();

    // 支付超时，线下扫码交易定义为5分钟
    $timeExpress = "5m";



    //第三方应用授权令牌,商户授权系统商开发模式下使用
    $appAuthToken = "";//根据真实值填写

    // 创建请求builder，设置请求参数
    $barPayRequestBuilder = new AlipayTradePayContentBuilder();
    $barPayRequestBuilder->setOutTradeNo($outTradeNo);
    $barPayRequestBuilder->setTotalAmount($totalAmount);
    $barPayRequestBuilder->setAuthCode($authCode);
    $barPayRequestBuilder->setTimeExpress($timeExpress);
    $barPayRequestBuilder->setSubject($subject);
   // $barPayRequestBuilder->setBody($body);
   // $barPayRequestBuilder->setUndiscountableAmount($undiscountableAmount);
   $barPayRequestBuilder->setExtendParams($extendParamsArr);
    //$barPayRequestBuilder->setGoodsDetailList($goodsDetailList);
    //$barPayRequestBuilder->setStoreId($storeId);
    //$barPayRequestBuilder->setOperatorId($operatorId);
    //$barPayRequestBuilder->setAlipayStoreId($alipayStoreId);

    $barPayRequestBuilder->setAppAuthToken($appAuthToken);

	$config['app_id']=$ac['app_id'];
	$config['alipay_public_key']=$ac['public_key'];
	$config['merchant_private_key']=$ac['private_key'];

    // 调用barPay方法获取当面付应答
    $barPay = new AlipayTradeService($config);
    $barPayResult = $barPay->barPay($barPayRequestBuilder);
	$fail_reason='';
    switch ($barPayResult->getTradeStatus()) {
        case "SUCCESS":
            //echo "支付宝支付成功:" . "<br>--------------------------<br>";
            //print_r($barPayResult->getResponse());
			$state=3;
            break;
        case "FAILED":
            //echo "支付宝支付失败!!!" . "<br>--------------------------<br>";
            if (!empty($barPayResult->getResponse())) {
                $r=($barPayResult->getResponse());
				$fail_reason=$r->msg;
            }
			$state=0;
            break;
        case "UNKNOWN":
            //echo "系统异常，订单状态未知!!!" . "<br>--------------------------<br>";
            if (!empty($barPayResult->getResponse())) {
                $r=($barPayResult->getResponse());
				$fail_reason=$r['msg'];
            }
			$state=0;
            break;
        default:
            //echo "不支持的交易状态，交易返回异常!!!";
			$fail_reason='异常';
			$state=1;
            break;
    }

	$sql="update ".$pdo->sys_pre."scanpay_pay set `state`='".$state."',`fail_reason`='".$fail_reason."' where `id`=".$p_id;
	$pdo->exec($sql);
	header('location:../../index.php?monxin=scanpay.pay&id='.$id.'&p_id='.$p_id);exit;
	
	return ;
	exit;


//}
?>