Writeoff.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\admin\model\Dakuanlog;
  5. use app\admin\model\Method;
  6. use app\admin\model\Order;
  7. use app\admin\model\Ordercode;
  8. use app\admin\model\Orderitem;
  9. use app\admin\model\Third;
  10. use app\common\controller\Api;
  11. use Yansongda\Pay\Exceptions\GatewayException;
  12. use Yansongda\Pay\Pay;
  13. class Writeoff extends Api
  14. {
  15. protected $noNeedLogin = ['saoma'];
  16. protected $noNeedRight = ['*'];
  17. public function saoma(){
  18. $data = input();
  19. $order_model = new Order();
  20. $order_code_model = new Ordercode();
  21. $order_item_model = new Orderitem();
  22. if(isset($data['ruchang'])){
  23. //入场码核销信息
  24. $code_info=$order_code_model->where('ruchang_number',$data['ruchang'])->find();
  25. }
  26. if(isset($data['wucan'])){
  27. //午餐码核销信息
  28. $code_info=$order_code_model->where('wucan_number',$data['wucan'])->find();
  29. }
  30. if(isset($data['yajin'])){
  31. //押金码核销信息
  32. $code_info=$order_code_model->where('yajin_number',$data['yajin'])->find();
  33. }
  34. $order_info=$order_model->where('id',$code_info['order_id'])->find();
  35. $item_info = $order_item_model->where('order_id',$code_info['order_id'])->find();
  36. $data_info['order_id'] =$order_info;
  37. $data_info['item_info']=$item_info;
  38. $data_info['code_info']=$code_info;
  39. $this->success('扫码核销页面信息',$data_info);
  40. }
  41. public function hexiao(){
  42. $data = input();
  43. $order_model= new Order();
  44. $order_code_model = new Ordercode();
  45. $user_model = new \app\admin\model\User();
  46. $user_third = new Third();
  47. $method_model = new Method();
  48. $dakuanlog_model = new Dakuanlog();
  49. $user = $this->auth->getUser();
  50. $admin_id=$user->id;
  51. if($user->pid>0){
  52. $admin_id = $user->pid;
  53. }
  54. if(isset($data['ruchang'])){
  55. //打款给商家
  56. $code_info = $order_code_model->where('ruchang_number',$data['ruchang'])->where('ruchang_status',1)->find();
  57. if(empty($code_info)){
  58. $this->error('该核销码已核销');
  59. }
  60. $method_info = $method_model->where('id',$code_info['method_id'])->find();
  61. // if($admin_id!=$method_info['user_id']){
  62. // $this->error('非本鱼塘订单');
  63. //
  64. // }
  65. $rebate = rebate($method_info['type'],$method_info['moshi']);
  66. $bili = (100-$rebate['scale'])/100;
  67. $user_info = $user_model->where('id',$method_info['user_id'])->field('z_phone,z_name')->find();
  68. $openid = $user_third->where('user_id',$method_info['user_id'])->value('openid');
  69. $money = $dakuanlog_model->where('user_id',$method_info['user_id'])->whereTime('create_time','today')->sum('price');
  70. if($money<15000){
  71. $type='wechat';
  72. $user_sec['openid']=$openid;
  73. if(empty($user_sec['openid'])){
  74. $this->error('商家微信没有绑定');
  75. }
  76. }
  77. else{
  78. $type='alipay';
  79. $user_sec['mobile']=$user_info['z_phone'];
  80. $user_sec['name']=$user_info['z_name'];
  81. if(empty($user_sec['mobile']||empty($user_sec['name']))){
  82. $this->error('商家支付宝没有绑定');
  83. }
  84. }
  85. // print_r($user_sec);die;
  86. $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);
  87. $resqul=$this->dakuan($code_info['ruchang_price']*$bili,$user_sec,$type);
  88. if($resqul){
  89. $dakuanlog_model->isUpdate('true',['id'=>$log_id])->save(['status'=>1]) ;
  90. }
  91. //入场码核销信息
  92. $order_code_model->isUpdate('true',['ruchang_number'=>$data['ruchang']])->save(['ruchang_status'=>2,'ruchang_time'=>time()]);
  93. $order_model->isUpdate('true',['id'=>$code_info['order_id']])->save(['status'=>3]);
  94. $this->success('入场核销码,核销成功');
  95. }
  96. if(isset($data['wucan'])){
  97. //打款给商家
  98. $code_info = $order_code_model->where('wucan_number',$data['wucan'])->where('wucan_status',1)->find();
  99. if(empty($code_info)){
  100. $this->error('该核销码已核销');
  101. }
  102. $method_info = $method_model->where('id',$code_info['method_id'])->find();
  103. if($admin_id!=$method_info['user_id']){
  104. $this->error('非本鱼塘订单');
  105. }
  106. $rebate = rebate($method_info['type'],$method_info['moshi']);
  107. $bili = (100-$rebate['scale'])/100;
  108. $user_info = $user_model->where('id',$method_info['user_id'])->field('z_phone')->find();
  109. $openid = $user_third->where('user_id',$method_info['user_id'])->value('openid');
  110. $user_sec['openid']=$openid;
  111. $user_sec['mobile']=$user_info['z_phone'];
  112. $money = $dakuanlog_model->where('user_id',$method_info['user_id'])->whereTime('create_time','today')->sum('price');
  113. if($money<15000){
  114. $type='wechat';
  115. $user_sec['openid']=$openid;
  116. if(empty($user_sec['openid'])){
  117. $this->error('商家微信没有绑定');
  118. }
  119. }
  120. else{
  121. $type='alipay';
  122. $user_sec['mobile']=$user_info['z_phone'];
  123. $user_sec['name']=$user_info['z_name'];
  124. if(empty($user_sec['z_phone']||empty($user_sec['z_name']))){
  125. $this->error('商家支付宝没有绑定');
  126. }
  127. }
  128. $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);
  129. $resqul=$this->dakuan($code_info['ruchang_price']*$bili,$user_sec,$type);
  130. if($resqul){
  131. $dakuanlog_model->isUpdate('true',['id'=>$log_id])->save(['status'=>1]) ;
  132. }
  133. //午餐码核销信息
  134. $order_code_model->isUpdate('true',['wucan_number'=>$data['wucan']])->save(['wucan_status'=>2,'wucan_time'=>time()]);
  135. $this->success('午餐核销码,核销成功');
  136. }
  137. if(isset($data['yajin'])){
  138. //退回到用户
  139. $code_info = $order_code_model->where('yajin_number',$data['yajin'])->where('yajin_status',1)->find();
  140. if(empty($code_info)){
  141. $this->error('该核销码已核销');
  142. }
  143. $order_model = new Order();
  144. $user_id= $order_model->where('id',$code_info['order_id'])->field('user_id,pay_type')->find();
  145. $user_info = $user_model->where('id',$user_id['user_id'])->field('z_phone')->find();
  146. $openid = $user_third->where('user_id',$user_id['user_id'])->value('openid');
  147. $user_sec['openid']=$openid;
  148. $user_sec['mobile']=$user_info['z_phone'];
  149. if($user_id['pay_type']=='微信支付'){
  150. $type='wechat';
  151. }
  152. else{
  153. $type='alipay';
  154. }
  155. $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);
  156. $resqul=$this->dakuan($code_info['yajin_price'],$user_sec,$type);
  157. if($resqul){
  158. $dakuanlog_model->isUpdate('true',['id'=>$log_id])->save(['status'=>1]) ;
  159. }
  160. //押金码核销信息
  161. $order_code_model->isUpdate('true',['yajin_number'=>$data['yajin']])->save(['yajin_status'=>2,'yajin_time'=>time()]);
  162. $order_model->isUpdate('true',['id'=>$code_info['order_id']])->save(['status'=>4]);
  163. $this->success('押金核销码,核销成功');
  164. }
  165. }
  166. public function refund_order($amount,$order_no,$type)
  167. {
  168. try {
  169. if ($type == 'wechat') {
  170. $config = Service::getConfig('wechat');
  171. $app = Pay::wechat($config);
  172. $order = [
  173. 'out_trade_no' => $order_no,
  174. 'out_refund_no' => time(),
  175. 'total_fee' => $amount*100,
  176. 'refund_fee' => $amount*100,
  177. 'refund_desc' => '订单退款',
  178. ];
  179. $result = $app->refund($order);
  180. } else {
  181. $config = Service::getConfig('alipay');
  182. $alipay = Pay::alipay($config);
  183. $order = [
  184. 'out_trade_no' =>$order_no,
  185. 'refund_amount' => $amount,
  186. ];
  187. $result = $alipay->refund($order);
  188. }
  189. return true;
  190. } catch (GatewayException $e) {
  191. return false;
  192. }
  193. }
  194. public function dakuan($amount,$user_info,$type)
  195. {
  196. try {
  197. if ($type == 'wechat') {
  198. $config = Service::getConfig('wechat');
  199. $app = Pay::wechat($config);
  200. $refund_data = [
  201. 'partner_trade_no' => time() . rand('000', '999'), //商户订单号
  202. 'openid' => $user_info['openid'], //收款人的openid
  203. 'check_name' => 'NO_CHECK', //NO_CHECK:不校验真实姓名\FORCE_CHECK:强校验真实姓名
  204. 'amount' => $amount, //企业付款金额,单位为分
  205. 'desc' => '打款', //付款说明
  206. ];
  207. $result = $app->transfer($refund_data);
  208. } else {
  209. $config = Service::getConfig('alipay');
  210. $alipay = Pay::alipay($config);
  211. $order = [
  212. 'out_biz_no' => time() . rand('000', '999'),
  213. 'trans_amount' => $amount,
  214. 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
  215. 'biz_scene' => 'DIRECT_TRANSFER',
  216. 'payee_info' => [
  217. 'identity' => $user_info['mobile'],
  218. 'identity_type' => 'ALIPAY_LOGON_ID',
  219. 'name'=>$user_info['name']
  220. ],
  221. ];
  222. $result = $alipay->transfer($order);
  223. }
  224. print_r($result);die;
  225. return true;
  226. } catch (GatewayException $e) {
  227. print_r($e);
  228. return false;
  229. }
  230. }
  231. public function dakuan_log($user_id,$type,$amount,$info,$pay_type){
  232. $data=[
  233. 'code_id'=>$info['code_id'],
  234. 'order_id'=>$info['order_id'],
  235. 'type'=>$type,
  236. 'pay_type'=>$pay_type,
  237. 'user_id'=>$user_id,
  238. 'price'=>$amount,
  239. 'status'=>2,
  240. 'create_time'=>time()
  241. ];
  242. $dakuanlog_model = new Dakuanlog();
  243. $log_id = $dakuanlog_model->insertGetId($data);
  244. return $log_id;
  245. }
  246. }