苗总组局小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

255 lines
8.9 KiB

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\shop\Type as TypeModel;
use app\admin\model\Shop as ShopModel;
use app\admin\model\Group as GroupModel;
use app\admin\model\Usergroup;
use app\admin\model\Order as OrderModel;
use app\admin\model\Venue;
use think\Db;
use addons\epay\library\Service;
use fast\Random;
use think\addons\Controller;
use Exception;
/**
* 微信支付宝整合插件首页
*
* 此控制器仅用于开发展示说明和测试,请自行添加一个新的控制器进行处理返回和回调事件,同时删除此控制器文件
*
* Class Index
* @package addons\epay\controller
*/
class Order extends Api
{
protected $model = null;
protected $noNeedRight = '*';
protected $noNeedLogin = ['*'];
public function _initialize()
{
parent::_initialize();
// if (!config("app_debug")) {
// $this->error("仅在开发环境下查看");
// }
}
public function createOrder(){
if($this->request->isPost()){
$uid = $this->auth->id;
$data = $this->request->post();
// 查询场次信息
$id = $data['id'];
//查询系统组局场次信息
$venue = Venue::get($id);
if(!$venue) $this->error('无此场次');
if($venue['ing_people'] >= $venue['max_people']) $this->error('该场次已满');
// 查询是否存在订单
$order = OrderModel::where('vid',$id)->where('uid',$uid)->where('status',0)->find();
$pay_price = $venue['pay_price']*$data['pay_people'];
if($order){
if($order->status >0) $this->error('该场次已购买');
$return['order_id'] = $order->id;
$return['orderno'] = "2".Random::numeric(15);
$return["pay_retime"] = $order->pay_retime;
$return["pay_price"] = $pay_price;
$order->orderno=$return['orderno'];
$order->pay_price=$pay_price;
$order->people_num=$data['pay_people'];
$order->price=$venue['pay_price'];
try {
if(!$order->save()) throw new Exception("生成订单失败");
} catch (Exception $e) {
return $this->error($e->getMessage());
}
}else{
try {
$GM = new GroupModel;
//查询组局信息
$group = $GM->getgroupdetail($venue->gid);
$group['venue'] = $venue;
// 生成订单
unset($group['theme_content']);
$insert['group_info'] = json_encode($group);
$insert['group_name'] = $group['title'];
$insert['shop_name'] = $group['shop_name'];
$insert['uid'] = $this->auth->id;
$insert['vid'] = $data['id'];
$insert['orderno'] = "2".Random::numeric(15);
$insert['gid'] = $venue['gid'];
$insert['pay_price'] = $pay_price;
$insert['username'] = $this->auth->nickname;
$insert['phone'] = $this->auth->mobile;
$insert['create'] = time();
$insert['pay_retime'] = $insert['create'] +3600;
$insert['vid'] = $data['id'];
$insert["people_num"] = $data['pay_people'];
$insert["price"] = $venue['pay_price'];
$return['orderno'] = $insert['orderno'];
$return['pay_retime'] = $insert['pay_retime'];
$return['pay_price'] = $pay_price;
if(!$return['order_id'] = OrderModel::insertGetId($insert)) throw new Exception("生成订单失败");
} catch (Exception $e) {
return $this->error($e->getMessage());
}
}
return $this->success('订单创建成功',$return);
}
}
public function updateOrder(){
}
public function beforePay(){
if($this->request->isPost()){
$min_user = new \app\admin\model\miniprogram\User;
$amount = $this->request->post('pay_price');
//订单号
$out_trade_no = $this->request->post('orderno');
$type = 'wechat';
$method = 'miniapp';
$min_user_info = $min_user->getbyUser_id($this->auth->id);
$openid = $min_user_info['openid'];
if (!$amount || $amount < 0) {
$this->error("支付金额必须大于0");
}
if (!$out_trade_no) {
$this->error("订单号不存在");
}
if (!$type || !in_array($type, ['alipay', 'wechat'])) {
$this->error("支付类型不能为空");
}
if (in_array($method, ['miniapp', 'mp']) && !$openid) {
$this->error("openid不能为空");
}
//订单标题
$title = '组局订单'.$out_trade_no;
//回调链接
$notifyurl = $this->request->root(true) . '/api/order/notifyx/paytype/' . $type;
$returnurl = $this->request->root(true) . '/api/order/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
try {
// 生成订单
if(!$response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method, $openid)) throw new Exception("生成订单失败");
} catch (Exception $e) {
return $this->error($e->getMessage());
}
return $this->success("微信支付预处理成功",$response);
}
return $this->error("支付信息失败");
}
public function notifyx()
{
\think\Log::record("进来了");
$paytype = $this->request->param('paytype');
$pay = Service::checkNotify($paytype);
if (!$pay) {
return json(['code' => 'FAIL', 'message' => '失败'], 500, ['Content-Type' => 'application/json']);
}
\think\Log::record("进来了");
// 获取回调数据,V3和V2的回调接收不同
$data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
Db::startTrans();
try {
\think\Log::record("进入try");
//微信支付V3返回和V2不同
if (Service::isVersionV3() && $paytype === 'wechat') {
\think\Log::record("判断v几");
$data = $data['resource']['ciphertext'];
$data['total_fee'] = $data['amount']['total'];
}
\think\Log::record($data);
//获取支付金额、订单号
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
$out_trade_no = $data['out_trade_no'];
\think\Log::record("回调成功,订单号:{$out_trade_no},金额:{$payamount}");
$order = OrderModel::where('orderno',$out_trade_no)->find();
if($order){
$venue = Venue::get($order->vid);
// 再最小组局人数和最大组局人数之间时状态改为1
if($venue->ing_people+1>=$venue->min_people && $venue->max_people>=$venue->ing_people+1) $venue->status = 1;
//超过最大人数
if($venue->ing_people+1>$venue->max_people) $order->status = -2;
$order->status = 1;
$order->pay_time = time();
$order->pay_price = $payamount;
$order->pay_type = $paytype;
$order->save();
$venue->ing_people= $venue->ing_people+$order->people_num;
$venue->save();
}
Db::commit();
//你可以在此编写订单逻辑
} catch (Exception $e) {
Db::rollback();
\think\Log::record("回调逻辑处理错误:" . $e->getMessage(), "error");
}
//下面这句必须要执行,且在此之前不能有任何输出
if (Service::isVersionV3()) {
return $pay->success()->getBody()->getContents();
} else {
return $pay->success()->send();
}
}
public function returnx()
{
$paytype = $this->request->param('paytype');
$out_trade_no = $this->request->param('out_trade_no');
$pay = Service::checkReturn($paytype);
if (!$pay) {
$this->error('签名错误', '');
}
//你可以在这里定义你的提示信息,但切记不可在此编写逻辑
$this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
}
public function confirmOrder(){
if($this->request->isPost()){
$uid = $this->auth->id;
$data = $this->request->post();
// 查询场次信息
$id = $data['id'];
//查询系统组局场次信息
$venue = Venue::get($id);
if(!$venue) $this->error('无此场次');
if($venue['ing_people'] >= $venue['max_people']) $this->error('该场次已满');
// 查询是否存在订单
$order = OrderModel::where('vid',$id)->where('uid',$uid)->find();
if($order && $order->status >0) $this->error('该场次已购买');
$GM = new GroupModel;
//查询组局信息
$group = $GM->getgroupdetail($venue->gid);
$return['pay_people'] = $data['pay_people']>0?$data['pay_people']:1;
$return['part_date'] = $venue['part_date'];
$return['price'] = $venue['pay_price'];
$return['pay_price'] = $venue['pay_price']*$return['pay_people'];
$return['can_join'] = $venue['max_people']-$venue['ing_people'];
$return['shop_name'] = $group['shop_name'];
$return['title'] = $group['title'];
$return['start_time_text'] = $venue['start_time_text'];
$return['end_time_text'] = $venue['end_time_text'];
$return['pay_price_text'] = $return['pay_people']."人x¥".$return['price'];
$return['phone'] = $this->auth->mobile;
$return['min_people'] =$venue['min_people'];
$return['max_people'] =$venue['max_people'];
return $this->success('获取成功',$return);
}
}
public function orderRefunds($refund){
try {
if(!Service::refunds($refund)) throw new Exception("退出组局失败");
} catch (Exception $e) {
return false;
}
return true;
}
}