<?php
/**
* 支付宝通知接口
* @author YinKu.Alan
* @date 2016-03-06
*/
class NotifyAction extends CommonAction {
// 后台配的验证KEY
const PAY_PWD = '2015518';
// pay_order表type字段类型:0-购买
const PAY_ORDER_TYPE_BUY = 0;
// pay_order表type字段类型:1-充值
const PAY_ORDER_TYPE_RECHARGE = 1;
// pay_order表order_type充值订单类型: 0-支付宝
const PAY_ORDER_ALIPAY = 0;
// pay_order表order_type充值订单类型: 1-财付通
const PAY_ORDER_TENPAY = 1;
public function _initialize() {
parent::_initialize();
}
public function index() {
try {
// 交易号
$PayNo = trim(I('PayNO', null));
// 付款金额
$PayJe = trim(I('PayJe', null));
// 付款说明
$PayMore = trim(I('PayMore', null));
// 交易时间
$PayTime = trim(I('PayTime', null));
// 验证KEY
$valideKey = trim(I('get.key', null));
//if ($valideKey != self::PAY_PWD) {
// throw new Exception('验证KEY不一致');
//}
if (empty($PayNo)) {
throw new Exception('交易号为空!');
}
if (empty($PayJe)) {
throw new Exception('付款金额为空!');
}
$user = M('user')->find($PayMore);
if ( ! $user['id']) {
throw new Exception("根据付款说明{$PayMore}无法查询到用户数据!" );
}
$map = array('outer_order_no' => $PayNo);
$order = M('pay_order')->where($map)->find();
if (isset($order['id'])) {
throw new Exception("交易号{$PayNo}已经完成了充值,充值信息是:编号({$order['order_no']}), 用户ID({$order['user_id']}), 支付金额({$order['pay_money']})! ");
}
$orderData = array();
$orderData['user_id'] = $user['id'];
$orderData['user_type'] = $user['user_type'];
$orderData['order_no'] = $this->create_orderno('APay');
$orderData['outer_order_no'] = $PayNo;
$orderData['pay_money'] = $PayJe;
$orderData['type'] = self::PAY_ORDER_TYPE_RECHARGE;
$orderData['comm'] = $PayMore;
$orderData['status'] = 1;
$orderData['addtime'] = date('Y-m-d H:i:s');
$orderData['pay_title'] = 'alipay';
$orderData['order_type'] = self::PAY_ORDER_ALIPAY;
M()->startTrans();
$flag = M('pay_order')->add($orderData);
if (false === $flag) {
M()->rollback();
throw new Exception('保存数据到pay_order表失败!');
}
$userData = array();
$userData['id'] = $user['id'];
$userData['money'] = $user['money'] + $PayJe;
$flag = M('user')->save($userData);
if (false === $flag) {
M()->rollback();
throw new Exception('更新用户表失败!');
}
$logData = array();
$logData['user_id'] = $user['id'];
$logData['stage'] = 'recharge';
$logData['money'] = $PayJe;
$logData['remain_money'] = $user['money'];
$logData['comm'] = "(用户充值) 订单号 {$orderData['order_no']}";
$logData['addtime'] = date('Y-m-d H:i:s');
$logData['order_no'] = null;
$flag = M('account_log')->add($logData);
if (false === $flag) {
M()->rollback();
throw new Exception('保存数据到account_log表失败!');
}
M()->commit();
} catch (Exception $e) {
Log::write($e->getMessage());
var_dump($e->getMessage());
}
}
}