<?php
namespace   Api\Controller;
use Think\Controller;
/**
 * 支付宝
 */
class AlipayController extends  Controller{

    /**
     * return_url接收页面
     */
    public function alipay_return(){
        // 引入支付宝
        vendor('Alipay.AlipayNotify','','.class.php');
        $config=$config=C('ALIPAY_CONFIG');
        $notify=new \AlipayNotify($config);
        // 验证支付数据
        $status=$notify->verifyReturn();
        if($status){
        // 支付成功后，修改支付状态为1，并记录付款帐号和付款时候
		$czsm="支付宝在线支付";
		$where=array(
		'payid'=>I("get.out_trade_no"),
		);
		$data=array(
		'status'=>'1',
		'payuser'=>I("get.buyer_email"),
		'paytime'=>strtotime(I("get.notify_time")),
		);
		D('Money')->where($where)->save($data);
		//同时将金额累加到用户表的余额中
		$map=array('id'=>I('session.id'),);
		D('User')->where($map)->setInc('money',I('get.total_fee'));
		
		$User=D('User')->where($map)->find();
		
		//如果开启了邮件通知
		if(C('EMAIL_OPEN')=="1"){
		//邮件相关模板变量替换
		$mbstr=array('${user}','${czje}','${czsm}','${srje}','${srsm}');
		$mbbl=array($User['userid'],I('get.total_fee'),$czsm,'-','-');
		send_email($User['email'],strreplace($mbstr,$mbbl,C('EMAIL_T_CW_CZ')),strreplace($mbstr,$mbbl,C('EMAIL_C_CW_CZ')));//向用户发送充值成功的邮件
		}
		
		//如果开启了短信通知
		if(C('SMS_OPEN')=="1"){
	
		//模板中的变量替换成实际值
		$smsmb=array(
		'user'=>$User['userid'],
		'czje'=>I('get.total_fee'),
		'czsm'=>$czsm,
		'srje'=>'-',
		'srsm'=>'-',
		);
		
		sendSms($User['phone'],C('SMS_QM'),C('SMS_CW_CZ'),$smsmb);//向用户发送充值成功的短信
		
		}
		
		$this->success('支付成功',U('User/Cwgl/czlist'));
		
		}else{
		$this->success('支付失败',U('User/Cwgl/czlist'));
		}
    }
    
    /**
     * notify_url接收页面
     */
    public function alipay_notify(){
        // 引入支付宝
        vendor('Alipay.AlipayNotify','','.class.php');
        $config=$config=C('ALIPAY_CONFIG');
        $alipayNotify = new \AlipayNotify($config);
        // 验证支付数据
        $verify_result = $alipayNotify->verifyNotify();
        if($verify_result) {
			
       $where=array(
		'payid'=>I("post.out_trade_no"),
		);
		$data=array(
		'status'=>'1',
		'payuser'=>I("post.buyer_email"),
		'paytime'=>I("post.notify_time"),
		);
		D('Money')->where($where)->save($data);	
		
		//同时将金额累加到用户表的余额中
		$map=array('id'=>I('session.id'),);
		D('User')->where($map)->setInc('money',I('post.total_fee'));
		
		$User=D('User')->where($map)->find();
		$czsm="支付宝在线支付";
		//如果开启了邮件通知
		if(C('EMAIL_OPEN')=="1"){
		//邮件相关模板变量替换
		$mbstr=array('${user}','${czje}','${czsm}','${srje}','${srsm}');
		$mbbl=array($User['userid'],I('get.total_fee'),$czsm,'-','-');
		send_email($User['email'],strreplace($mbstr,$mbbl,C('EMAIL_T_CW_CZ')),strreplace($mbstr,$mbbl,C('EMAIL_C_CW_CZ')));//向用户发送充值成功的邮件
		}
		
		//如果开启了短信通知
		if(C('SMS_OPEN')=="1"){
	
		//模板中的变量替换成实际值
		$smsmb=array(
		'user'=>$User['userid'],
		'czje'=>I('get.total_fee'),
		'czsm'=>$czsm,
		'srje'=>'-',
		'srsm'=>'-',
		);
		
		sendSms($User['phone'],C('SMS_QM'),C('SMS_CW_CZ'),$smsmb);//向用户发送充值成功的短信
		
		}
		
		
        }else {
            echo "支付失败";
        }
    }

    
}