| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- /**
- * 支付公用类
- */
- namespace app\api\services;
- use app\common\model\ShopGoodsModel;
- use app\common\model\ShopOrderGoodsModel as OrderGoods;
- use app\common\model\ShopOrderModel;
- use app\common\model\ShopOrderModel as Order;
- use app\common\model\UserModel;
- use app\common\model\UserMoneyModel;
- use app\common\model\UserShouxufeilogModel;
- use app\common\model\VipOrderModel;
- use jobs\VipOrderJob;
- use services\CacheServices;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Env;
- abstract class BasePayServices
- {
- /**
- * 支付状态
- * @var bool
- */
- protected $pay_status = false;
- /**
- * 支付回调信息
- * @var string
- */
- protected $notify_info = '';
- /**
- * 支付通道 1 支付宝 2 微信
- * @var int
- */
- protected $pay_way = 1;
- /**
- * 费用配置
- * @param int $uid
- * @param array $data
- * @param int $type
- * @return array
- */
- public function _payConf(int $uid, array $data, int $type = 1)
- {
- $body = $total_amount = $remarks = '';
- switch ($data['order_type']) {
- case 1: // 充值
- $body = '充值';
- $total_amount = $data['money'];
- break;
- case 2: // 实名认证
- $total_amount = Db::name('user_data')->where('uid', $uid)->value('rz_money');
- // if (!$total_amount || $total_amount < 1.5)
- // throw new Exception('认证费用不正确');
- $body = '实名认证费用';
- break;
- case 3: // 开通会员
- $vip_money = Env::get('app.VIP_MONEY', 38);
- if (!$vip_money > 0) {
- throw new Exception('会员配置不存在');
- }
- $body = '开通会员';
- $total_amount = $vip_money;
- $remarks = $data['order_id'];
- break;
- case 4: //购买商品
- $order_sn = $data['order_id'];
- // $checkingOrder = Order::checkingOrder($order_sn, $data['money'], $uid);
- // new
- $checkingOrder = Order::checkingOrder($order_sn, 0, $uid);
- if (!$checkingOrder) {
- throw new Exception('校验订单失败');
- }
- if ($checkingOrder['flag'] !== 200) {
- throw new Exception($checkingOrder['msg']);
- }
- $body = '购买商品';
- $total_amount = $checkingOrder['orderPayment'];
- $remarks = $order_sn;
- //更改订单支付状态为线下支付待审核状态
- if ($this->data['channel'] == 4 && $this->data['order_type'] == 4) {
- Order::where('order_sn', 'in', $order_sn)->save(['pay_type' => 4, 'status' => 4]);
- }
- break;
- }
- if (!$body || !$total_amount || $total_amount <= 0)
- throw new Exception('配置错误');
- // 这是测试 线要干掉
- // $total_amount = floatval('0.'.mt_rand(1, 3).mt_rand(1, 9));;
- return [$body, $type == 1 ? $total_amount : bcmul($total_amount, 100, 2), $data['order_type'], $remarks, $data['trade_type'], $data['channel'], isset($data['voucher_img']) ? $data['voucher_img'] : ''];
- }
- /**
- * 保存订单信息
- * @param array $param
- * @param int $pay_way
- */
- public function setPaymentOrder(array $param, int $pay_way = 1)
- {
- $insert = [
- 'total_fee' => $param['total_amount'],
- 'trade_type' => $param['trade_type'],
- 'body' => $param['body'],
- 'state' => 7,
- 'out_trade_no' => $param['out_trade_no'],
- 'pay_way' => $param['pay_way'],
- 'remarks' => $param['remarks'],
- 'order_type' => $param['order_type'],
- 'uid' => $param['uid'],
- 'voucher_img' => $param['voucher_img'],
- 'out_trade_no1'=>(isset($param['out_trade_no1'])?$param['out_trade_no1']:(isset($param[''])))
- ];
- Db::name('payment')->insert($insert);
- }
- /**
- * 更改订单状态
- * @return mixed
- */
- public function afterPay()
- {
- if ($this->pay_status && !empty($this->notify_info)) {
- $pay_info = Db::name('payment')->where('out_trade_no', $this->notify_info['out_trade_no'])->where('state', 7)->where('pay_way', 2)->findOrEmpty();
- if (!empty($pay_info)) {
- Db::startTrans();
- try {
- Db::name('payment')->where('id', $pay_info['id'])->update(['state' => 6, 'pay_at' => date('Y-m-d H:i:s')]); // 更改订单状态
- switch ($pay_info['order_type']) {
- case 1: // 充值
- // $pay_num = Db::name('payment')->where(['uid' => $pay_info['uid'], 'state' => 6, 'order_type' => 1])->count('id');
- // set_user_money($pay_info['uid'], $pay_info['total_fee'], 1);
- // if ($pay_num == 1 && $pay_info['total_fee'] >= 100) { // 首次充值并且金额大于100,送月会员
- // $user = Db::name('user')->where('id', $pay_info['uid'])->field('vip,vip_expire_time')->find();
- // $date = new \DateTime($user['vip_expire_time']);
- // $date->add(new \DateInterval(get_vip_datetime_string('monthly_card')['time']));
- // $vip_expire_time = $date->format('Y-m-d H:i:s'); // vip到期时间
- // $user_update['vip_expire_time'] = $vip_expire_time;
- // $user_update['vip_type'] = 'monthly_card';
- // $user['vip'] < 1 && $user_update['vip'] = get_vip_datetime_string('monthly_card')['vip_type'];
- // Db::name('user')->where('id', $pay_info['uid'])->update($user_update);
- // Db::name('vip_log')->insert(['uid' => $pay_info['uid'], 'type' => 2, 'pay_price' => $pay_info['total_fee'], 'vip_expire_time' => $vip_expire_time, 'vip_type' => 'monthly_card']);
- // }
- break;
- case 3:
- // 充值会员
- // CacheServices::set('is_lock_vipdata', 2);
- // 更改订单状态
- // $order = VipOrderModel::where('order_id', $pay_info['remarks'])->find();
- //
- // // 更新viporder为支付状态
- // VipOrderModel::where('order_id', $pay_info['remarks'])->save(['status' => 2]);
- //
- // $buy_info = Db::name('user')->where('id', $order['user_id'])->find();
- // Db::name('user')->where('id', $order['user_id'])->inc('vip_count', 1)->update();
- // Db::name('user')->where('id', $order['user_id'])->save(['is_vip' => 1]);
- // Db::name('user')->where('id', $order['user_id'])->inc('target_inc', 3069)->update();
- // $curpid = $buy_info['pid'];
- // if ($curpid > 0) {
- // while (1) {
- // if ($curpid == 0 || empty($curpid)) {
- // break;
- // }
- // $cur_user = Db::name('user')->where('id', $curpid)->find();
- // Db::name('user')->where('id', $curpid)->inc('group_vip_count', 1)->update();
- // $curpid = $cur_user['pid'];
- // }
- // }
- //
- //// $val = CacheServices::get('is_lock_vipdata', 0);
- //// if ($val == 1){
- //// return api_error_return('请稍后再试1');
- //// }
- //
- //// CacheServices::set('is_lock_vipdata', 1);
- //// Db::startTrans();
- //// try {
- // $level = Db::name('vip_relation_data')->lock(true)->max('level');
- // if (empty($level)) {
- // $level = 1;
- // } else {
- // $maxid = Db::name('vip_relation_data')->lock(true)->max('id');
- // if ($maxid >= (pow(2, $level) - 1)) {
- // ++$level;
- // };
- // }
- // if ($level > 1) {
- // $curarr = CacheServices::get('vip_level_data' . $level);
- // if (!$curarr) {
- // $info = Db::name('vip_level_data')->where(['level' => $level])->find();
- // $curarr = explode(',', $info['value']);
- // CacheServices::set('vip_level_data' . $level, $curarr);
- // }
- // // 上一层最大的id
- // $max_level_info_id = Db::name('vip_relation_data')->where(['level' => $level - 1])->field('id')->lock(true)->max('id');
- // $pid = $curarr[$maxid - $max_level_info_id];
- // } else {
- // $pid = 0;
- // }
- //
- // $insertData = [
- // 'create_time' => getCurTimeStamp(),
- // 'pid' => $pid,
- // 'level' => $level,
- // 'uid' => $buy_info['id']
- // ];
- //
- // Db::name('vip_relation_data')->lock(true)->insertGetId($insertData);
- //// \utils\Queue::instance()->log('会员VIP订单执行成功' . $buy_info['id'])->do('VipOrderBack')->job(VipOrderJob::class)->push([$level, $buy_info['id'], $pid]);
- //
- // break;
- case 4: //购买商品
- // $order = Order::where(['order_sn' => explode(',', $pay_info['remarks'])])->select()->toArray();
- // Order::payDoneOrderCallback($order, 3, $pay_info['uid']);
- $m_goods = new ShopGoodsModel();
- $m_user = new UserModel();
- $m_shoporder = new ShopOrderModel();
- // 更改订单状态
- $order = ShopOrderModel::where('order_sn', $pay_info['remarks'])->find();
- $nowTime = date('Y-m-d H:i:s', time());
- Db::name('shop_order')->where(['order_sn' => $pay_info['remarks']])
- ->update(['pay_type' => 2, 'status' => 1, 'updated_time' => $nowTime]);
- // 增加销量
- $orderGoods = OrderGoods::where(['order_id' => $order['order_id']])->select()->toArray();
- foreach ($orderGoods as $k => $v) {
- ShopGoodsModel::where(['goods_id' => $v['goods_id']])->inc('sales_volume', $v['num'])->inc('real_sales_volume', $v['num'])->update();
- }
- // 送积分
- edit_user_score(5, $order['user_id'], $order['rebate_score']);
- edit_user_redscore(1, $order['user_id'], $order['rebate_lock_score']);
- break;
- }
- Db::commit();
- echo 'success';
- die;
- } catch (\Exception $e) {
- Db::rollback();
- echo $e->getMessage();
- die;
- }
- }
- echo 'success';
- die;
- }
- echo 'failure';
- die;
- }
- }
|