123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- namespace app\api\controller;
- use EasyWeChat\Factory;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use think\Model;
- use app\api\controller\Evaluate;
- /**
- * 支付管理类
- * Class Refund
- * @package app\api\controller\Refund
- */
- class Pay extends Controller
- {
- //小程序微信支付(type为1时是货主端微信配置,type为2时是接单端微信配置)
- public static function wx_pay($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;
- }
- }
- //app微信支付
- public static function wx_pay_app($name='保证金',$out_trade_no,$total_fee,$notify_url){
- try{
- if(empty($out_trade_no) || empty($total_fee) || empty($notify_url)){
- return false;
- }
- $app = Factory::payment(config('app.worker_app_wx_pay'));
- $result = $app->order->unify([
- 'body' => $name,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $total_fee*100,
- 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
- ]);
- $jssdk = $app->jssdk;
- $config = $jssdk->appConfig($result['prepay_id']);
- return $config;
- }catch (Exception $e){
- return false;
- }
- }
- //充值余额回调
- public function balance_notify(){
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_list = Db::name('store_balance_order')->where('status',0)->where('pay_no',$pay_no)->find();
- if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户支付成功处理
- $order_info = Db::name('store_balance_order')->where('pay_no',$pay_no)->find();
- Db::startTrans();
- $res = Db::table('store_balance_order')->where('user_id',$order_info['user_id'])->where('pay_no',$order_info['pay_no'])->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1]);
- $error = 0;
- if(!$res){
- $error = 1;
- Db::rollback();
- }
- $balance_data = array(
- 'amount' => $order_info['price_total'],
- 'user_id' => $order_info['user_id'],
- 'order_id' => $order_info['id'],
- 'status' => 1
- );
- $balance_list = Db::name('store_balance_list')->insert($balance_data);
- if(!$balance_list){
- $error = 2;
- Db::rollback();
- }
- $member_balance = Db::name('store_member')->where('id',$order_info['user_id'])->setInc('balance',$order_info['price_total']);
- if(!$member_balance){
- $error = 3;
- Db::rollback();
- }
- $member_balance_all = Db::name('store_member')->where('id',$order_info['user_id'])->setInc('balance_all',$order_info['price_total']);
- if(!$member_balance_all){
- $error = 4;
- Db::rollback();
- }
- if($error == 0){
- Db::commit();
- }
- return true; // 返回处理完成
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- //订单支付回调
- public function order_notify(){
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_list = Db::name('store_order')->where('status',1)->where('pay_no',$pay_no)->find();
- if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户支付成功处理
- $order_info = Db::name('store_order')->field('id,worker_id')->where('pay_no',$pay_no)->find();
- Db::startTrans();
- $res = Db::table('store_order')->where('id',$order_info['id'])->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_status'=>1,'status'=>2]);
- $error = 0;
- if(!$res){
- $error = 1;
- Db::rollback();
- }
- if($order_info['worker_id']){
- $rebate_res = $this->serve_rebate($order_info['id']);
- if(!$rebate_res['code']){
- $error = 2;
- Db::rollback();
- }
- }
- if($error == 0){
- Db::commit();
- }
- return true; // 返回处理完成
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- //增加费用订单支付回调
- public function expenses_order_notify(){
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_list = Db::name('store_expenses_order')->where('status',0)->where('pay_no',$pay_no)->find();
- if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- $order_info = Db::name('store_expenses_order')->field('id,order_id,price_total,reason')->where('pay_no',$pay_no)->find();
- // 用户支付成功处理
- Db::startTrans();
- $expenses_order_res = Db::table('store_expenses_order')->where('id',$order_info['id'])->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1]);
- $error = 0;
- if(!$expenses_order_res){
- $error = 1;
- Db::rollback();
- }
- $rebate_res = $this->add_serve_rebate($order_info['id']);
- if(!$rebate_res){
- $error = 2;
- Db::rollback();
- }
- $store_order_info = Db::name('store_order')->field('price_total,price_amount')->where('id',$order_info['order_id'])->find();
- $store_order_res = Db::name('store_order')->where('id',$order_info['order_id'])->update(['expenses_time'=>date('Y-m-d H:i:s'),'expenses_pay_type'=>1,'expenses_amount'=>$order_info['price_total'],'expenses_reason'=>$order_info['reason'],'price_total'=>$store_order_info['price_total'] + $order_info['price_total'],'price_amount'=>$store_order_info['price_amount'] + $order_info['price_total']]);
- if(!$store_order_res){
- $error = 3;
- Db::rollback();
- }
- if($error == 0){
- Db::commit();
- }else{
- Db::rollback();
- }
- return true; // 返回处理完成
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- //保证金支付回调
- public function deposit_notify(){
- $app = Factory::payment(config('app.worker_wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_list = Db::name('store_deposit_order')->where('status',0)->where('pay_no',$pay_no)->find();
- if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户支付成功处理
- Db::table('store_deposit_order')->where('pay_no',$pay_no)->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1]);
- $wid = Db::table('store_deposit_order')->where('pay_no',$pay_no)->value('worker_id');
- //更新是否缴纳保证金
- Db::name('store_worker')->where('id',$wid)->update(array('deposit'=>1));
- //添加运输路线
- $county_arr = explode(',',$order_list['county']);
- foreach ($county_arr as $area){
- $city_id = Db::name('store_area')->where('id',$area)->value('pid');
- $province_id = Db::name('store_area')->where('id',$city_id)->value('pid');
- $area_data = array(
- 'worker_id' => $wid,
- 'start_province_id' => $order_list['start_province'],
- 'start_city_id' => $order_list['start_city'],
- 'start_county_id' => $order_list['start_county'],
- 'end_province_id' => $province_id,
- 'end_city_id' => $city_id,
- 'end_county_id' => $area
- );
- Db::name('store_serve_area')->insert($area_data);
- }
- return true; // 返回处理完成
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- //会员费用回调
- public function membership_notify(){
- $app = Factory::payment(config('app.worker_wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_list = Db::name('store_membership_order')->where('status',0)->where('pay_no',$pay_no)->find();
- if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户支付成功处理
- Db::table('store_membership_order')->where('pay_no',$pay_no)->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1]);
- $order_info = Db::table('store_membership_order')->where('pay_no',$pay_no)->find();
- //添加接单人员会员信息
- $vip_time = Db::name('store_worker')->where('id',$order_info['worker_id'])->value('vip_time');
- $vip_timestamp = 0;
- if($order_info['type'] == 1){
- $vip_timestamp = strtotime('next month');
- }elseif($order_info['type'] == 2){
- $vip_timestamp = strtotime('+3 month');
- }elseif ($order_info['type'] == 3){
- $vip_timestamp = strtotime('+1years');
- }
- if($vip_time && $vip_time > time()){
- $vip_timestamp = $vip_timestamp + ($vip_time - time());
- }
- Db::name('store_worker')->where('id',$order_info['worker_id'])->update(array('vip'=>$order_info['type'],'vip_time'=>$vip_timestamp));
- return true; // 返回处理完成
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- //自动报价回调
- public function offer_notify(){
- $app = Factory::payment(config('app.worker_wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- $order_list = Db::name('store_offer_order')->where('status',0)->where('pay_no',$pay_no)->find();
- if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户支付成功处理
- Db::table('store_offer_order')->where('pay_no',$pay_no)->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1]);
- $order_info = Db::table('store_offer_order')->where('pay_no',$pay_no)->find();
- //添加接单人员自动报价信息
- $offer_time = Db::name('store_worker')->where('id',$order_info['worker_id'])->value('offer_time');
- $offer_timestamp = 0;
- if($order_info['type'] == 1){
- $offer_timestamp = strtotime('next month');
- }elseif($order_info['type'] == 2){
- $offer_timestamp = strtotime('+3 month');
- }elseif ($order_info['type'] == 3){
- $offer_timestamp = strtotime('+1years');
- }
- if($offer_time && $offer_time > time()){
- $offer_timestamp = $offer_timestamp + ($offer_time - time());
- }
- Db::name('store_worker')->where('id',$order_info['worker_id'])->update(array('offer_time'=>$offer_timestamp));
- return true; // 返回处理完成
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- /**
- * 余额支付(雇主端)
- * $order_id 订单ID
- * $table_type 订单类型 1:store_order(订单),5:store_expenses_order(增加费用)
- */
- public function balance_pay($order_id,$table_type = 1){
- $field = 'user_id,price_total';
- $table_name = $table_type == 1?'store_order':'store_expenses_order';
- $order_info = Db::name($table_name)->field($field)->where('id',$order_id)->find();
- if(empty($order_info) || $order_info['price_total'] <= 0){
- return false;
- }
- $amount = $order_info['price_total'];
- $user_id = $order_info['user_id'];
- Db::startTrans();
- $balance_data = array(
- 'amount' => $amount,
- 'user_id' => $user_id,
- 'type' => 2,
- 'order_id' => $order_id,
- 'table_type' => $table_type,
- 'status' => 1
- );
- $error = 0;
- $balance = Db::name('store_balance_list')->insert($balance_data);
- if(!$balance){
- $error = 1;
- Db::rollback();
- }
- $member = Db::name('store_member')->where('id',$user_id)->setDec('balance',$amount);
- if(!$member){
- $error = 2;
- Db::rollback();
- }
- if($error == 0){
- Db::commit();
- return true;
- }else{
- return false;
- }
- }
- /**
- * 余额支付(接单端)
- * $order_id 订单ID
- */
- public function worker_balance_pay($order_id,$table_type){
- $field = 'price_total,worker_id';
- $table_type_arr = array(1=>'store_order',2=>'store_deposit_order',3=>'store_offer_order',4=>'store_grab_order',6=>'store_membership_order');
- $table_name = $table_type_arr[$table_type];
- $order_info = Db::name($table_name)->field($field)->where('id',$order_id)->find();
- if(empty($order_info) || $order_info['price_total'] <= 0){
- return false;
- }
- $amount = $order_info['price_total'];
- $worker_id = $order_info['worker_id'];
- Db::startTrans();
- $balance_data = array(
- 'amount' => $amount,
- 'user_id' => $worker_id,
- 'user_type' => 2,
- 'type' => 2,
- 'order_id' => $order_id,
- 'table_type' => $table_type,
- 'status' => 1
- );
- $error = 0;
- $balance = Db::name('store_balance_list')->insert($balance_data);
- if(!$balance){
- $error = 1;
- Db::rollback();
- }
- $worker = Db::name('store_worker')->where('id',$worker_id)->setDec('balance',$amount);
- if(!$worker){
- $error = 2;
- Db::rollback();
- }
- if($error == 0){
- Db::commit();
- return true;
- }else{
- return false;
- }
- }
- /**
- * 订单支付完成进行返利(未到账,等货主点击确认完成才到账)
- * $order_id 订单ID
- */
- public function serve_rebate($order_id){
- $field = 'worker_id,price_total,worker_ratio';
- $order_info = Db::name('store_order')->field($field)->where('id',$order_id)->find();
- if(empty($order_info) || $order_info['price_total'] <= 0 || $order_info['worker_ratio'] <= 0){
- return false;
- }
- //接单人员拿到的服务余额
- $worker_serve_amount = sprintf("%.2f",$order_info['price_total'] * $order_info['worker_ratio'] / 100);
- $worker_balance_data = array(
- 'amount' => $worker_serve_amount,
- 'user_id' => $order_info['worker_id'],
- 'user_type' => 2,
- 'get_type' => 2,
- 'order_id' => $order_id
- );
- $worker_balance = Db::name('store_balance_list')->insert($worker_balance_data);
- if($worker_balance){
- $data = array('code'=>1,'返利成功');
- }else{
- $data = array('code'=>0,'返利失败');
- }
- return $data;
- }
- /**
- * 增加费用支付完成进行追加返利
- * $order_id 增加费用订单ID
- */
- public function add_serve_rebate($order_id){
- $expenses_order_info = Db::name('store_expenses_order')->field('price_total,order_id')->where('id',$order_id)->find();
- if(empty($expenses_order_info) || $expenses_order_info['price_total'] <= 0 || $expenses_order_info['order_id'] <= 0){
- return false;
- }
- $field = 'worker_id,price_total,worker_ratio';
- $order_info = Db::name('store_order')->field($field)->where('id',$expenses_order_info['order_id'])->find();
- if(empty($order_info) || $order_info['worker_ratio'] <= 0){
- return false;
- }
- //增加接单人员拿到的服务余额
- //获取下单服务拥挤返利的信息
- $balance_info = Db::name('store_balance_list')->field('id,amount')->where('user_id',$order_info['worker_id'])->where('user_type',2)->where('order_id',$expenses_order_info['order_id'])->where('table_type',1)->find();
- if(empty($balance_info)){
- return false;
- }
- $worker_serve_amount = sprintf("%.2f",$expenses_order_info['price_total'] * $order_info['worker_ratio'] / 100);
- $worker_balance = Db::name('store_balance_list')->where('id',$balance_info['id'])->update(array('amount'=>$balance_info['amount'] + $worker_serve_amount));
- if($worker_balance){
- $data = array('code'=>1,'增加返利成功');
- }else{
- $data = array('code'=>0,'增加返利失败');
- }
- return $data;
- }
- }
|