PayService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\pay;
  12. use app\dict\common\ChannelDict;
  13. use app\dict\pay\PayDict;
  14. use app\model\pay\Pay;
  15. use app\service\core\paytype\CoreOfflineService;
  16. use core\base\BaseAdminService;
  17. /**
  18. * 支付服务层
  19. */
  20. class PayService extends BaseAdminService
  21. {
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->model = new Pay();
  26. }
  27. /**
  28. * 支付记录表
  29. * @param array $where
  30. * @return array
  31. */
  32. public function getPage(array $where)
  33. {
  34. $field = 'id, site_id, main_id, out_trade_no, trade_type, trade_id, trade_no, body, money, voucher, status, json, create_time, pay_time, cancel_time, type, mch_id, main_type, channel, fail_reason';
  35. $search_model = $this->model->where([ [ 'status', 'in', '2,3' ] ])->withSearch([ 'create_time', 'out_trade_no', 'type', 'channel' ], $where)
  36. ->with([
  37. 'member' => function($query) {
  38. $query->field('member_id, nickname, headimg, mobile');
  39. }
  40. ])
  41. ->field($field)->append([ 'type_name', 'channel_name', 'status_name' ])->order('create_time desc');
  42. return $this->pageQuery($search_model);
  43. }
  44. /**
  45. * 待审核支付记录
  46. * @param array $where
  47. * @return array
  48. */
  49. public function getAuditPage(array $where)
  50. {
  51. $field = 'id, out_trade_no, type, money, body, voucher, create_time, trade_id, trade_type, status';
  52. $search_model = $this->model->where([ [ 'type', '=', PayDict::OFFLINEPAY ] ])->withSearch([ 'create_time', 'out_trade_no', 'status' ], $where)->field($field)->append([ 'type_name' ])->order('create_time desc');
  53. return $this->pageQuery($search_model);
  54. }
  55. /**
  56. * 获取交易详情
  57. * @param int $id
  58. * @return array
  59. */
  60. public function getDetail(int $id)
  61. {
  62. $field = 'id,out_trade_no,trade_type,trade_id,trade_no,body,money,voucher,status,create_time,pay_time,cancel_time,type,channel,fail_reason';
  63. return $this->model->where([ [ 'id', '=', $id ] ])
  64. ->field($field)
  65. ->append([ 'type_name', 'channel_name', 'status_name' ])
  66. ->findOrEmpty()
  67. ->toArray();
  68. }
  69. /**
  70. * 支付审核通过
  71. * @param string $out_trade_no
  72. * @return null
  73. */
  74. public function pass(string $out_trade_no)
  75. {
  76. return ( new CoreOfflineService() )->pass($out_trade_no);
  77. }
  78. /**
  79. * 支付审核未通过
  80. * @param string $out_trade_no
  81. * @param string $reason
  82. */
  83. public function refuse(string $out_trade_no, string $reason)
  84. {
  85. return ( new CoreOfflineService() )->refuse($out_trade_no, $reason);
  86. }
  87. /**
  88. * 按支付方式统计支付金额
  89. * @return array
  90. */
  91. public function getPayTypeStat()
  92. {
  93. $data = [];
  94. $list = array_filter(PayDict::getPayType(), function($value){
  95. return $value['key'] !== PayDict::FRIENDSPAY;
  96. });
  97. $pay_type_list = array_keys($list);
  98. foreach ($pay_type_list as $type) {
  99. $sum = $this->model->where([ [ 'type', '=', $type ], [ 'status', '=', PayDict::STATUS_FINISH ] ])->sum('money');
  100. $data[ $type ] = $sum;
  101. }
  102. return $data;
  103. }
  104. /**
  105. * 按支付渠道统计支付金额
  106. * @return array
  107. */
  108. public function getChannelStat()
  109. {
  110. $data = [];
  111. $channel_list = array_keys(ChannelDict::getType());
  112. foreach ($channel_list as $channel) {
  113. $sum = $this->model->where([ [ 'channel', '=', $channel ], [ 'status', '=', PayDict::STATUS_FINISH ] ])->sum('money');
  114. $data[ $channel ] = $sum;
  115. }
  116. return $data;
  117. }
  118. }