<?php
/**
 * 封装支付宝类
 *
 * @author sun
 */
class Alipay {
    public function __construct() {
        
        //暂设引入支付配置信息
        require_once("alipay.config.php");
        $this->alipay_config = $alipay_config;
    }
    
    //支付接口
    public function dopay($para_temp, $method) {
        //构造要请求的参数数组，无需改动
        $parameter = array(
            "service"           => "create_direct_pay_by_user",
            "partner"           => trim($this->alipay_config['partner']),//这里是你在成功申请支付宝接口后获取到的PID；
            "payment_type"      => 1, //支付类型
            "notify_url"        => $para_temp['page_ret_url'],//服务器异步通知页面路径
            "return_url"        => $para_temp['bg_ret_url'], //页面跳转同步通知页面路径
            "seller_email"      => 'house365nj@126.com', //卖家支付宝帐户
            "out_trade_no"      => $para_temp['transaction_id'], //商户订单号
            "subject"           => $para_temp['ord_desc'], //订单名称
            "total_fee"         => $para_temp['price'], //付款金额
            "body"              => '付房豆', //订单描述
            "show_url"          => '', //商品展示地址 
            "anti_phishing_key" => '',
            "exter_invoke_ip"   => $para_temp['ip'], //客户端的IP地址 
            "_input_charset"    => trim(strtolower($this->alipay_config['input_charset']))
         );
        require_once("lib/alipay_submit.class.php");
        $alipaySubmit = new AlipaySubmit($this->alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter, $method, "确认");
        return $html_text;
    }
    
    //异步执行
    public function notify() {
        require_once("lib/alipay_notify.class.php");
        $alipayNotify = new AlipayNotify($this->alipay_config);//计算得出通知验证结果
        $verify_result = $alipayNotify->verifyNotify();
        if ($verify_result) 
        {
            if ($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') 
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    
    //成功返回
    public function success() {
        require_once("lib/alipay_notify.class.php");
        $alipayNotify = new AlipayNotify($this->alipay_config);//计算得出通知验证结果
        $verify_result = $alipayNotify->verifyReturn();
        if ($verify_result) 
        {
            if ($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') 
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}
