<?php
include(dirname(dirname(dirname(__FILE__)))."/./configs/config.php");
include(dirname(dirname(dirname(__FILE__)))."/./configs/db_config.php");
include(dirname(dirname(dirname(__FILE__)))."/./includes/base.php");
header("Content-Type:text/html;charset=GB2312");
session_start();
date_default_timezone_set('PRC');
$phpcolor = new mysql();
$phpcolor->open();
require_once("alipay.config.php");
require_once("lib/alipay_notify.class.php");
//计算得出通知验证结果
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();
//验证成功
if($verify_result)
{
  //商户订单号
  $out_trade_no = $_GET['out_trade_no'];
  //支付宝交易号
  $trade_no = $_GET['trade_no'];
  //交易状态
  $trade_status = $_GET['trade_status'];
  //自动发货
  if($_GET['trade_status'] == 'WAIT_SELLER_SEND_GOODS')
  {
    require_once("lib/alipay_submit.class.php");
	/**************************请求参数**************************/
    //物流公司名称，必填
    $logistics_name = '自动充值';
    //物流发货单号
	$invoice_no = $out_trade_no;
    //物流运输类型
    $transport_type = 'POST';
    //三个值可选：POST（平邮）、EXPRESS（快递）、EMS（EMS）
    /************************************************************/
    //构造要请求的参数数组，无需改动
    $parameter = array(
		"service" => "send_goods_confirm_by_platform",
		"partner" => trim($alipay_config['partner']),
		"trade_no"	=> $trade_no,
		"logistics_name"	=> $logistics_name,
		"invoice_no"	=> $invoice_no,
		"transport_type"	=> $transport_type,
		"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
    );
    //建立请求
    $alipaySubmit = new AlipaySubmit($alipay_config);
    $html_text = $alipaySubmit->buildRequestHttp($parameter);
    //解析XML，注意：该功能PHP5环境及以上支持，需开通curl、SSL等PHP配置环境。建议本地调试时使用PHP开发软件
    $doc = new DOMDocument();
    $doc->loadXML($html_text);
	if(!empty($doc->getElementsByTagName("alipay")->item(0)->nodeValue))
	{
	  $alipay = $doc->getElementsByTagName("alipay")->item(0)->nodeValue;
    }
	$phpcolor->table="pay";
	$phpcolor->field="status='2'";
	$phpcolor->where="sid = '".$out_trade_no."' and status='1'";
	$phpcolor->upgrade();
    jump($core_url."pay.php");
  }
  //支付成功
  else if($_GET['trade_status'] == 'TRADE_FINISHED')
  {
	$phpcolor->table="pay";
	$phpcolor->field="username,money";
	$phpcolor->where="sid = '".$out_trade_no."' and status>=1";
	if($pay=$phpcolor->exist(1))
	{
	  $phpcolor->field="status='0'";
      $phpcolor->where="sid = '".$out_trade_no."'";
      $phpcolor->upgrade();
      $phpcolor->table="user";
      $phpcolor->field="money=money+".$pay['money'];
      $phpcolor->where="username = '".$pay['username']."'";
      $phpcolor->upgrade();
	}
    jump($core_url."pay.php");
  }
  else
    jump($core_url."error.php?id=21");
}
//验证失败
else
  jump($core_url."error.php?id=21");
?>