PaymentService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\PaymentModel;
  4. use utils\RedisCache;
  5. /**
  6. * 支付服务 by wes
  7. * Class PaymentService
  8. * @package app\common\service
  9. */
  10. class PaymentService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new PaymentModel();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if(!self::$instance){
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 按状态获取支付单数量
  31. * @param $uid
  32. * @param $orderType
  33. * @param $state
  34. * @param int $time 时间/小时,默认2小时
  35. * @return array|mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getPaymentCountByState($uid, $orderSn='', $orderType, $state, $time = 2)
  41. {
  42. $cacheKey = "caches:paymentCall:u{$uid}_ot{$orderType}_s{$state}_{$time}_{$orderSn}";
  43. $data = RedisCache::get($cacheKey);
  44. if($data){
  45. return $data;
  46. }
  47. $where = [];
  48. if($orderType){
  49. $where['order_type'] = $orderType;
  50. }
  51. if($state){
  52. $where['state'] = $state;
  53. }
  54. $data = $this->model->where($where)->where(function ($query) use($time,$orderSn){
  55. if($time>0){
  56. $query->where('creat_at','>=', time() - $time * 3600);
  57. }
  58. if($orderSn){
  59. $query->where('remarks',$orderSn);
  60. }
  61. })->count('id');
  62. if($data){
  63. RedisCache::set($cacheKey, $data, rand(3,5));
  64. }
  65. return $data;
  66. }
  67. /**
  68. * 验证订单是否已支付
  69. * @param $uid
  70. * @param $orderSn
  71. * @return array|mixed
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function checkPaymentState($uid, $orderSn)
  77. {
  78. $cacheKey = "caches:paymentState:u{$uid}_sn{$orderSn}";
  79. $data = RedisCache::get($cacheKey);
  80. if($data){
  81. return $data;
  82. }
  83. $data = $this->model->where(['state'=> 6])->where(function ($query) use($orderSn){
  84. if($orderSn){
  85. $query->where('remarks', $orderSn);
  86. }
  87. })->value('id');
  88. if($data){
  89. RedisCache::set($cacheKey, $data, rand(2,3));
  90. }
  91. return $data;
  92. }
  93. public function getCacheInfo($outTradeNo, $state=7, $field='')
  94. {
  95. $cacheKey = "caches:payment:info:otn{$outTradeNo}_{$state}".($field? '_'.md5($field):'');
  96. $data = RedisCache::get($cacheKey);
  97. if($data){
  98. return $data;
  99. }
  100. $where = ['out_trade_no'=> $outTradeNo];
  101. if($state){
  102. $where['state'] = $state;
  103. }
  104. $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';
  105. $data = $this->model->where($where)
  106. ->field($field)
  107. ->value('id');
  108. if($data){
  109. RedisCache::set($cacheKey, $data, rand(3,5));
  110. }
  111. return $data;
  112. }
  113. }