PaymentOrderService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. use App\Services\WalletService;
  12. use App\Services\BalanceLogService;
  13. /**
  14. * 投注
  15. */
  16. class PaymentOrderService extends BaseService
  17. {
  18. const TYPE_PAY = 1; // 代收
  19. const TYPE_PAYOUT = 2; // 代付
  20. const STATUS_STAY = 0; // 待处理
  21. const STATUS_PROCESS = 1; // 处理中
  22. const STATUS_SUCCESS = 2; // 成功
  23. const STATUS_FAIL = 3; // 失败
  24. /**
  25. * @description: 模型
  26. * @return {string}
  27. */
  28. public static function model() :string
  29. {
  30. return PaymentOrder::class;
  31. }
  32. /**
  33. * @description: 枚举
  34. * @return {*}
  35. */
  36. public static function enum() :string
  37. {
  38. return '';
  39. }
  40. /**
  41. * @description: 获取查询条件
  42. * @param {array} $search 查询内容
  43. * @return {array}
  44. */
  45. public static function getWhere(array $search = []) :array
  46. {
  47. $where = [];
  48. if(isset($search['member_id']) && !empty($search['member_id'])){
  49. $where[] = ['member_id', '=', $search['member_id']];
  50. }
  51. if(isset($search['type']) && !empty($search['type'])){
  52. $where[] = ['type', '=', $search['type']];
  53. }
  54. if(isset($search['channel']) && !empty($search['channel'])){
  55. $where[] = ['channel', '=', $search['channel']];
  56. }
  57. if(isset($search['id']) && !empty($search['id'])){
  58. $where[] = ['id', '=', $search['id']];
  59. }
  60. if(isset($search['status']) && $search['status'] != ''){
  61. $where[] = ['status', '=', $search['status']];
  62. }
  63. return $where;
  64. }
  65. /**
  66. * @description: 查询单条数据
  67. * @param array $search
  68. * @return \App\Models\Coin|null
  69. */
  70. public static function findOne(array $search): ?PaymentOrder
  71. {
  72. return self::model()::where(self::getWhere($search))->first();
  73. }
  74. /**
  75. * @description: 查询所有数据
  76. * @param array $search
  77. * @return \Illuminate\Database\Eloquent\Collection
  78. */
  79. public static function findAll(array $search = [])
  80. {
  81. return self::model()::where(self::getWhere($search))->get();
  82. }
  83. /**
  84. * @description: 分页查询
  85. * @param array $search
  86. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  87. */
  88. public static function paginate(array $search = [])
  89. {
  90. $limit = isset($search['limit'])?$search['limit']:15;
  91. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  92. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  93. }
  94. /**
  95. * @description:
  96. * @param {*} $params
  97. * @return {*}
  98. */
  99. public static function submit($params = [])
  100. {
  101. $result = false;
  102. $msg['code'] = self::NOT;
  103. $msg['msg'] = '';
  104. // 2. 判断是否是更新
  105. if (!empty($params['id'])) {
  106. // 更新
  107. $info = self::findOne(['id'=>$params['id']] );
  108. if (!$info) {
  109. $msg['msg'] = '数据不存在!';
  110. }else{
  111. $result = $info->update($params);
  112. $id = $params['id'];
  113. }
  114. } else {
  115. // 创建
  116. $result = $info = self::model()::create($params);
  117. $id = $result->id;
  118. }
  119. if($result){
  120. $msg['code'] = self::YES;
  121. $msg['msg'] = '设置成功';
  122. $msg['key'] = $id;
  123. }else{
  124. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  125. }
  126. return $msg;
  127. }
  128. public static function createPayout($memberId ,$amount ,$channel ,$bank_name ,$account ,$card_no)
  129. {
  130. $default_amount = $amount;
  131. $result = [];
  132. $result['chat_id'] = $memberId;
  133. $wallet = WalletService::findOne(['member_id' => $memberId]);
  134. $balance = $wallet->available_balance;
  135. if (bccomp($balance, $amount, 2) < 0) {
  136. $result['text'] = '您的钱包余额不足!';
  137. return $result;
  138. }
  139. $available_balance = bcsub($balance, $amount, 10);
  140. $data = [];
  141. $data['type'] = self::TYPE_PAYOUT;
  142. $order_no = self::createOrderNo('sj'.$data['type'].'_',$memberId);
  143. $data['order_no'] = $order_no;
  144. $data['member_id'] = $memberId;
  145. $amount = number_format($amount, 2, '.', '');
  146. $data['amount'] = $amount;
  147. $data['channel'] = $channel;
  148. $data['bank_name'] = $bank_name;
  149. $data['account'] = $account;
  150. $data['card_no'] = $card_no;
  151. $data['callback_url'] = SanJinService::getNotifyUrl();
  152. $data['status'] = self::STATUS_STAY;
  153. $ret = SanJinService::payout($amount,$order_no,$bank_name,$account,$card_no);
  154. if($ret['code'] == 200){
  155. DB::beginTransaction();
  156. try {
  157. $wallet->available_balance = $available_balance;
  158. $wallet->save();
  159. $data['status'] = self::STATUS_PROCESS;
  160. $info = self::model()::create($data);
  161. $id = $info->id;
  162. BalanceLogService::addLog(
  163. $memberId,
  164. $default_amount,
  165. $balance,
  166. $available_balance,
  167. '三方提现',
  168. $id,
  169. '钱宝提现'
  170. );
  171. $text = "✅ 提现申请已提交!\n\n";
  172. $text .= "钱包余额:{$available_balance} RMB\n";
  173. $text .= "提现金额:{$default_amount} RMB\n";
  174. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  175. $result['text'] = $text;
  176. DB::commit();
  177. }catch (\Exception $e) {
  178. DB::rollBack();
  179. $result['text'] = $e->getMessage();
  180. return $result;
  181. // 记录错误日志或抛出异常
  182. }
  183. }else{
  184. $result['text'] = $ret['msg'];
  185. return $result;
  186. }
  187. return $result;
  188. }
  189. }