HuiPay.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\common\controller\Api;
  5. use app\utils\CurlUtil;
  6. use app\utils\Easywechat\MiniAppService;
  7. use app\utils\JingXiu\JingXiuPayUtil;
  8. use app\utils\LogUtil;
  9. use app\utils\PayUtil;
  10. use think\Cache;
  11. use think\Db;
  12. use think\Exception;
  13. use think\Request;
  14. /**
  15. * 通行证
  16. */
  17. class HuiPay extends Api
  18. {
  19. // 日志模块名称
  20. const LOG_MODULE = 'HuiPay';
  21. protected $noNeedLogin = ['vip_recharge', 'gold_recharge', 'pay', 'pay_notify'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. public function __construct(Request $request = null)
  28. {
  29. parent::__construct($request);
  30. //日志统一写入
  31. register_shutdown_function([new LogUtil, 'close']);
  32. LogUtil::getInstance('Api/'); //设置日志存入通道
  33. }
  34. /**
  35. * 会员充值 (跳转小程序充值)
  36. * @return void
  37. */
  38. public function vip_recharge()
  39. {
  40. Db::startTrans();
  41. try {
  42. $rc_id = input('rc_id', 0);
  43. $pay_type = input('pay_type', 'wechat');
  44. $uid = $this->auth->id;
  45. if (!in_array($pay_type, ['wechat', 'alipay'])) {
  46. throw new Exception('错误的支付类型');
  47. }
  48. if (!$rc_id) {
  49. throw new Exception('请选择会员套餐');
  50. }
  51. //赋值money
  52. $recharge_config = Db::name('payvip_config')->where('id', $rc_id)->find();
  53. $money = $recharge_config['money'];
  54. if ($money <= 0) {
  55. throw new Exception('支付金额必须大于0');
  56. }
  57. if ($money > 10000) {
  58. throw new Exception('支付金额太大');
  59. }
  60. //会员等级冲突
  61. //当前是会员,但是却要向下级续费,直接提示报错
  62. //修改等级,向上立刻改,向下不允许
  63. $wallet_info = model('wallet')->getWallet($this->auth->id);
  64. if ($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']) {
  65. throw new Exception('当前会员没有过期,不能续费');
  66. }
  67. //创建订单
  68. $data = [];
  69. $data['status'] = 0;
  70. $pay_no = createUniqueNo('V', $uid);
  71. $data['pay_no'] = $pay_no;
  72. $data['money'] = $money;
  73. $data['payment_class'] = $pay_type;
  74. $data['user_id'] = $uid;
  75. $data['ext_info'] = json_encode(['subject' => '充值vip支付']);
  76. $data['memo'] = '充值会员支付';
  77. $data['queue_status'] = 1;//队列状态:0不参与;1参与;2:处理成功
  78. $data['createtime'] = time();
  79. //$data['payment'] = 'miniapp';
  80. $data['payment'] = 'app';
  81. $pay_order = Db::name('pay_order')->insertGetId($data);
  82. //创建回调
  83. $even_data = [];
  84. $even_data['event'] = 'success';
  85. $even_data['class'] = 'app\common\model\Recharge';
  86. $even_data['method'] = 'vippaysucc';
  87. $even_data['args'] = json_encode(['user_id' => $uid, 'days' => $recharge_config['days'], 'vip_level' => $recharge_config['vip_level'], 'gold_num' => $recharge_config['gold_num'], 'money' => $money]);
  88. $even_data['pay_no'] = $pay_no;
  89. $pay_event = Db::name('pay_event')->insertGetId($even_data);
  90. if (!$pay_order || !$pay_event) {
  91. throw new Exception('下单失败');
  92. }
  93. Db::commit();
  94. $pay = new JingXiuPayUtil();
  95. if (!$pay->wxWapPay($pay_no,$money,'充值会员','/api/hui_pay/pay_notify',$pay_type)){
  96. $this->error($pay->getMessage());
  97. }
  98. $data = $pay->getData();
  99. $this->success('success', $data);
  100. } catch (Exception $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. }
  105. /**
  106. * 金币充值
  107. * @return void
  108. */
  109. public function gold_recharge()
  110. {
  111. Db::startTrans();
  112. try {
  113. $rc_id = input_post('rc_id', 0);
  114. $pay_type = input_post('pay_type','wechat');
  115. $freemoney = input_post('freemoney', 0, 'intval');
  116. $uid = $this->auth->id;
  117. if (!$rc_id && !$freemoney) {
  118. throw new Exception('请选择或填写充值金额');
  119. }
  120. if (!in_array($pay_type, ['wechat', 'alipay'])) {
  121. throw new Exception('错误的支付类型');
  122. }
  123. //赋值money
  124. if ($rc_id) {
  125. $recharge_config = Db::name('paygold_config')->where('id', $rc_id)->find();
  126. $money = $recharge_config ? $recharge_config['money'] : 0;
  127. $gold = $recharge_config ? $recharge_config['gold'] : 0;
  128. $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
  129. $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
  130. $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
  131. }
  132. //自由输入覆盖
  133. if (!empty($freemoney)) {
  134. $rc_id = 0;
  135. $money = floatval($freemoney);
  136. $bili = config('site.money_to_gold') ?: 10;
  137. $gold = bcmul($money, $bili, 0);
  138. $first_gold = 0;
  139. $first_vipdays = 0;
  140. $vip_gold = 0;
  141. }
  142. if ($money <= 0) {
  143. throw new Exception('支付金额必须大于0');
  144. }
  145. if ($money > 10000) {
  146. throw new Exception('支付金额太大');
  147. }
  148. //查询是不是会员,若不是则不赠送金币
  149. $vip_endtime = Db::name('user_wallet')->where('user_id', $this->auth->id)->value('vip_endtime');
  150. if ($vip_endtime < time()) {
  151. $vip_gold = 0;
  152. }
  153. //创建订单
  154. $data = [];
  155. $data['status'] = 0;
  156. $pay_no = createUniqueNo('P', $uid);
  157. $data['pay_no'] = $pay_no;
  158. $data['money'] = $money;
  159. $data['payment_class'] = $pay_type;
  160. $data['user_id'] = $uid;
  161. $data['ext_info'] = json_encode(['subject' => '充值金币支付']);
  162. $data['memo'] = '充值金币支付';
  163. $data['queue_status'] = 1;//队列状态:0不参与;1参与;2:处理成功
  164. $data['createtime'] = time();
  165. $data['payment'] = 'app';
  166. $pay_order = Db::name('pay_order')->insertGetId($data);
  167. //创建回调
  168. $even_data = [];
  169. $even_data['event'] = 'success';
  170. $even_data['class'] = 'app\common\model\Recharge';
  171. $even_data['method'] = 'goldpaysucc';
  172. $even_data['args'] = json_encode(['user_id' => $uid, 'gold' => $gold, 'money' => $money, 'pg_id' => $rc_id, 'first_gold' => $first_gold, 'first_vipdays' => $first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
  173. $even_data['pay_no'] = $pay_no;
  174. $pay_event = Db::name('pay_event')->insertGetId($even_data);
  175. if (!$pay_order || !$pay_event) {
  176. throw new Exception('下单失败');
  177. }
  178. Db::commit();
  179. $pay = new JingXiuPayUtil();
  180. if (!$pay->wxWapPay($pay_no,$money,'充值金币','/api/hui_pay/pay_notify',$pay_type)){
  181. $this->error($pay->getMessage());
  182. }
  183. $data = $pay->getData();
  184. $this->success('success', $data);
  185. } catch (Exception $e) {
  186. Db::rollback();
  187. $this->error($e->getMessage());
  188. }
  189. }
  190. /**
  191. * 支付回调
  192. *
  193. * @param Request $request
  194. * @return string
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\ModelNotFoundException
  197. * @throws \think\exception\DbException
  198. */
  199. public function pay_notify(Request $request)
  200. {
  201. $params = $request->param();
  202. LogUtil::info('支付回调参数', self::LOG_MODULE, __FUNCTION__, $params);
  203. // 校验回调信息
  204. if (empty($params['order_status']) || $params['order_status'] != 'SUCCESS' || empty($params['out_trade_no'])) {
  205. LogUtil::info('回调信息有误', self::LOG_MODULE, __FUNCTION__,$params['order_status'] ?? 'error');
  206. return self::response201('支付信息有误');
  207. }
  208. // 校验签名
  209. $huiPay = new JingXiuPayUtil();
  210. if (!$huiPay->checkSign($params)){
  211. LogUtil::info('签名校验失败', self::LOG_MODULE, __FUNCTION__);
  212. return self::response201('签名校验失败');
  213. }
  214. // 开始处理业务逻辑
  215. $pay_no = $params['out_trade_no'];
  216. LogUtil::info('订单号', self::LOG_MODULE, __FUNCTION__, $pay_no);
  217. //查询订单信息
  218. $order_info = Db::name('pay_order')->where('pay_no', $pay_no)->find();
  219. if (!$order_info) {
  220. LogUtil::info('订单信息不存在', self::LOG_MODULE, __FUNCTION__);
  221. return self::response201('订单信息不存在');
  222. }
  223. if ($order_info['status'] == 3){
  224. LogUtil::info('订单信息_已删除', self::LOG_MODULE, __FUNCTION__, [
  225. 'status' => $order_info['status']
  226. ]);
  227. return self::response201('充值入账失败');
  228. }
  229. if ($order_info['status'] == 1 || $order_info['status'] == 2) {
  230. LogUtil::info('充值入账更新余额失败_status已更新过', self::LOG_MODULE, __FUNCTION__, [
  231. 'status' => $order_info['status']
  232. ]);
  233. return self::response201('充值入账失败');
  234. }
  235. $extendType = '';
  236. //区分vip 还是 冲金币
  237. $is_g = stripos($pay_no, 'P');
  238. if ($is_g !== false) {
  239. $extendType = 'gold';
  240. } else {
  241. $is_v = stripos($pay_no, 'V');
  242. if ($is_v !== false) {
  243. $extendType = 'vip';
  244. }
  245. }
  246. Db::startTrans();
  247. try {
  248. //status已更新过
  249. $result = Db::name('pay_order')->where('pay_no', $pay_no)->where('status',$order_info['status'])->setField(['status' => 2]);
  250. if (!$result) {
  251. LogUtil::info('充值入账更新余额失败_status更新状态2不成功', self::LOG_MODULE, __FUNCTION__, [
  252. 'status' => $order_info['status']
  253. ]);
  254. Db::rollback();
  255. return self::response201('充值入账失败');
  256. }
  257. //你可以在此编写订单逻辑
  258. $payEvent = Db::name('pay_event')->where('pay_no', $pay_no)->find();
  259. $args = json_decode($payEvent['args'] ?? '', true);
  260. $rechargeM = new \app\common\model\Recharge();
  261. if ($extendType == 'gold') {
  262. $payRes = $rechargeM->goldpaysucc($pay_no, $args,1);
  263. $payTypeStr = '充值';
  264. } elseif ($extendType == 'vip') {
  265. $payRes = $rechargeM->vippaysucc($pay_no, $args);
  266. $payTypeStr = 'vip';
  267. } else {
  268. $payRes = false;
  269. $payTypeStr = '未知支付类型';
  270. }
  271. if (!$payRes) {
  272. Db::rollback();
  273. LogUtil::info('更新失败', self::LOG_MODULE, __FUNCTION__, $payTypeStr);
  274. return self::response201('更新失败');
  275. }
  276. Db::commit();
  277. LogUtil::info('订单处理成功', self::LOG_MODULE, __FUNCTION__, $payTypeStr);
  278. return self::response200();
  279. }catch (\Exception $e){
  280. Db::rollback();
  281. LogUtil::info('更新失败', self::LOG_MODULE, __FUNCTION__, $e);
  282. return self::response201('更新失败');
  283. }
  284. }
  285. /**
  286. * 成功返回
  287. * @param string $message
  288. * @return string
  289. */
  290. private static function response200(string $message = 'SUCCESS')
  291. {
  292. return $message;
  293. }
  294. /**
  295. * 失败返回201
  296. * @param string $message
  297. * @return string
  298. */
  299. private static function response201(string $message = 'error')
  300. {
  301. return $message;
  302. }
  303. }