Lave.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Company;
  4. use app\admin\model\WechatPlan;
  5. use app\admin\model\WechatPlanRecord;
  6. use app\common\controller\Api;
  7. use app\common\model\{Category, Lave as LaveModel, User};
  8. use liuniu\repositories\LaveRepository;
  9. use liuniu\UtilService;
  10. use liuniu\WechatService;
  11. use think\Request;
  12. class Lave extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. public function lst(Request $request)
  17. {
  18. $where = UtilService::getMore(
  19. [
  20. ['page', 1],
  21. ['limit', 10],
  22. ['cid', $this->cid],
  23. ['paid', 1],
  24. ['category_id', 0],
  25. ['order', ''],
  26. ['key', ''],
  27. ], $request
  28. );
  29. $this->success('获取成功', LaveModel::lst($where));
  30. }
  31. public function ify()
  32. {
  33. $this->success('获取成功', Category::getCategoryArray('lave', null, $this->cid));
  34. }
  35. public function create(Request $request)
  36. {
  37. $where = UtilService::postMore(
  38. [
  39. ['cid', $this->cid],
  40. ['user_id', $this->auth->getUserinfo()['id']],
  41. ['order_name', ''],
  42. ['category_id', 0],
  43. ['amount', 0],
  44. ['name', ''],
  45. ['contact', ''],
  46. ['tel', 0],
  47. ['address', ''],
  48. ['is_open', '0'],
  49. ['is_ticket', '0'],
  50. ['pay_type', '0'],
  51. ['type', '0'],
  52. ['help_id', 0],
  53. ['from', 'weixin'],
  54. ], $request
  55. );
  56. $where1 = $where;
  57. unset($where1['from']);
  58. $where1['order_id'] = LaveModel::getNewOrderId();
  59. if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
  60. @file_put_contents("lave.txt", json_encode($where1));
  61. $order = LaveModel::create($where1);
  62. cache('lave_' . $where['user_id'], '1', 10);
  63. if (!$order) $this->error(LaveModel::getErrorInfo());
  64. $orderId = $order['order_id'];
  65. $info = compact('orderId');
  66. if ($orderId) {
  67. $orderInfo = LaveModel::where('order_id', $orderId)->find();
  68. if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
  69. if ($orderInfo['paid']) $this->error('支付已支付!');
  70. try {
  71. if ($where['from'] == 'routine') {
  72. $jsConfig = LaveRepository::jsPay($this->cid, $orderId); //创建订单jspay
  73. } else if ($where['from'] == 'weixinh5') {
  74. $jsConfig = LaveRepository::h5Pay($this->cid, $orderId);
  75. } else {
  76. $jsConfig = LaveRepository::wxPay($this->cid, $orderId);
  77. }
  78. } catch (\Exception $e) {
  79. return $this->error($e->getMessage());
  80. }
  81. $info['jsConfig'] = $jsConfig;
  82. return $this->success('订单创建成功', $info);
  83. } else $this->error(LaveModel::getErrorInfo());
  84. }
  85. public function mylst(Request $request)
  86. {
  87. $where = UtilService::getMore(
  88. [
  89. ['page', 1],
  90. ['limit', 10],
  91. ['cid', $this->cid],
  92. ['user_id', $this->auth->getUserinfo()['id']],
  93. ['paid', -1],
  94. ['category_id', 0],
  95. ], $request
  96. );
  97. $this->success('获取成功', LaveModel::lst($where));
  98. }
  99. /**
  100. * 订单支付
  101. * @param Request $request
  102. * @return mixed
  103. */
  104. public function pay(Request $request)
  105. {
  106. list($uni, $paytype, $from) = UtilService::postMore([
  107. ['uni', ''],
  108. ['paytype', '0'],
  109. ['from', 'weixin']
  110. ], $request, true);
  111. if (!$uni) $this->error('参数错误!');
  112. $order = LaveModel::where('cid', $this->cid)->where('order_id', $uni)->find();
  113. if (!$order)
  114. $this->error('订单不存在!');
  115. if ($order['paid'])
  116. $this->error('该订单已支付!');
  117. $order['pay_type'] = $paytype; //重新支付选择支付方式
  118. switch ($order['pay_type']) {
  119. case '0':
  120. try {
  121. if ($from == 'routine') {
  122. $jsConfig = LaveRepository::jsPay($this->cid, $order); //订单列表发起支付
  123. } else if ($from == 'weixinh5') {
  124. $jsConfig = LaveRepository::h5Pay($this->cid, $order);
  125. } else {
  126. $jsConfig = LaveRepository::wxPay($this->cid, $order);
  127. }
  128. } catch (\Exception $e) {
  129. $this->error($e->getMessage());
  130. }
  131. $this->success('获取成功', $jsConfig);
  132. break;
  133. }
  134. return $this->error('支付方式错误');
  135. }
  136. // 支付中签约
  137. public function createSign(Request $request)
  138. {
  139. // var_dump(123);die();
  140. $where = UtilService::postMore(
  141. [
  142. ['cid', $this->cid],
  143. ['user_id', $this->auth->getUserinfo()['id']],
  144. // ['user_id', 0],
  145. ['order_name', ''],
  146. ['category_id', 0],
  147. ['amount', 0],
  148. ['name', ''],
  149. ['contact', ''],
  150. ['tel', 0],
  151. ['address', ''],
  152. ['is_open', '0'],
  153. ['is_ticket', '0'],
  154. ['pay_type', '0'],
  155. ['type', '0'],
  156. ['help_id', 0],
  157. ['plan_id', 0],
  158. ['from', 'weixin'],
  159. ], $request
  160. );
  161. $where1 = $where;
  162. unset($where1['from']);
  163. $where1['order_id'] = LaveModel::getNewOrderId();
  164. $contract_display_account = User::where('id', $where1['user_id'])->value('nickname');
  165. if (empty($contract_display_account)){
  166. $this->error('用户不存在!');
  167. }
  168. if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
  169. @file_put_contents("lave.txt", json_encode($where1));
  170. $plan_id=$where1['plan_id'];
  171. unset($where1['plan_id']);
  172. $order = LaveModel::create($where1);
  173. $where1['contract_code'] = WechatPlanRecord::getNewCode();
  174. $where1['contract_display_account']=$contract_display_account;
  175. cache('lave_' . $where['user_id'], '1', 10);
  176. if (!$order) $this->error(LaveModel::getErrorInfo());
  177. $orderId = $order['order_id'];
  178. $info = compact('orderId');
  179. $order['plan_id'] = $plan_id;
  180. if ($orderId) {
  181. $orderInfo = LaveModel::where('order_id', $orderId)->find();
  182. if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
  183. if ($orderInfo['paid']) $this->error('支付已支付!');
  184. // 创建签约
  185. $plan_record=[
  186. 'plan_id'=>$plan_id,
  187. 'cid'=>$where['cid'],
  188. 'uid'=>$where['user_id'],
  189. 'price'=>$where['amount'],
  190. 'is_signing'=>0,
  191. 'contract_code'=>$where1['contract_code'],
  192. 'contract_display_account'=>$where1['contract_display_account'],
  193. 'is_open'=>$where1['is_open'],
  194. 'tel'=>$where1['tel'],
  195. 'category_id'=>$where1['category_id'],
  196. 'order_name'=>$where1['order_name'],
  197. ];
  198. $record=WechatPlanRecord::create($plan_record);
  199. $plan_record['spbill_create_ip']=User::where('id', $where1['user_id'])->value('loginip');
  200. $plan_record['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $where['cid'];
  201. try {
  202. if ($where['from'] == 'routine') {
  203. $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId,$plan_record); //创建订单jspay
  204. } else if ($where['from'] == 'weixinh5') {
  205. $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId,$plan_record);
  206. } else {
  207. $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId,$plan_record);
  208. }
  209. } catch (\Exception $e) {
  210. return $this->error($e->getMessage());
  211. }
  212. $info['jsConfig'] = $jsConfig;
  213. // 增加参与人数
  214. $count=WechatPlanRecord::where('uid', $where1['user_id'])->count();
  215. if ($count==0){
  216. $num=WechatPlan::where('id', $plan_id)->value('num');
  217. WechatPlan::where('id', $plan_id)->update(['num' => $num+1]);
  218. }
  219. return $this->success('订单创建成功', $info);
  220. } else $this->error(LaveModel::getErrorInfo());
  221. }
  222. /**
  223. * 签约订单支付
  224. * @param Request $request
  225. * @return mixed
  226. */
  227. public function paySign(Request $request)
  228. {
  229. // var_dump(456);die();
  230. list($uni, $paytype, $from) = UtilService::postMore([
  231. ['uni', ''],
  232. ['paytype', '0'],
  233. ['from', 'weixin']
  234. ], $request, true);
  235. if (!$uni) $this->error('参数错误!');
  236. $order = LaveModel::where('cid', $this->cid)->where('order_id', $uni)->find();
  237. if (!$order)
  238. $this->error('订单不存在!');
  239. if ($order['paid'])
  240. $this->error('该订单已支付!');
  241. $order['pay_type'] = $paytype; //重新支付选择支付方式
  242. switch ($order['pay_type']) {
  243. case '0':
  244. try {
  245. if ($from == 'routine') {
  246. $jsConfig = LaveRepository::jsPaySign($this->cid, $order); //订单列表发起支付
  247. } else if ($from == 'weixinh5') {
  248. $jsConfig = LaveRepository::h5PaySign($this->cid, $order);
  249. } else {
  250. $jsConfig = LaveRepository::wxPaySign($this->cid, $order);
  251. }
  252. } catch (\Exception $e) {
  253. $this->error($e->getMessage());
  254. }
  255. $this->success('获取成功', $jsConfig);
  256. break;
  257. }
  258. return $this->error('支付方式错误');
  259. }
  260. // 申请扣款
  261. public function payPap(Request $request)
  262. {
  263. @file_put_contents("quanju.txt", "测试申请扣款-\r\n", 8);
  264. die();
  265. $list=WechatPlanRecord::where('is_signing',0)->select();
  266. $cid=$this->cid;
  267. $arr=[];
  268. $arr['body']='月捐款';
  269. foreach ($list as $k => $v){
  270. $cid=$v['cid'];
  271. // if ($this->querySign($cid,$v['uid'],$v['plan_id'],$v['contract_code'])){
  272. $arr['mch_id'] =Company::where('id', $cid)->value('pay_weixin_mchid');
  273. $arr['out_trade_no']=LaveModel::getNewOrderId();
  274. $arr['total_fee'] = $v['price'];
  275. // $arr['trade_type'] = 'PAP';
  276. $arr['contract_id'] = Company::where('id', $cid)->value('contract_id');
  277. // $rs = WechatService::paymentOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'JSAPI', [], $cid);
  278. $rs = WechatService::papPayApply($arr['out_trade_no'], $arr['total_fee'], "lave", '月捐款', 'PAP', [], $cid,$arr['mch_id'],$arr['contract_id']);
  279. $where1['cid']=$cid;
  280. $where1['user_id']=$this->auth->getUserinfo()['id'];
  281. $where1['order_name']='月捐款';
  282. $where1['category_id']=81;
  283. $where1['amount']=$arr['total_fee'];
  284. $where1['name']=$this->auth->getUserinfo()['username'];
  285. $where1['contact']='';
  286. $where1['tel']=$this->auth->getUserinfo()['mobile'];
  287. $where1['address']='用户未填写联系地址';
  288. $where1['is_open']=$v['is_open'];
  289. $where1['is_ticket']='0';
  290. $where1['type']='0';
  291. $where1['help_id']='0';
  292. $order = LaveModel::create($where1);
  293. // }
  294. }
  295. }
  296. // 解除签约
  297. public function deleteSign(Request $request){
  298. $where = UtilService::postMore(
  299. [
  300. ['cid', $this->cid],
  301. ['user_id', $this->auth->getUserinfo()['id']],
  302. ], $request
  303. );
  304. $sign_info=WechatPlanRecord::where('cid', $where['cid'])->where('uid', $where['user_id'])->where('is_signing',1)->find();
  305. if (!$sign_info){
  306. $this->error('未找到签约信息!');
  307. }
  308. $mch_id =Company::where('id', $where['cid'])->value('pay_weixin_mchid');
  309. $plan_id=$sign_info['plan_id'];
  310. $contract_code=$sign_info['contract_code'];
  311. // $mch_id,$contract_code,$pan_id,$version='1.0',$options=[],$cid=0)
  312. $rs=WechatService::deleteSign($mch_id,$contract_code,$plan_id,'1.0',[],$where['cid']);
  313. @file_put_contents("quanju.txt", json_encode($rs)."-解除签约返回结果\r\n", 8);
  314. if ($rs){
  315. WechatPlanRecord::where('cid', $where['cid'])->where('uid', $where['user_id'])->where('is_signing',0)->update(['is_signing'=>1]);
  316. $this->success('解除签约成功!');
  317. }else{
  318. $this->error('解除签约失败!');
  319. }
  320. }
  321. // 查询签约状态
  322. // public function querySign(int $cid,int $uid,int $plan_id,string $contract_code)
  323. // {
  324. //// $where = UtilService::postMore(
  325. //// [
  326. //// ['cid', $this->cid],
  327. //// ['user_id', $this->auth->getUserinfo()['id']],
  328. //// ], $request
  329. //// );
  330. // $sign_info = WechatPlanRecord::where('cid', $cid)->where('uid', $uid)->where('is_signing', 1)->find();
  331. // if (!$sign_info) {
  332. // $this->error('未找到签约信息!');
  333. // }
  334. // $mch_id = Company::where('id', $cid)->value('pay_weixin_mchid');
  335. //// $plan_id = $sign_info['plan_id'];
  336. //// $contract_code = $sign_info['contract_code'];
  337. // $rs = WechatService::querySign($mch_id, $contract_code, $plan_id);
  338. // @file_put_contents("quanju.txt", json_encode($rs) . "-查询签约状态返回结果\r\n", 8);
  339. // if ($rs['return_code'] == 'SUCCESS') {
  340. // if ($rs['contract_state'] == 1) {
  341. // WechatPlanRecord::where('cid',$cid)->where('uid', $uid)->where('is_signing', 1)->update(['is_signing' => 0]);
  342. // return false;
  343. // }else{
  344. // WechatPlanRecord::where('cid',$cid)->where('uid', $uid)->where('is_signing', 1)->update(['contract_id' => $rs['contract_id']]);
  345. // return true;
  346. // }
  347. // } else {
  348. // $this->error('查询签约状态失败!');
  349. // }
  350. // }
  351. // 测试查询签约状态
  352. public function csquerySign(Request $request)
  353. {
  354. $where = UtilService::postMore(
  355. [
  356. ['cid', ''],
  357. ['user_id', ''],
  358. ['contract_code',''],
  359. ['plan_id', ''],
  360. ], $request
  361. );
  362. // $sign_info = WechatPlanRecord::where('cid', $cid)->where('uid', $uid)->where('is_signing', 1)->find();
  363. // if (!$sign_info) {
  364. // $this->error('未找到签约信息!');
  365. // }
  366. $mch_id = Company::where('id', $where['cid'])->value('pay_weixin_mchid');
  367. // $plan_id = $sign_info['plan_id'];
  368. // $contract_code = $sign_info['contract_code'];
  369. $rs = WechatService::querySign($mch_id, $where['contract_code'], $where['plan_id']);
  370. // if ($rs['return_code'] == 'SUCCESS') {
  371. // if ($rs['contract_state'] == 1) {
  372. // WechatPlanRecord::where('cid',$cid)->where('uid', $uid)->where('is_signing', 1)->update(['is_signing' => 0]);
  373. // return false;
  374. // }else{
  375. // WechatPlanRecord::where('cid',$cid)->where('uid', $uid)->where('is_signing', 1)->update(['contract_id' => $rs['contract_id']]);
  376. // return true;
  377. // }
  378. // } else {
  379. // $this->error('查询签约状态失败!');
  380. // }
  381. }
  382. public function plan_lst(Request $request)
  383. {
  384. $where = UtilService::getMore(
  385. [
  386. ['page', 1],
  387. ['limit', 10],
  388. ['cid', $this->cid],
  389. // ['paid', 1],
  390. // ['category_id', 0],
  391. // ['order', ''],
  392. // ['key', ''],
  393. ], $request
  394. );
  395. $this->success('获取成功', WechatPlan::lst($where));
  396. }
  397. public function plan_read(Request $request)
  398. {
  399. $where = UtilService::getMore(
  400. [
  401. ['id', 0],
  402. ['cid', $this->cid],
  403. ], $request
  404. );
  405. $this->success('获取成功', WechatPlan::read($where));
  406. }
  407. // 签约列表
  408. public function user_plan_lst(Request $request)
  409. {
  410. $where = UtilService::getMore(
  411. [
  412. ['cid', 0],
  413. // ['cid', $this->cid],
  414. // ['uid', $this->auth->getUserinfo()['id']],
  415. ['uid', 0],
  416. ], $request
  417. );
  418. if (!isset($where['uid'])) $this->error('用户不存在');
  419. if (!isset($where['cid'])) $this->error('请选择正确的城市的红十字会');
  420. $this->success('获取成功', WechatPlanRecord::record_lst($where));
  421. }
  422. // 签约记录详情
  423. public function user_record_info(Request $request)
  424. {
  425. $where = UtilService::getMore(
  426. [
  427. ['id', 0],
  428. // ['cid', 0],
  429. // ['cid', $this->cid],
  430. // ['user_id', $this->auth->getUserinfo()['id']],
  431. // ['user_id', 0],
  432. ], $request
  433. );
  434. $this->success('获取成功', WechatPlanRecord::record_info($where));
  435. }
  436. }
  437. ?>