| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace app\common\service;
- use app\common\model\PaymentModel;
- use utils\RedisCache;
- /**
- * 支付服务 by wes
- * Class PaymentService
- * @package app\common\service
- */
- class PaymentService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new PaymentModel();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 按状态获取支付单数量
- * @param $uid
- * @param $orderType
- * @param $state
- * @param int $time 时间/小时,默认2小时
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getPaymentCountByState($uid, $orderSn='', $orderType, $state, $time = 2)
- {
- $cacheKey = "caches:paymentCall:u{$uid}_ot{$orderType}_s{$state}_{$time}_{$orderSn}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $where = [];
- if($orderType){
- $where['order_type'] = $orderType;
- }
- if($state){
- $where['state'] = $state;
- }
- $data = $this->model->where($where)->where(function ($query) use($time,$orderSn){
- if($time>0){
- $query->where('creat_at','>=', time() - $time * 3600);
- }
- if($orderSn){
- $query->where('remarks',$orderSn);
- }
- })->count('id');
- if($data){
- RedisCache::set($cacheKey, $data, rand(3,5));
- }
- return $data;
- }
- /**
- * 验证订单是否已支付
- * @param $uid
- * @param $orderSn
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function checkPaymentState($uid, $orderSn)
- {
- $cacheKey = "caches:paymentState:u{$uid}_sn{$orderSn}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['state'=> 6])->where(function ($query) use($orderSn){
- if($orderSn){
- $query->where('remarks', $orderSn);
- }
- })->value('id');
- if($data){
- RedisCache::set($cacheKey, $data, rand(2,3));
- }
- return $data;
- }
- public function getCacheInfo($outTradeNo, $state=7, $field='')
- {
- $cacheKey = "caches:payment:info:otn{$outTradeNo}_{$state}".($field? '_'.md5($field):'');
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $where = ['out_trade_no'=> $outTradeNo];
- if($state){
- $where['state'] = $state;
- }
- $field = $field? $field : 'id,out_trade_no,uid,total_fee,state,trade_type,out_trade_no1,hy_token_id,syl_sureorderid,hy_bill_no,is_retreat,pay_way,order_type,sid,remarks,trade_no';
- $data = $this->model->where($where)
- ->field($field)
- ->value('id');
- if($data){
- RedisCache::set($cacheKey, $data, rand(3,5));
- }
- return $data;
- }
- }
|