Lave.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. ['order_name', ''],
  145. ['category_id', 0],
  146. ['amount', 0],
  147. ['name', ''],
  148. ['contact', ''],
  149. ['tel', 0],
  150. ['address', ''],
  151. ['is_open', '0'],
  152. ['is_ticket', '0'],
  153. ['pay_type', '0'],
  154. ['type', '0'],
  155. ['help_id', 0],
  156. ['from', 'weixin'],
  157. ], $request
  158. );
  159. $where1 = $where;
  160. unset($where1['from']);
  161. $where1['order_id'] = LaveModel::getNewOrderId();
  162. $where1['contract_code'] = WechatPlan::getNewCode();
  163. $contract_display_account = User::where('id', $where1['user_id'])->value('nickname');
  164. if (empty($contract_display_account)){
  165. $this->error('用户不存在!');
  166. }
  167. $where1['contract_display_account']=$contract_display_account;
  168. if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
  169. @file_put_contents("lave.txt", json_encode($where1));
  170. $order = LaveModel::create($where1);
  171. cache('lave_' . $where['user_id'], '1', 10);
  172. if (!$order) $this->error(LaveModel::getErrorInfo());
  173. $orderId = $order['order_id'];
  174. $info = compact('orderId');
  175. $order['plan_id'] = WechatPlan::where('cid', $where['cid'])->value('plan_id');
  176. if ($orderId) {
  177. $orderInfo = LaveModel::where('order_id', $orderId)->find();
  178. if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
  179. if ($orderInfo['paid']) $this->error('支付已支付!');
  180. // 创建签约
  181. $plan_record=[
  182. 'plan_id'=>WechatPlan::where('price', $where['amount'])->value('plan_id'),
  183. 'cid'=>$where['cid'],
  184. 'uid'=>$where['user_id'],
  185. 'price'=>$where['amount'],
  186. 'is_signing'=>0,
  187. 'contract_code'=>$where1['contract_code'],
  188. 'contract_display_account'=>$where1['contract_display_account'],
  189. 'is_open'=>$where1['is_open'],
  190. ];
  191. $record=WechatPlanRecord::create($plan_record);
  192. $plan_record['spbill_create_ip']=User::where('id', $where1['user_id'])->value('loginip');
  193. $plan_record['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $where['cid'];
  194. try {
  195. if ($where['from'] == 'routine') {
  196. $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId,$plan_record); //创建订单jspay
  197. } else if ($where['from'] == 'weixinh5') {
  198. $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId,$plan_record);
  199. } else {
  200. $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId,$plan_record);
  201. }
  202. } catch (\Exception $e) {
  203. return $this->error($e->getMessage());
  204. }
  205. $info['jsConfig'] = $jsConfig;
  206. return $this->success('订单创建成功', $info);
  207. } else $this->error(LaveModel::getErrorInfo());
  208. }
  209. /**
  210. * 签约订单支付
  211. * @param Request $request
  212. * @return mixed
  213. */
  214. public function paySign(Request $request)
  215. {
  216. // var_dump(456);die();
  217. list($uni, $paytype, $from) = UtilService::postMore([
  218. ['uni', ''],
  219. ['paytype', '0'],
  220. ['from', 'weixin']
  221. ], $request, true);
  222. if (!$uni) $this->error('参数错误!');
  223. $order = LaveModel::where('cid', $this->cid)->where('order_id', $uni)->find();
  224. if (!$order)
  225. $this->error('订单不存在!');
  226. if ($order['paid'])
  227. $this->error('该订单已支付!');
  228. $order['pay_type'] = $paytype; //重新支付选择支付方式
  229. switch ($order['pay_type']) {
  230. case '0':
  231. try {
  232. if ($from == 'routine') {
  233. $jsConfig = LaveRepository::jsPaySign($this->cid, $order); //订单列表发起支付
  234. } else if ($from == 'weixinh5') {
  235. $jsConfig = LaveRepository::h5PaySign($this->cid, $order);
  236. } else {
  237. $jsConfig = LaveRepository::wxPaySign($this->cid, $order);
  238. }
  239. } catch (\Exception $e) {
  240. $this->error($e->getMessage());
  241. }
  242. $this->success('获取成功', $jsConfig);
  243. break;
  244. }
  245. return $this->error('支付方式错误');
  246. }
  247. // 申请扣款
  248. public function payPap(Request $request)
  249. {
  250. $list=WechatPlanRecord::where('is_signing',0)->select();
  251. $cid=$this->cid;
  252. $arr=[];
  253. $arr['body']='月捐款';
  254. foreach ($list as $k => $v){
  255. $cid=$v['cid'];
  256. $arr['mch_id'] =Company::where('id', $cid)->value('mch_id');
  257. $arr['out_trade_no']=LaveModel::getNewOrderId();
  258. $arr['total_fee'] = $v['price'];
  259. // $arr['trade_type'] = 'PAP';
  260. $arr['contract_id'] = Company::where('id', $cid)->value('contract_id');
  261. // $rs = WechatService::paymentOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'JSAPI', [], $cid);
  262. $rs = WechatService::papPayApply($arr['out_trade_no'], $arr['total_fee'], "lave", '月捐款', 'PAP', [], $cid,$arr['mch_id'],$arr['contract_id']);
  263. $where1['cid']=$cid;
  264. $where1['user_id']=$this->auth->getUserinfo()['id'];
  265. $where1['order_name']='月捐款';
  266. $where1['category_id']=81;
  267. $where1['amount']=$arr['total_fee'];
  268. $where1['name']=$this->auth->getUserinfo()['username'];
  269. $where1['contact']='';
  270. $where1['tel']=$this->auth->getUserinfo()['mobile'];
  271. $where1['address']='用户未填写联系地址';
  272. $where1['is_open']=$v['is_open'];
  273. $where1['is_ticket']='0';
  274. $where1['type']='0';
  275. $where1['help_id']='0';
  276. $order = LaveModel::create($where1);
  277. }
  278. $where = UtilService::postMore(
  279. [
  280. ['cid', $this->cid],
  281. ['user_id', $this->auth->getUserinfo()['id']],
  282. ['order_name', 'order_name'],
  283. ['category_id', 0],
  284. ['amount', 0],
  285. ['name', ''],
  286. ['contact', ''],
  287. ['tel', 0],
  288. ['address', ''],
  289. ['is_open', '0'],
  290. ['is_ticket', '0'],
  291. ['type', '0'],
  292. ['help_id', 0],
  293. ], $request
  294. );
  295. $where1 = $where;
  296. unset($where1['from']);
  297. $where1['order_id'] = LaveModel::getNewOrderId();
  298. if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
  299. @file_put_contents("lave.txt", json_encode($where1));
  300. $order = LaveModel::create($where1);
  301. cache('lave_' . $where['user_id'], '1', 10);
  302. if (!$order) $this->error(LaveModel::getErrorInfo());
  303. $orderId = $order['order_id'];
  304. $info = compact('orderId');
  305. $order['plan_id'] = WechatPlan::where('price', $where['amount'])->value('plan_id');
  306. if ($orderId) {
  307. $orderInfo = LaveModel::where('order_id', $orderId)->find();
  308. if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
  309. if ($orderInfo['paid']) $this->error('支付已支付!');
  310. try {
  311. if ($where['from'] == 'routine') {
  312. $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId); //创建订单jspay
  313. } else if ($where['from'] == 'weixinh5') {
  314. $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId);
  315. } else {
  316. $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId);
  317. }
  318. } catch (\Exception $e) {
  319. return $this->error($e->getMessage());
  320. }
  321. $info['jsConfig'] = $jsConfig;
  322. return $this->success('订单创建成功', $info);
  323. } else $this->error(LaveModel::getErrorInfo());
  324. }
  325. }
  326. ?>