PaymentOrderService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\PaymentOrder;
  5. use App\Models\Config;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Services\Payment\SanJinService;
  11. /**
  12. * 投注
  13. */
  14. class PaymentOrderService extends BaseService
  15. {
  16. const TYPE_PAY = 1; // 代收
  17. const TYPE_PAYOUT = 1; // 代付
  18. const STATUS_STAY = 0; // 待处理
  19. const STATUS_PROCESS = 1; // 处理中
  20. const STATUS_SUCCESS = 2; // 成功
  21. const STATUS_FAIL = 3; // 失败
  22. /**
  23. * @description: 模型
  24. * @return {string}
  25. */
  26. public static function model() :string
  27. {
  28. return PaymentOrder::class;
  29. }
  30. /**
  31. * @description: 枚举
  32. * @return {*}
  33. */
  34. public static function enum() :string
  35. {
  36. return '';
  37. }
  38. /**
  39. * @description: 获取查询条件
  40. * @param {array} $search 查询内容
  41. * @return {array}
  42. */
  43. public static function getWhere(array $search = []) :array
  44. {
  45. $where = [];
  46. if(isset($search['member_id']) && !empty($search['member_id'])){
  47. $where[] = ['member_id', '=', $search['member_id']];
  48. }
  49. if(isset($search['type']) && !empty($search['type'])){
  50. $where[] = ['type', '=', $search['type']];
  51. }
  52. if(isset($search['channel']) && !empty($search['channel'])){
  53. $where[] = ['channel', '=', $search['channel']];
  54. }
  55. if(isset($search['id']) && !empty($search['id'])){
  56. $where[] = ['id', '=', $search['id']];
  57. }
  58. if(isset($search['status']) && $search['status'] != ''){
  59. $where[] = ['status', '=', $search['status']];
  60. }
  61. return $where;
  62. }
  63. /**
  64. * @description: 查询单条数据
  65. * @param array $search
  66. * @return \App\Models\Coin|null
  67. */
  68. public static function findOne(array $search): ?PaymentOrder
  69. {
  70. return self::model()::where(self::getWhere($search))->first();
  71. }
  72. /**
  73. * @description: 查询所有数据
  74. * @param array $search
  75. * @return \Illuminate\Database\Eloquent\Collection
  76. */
  77. public static function findAll(array $search = [])
  78. {
  79. return self::model()::where(self::getWhere($search))->get();
  80. }
  81. /**
  82. * @description: 分页查询
  83. * @param array $search
  84. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  85. */
  86. public static function paginate(array $search = [])
  87. {
  88. $limit = isset($search['limit'])?$search['limit']:15;
  89. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  90. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  91. }
  92. /**
  93. * @description:
  94. * @param {*} $params
  95. * @return {*}
  96. */
  97. public static function submit($params = [])
  98. {
  99. $result = false;
  100. $msg['code'] = self::NOT;
  101. $msg['msg'] = '';
  102. // 2. 判断是否是更新
  103. if (!empty($params['id'])) {
  104. // 更新
  105. $info = self::findOne(['id'=>$params['id']] );
  106. if (!$info) {
  107. $msg['msg'] = '数据不存在!';
  108. }else{
  109. $result = $info->update($params);
  110. $id = $params['id'];
  111. }
  112. } else {
  113. // 创建
  114. $result = $info = self::model()::create($params);
  115. $id = $result->id;
  116. }
  117. if($result){
  118. $msg['code'] = self::YES;
  119. $msg['msg'] = '设置成功';
  120. $msg['key'] = $id;
  121. }else{
  122. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  123. }
  124. return $msg;
  125. }
  126. public static function createPayout($memberId ,$amount ,$channel ,$bank_name ,$account ,$card_no)
  127. {
  128. $data = [];
  129. $data['type'] = self::TYPE_PAYOUT;
  130. $order_no = self::createOrderNo('sj'.$data['type'].'_',$memberId);
  131. $data['order_no'] = $order_no;
  132. $data['member_id'] = $memberId;
  133. $amount = number_format($amount, 2, '.', '');
  134. $data['amount'] = $amount;
  135. $data['channel'] = $channel;
  136. $data['bank_name'] = $bank_name;
  137. $data['account'] = $account;
  138. $data['card_no'] = $card_no;
  139. $data['callback_url'] = SanJinService::getNotifyUrl();
  140. $data['status'] = self::STATUS_STAY;
  141. $ret = SanJinService::payout($amount,$order_no,$bank_name,$account,$card_no);
  142. if($ret['code'] = 200){
  143. $data['status'] = self::STATUS_PROCESS;
  144. }else{
  145. $data['status'] = self::STATUS_FAIL;
  146. $data['remark'] = $ret['msg'];
  147. }
  148. self::model()::create($data);
  149. return $ret;
  150. }
  151. }