Pay.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. namespace app\api\controller;
  3. use EasyWeChat\Factory;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. use app\api\controller\Evaluate;
  9. /**
  10. * 支付管理类
  11. * Class Refund
  12. * @package app\api\controller\Refund
  13. */
  14. class Pay extends Controller
  15. {
  16. //小程序微信支付(type为1时是货主端微信配置,type为2时是接单端微信配置)
  17. public static function wx_pay($name='充值',$out_trade_no,$total_fee,$notify_url,$trade_type = 'JSAPI',$openid,$type = 1){
  18. try{
  19. if(empty($out_trade_no) || empty($total_fee) || empty($notify_url)){
  20. return false;
  21. }
  22. if($type == 1){
  23. $app = Factory::payment(config('app.wx_pay'));
  24. }else{
  25. $app = Factory::payment(config('app.worker_wx_pay'));
  26. }
  27. $parameter = array(
  28. 'body' => $name,
  29. 'out_trade_no' => $out_trade_no,
  30. 'total_fee' => $total_fee*100,
  31. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  32. 'trade_type' => $trade_type, // 请对应换成你的支付方式对应的值类型
  33. );
  34. if($trade_type != 'APP'){
  35. $parameter['openid'] = $openid;
  36. }
  37. $result = $app->order->unify($parameter);
  38. $jssdk = $app->jssdk;
  39. $config = $jssdk->sdkConfig($result['prepay_id']);
  40. return $config;
  41. }catch (Exception $e){
  42. return false;
  43. }
  44. }
  45. //app微信支付
  46. public static function wx_pay_app($name='保证金',$out_trade_no,$total_fee,$notify_url){
  47. try{
  48. if(empty($out_trade_no) || empty($total_fee) || empty($notify_url)){
  49. return false;
  50. }
  51. $app = Factory::payment(config('app.worker_app_wx_pay'));
  52. $result = $app->order->unify([
  53. 'body' => $name,
  54. 'out_trade_no' => $out_trade_no,
  55. 'total_fee' => $total_fee*100,
  56. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  57. 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
  58. ]);
  59. $jssdk = $app->jssdk;
  60. $config = $jssdk->appConfig($result['prepay_id']);
  61. return $config;
  62. }catch (Exception $e){
  63. return false;
  64. }
  65. }
  66. //充值余额回调
  67. public function balance_notify(){
  68. $app = Factory::payment(config('app.wx_pay'));
  69. $response = $app->handlePaidNotify(function ($message, $fail) {
  70. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  71. $pay_no = $message['out_trade_no'];
  72. $order_list = Db::name('store_balance_order')->where('status',0)->where('pay_no',$pay_no)->find();
  73. if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
  74. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  75. }
  76. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  77. // 用户支付成功处理
  78. $order_info = Db::name('store_balance_order')->where('pay_no',$pay_no)->find();
  79. Db::startTrans();
  80. $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]);
  81. $error = 0;
  82. if(!$res){
  83. $error = 1;
  84. Db::rollback();
  85. }
  86. $balance_data = array(
  87. 'amount' => $order_info['price_total'],
  88. 'user_id' => $order_info['user_id'],
  89. 'order_id' => $order_info['id'],
  90. 'status' => 1
  91. );
  92. $balance_list = Db::name('store_balance_list')->insert($balance_data);
  93. if(!$balance_list){
  94. $error = 2;
  95. Db::rollback();
  96. }
  97. $member_balance = Db::name('store_member')->where('id',$order_info['user_id'])->setInc('balance',$order_info['price_total']);
  98. if(!$member_balance){
  99. $error = 3;
  100. Db::rollback();
  101. }
  102. $member_balance_all = Db::name('store_member')->where('id',$order_info['user_id'])->setInc('balance_all',$order_info['price_total']);
  103. if(!$member_balance_all){
  104. $error = 4;
  105. Db::rollback();
  106. }
  107. if($error == 0){
  108. Db::commit();
  109. }
  110. return true; // 返回处理完成
  111. } else {
  112. return $fail('通信失败,请稍后再通知我');
  113. }
  114. });
  115. $response->send();
  116. }
  117. //订单支付回调
  118. public function order_notify(){
  119. $app = Factory::payment(config('app.wx_pay'));
  120. $response = $app->handlePaidNotify(function ($message, $fail) {
  121. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  122. $pay_no = $message['out_trade_no'];
  123. $order_list = Db::name('store_order')->where('status',1)->where('pay_no',$pay_no)->find();
  124. if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
  125. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  126. }
  127. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  128. // 用户支付成功处理
  129. $order_info = Db::name('store_order')->field('id,worker_id')->where('pay_no',$pay_no)->find();
  130. Db::startTrans();
  131. $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]);
  132. $error = 0;
  133. if(!$res){
  134. $error = 1;
  135. Db::rollback();
  136. }
  137. if($order_info['worker_id']){
  138. $rebate_res = $this->serve_rebate($order_info['id']);
  139. if(!$rebate_res['code']){
  140. $error = 2;
  141. Db::rollback();
  142. }
  143. }
  144. if($error == 0){
  145. Db::commit();
  146. }
  147. return true; // 返回处理完成
  148. } else {
  149. return $fail('通信失败,请稍后再通知我');
  150. }
  151. });
  152. $response->send();
  153. }
  154. //增加费用订单支付回调
  155. public function expenses_order_notify(){
  156. $app = Factory::payment(config('app.wx_pay'));
  157. $response = $app->handlePaidNotify(function ($message, $fail) {
  158. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  159. $pay_no = $message['out_trade_no'];
  160. $order_list = Db::name('store_expenses_order')->where('status',0)->where('pay_no',$pay_no)->find();
  161. if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
  162. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  163. }
  164. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  165. $order_info = Db::name('store_expenses_order')->field('id,order_id,price_total,reason')->where('pay_no',$pay_no)->find();
  166. // 用户支付成功处理
  167. Db::startTrans();
  168. $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]);
  169. $error = 0;
  170. if(!$expenses_order_res){
  171. $error = 1;
  172. Db::rollback();
  173. }
  174. $rebate_res = $this->add_serve_rebate($order_info['id']);
  175. if(!$rebate_res){
  176. $error = 2;
  177. Db::rollback();
  178. }
  179. $store_order_info = Db::name('store_order')->field('price_total,price_amount')->where('id',$order_info['order_id'])->find();
  180. $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']]);
  181. if(!$store_order_res){
  182. $error = 3;
  183. Db::rollback();
  184. }
  185. if($error == 0){
  186. Db::commit();
  187. }else{
  188. Db::rollback();
  189. }
  190. return true; // 返回处理完成
  191. } else {
  192. return $fail('通信失败,请稍后再通知我');
  193. }
  194. });
  195. $response->send();
  196. }
  197. //保证金支付回调
  198. public function deposit_notify(){
  199. $app = Factory::payment(config('app.worker_wx_pay'));
  200. $response = $app->handlePaidNotify(function ($message, $fail) {
  201. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  202. $pay_no = $message['out_trade_no'];
  203. $order_list = Db::name('store_deposit_order')->where('status',0)->where('pay_no',$pay_no)->find();
  204. if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
  205. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  206. }
  207. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  208. // 用户支付成功处理
  209. 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]);
  210. $wid = Db::table('store_deposit_order')->where('pay_no',$pay_no)->value('worker_id');
  211. //更新是否缴纳保证金
  212. Db::name('store_worker')->where('id',$wid)->update(array('deposit'=>1));
  213. //添加运输路线
  214. $county_arr = explode(',',$order_list['county']);
  215. foreach ($county_arr as $area){
  216. $city_id = Db::name('store_area')->where('id',$area)->value('pid');
  217. $province_id = Db::name('store_area')->where('id',$city_id)->value('pid');
  218. $area_data = array(
  219. 'worker_id' => $wid,
  220. 'start_province_id' => $order_list['start_province'],
  221. 'start_city_id' => $order_list['start_city'],
  222. 'start_county_id' => $order_list['start_county'],
  223. 'end_province_id' => $province_id,
  224. 'end_city_id' => $city_id,
  225. 'end_county_id' => $area
  226. );
  227. Db::name('store_serve_area')->insert($area_data);
  228. }
  229. return true; // 返回处理完成
  230. } else {
  231. return $fail('通信失败,请稍后再通知我');
  232. }
  233. });
  234. $response->send();
  235. }
  236. //会员费用回调
  237. public function membership_notify(){
  238. $app = Factory::payment(config('app.worker_wx_pay'));
  239. $response = $app->handlePaidNotify(function ($message, $fail) {
  240. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  241. $pay_no = $message['out_trade_no'];
  242. $order_list = Db::name('store_membership_order')->where('status',0)->where('pay_no',$pay_no)->find();
  243. if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
  244. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  245. }
  246. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  247. // 用户支付成功处理
  248. 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]);
  249. $order_info = Db::table('store_membership_order')->where('pay_no',$pay_no)->find();
  250. //添加接单人员会员信息
  251. $vip_time = Db::name('store_worker')->where('id',$order_info['worker_id'])->value('vip_time');
  252. $vip_timestamp = 0;
  253. if($order_info['type'] == 1){
  254. $vip_timestamp = strtotime('next month');
  255. }elseif($order_info['type'] == 2){
  256. $vip_timestamp = strtotime('+3 month');
  257. }elseif ($order_info['type'] == 3){
  258. $vip_timestamp = strtotime('+1years');
  259. }
  260. if($vip_time && $vip_time > time()){
  261. $vip_timestamp = $vip_timestamp + ($vip_time - time());
  262. }
  263. Db::name('store_worker')->where('id',$order_info['worker_id'])->update(array('vip'=>$order_info['type'],'vip_time'=>$vip_timestamp));
  264. return true; // 返回处理完成
  265. } else {
  266. return $fail('通信失败,请稍后再通知我');
  267. }
  268. });
  269. $response->send();
  270. }
  271. //自动报价回调
  272. public function offer_notify(){
  273. $app = Factory::payment(config('app.worker_wx_pay'));
  274. $response = $app->handlePaidNotify(function ($message, $fail) {
  275. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  276. $pay_no = $message['out_trade_no'];
  277. $order_list = Db::name('store_offer_order')->where('status',0)->where('pay_no',$pay_no)->find();
  278. if (empty($order_list)) { // 如果订单不存在 或者 订单已经支付过了
  279. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  280. }
  281. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  282. // 用户支付成功处理
  283. 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]);
  284. $order_info = Db::table('store_offer_order')->where('pay_no',$pay_no)->find();
  285. //添加接单人员自动报价信息
  286. $offer_time = Db::name('store_worker')->where('id',$order_info['worker_id'])->value('offer_time');
  287. $offer_timestamp = 0;
  288. if($order_info['type'] == 1){
  289. $offer_timestamp = strtotime('next month');
  290. }elseif($order_info['type'] == 2){
  291. $offer_timestamp = strtotime('+3 month');
  292. }elseif ($order_info['type'] == 3){
  293. $offer_timestamp = strtotime('+1years');
  294. }
  295. if($offer_time && $offer_time > time()){
  296. $offer_timestamp = $offer_timestamp + ($offer_time - time());
  297. }
  298. Db::name('store_worker')->where('id',$order_info['worker_id'])->update(array('offer_time'=>$offer_timestamp));
  299. return true; // 返回处理完成
  300. } else {
  301. return $fail('通信失败,请稍后再通知我');
  302. }
  303. });
  304. $response->send();
  305. }
  306. /**
  307. * 余额支付(雇主端)
  308. * $order_id 订单ID
  309. * $table_type 订单类型 1:store_order(订单),5:store_expenses_order(增加费用)
  310. */
  311. public function balance_pay($order_id,$table_type = 1){
  312. $field = 'user_id,price_total';
  313. $table_name = $table_type == 1?'store_order':'store_expenses_order';
  314. $order_info = Db::name($table_name)->field($field)->where('id',$order_id)->find();
  315. if(empty($order_info) || $order_info['price_total'] <= 0){
  316. return false;
  317. }
  318. $amount = $order_info['price_total'];
  319. $user_id = $order_info['user_id'];
  320. Db::startTrans();
  321. $balance_data = array(
  322. 'amount' => $amount,
  323. 'user_id' => $user_id,
  324. 'type' => 2,
  325. 'order_id' => $order_id,
  326. 'table_type' => $table_type,
  327. 'status' => 1
  328. );
  329. $error = 0;
  330. $balance = Db::name('store_balance_list')->insert($balance_data);
  331. if(!$balance){
  332. $error = 1;
  333. Db::rollback();
  334. }
  335. $member = Db::name('store_member')->where('id',$user_id)->setDec('balance',$amount);
  336. if(!$member){
  337. $error = 2;
  338. Db::rollback();
  339. }
  340. if($error == 0){
  341. Db::commit();
  342. return true;
  343. }else{
  344. return false;
  345. }
  346. }
  347. /**
  348. * 余额支付(接单端)
  349. * $order_id 订单ID
  350. */
  351. public function worker_balance_pay($order_id,$table_type){
  352. $field = 'price_total,worker_id';
  353. $table_type_arr = array(1=>'store_order',2=>'store_deposit_order',3=>'store_offer_order',4=>'store_grab_order',6=>'store_membership_order');
  354. $table_name = $table_type_arr[$table_type];
  355. $order_info = Db::name($table_name)->field($field)->where('id',$order_id)->find();
  356. if(empty($order_info) || $order_info['price_total'] <= 0){
  357. return false;
  358. }
  359. $amount = $order_info['price_total'];
  360. $worker_id = $order_info['worker_id'];
  361. Db::startTrans();
  362. $balance_data = array(
  363. 'amount' => $amount,
  364. 'user_id' => $worker_id,
  365. 'user_type' => 2,
  366. 'type' => 2,
  367. 'order_id' => $order_id,
  368. 'table_type' => $table_type,
  369. 'status' => 1
  370. );
  371. $error = 0;
  372. $balance = Db::name('store_balance_list')->insert($balance_data);
  373. if(!$balance){
  374. $error = 1;
  375. Db::rollback();
  376. }
  377. $worker = Db::name('store_worker')->where('id',$worker_id)->setDec('balance',$amount);
  378. if(!$worker){
  379. $error = 2;
  380. Db::rollback();
  381. }
  382. if($error == 0){
  383. Db::commit();
  384. return true;
  385. }else{
  386. return false;
  387. }
  388. }
  389. /**
  390. * 订单支付完成进行返利(未到账,等货主点击确认完成才到账)
  391. * $order_id 订单ID
  392. */
  393. public function serve_rebate($order_id){
  394. $field = 'worker_id,price_total,worker_ratio';
  395. $order_info = Db::name('store_order')->field($field)->where('id',$order_id)->find();
  396. if(empty($order_info) || $order_info['price_total'] <= 0 || $order_info['worker_ratio'] <= 0){
  397. return false;
  398. }
  399. //接单人员拿到的服务余额
  400. $worker_serve_amount = sprintf("%.2f",$order_info['price_total'] * $order_info['worker_ratio'] / 100);
  401. $worker_balance_data = array(
  402. 'amount' => $worker_serve_amount,
  403. 'user_id' => $order_info['worker_id'],
  404. 'user_type' => 2,
  405. 'get_type' => 2,
  406. 'order_id' => $order_id
  407. );
  408. $worker_balance = Db::name('store_balance_list')->insert($worker_balance_data);
  409. if($worker_balance){
  410. $data = array('code'=>1,'返利成功');
  411. }else{
  412. $data = array('code'=>0,'返利失败');
  413. }
  414. return $data;
  415. }
  416. /**
  417. * 增加费用支付完成进行追加返利
  418. * $order_id 增加费用订单ID
  419. */
  420. public function add_serve_rebate($order_id){
  421. $expenses_order_info = Db::name('store_expenses_order')->field('price_total,order_id')->where('id',$order_id)->find();
  422. if(empty($expenses_order_info) || $expenses_order_info['price_total'] <= 0 || $expenses_order_info['order_id'] <= 0){
  423. return false;
  424. }
  425. $field = 'worker_id,price_total,worker_ratio';
  426. $order_info = Db::name('store_order')->field($field)->where('id',$expenses_order_info['order_id'])->find();
  427. if(empty($order_info) || $order_info['worker_ratio'] <= 0){
  428. return false;
  429. }
  430. //增加接单人员拿到的服务余额
  431. //获取下单服务拥挤返利的信息
  432. $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();
  433. if(empty($balance_info)){
  434. return false;
  435. }
  436. $worker_serve_amount = sprintf("%.2f",$expenses_order_info['price_total'] * $order_info['worker_ratio'] / 100);
  437. $worker_balance = Db::name('store_balance_list')->where('id',$balance_info['id'])->update(array('amount'=>$balance_info['amount'] + $worker_serve_amount));
  438. if($worker_balance){
  439. $data = array('code'=>1,'增加返利成功');
  440. }else{
  441. $data = array('code'=>0,'增加返利失败');
  442. }
  443. return $data;
  444. }
  445. }