auth->id; if (!in_array($pay_type, ['wechat', 'alipay'])) { throw new Exception('错误的支付类型'); } if (!$rc_id) { throw new Exception('请选择会员套餐'); } //赋值money $recharge_config = Db::name('payvip_config')->where('id', $rc_id)->find(); $money = $recharge_config['money']; if ($money <= 0) { throw new Exception('支付金额必须大于0'); } if ($money > 10000) { throw new Exception('支付金额太大'); } //会员等级冲突 //当前是会员,但是却要向下级续费,直接提示报错 //修改等级,向上立刻改,向下不允许 $wallet_info = model('wallet')->getWallet($this->auth->id); if ($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']) { throw new Exception('当前会员没有过期,不能续费'); } //创建订单 $data = []; $data['status'] = 0; $pay_no = createUniqueNo('V', $uid); $data['pay_no'] = $pay_no; $data['money'] = $money; $data['payment_class'] = $pay_type; $data['user_id'] = $uid; $data['ext_info'] = json_encode(['subject' => '充值vip支付']); $data['memo'] = '充值会员支付'; $data['queue_status'] = 1;//队列状态:0不参与;1参与;2:处理成功 $data['createtime'] = time(); //$data['payment'] = 'miniapp'; $data['payment'] = 'app'; $pay_order = Db::name('pay_order')->insertGetId($data); //创建回调 $even_data = []; $even_data['event'] = 'success'; $even_data['class'] = 'app\common\model\Recharge'; $even_data['method'] = 'vippaysucc'; $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]); $even_data['pay_no'] = $pay_no; $pay_event = Db::name('pay_event')->insertGetId($even_data); if (!$pay_order || !$pay_event) { throw new Exception('下单失败'); } Db::commit(); $pay = new JingXiuPayUtil(); if (!$pay->wxWapPay($pay_no,$money,'充值会员','/api/hui_pay/pay_notify',$pay_type)){ $this->error($pay->getMessage()); } $data = $pay->getData(); $this->success('success', $data); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } /** * 金币充值 * @return void */ public function gold_recharge() { Db::startTrans(); try { $rc_id = input_post('rc_id', 0); $pay_type = input_post('pay_type','wechat'); $freemoney = input_post('freemoney', 0, 'intval'); $uid = $this->auth->id; if (!$rc_id && !$freemoney) { throw new Exception('请选择或填写充值金额'); } if (!in_array($pay_type, ['wechat', 'alipay'])) { throw new Exception('错误的支付类型'); } //赋值money if ($rc_id) { $recharge_config = Db::name('paygold_config')->where('id', $rc_id)->find(); $money = $recharge_config ? $recharge_config['money'] : 0; $gold = $recharge_config ? $recharge_config['gold'] : 0; $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0; $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0; $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0; } //自由输入覆盖 if (!empty($freemoney)) { $rc_id = 0; $money = floatval($freemoney); $bili = config('site.money_to_gold') ?: 10; $gold = bcmul($money, $bili, 0); $first_gold = 0; $first_vipdays = 0; $vip_gold = 0; } if ($money <= 0) { throw new Exception('支付金额必须大于0'); } if ($money > 10000) { throw new Exception('支付金额太大'); } //查询是不是会员,若不是则不赠送金币 $vip_endtime = Db::name('user_wallet')->where('user_id', $this->auth->id)->value('vip_endtime'); if ($vip_endtime < time()) { $vip_gold = 0; } //创建订单 $data = []; $data['status'] = 0; $pay_no = createUniqueNo('P', $uid); $data['pay_no'] = $pay_no; $data['money'] = $money; $data['payment_class'] = $pay_type; $data['user_id'] = $uid; $data['ext_info'] = json_encode(['subject' => '充值金币支付']); $data['memo'] = '充值金币支付'; $data['queue_status'] = 1;//队列状态:0不参与;1参与;2:处理成功 $data['createtime'] = time(); $data['payment'] = 'app'; $pay_order = Db::name('pay_order')->insertGetId($data); //创建回调 $even_data = []; $even_data['event'] = 'success'; $even_data['class'] = 'app\common\model\Recharge'; $even_data['method'] = 'goldpaysucc'; $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]); $even_data['pay_no'] = $pay_no; $pay_event = Db::name('pay_event')->insertGetId($even_data); if (!$pay_order || !$pay_event) { throw new Exception('下单失败'); } Db::commit(); $pay = new JingXiuPayUtil(); if (!$pay->wxWapPay($pay_no,$money,'充值金币','/api/hui_pay/pay_notify',$pay_type)){ $this->error($pay->getMessage()); } $data = $pay->getData(); $this->success('success', $data); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } /** * 支付回调 * * @param Request $request * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function pay_notify(Request $request) { $params = $request->param(); LogUtil::info('支付回调参数', self::LOG_MODULE, __FUNCTION__, $params); // 校验回调信息 if (empty($params['order_status']) || $params['order_status'] != 'SUCCESS' || empty($params['out_trade_no'])) { LogUtil::info('回调信息有误', self::LOG_MODULE, __FUNCTION__,$params['order_status'] ?? 'error'); return self::response201('支付信息有误'); } // 校验签名 $huiPay = new JingXiuPayUtil(); if (!$huiPay->checkSign($params)){ LogUtil::info('签名校验失败', self::LOG_MODULE, __FUNCTION__); return self::response201('签名校验失败'); } // 开始处理业务逻辑 $pay_no = $params['out_trade_no']; LogUtil::info('订单号', self::LOG_MODULE, __FUNCTION__, $pay_no); //查询订单信息 $order_info = Db::name('pay_order')->where('pay_no', $pay_no)->find(); if (!$order_info) { LogUtil::info('订单信息不存在', self::LOG_MODULE, __FUNCTION__); return self::response201('订单信息不存在'); } if ($order_info['status'] == 3){ LogUtil::info('订单信息_已删除', self::LOG_MODULE, __FUNCTION__, [ 'status' => $order_info['status'] ]); return self::response201('充值入账失败'); } if ($order_info['status'] == 1 || $order_info['status'] == 2) { LogUtil::info('充值入账更新余额失败_status已更新过', self::LOG_MODULE, __FUNCTION__, [ 'status' => $order_info['status'] ]); return self::response201('充值入账失败'); } $extendType = ''; //区分vip 还是 冲金币 $is_g = stripos($pay_no, 'P'); if ($is_g !== false) { $extendType = 'gold'; } else { $is_v = stripos($pay_no, 'V'); if ($is_v !== false) { $extendType = 'vip'; } } Db::startTrans(); try { //status已更新过 $result = Db::name('pay_order')->where('pay_no', $pay_no)->where('status',$order_info['status'])->setField(['status' => 2]); if (!$result) { LogUtil::info('充值入账更新余额失败_status更新状态2不成功', self::LOG_MODULE, __FUNCTION__, [ 'status' => $order_info['status'] ]); Db::rollback(); return self::response201('充值入账失败'); } //你可以在此编写订单逻辑 $payEvent = Db::name('pay_event')->where('pay_no', $pay_no)->find(); $args = json_decode($payEvent['args'] ?? '', true); $rechargeM = new \app\common\model\Recharge(); if ($extendType == 'gold') { $payRes = $rechargeM->goldpaysucc($pay_no, $args,1); $payTypeStr = '充值'; } elseif ($extendType == 'vip') { $payRes = $rechargeM->vippaysucc($pay_no, $args); $payTypeStr = 'vip'; } else { $payRes = false; $payTypeStr = '未知支付类型'; } if (!$payRes) { Db::rollback(); LogUtil::info('更新失败', self::LOG_MODULE, __FUNCTION__, $payTypeStr); return self::response201('更新失败'); } Db::commit(); LogUtil::info('订单处理成功', self::LOG_MODULE, __FUNCTION__, $payTypeStr); return self::response200(); }catch (\Exception $e){ Db::rollback(); LogUtil::info('更新失败', self::LOG_MODULE, __FUNCTION__, $e); return self::response201('更新失败'); } } /** * 成功返回 * @param string $message * @return string */ private static function response200(string $message = 'SUCCESS') { return $message; } /** * 失败返回201 * @param string $message * @return string */ private static function response201(string $message = 'error') { return $message; } }