123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace app\api\controller;
- use addons\epay\library\Service;
- use app\common\controller\Api;
- use app\utils\CurlUtil;
- use app\utils\Easywechat\MiniAppService;
- use app\utils\JingXiu\JingXiuPayUtil;
- use app\utils\LogUtil;
- use app\utils\PayUtil;
- use think\Cache;
- use think\Db;
- use think\Exception;
- use think\Request;
- /**
- * 通行证
- */
- class HuiPay extends Api
- {
- // 日志模块名称
- const LOG_MODULE = 'HuiPay';
- protected $noNeedLogin = ['vip_recharge', 'gold_recharge', 'pay', 'pay_notify'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- }
- public function __construct(Request $request = null)
- {
- parent::__construct($request);
- //日志统一写入
- register_shutdown_function([new LogUtil, 'close']);
- LogUtil::getInstance('Api/'); //设置日志存入通道
- }
- /**
- * 会员充值 (跳转小程序充值)
- * @return void
- */
- public function vip_recharge()
- {
- Db::startTrans();
- try {
- $rc_id = input('rc_id', 0);
- $pay_type = input('pay_type', 'wechat');
- $uid = $this->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;
- }
- }
|