作者 潘浩文

XIAOPAN

... ... @@ -566,42 +566,79 @@ class OrderController extends HomeBaseController{
}
/**
/**
* @title 统一下单
* @description 微信统一下单
* @author 董瑞恩
* @url /portal/order/pay
* @method GET
*
* @param name:order_no type:String require:1 default:无 other: desc:订单号
*
* @return data:返回用于调用支付的参数
*/
public function pay($order_no){
$order=Db::name('order')->where(['order_no'=>$order_no,'state'=>2])->find();
$openId=Db::name('users')->where('id',$order['users_id'])->find()['open_id'];
$body='支付';
$price=$order['price']*100;//订单价格
$notify_url=url('order/notify','','',true);//回调地址
$out_trade_no=$order_no.$this->create_noncestr(4);
$wxPay=new WeixinPay($openId,$out_trade_no,$body,$price,$notify_url);
$pay=$wxPay->pay();
if (isset($pay['package'])){
$data=[
'state'=>1,
'pay'=>$pay
];
$this->apiResponse(200,'success',$data);
}else{
$data=[
'state'=>0,
'message'=>'统一下单失败',
'error' => $pay
];
$this->apiResponse(200,'success',$data);
}
public function pay(){
$param=$this->request->param();
$order = Db::name('order')->where(['order_no' => $param['order_no'], 'state' => 2])->find();
$user = Db::name('users')->where('id', $order['users_id'])->find();
$price = $order['price'] * 100;//订单价格
//微信直接支付
// if ($param['type']==1) {
// $body = '支付';
// $notify_url = url('order/notify', '', '', true);//回调地址
// $out_trade_no = $param['order_no'] . $this->create_noncestr(4);
// $wxPay = new WeixinPay($user['open_id'], $out_trade_no, $body, $price, $notify_url);
// $pay = $wxPay->pay();
// if (isset($pay['package'])) {
// $data = [
// 'state' => 1,
// 'pay' => $pay
// ];
// $this->apiResponse(200, 'success', $data);
// } else {
// $data = [
// 'state' => 0,
// 'message' => '统一下单失败',
// 'error' => $pay
// ];
// $this->apiResponse(200, 'success', $data);
// }
// }
//余额支付
if ($user['fee']>$price){
Db::startTrans();
try {
Db::name('users')->where('id', $user['id'])->setDec('fee',$price);
Db::name('order')->where('id',$order['id'])->update(['state'=>3]);
Db::name('fee_log')->insert(['user_id'=>$user['id'],'fee'=>$price,'type'=>2,'add_time'=>time()]);
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
$this->apiResponse(400,'支付失败');
}
$this->apiResponse(201, '余额支付成功');
}elseif ($user['fee']<$price){
Db::name('order')->where('id',$order['id'])->update(['group_money'=>($price-$user['fee'])]);
$body = '支付';
$notify_url = url('order/group', '', '', true);//回调地址
$out_trade_no = $param['order_no'] . $this->create_noncestr(4);
$wxPay = new WeixinPay($user['open_id'], $out_trade_no, $body, $price-$user['fee'], $notify_url);
$pay = $wxPay->pay();
if (isset($pay['package'])) {
$data = [
'state' => 1,
'pay' => $pay
];
$this->apiResponse(200, 'success', $data);
} else {
$data = [
'state' => 0,
'message' => '统一下单失败',
'error' => $pay
];
$this->apiResponse(200, 'success', $data);
}
}
}
/**
* @title 充值生成订单
... ... @@ -656,7 +693,6 @@ class OrderController extends HomeBaseController{
$user_map['id']=$order['users_id'];
$user=\db('users')->where($user_map)->find();
if ($order['price']>$user['fee']){
$this->apiResponse('400','余额不足!');
}
$result=\db('users')->where($user_map)->setDec('fee',$order['price']);
... ... @@ -705,7 +741,36 @@ class OrderController extends HomeBaseController{
}
}
//组合支付回调
public function group(){
$param = $this->request->param();
if ($param == null) {
$param = file_get_contents("php://input");
if ($param == null) {
$param = $GLOBALS['HTTP_RAW_POST_DATA'];
}
}
$wxPay=new WeixinPay();
$data = $wxPay->xmlToArray($param);
$Sign = $data['sign'];
//支付成功回调后变更订单状态
$mySign = $wxPay->getSign($data);
$order_no =substr($data['out_trade_no'], 0, -4);
$order=\db('order')->where(['order_no'=>$order_no])->find();
if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
try{
Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
Db::name('fee_log')->insert(['user_id'=>$order['users_id'],'type'=>2,'fee'=>($order['money']-$order['group_money'])]);
Db::name('users')->where('id',$order['users_id'])->setDec('fee',($order['money']-$order['group_money']));
}catch (\Exception $exception){
$this->apiResponse(301,'error:'.$exception->getMessage());
}
return "<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>";
}
}
function create_noncestr($length = 4){
$chars = "0123456789";
... ...
... ... @@ -166,7 +166,7 @@ class PayController extends HomeBaseController
*/
public function luckyMoney(){
//todo 退款逻辑应该加入百荣签名验证规则,避免出现被盗用
/* $param=$this->request->param();
$param=$this->request->param();
$signature = $param['s'];
$arithmetic['timeStamp']= $param['t'];
$arithmetic['randomStr']= $param['r'];
... ... @@ -174,7 +174,7 @@ class PayController extends HomeBaseController
$str = arithmetic($arithmetic);
if($str != $signature){
$this->error('签名验证失败');
}*/
}
$app = new Application($this->options);
$luckyMoney = $app->lucky_money;
... ...
... ... @@ -270,7 +270,6 @@ class UsersController extends HomeBaseController{
public function getOrderById2(){
$users_id=$this->request->param('users_id');
$order= Db::name('order')->where(['users_id'=>$users_id,'state'=>2,'type'=>'1'])->find();
if (empty($order)){
$this->apiResponse(200,'没有未支付订单');
}
... ...