HuiPay.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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\LogUtil;
  8. use app\utils\PayUtil;
  9. use think\Cache;
  10. use think\Db;
  11. use think\Exception;
  12. use think\Request;
  13. /**
  14. * 通行证
  15. */
  16. class HuiPay extends Api
  17. {
  18. // 日志模块名称
  19. const LOG_MODULE = 'HuiPay';
  20. protected $noNeedLogin = ['vip_recharge', 'gold_recharge', 'pay', 'pay_notify'];
  21. protected $noNeedRight = '*';
  22. // h5 跳转小程序
  23. const H5 = 'https://h5-min-pay-1gczed24bbbe3db8-1317709175.tcloudbaseapp.com/suning-pay.html';
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. }
  28. public function __construct(Request $request = null)
  29. {
  30. parent::__construct($request);
  31. //日志统一写入
  32. register_shutdown_function([new LogUtil, 'close']);
  33. LogUtil::getInstance('Api/'); //设置日志存入通道
  34. }
  35. /**
  36. * 会员充值 (跳转小程序充值)
  37. * @return void
  38. */
  39. public function vip_recharge()
  40. {
  41. Db::startTrans();
  42. try {
  43. $rc_id = input('rc_id',0);
  44. $pay_type = input('pay_type','wechat');
  45. $uid = $this->auth->id;
  46. if ($pay_type != 'wechat') {
  47. throw new Exception('支付类型有误');
  48. }
  49. if(!$rc_id){
  50. throw new Exception('请选择会员套餐');
  51. }
  52. //赋值money
  53. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  54. $money = $recharge_config['money'];
  55. if($money <= 0) {
  56. throw new Exception('支付金额必须大于0');
  57. }
  58. if($money > 10000){
  59. throw new Exception('支付金额太大');
  60. }
  61. //会员等级冲突
  62. //当前是会员,但是却要向下级续费,直接提示报错
  63. //修改等级,向上立刻改,向下不允许
  64. $wallet_info = model('wallet')->getWallet($this->auth->id);
  65. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  66. throw new Exception('当前会员没有过期,不能续费');
  67. }
  68. //创建订单
  69. $data = [];
  70. $data['status'] = 0;
  71. $pay_no = createUniqueNo('V',$uid);
  72. $data['pay_no'] = $pay_no;
  73. $data['money'] = $money;
  74. $data['payment_class'] = $pay_type;
  75. $data['user_id'] = $uid;
  76. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  77. $data['memo'] = '充值会员支付';
  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. // h5跳转小程序支付链接
  94. $url = self::H5."?order_no={$pay_no}";
  95. Db::commit();
  96. $this->success('success',[
  97. 'url' => $url
  98. ]);
  99. } catch (Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. /**
  105. * 金币充值
  106. * @return void
  107. */
  108. public function gold_recharge()
  109. {
  110. Db::startTrans();
  111. try {
  112. $rc_id = input_post('rc_id',0);
  113. $pay_type = 'wechat';
  114. $freemoney = input_post('freemoney', 0, 'intval');
  115. $uid = $this->auth->id;
  116. if(!$rc_id && !$freemoney){
  117. throw new Exception('请选择或填写充值金额');
  118. }
  119. if (!in_array($pay_type,['wechat','alipay'])) {
  120. throw new Exception('错误的支付类型');
  121. }
  122. //赋值money
  123. if($rc_id){
  124. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  125. $money = $recharge_config ? $recharge_config['money']: 0;
  126. $gold = $recharge_config ? $recharge_config['gold'] : 0;
  127. $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
  128. $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
  129. $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
  130. }
  131. //自由输入覆盖
  132. if(!empty($freemoney)){
  133. $rc_id = 0;
  134. $money = floatval($freemoney);
  135. $bili = config('site.money_to_gold') ?: 10;
  136. $gold = bcmul($money,$bili,0);
  137. $first_gold = 0;
  138. $first_vipdays = 0;
  139. $vip_gold = 0;
  140. }
  141. if($money <= 0) {
  142. throw new Exception('支付金额必须大于0');
  143. }
  144. if($money > 10000){
  145. throw new Exception('支付金额太大');
  146. }
  147. //查询是不是会员,若不是则不赠送金币
  148. $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  149. if ($vip_endtime < time()) {
  150. $vip_gold = 0;
  151. }
  152. //创建订单
  153. $data = [];
  154. $data['status'] = 0;
  155. $pay_no = createUniqueNo('P',$uid);
  156. $data['pay_no'] = $pay_no;
  157. $data['money'] = $money;
  158. $data['payment_class'] = $pay_type;
  159. $data['user_id'] = $uid;
  160. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  161. $data['memo'] = '充值金币支付';
  162. $data['createtime'] = time();
  163. $data['payment'] = 'app';
  164. $pay_order = Db::name('pay_order')->insertGetId($data);
  165. //创建回调
  166. $even_data = [];
  167. $even_data['event'] = 'success';
  168. $even_data['class'] = 'app\common\model\Recharge';
  169. $even_data['method'] = 'goldpaysucc';
  170. $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]);
  171. $even_data['pay_no'] = $pay_no;
  172. $pay_event = Db::name('pay_event')->insertGetId($even_data);
  173. if (!$pay_order || !$pay_event){
  174. throw new Exception('下单失败');
  175. }
  176. // h5跳转小程序支付链接
  177. $url = self::H5."?order_no={$pay_no}";
  178. Db::commit();
  179. $this->success('success',[
  180. 'url' => $url
  181. ]);
  182. } catch (Exception $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. }
  187. /**
  188. * 汇付 支付
  189. */
  190. public function pay()
  191. {
  192. $params = \request()->post();
  193. $order_no = $params['order_no'] ?? '';
  194. if ($params['openid'] != 9696){
  195. $wxInfo = Cache::get($params['openid'] ?? '');
  196. $openid = $wxInfo['openid'] ?? '';
  197. }else{
  198. $openid = 'ol8qS68vKSgWJ3Unrgfyi3rkakcQ';
  199. }
  200. if (empty($order_no) || empty($openid)){
  201. $this->error('支付超时,请重新下单支付');
  202. }
  203. // 获取下单信息
  204. $order = Db::name('pay_order')->where('pay_no',$order_no)->find();
  205. if (!$order){
  206. $this->error('订单不存在,请重新下单支付');
  207. }
  208. $money = $order['money'];
  209. $money = '0.01';// 测试支付
  210. $pay = new PayUtil();
  211. $notify_url = CurlUtil::getHttp('/api/hui_pay/pay_notify',false);
  212. if (!$pay->jsPay($openid, $order_no, $money, $order['memo'] ?? '商品下单支付', $notify_url)){
  213. $this->error($pay->getMessage());
  214. }
  215. $res = $pay->getData();
  216. if (empty($res['data']['pay_info']) || !$pay_info = json_decode($res['data']['pay_info'],true)){
  217. $this->error('支付信息有误');
  218. // exit;
  219. // 这里 不加 exit 编辑器可能会提示 $pay_info 有可能不存在的变量 是因为 fastadmin 自带 error 写法不规范导致的
  220. }
  221. $this->success('success', [
  222. 'pay_info' => $pay_info,// 这里编辑器可能会提示 有可能不存在的变量 是因为 fastadmin 自带 error 写法不规范导致的
  223. 'order_no' => $order_no
  224. ]);
  225. }
  226. /**
  227. * 支付回调
  228. * @return void
  229. */
  230. public function pay_notify(Request $request)
  231. {
  232. $params = $request->param();
  233. // 消息主体信息
  234. $resp_data = json_decode(stripslashes(htmlspecialchars_decode($params['resp_data'] ?? '')),true);
  235. unset($params['resp_data']);
  236. LogUtil::info('支付回调参数', self::LOG_MODULE, __FUNCTION__,[
  237. 'params' => $params,
  238. 'resp_data' => $resp_data,
  239. ]);
  240. if (empty($params['resp_code']) || $params['resp_code'] != '00000000' || empty($resp_data)){
  241. LogUtil::info('回调信息有误', self::LOG_MODULE, __FUNCTION__,"resp_code error");
  242. $this->error('支付信息有误');
  243. }
  244. $this->success();
  245. }
  246. }