123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace app\api\controller;
- use addons\epay\library\Service;
- use app\admin\model\Dakuanlog;
- use app\admin\model\Method;
- use app\admin\model\Order;
- use app\admin\model\Ordercode;
- use app\admin\model\Orderitem;
- use app\admin\model\Third;
- use app\common\controller\Api;
- use Yansongda\Pay\Exceptions\GatewayException;
- use Yansongda\Pay\Pay;
- class Writeoff extends Api
- {
- protected $noNeedLogin = ['saoma'];
- protected $noNeedRight = ['*'];
- public function saoma(){
- $data = input();
- $order_model = new Order();
- $order_code_model = new Ordercode();
- $order_item_model = new Orderitem();
- if(isset($data['ruchang'])){
- //入场码核销信息
- $code_info=$order_code_model->where('ruchang_number',$data['ruchang'])->find();
- }
- if(isset($data['wucan'])){
- //午餐码核销信息
- $code_info=$order_code_model->where('wucan_number',$data['wucan'])->find();
- }
- if(isset($data['yajin'])){
- //押金码核销信息
- $code_info=$order_code_model->where('yajin_number',$data['yajin'])->find();
- }
- $order_info=$order_model->where('id',$code_info['order_id'])->find();
- $item_info = $order_item_model->where('order_id',$code_info['order_id'])->find();
- $data_info['order_id'] =$order_info;
- $data_info['item_info']=$item_info;
- $data_info['code_info']=$code_info;
- $this->success('扫码核销页面信息',$data_info);
- }
- public function hexiao(){
- $data = input();
- $order_model= new Order();
- $order_code_model = new Ordercode();
- $user_model = new \app\admin\model\User();
- $user_third = new Third();
- $method_model = new Method();
- $dakuanlog_model = new Dakuanlog();
- $user = $this->auth->getUser();
- $admin_id=$user->id;
- if($user->pid>0){
- $admin_id = $user->pid;
- }
- if(isset($data['ruchang'])){
- //打款给商家
- $code_info = $order_code_model->where('ruchang_number',$data['ruchang'])->where('ruchang_status',1)->find();
- if(empty($code_info)){
- $this->error('该核销码已核销');
- }
- $method_info = $method_model->where('id',$code_info['method_id'])->find();
- // if($admin_id!=$method_info['user_id']){
- // $this->error('非本鱼塘订单');
- //
- // }
- $rebate = rebate($method_info['type'],$method_info['moshi']);
- $bili = (100-$rebate['scale'])/100;
- $user_info = $user_model->where('id',$method_info['user_id'])->field('z_phone,z_name')->find();
- $openid = $user_third->where('user_id',$method_info['user_id'])->value('openid');
- $money = $dakuanlog_model->where('user_id',$method_info['user_id'])->whereTime('create_time','today')->sum('price');
- if($money<15000){
- $type='wechat';
- $user_sec['openid']=$openid;
- if(empty($user_sec['openid'])){
- $this->error('商家微信没有绑定');
- }
- }
- else{
- $type='alipay';
- $user_sec['mobile']=$user_info['z_phone'];
- $user_sec['name']=$user_info['z_name'];
- if(empty($user_sec['mobile']||empty($user_sec['name']))){
- $this->error('商家支付宝没有绑定');
- }
- }
- // print_r($user_sec);die;
- $log_id = $this->dakuan_log($method_info['user_id'],1,$code_info['ruchang_price']*$bili,['code_id'=>$code_info['id'],'order_id'=>$code_info['order_id']],$type);
- $resqul=$this->dakuan($code_info['ruchang_price']*$bili,$user_sec,$type);
- if($resqul){
- $dakuanlog_model->isUpdate('true',['id'=>$log_id])->save(['status'=>1]) ;
- }
- //入场码核销信息
- $order_code_model->isUpdate('true',['ruchang_number'=>$data['ruchang']])->save(['ruchang_status'=>2,'ruchang_time'=>time()]);
- $order_model->isUpdate('true',['id'=>$code_info['order_id']])->save(['status'=>3]);
- $this->success('入场核销码,核销成功');
- }
- if(isset($data['wucan'])){
- //打款给商家
- $code_info = $order_code_model->where('wucan_number',$data['wucan'])->where('wucan_status',1)->find();
- if(empty($code_info)){
- $this->error('该核销码已核销');
- }
- $method_info = $method_model->where('id',$code_info['method_id'])->find();
- if($admin_id!=$method_info['user_id']){
- $this->error('非本鱼塘订单');
- }
- $rebate = rebate($method_info['type'],$method_info['moshi']);
- $bili = (100-$rebate['scale'])/100;
- $user_info = $user_model->where('id',$method_info['user_id'])->field('z_phone')->find();
- $openid = $user_third->where('user_id',$method_info['user_id'])->value('openid');
- $user_sec['openid']=$openid;
- $user_sec['mobile']=$user_info['z_phone'];
- $money = $dakuanlog_model->where('user_id',$method_info['user_id'])->whereTime('create_time','today')->sum('price');
- if($money<15000){
- $type='wechat';
- $user_sec['openid']=$openid;
- if(empty($user_sec['openid'])){
- $this->error('商家微信没有绑定');
- }
- }
- else{
- $type='alipay';
- $user_sec['mobile']=$user_info['z_phone'];
- $user_sec['name']=$user_info['z_name'];
- if(empty($user_sec['z_phone']||empty($user_sec['z_name']))){
- $this->error('商家支付宝没有绑定');
- }
- }
- $log_id = $this->dakuan_log($method_info['user_id'],1,$code_info['wucan_price']*$bili,['code_id'=>$code_info['id'],'order_id'=>$code_info['order_id']],$type);
- $resqul=$this->dakuan($code_info['ruchang_price']*$bili,$user_sec,$type);
- if($resqul){
- $dakuanlog_model->isUpdate('true',['id'=>$log_id])->save(['status'=>1]) ;
- }
- //午餐码核销信息
- $order_code_model->isUpdate('true',['wucan_number'=>$data['wucan']])->save(['wucan_status'=>2,'wucan_time'=>time()]);
- $this->success('午餐核销码,核销成功');
- }
- if(isset($data['yajin'])){
- //退回到用户
- $code_info = $order_code_model->where('yajin_number',$data['yajin'])->where('yajin_status',1)->find();
- if(empty($code_info)){
- $this->error('该核销码已核销');
- }
- $order_model = new Order();
- $user_id= $order_model->where('id',$code_info['order_id'])->field('user_id,pay_type')->find();
- $user_info = $user_model->where('id',$user_id['user_id'])->field('z_phone')->find();
- $openid = $user_third->where('user_id',$user_id['user_id'])->value('openid');
- $user_sec['openid']=$openid;
- $user_sec['mobile']=$user_info['z_phone'];
- if($user_id['pay_type']=='微信支付'){
- $type='wechat';
- }
- else{
- $type='alipay';
- }
- $log_id = $this->dakuan_log($user_id['user_id'],1,$code_info['yajin_price'],['code_id'=>$code_info['id'],'order_id'=>$code_info['order_id']],$type);
- $resqul=$this->dakuan($code_info['yajin_price'],$user_sec,$type);
- if($resqul){
- $dakuanlog_model->isUpdate('true',['id'=>$log_id])->save(['status'=>1]) ;
- }
- //押金码核销信息
- $order_code_model->isUpdate('true',['yajin_number'=>$data['yajin']])->save(['yajin_status'=>2,'yajin_time'=>time()]);
- $order_model->isUpdate('true',['id'=>$code_info['order_id']])->save(['status'=>4]);
- $this->success('押金核销码,核销成功');
- }
- }
- public function refund_order($amount,$order_no,$type)
- {
- try {
- if ($type == 'wechat') {
- $config = Service::getConfig('wechat');
- $app = Pay::wechat($config);
- $order = [
- 'out_trade_no' => $order_no,
- 'out_refund_no' => time(),
- 'total_fee' => $amount*100,
- 'refund_fee' => $amount*100,
- 'refund_desc' => '订单退款',
- ];
- $result = $app->refund($order);
- } else {
- $config = Service::getConfig('alipay');
- $alipay = Pay::alipay($config);
- $order = [
- 'out_trade_no' =>$order_no,
- 'refund_amount' => $amount,
- ];
- $result = $alipay->refund($order);
- }
- return true;
- } catch (GatewayException $e) {
- return false;
- }
- }
- public function dakuan($amount,$user_info,$type)
- {
- try {
- if ($type == 'wechat') {
- $config = Service::getConfig('wechat');
- $app = Pay::wechat($config);
- $refund_data = [
- 'partner_trade_no' => time() . rand('000', '999'), //商户订单号
- 'openid' => $user_info['openid'], //收款人的openid
- 'check_name' => 'NO_CHECK', //NO_CHECK:不校验真实姓名\FORCE_CHECK:强校验真实姓名
- 'amount' => $amount, //企业付款金额,单位为分
- 'desc' => '打款', //付款说明
- ];
- $result = $app->transfer($refund_data);
- } else {
- $config = Service::getConfig('alipay');
- $alipay = Pay::alipay($config);
- $order = [
- 'out_biz_no' => time() . rand('000', '999'),
- 'trans_amount' => $amount,
- 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
- 'biz_scene' => 'DIRECT_TRANSFER',
- 'payee_info' => [
- 'identity' => $user_info['mobile'],
- 'identity_type' => 'ALIPAY_LOGON_ID',
- 'name'=>$user_info['name']
- ],
- ];
- $result = $alipay->transfer($order);
- }
- print_r($result);die;
- return true;
- } catch (GatewayException $e) {
- print_r($e);
- return false;
- }
- }
- public function dakuan_log($user_id,$type,$amount,$info,$pay_type){
- $data=[
- 'code_id'=>$info['code_id'],
- 'order_id'=>$info['order_id'],
- 'type'=>$type,
- 'pay_type'=>$pay_type,
- 'user_id'=>$user_id,
- 'price'=>$amount,
- 'status'=>2,
- 'create_time'=>time()
- ];
- $dakuanlog_model = new Dakuanlog();
- $log_id = $dakuanlog_model->insertGetId($data);
- return $log_id;
- }
- }
|