123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use EasyWeChat\Factory;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use app\api\controller\Evaluate;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- /**
- * 支付管理类
- * Class Refund
- * @package app\api\controller\Refund
- */
- class Pay extends Controller
- {
- //小程序微信支付(type为1时是货主端微信配置,type为2时是接单端微信配置)
- public static function wxPay($name='订单支付',$out_trade_no,$total_fee,$notify_url,$trade_type = 'JSAPI',$openid,$type = 1){
- try{
- if(empty($out_trade_no) || empty($total_fee) || empty($notify_url)) return false;
- if($type == 1){
- $app = Factory::payment(config('app.wx_pay'));
- }else{
- $app = Factory::payment(config('app.worker_wx_pay'));
- }
- $parameter = array(
- 'body' => $name,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $total_fee*100,
- 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => $trade_type, // 请对应换成你的支付方式对应的值类型
- );
- if($trade_type != 'APP'){
- $parameter['openid'] = $openid;
- }
- $result = $app->order->unify($parameter);
- $jssdk = $app->jssdk;
- $config = $jssdk->sdkConfig($result['prepay_id']);
- return $config;
- }catch (Exception $e){
- return false;
- }
- }
- /**
- * 抽奖订单回调
- */
- public function goodsOrderNotify()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'goods_order');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_info = Db::name('goods_order')->where('pay_no',$pay_no)->find();
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- $goods_info = Db::table('store_goods')->find($order_info['goods_id']);
- $spec = json_decode($goods_info['spec'],true);
- // 抽奖
- $draw_data = draw_lottery($spec,'store_num',$order_info['goods_num']);
- $magic_data = [];// 魔玩柜数据
- $magic_day = intval(sysconf('magic_days'));// 获取设置天数
- foreach ($draw_data as $k=>$n) {
- $spec[$k]['store_num'] -= $n;
- $magic_data[]=[
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $order_info['uid'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => $n,
- 'base_num' => $n,
- 'crystal' => $spec[$k]['crystal'],
- 'case_name' => $spec[$k]['award_name'],
- 'case_cover' => $spec[$k]['spec_img'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- ];
- }
- $magic_draw = [];// 待抽将数据
- foreach ($magic_data as $mv)
- {
- for ($i=0;$i< $mv['base_num'];$i++ ) {
- $magic_draw[]=[
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $order_info['uid'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 0,
- 'award_status' => 1,
- 'award_name' => $mv['case_name'],
- 'award_cover' => $mv['case_cover'],
- ];
- }
- }
- Db::startTrans();
- // 更新商品库存
- Db::table('store_goods')->where('id',$order_info['goods_id'])->update(['spec'=>json_encode($spec)]);
- // 魔玩柜
- Db::table('magic_case')->insertAll($magic_data);
- // 抽奖数据
- Db::table('magic_draw')->insertAll($magic_draw);
- // 特殊奖励
- $special_award = json_decode($goods_info['special_award'],true);
- $special_point = $goods_info['special_point'];
- $front_court = $special_award[0];// 前场设置
- $back_court = $special_award[1]; // 后场设置
- $is_draw = 0;
- // 前场未抽奖
- if($front_court['is_over'] == 0) {
- $front_magic = Db::table('magic_draw')
- ->field('id,user_id,order_id')
- ->where(['goods_id'=>$goods_info['id'],'status'=>0,'is_special'=>0])
- ->limit(0,$special_point)
- ->order('id asc')
- ->select();
- // 前场抽奖次数达到前场抽奖节点
- if(count($front_magic) == $special_point) {
- $is_draw = 1;
- $special_award[0]['is_over'] = 1;// 前场抽奖完成标识
- $rand_id = array_rand($front_magic,1);// 中奖记录ids
- $front_user = $front_magic[$rand_id]['user_id']; // 中奖会员id
- $last_id = $front_magic[count($front_magic)-1]['id'];
- // 中奖记录插入魔玩柜
- $front_case = [
- 'order_id' => $front_magic[$rand_id]['order_id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $front_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => 1,
- 'base_num' => 1,
- 'source' => 1,
- 'crystal' => $front_court['crystal'],
- 'case_name' => $front_court['goods'],
- 'case_cover' =>$front_court['cover'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- ];
- Db::table('magic_case')->insert($front_case);
- $special_award1 = [
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $front_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- 'award_status' => 0,
- 'award_name' => $front_court['goods'],
- 'award_cover' =>$front_court['cover'],
- 'is_special' => 1
- ];
- Db::table('magic_draw')->insert($special_award1);
- // 更新抽奖记录
- Db::table('magic_draw')
- ->where('goods_id','=',$goods_info['id'])->where('id','<=', $last_id)
- ->where('id','<>',$front_magic[$rand_id]['id'])
- ->where('status','=',0)
- ->where('is_special','=',0)
- ->update(['status'=>1]);
- Db::table('magic_draw')
- ->where('id','=', $front_magic[$rand_id]['id'])
- ->where('status','=',0)
- ->where('is_special','=',0)
- ->update(['status'=>1,'award_status'=>2]);
- // 生成消息记录
- send_user_message($front_user,1,'恭喜您被大奖砸中!获得商品:'.$front_court['goods']);
- }
- }
- // 后场未抽奖 && 商品售罄
- if( $special_award[0]['is_over'] ==1 && $back_court['is_over'] == 0 && $goods_info['stock'] == 0){
- $back_num = $goods_info['base_stock'] - $special_point;
- $back_magic = Db::table('magic_draw')
- ->field('id,user_id,order_id')
- ->where(['goods_id'=>$goods_info['id'],'status'=>0,'is_special'=>0])
- ->limit(0,$back_num)
- ->order('id asc')
- ->select();
- if($back_num == count($back_magic)) {
- $is_draw = 1;
- $special_award[1]['is_over'] = 1;// 后场抽奖完成标识
- $rand_id = array_rand($back_magic,1);// 中奖记录id
- $back_user = $back_magic[$rand_id]['user_id']; // 中奖会员id
- $back_case = [
- 'order_id' => $back_magic[$rand_id]['order_id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $back_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => 1,
- 'base_num' => 1,
- 'source' => 2,
- 'crystal' => $back_court['crystal'],
- 'case_name' => $back_court['goods'],
- 'case_cover' =>$back_court['cover'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- ];
- Db::table('magic_case')->insert($back_case);
- $special_award2 = [
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $back_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- 'award_status' => 0,
- 'award_name' => $back_court['goods'],
- 'award_cover' =>$back_court['cover'],
- 'is_special' => 1
- ];
- Db::table('magic_draw')->insert($special_award2);
- // 更新抽奖记录
- Db::table('magic_draw')
- ->where('goods_id','=',$goods_info['id'])->where('status','=', 0)
- ->where('id','<>',$back_magic[$rand_id]['id'])
- ->where('is_special','=',0)
- ->update(['status'=>1]);
- Db::table('magic_draw')->where('id','=', $back_magic[$rand_id]['id'])
- ->where('is_special','=',0)
- ->update(['status'=>1,'award_status'=>3]);
- // 生成消息记录
- send_user_message($back_user,1,'恭喜您被大奖砸中!!获得商品:'.$back_court['goods']);
- }
- }
- // 更新商品抽奖详情
- if($is_draw) Db::table('store_goods')->where('id',$goods_info['id'])->update(['special_award'=>json_encode($special_award)]);
- // 更新订单状态
- $res = Db::table('goods_order')->where('id',$order_info['id'])
- ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>1,'status'=>1]);
- if(!$res){
- Db::rollback();
- }else{
- Db::commit();
- }
- return true; // 返回处理完成
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- /**
- * 魔玩柜订单回调
- */
- public function magicOrderNotify()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'magic_order');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_info = Db::name('magic_order')->where('pay_no',$pay_no)->find();
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- Db::startTrans();
- // 更新订单状态
- $res = Db::table('magic_order')->where('id',$order_info['id'])
- ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>1,'status'=>1]);
- if(!$res){
- Db::rollback();
- }else{
- Db::commit();
- }
- return true; // 返回处理完成
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- /**
- *明信片充值
- */
- public function crystalRecharge()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'crystal_order');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_info = Db::name('crystal_order')->where('pay_no',$pay_no)->find();
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- Db::startTrans();
- $user_info = Db::table('store_member')->find($order_info['uid']);
- // 更新会员余额
- Db::table('store_member')->where('id',$order_info['uid'])->update(['crystal_cash'=>bcadd($user_info['crystal_cash'],$order_info['crystal'],2)]);
- // 明信片明细
- balance_log($order_info['uid'],$order_info['crystal'],'余额充值',2,$order_info['id']);
- // 更新订单状态
- $res = Db::table('crystal_order')->where('id',$order_info['id'])
- ->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>1,'status'=>1]);
- if(!$res){
- Db::rollback();
- }else{
- Db::commit();
- }
- return true; // 返回处理完成
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- /**
- * 兑换订单回调
- */
- public function exchangeOrderNotify(){
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'exchange_order');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_info = Db::name('exchange_order')->where('pay_no',$pay_no)->find();
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- Db::startTrans();
- // 更新订单状态
- $res = Db::table('exchange_order')->where('id',$order_info['id'])
- ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1]);
- if(!$res){
- Db::rollback();
- }else{
- Db::commit();
- }
- return true; // 返回处理完成
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- /**
- * 集市订单回调
- */
- public function marketOrderNotify(){
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'market_order');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_info = Db::name('market_order')->where('pay_no',$pay_no)->find();
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- Db::startTrans();
- // 更新订单状态
- $res = Db::table('market_order')->where('id',$order_info['id'])
- ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1]);
- //$freight = get_freight($order_info['goods_num']);
- $freight = bcadd(sysconf('market_freight'),0,2);
- $vendor_info = Db::table('store_member')->find($order_info['vendor_id']);
- $sell_money = Db::table('market_goods')->where('id',$order_info['goods_id'])->value('sell_price');
- //$sell_money = bcsub($order_info['price_total'],$freight,2);
- if($sell_money > 0) {
- Db::table('store_member')->where('id',$vendor_info['id'])->update(['crystal_cash'=>bcadd($vendor_info['crystal_cash'],$sell_money,2)]);
- balance_log($order_info['vendor_id'],$sell_money,'集市商品售卖',14,$order_info['id']);
- }
- if(!$res){
- Db::rollback();
- }else{
- Db::commit();
- }
- return true; // 返回处理完成
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- /**
- * 记录支付日志
- * @param $message
- * @param $table_name
- */
- public function payResultLog($message,$table_name)
- {
- // 回调记录
- $ret_arr = [];
- $ret_arr['transaction_id'] = isset($message['transaction_id']) ?$message['transaction_id']: '';
- $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
- $ret_arr['return_code'] = isset($message['return_code']) ?$message['return_code']: '';
- $ret_arr['result_code'] = isset($message['result_code']) ?$message['result_code']: '';
- $ret_arr['create_at'] = date('Y-m-d H:i:s');
- $ret_arr['order_table'] = $table_name;
- $ret_arr['result'] = json_encode($message);
- Db::table('order_pay_result')->insert($ret_arr);
- }
- }
|