PaymentOrderService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. // 在调用三方支付前开始事务
  134. DB::beginTransaction();
  135. try {
  136. $wallet = WalletService::findOne(['member_id' => $memberId]);
  137. if (!$wallet) {
  138. $result['text'] = '钱包不存在!';
  139. return $result;
  140. }
  141. $balance = $wallet->available_balance;
  142. if (bccomp($balance, $amount, 2) < 0) {
  143. $result['text'] = '您的钱包余额不足!';
  144. return $result;
  145. }
  146. $available_balance = bcsub($balance, $amount, 10);
  147. $data = [];
  148. $data['type'] = self::TYPE_PAYOUT;
  149. $order_no = self::createOrderNo('sj'.$data['type'].'_', $memberId);
  150. $data['order_no'] = $order_no;
  151. $data['member_id'] = $memberId;
  152. $amount = number_format($amount, 2, '.', '');
  153. $data['amount'] = $amount;
  154. $data['channel'] = $channel;
  155. $data['bank_name'] = $bank_name;
  156. $data['account'] = $account;
  157. $data['card_no'] = $card_no;
  158. $data['callback_url'] = SanJinService::getNotifyUrl();
  159. $data['status'] = self::STATUS_STAY;
  160. // 先预扣款(锁定资金)
  161. $wallet->available_balance = $available_balance;
  162. if (!$wallet->save()) {
  163. DB::rollBack();
  164. $result['text'] = '钱包更新失败!';
  165. return $result;
  166. }
  167. // 创建待处理状态的提现记录
  168. $info = self::model()::create($data);
  169. $id = $info->id;
  170. // 记录余额变动日志
  171. BalanceLogService::addLog(
  172. $memberId,
  173. $default_amount,
  174. $balance,
  175. $available_balance,
  176. '三方提现',
  177. $id,
  178. '钱宝提现'
  179. );
  180. // 提交事务,确保预扣款成功
  181. DB::commit();
  182. } catch (\Exception $e) {
  183. // 预扣款失败,回滚事务
  184. DB::rollBack();
  185. $result['text'] = '系统繁忙,请稍后重试!';
  186. Log::error('提现预扣款失败: ' . $e->getMessage(), [
  187. 'member_id' => $memberId,
  188. 'amount' => $amount
  189. ]);
  190. return $result;
  191. }
  192. // 调用三方支付接口(在事务外)
  193. $ret = SanJinService::payout($amount, $order_no, $bank_name, $account, $card_no);
  194. if ($ret['code'] == 200) {
  195. // 更新提现记录状态为处理中
  196. DB::beginTransaction();
  197. try {
  198. $info->status = self::STATUS_PROCESS;
  199. $info->save();
  200. DB::commit();
  201. $text = "✅ 提现申请已提交!\n\n";
  202. $text .= "钱包余额:{$available_balance} RMB\n";
  203. $text .= "提现金额:{$default_amount} RMB\n";
  204. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  205. $result['text'] = $text;
  206. } catch (\Exception $e) {
  207. DB::rollBack();
  208. // 状态更新失败,但资金已扣款且三方支付已成功
  209. // 这里可以记录告警,需要人工干预
  210. Log::error('提现状态更新失败: ' . $e->getMessage(), [
  211. 'member_id' => $memberId,
  212. 'order_no' => $order_no
  213. ]);
  214. $result['text'] = '提现申请已提交,系统处理中...';
  215. }
  216. } else {
  217. // 三方支付失败,需要回滚之前的预扣款
  218. DB::beginTransaction();
  219. try {
  220. // 恢复钱包余额
  221. $wallet->available_balance = $balance;
  222. $wallet->save();
  223. // 更新提现记录状态为失败
  224. $info->status = self::STATUS_FAIL;
  225. $info->remark = $ret['msg'];
  226. $info->save();
  227. // 记录退款日志
  228. BalanceLogService::addLog(
  229. $memberId,
  230. $default_amount,
  231. $available_balance,
  232. $balance,
  233. '三方提现',
  234. $id,
  235. '提现失败退款'
  236. );
  237. DB::commit();
  238. $result['text'] = $ret['msg'];
  239. } catch (\Exception $e) {
  240. DB::rollBack();
  241. // 回滚失败,需要记录告警,人工干预
  242. Log::error('提现失败回滚异常: ' . $e->getMessage(), [
  243. 'member_id' => $memberId,
  244. 'order_no' => $order_no,
  245. 'amount' => $amount
  246. ]);
  247. $result['text'] = '提现失败,请联系客服处理退款!';
  248. }
  249. }
  250. return $result;
  251. }
  252. }