WithdrawTransferService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace addons\Mch\common\services;
  3. use addons\Mch\common\models\Mch;
  4. use addons\Mch\common\models\MchWithdrawLog;
  5. /**
  6. * 提现事务服务
  7. */
  8. class WithdrawTransferService
  9. {
  10. /**
  11. * @param array $data
  12. * @return bool
  13. */
  14. public function refund(array $data): bool
  15. {
  16. $trans = \Yii::$app->db->beginTransaction();
  17. try {
  18. // 根据流水号获取提现记录
  19. $withdrawLog = MchWithdrawLog::find()->where(['id' => $data['withdrawal_source_id'], 'status' => 1])->one();
  20. if (!$withdrawLog) {
  21. throw new \Exception("提现记录不存在");
  22. }
  23. $withdrawLog->withdraw_status = 2;
  24. if (!$withdrawLog->save()) {
  25. throw new \Exception("提现状态修改失败");
  26. }
  27. $mch = Mch::find()->where([
  28. 'id' => $withdrawLog->mch_id,
  29. 'mall_id' => $withdrawLog->mall_id,
  30. 'user_id' => $withdrawLog->user_id,
  31. 'status' => 1
  32. ])->one();
  33. if ($mch) {
  34. $mch->balance += $withdrawLog->num;
  35. if (!$mch->save()) {
  36. throw new \Exception("商户余额修改失败");
  37. }
  38. \Yii::$app->custom->logs("商户提现回退:" . $withdrawLog->user_id, '回退金额:' . $withdrawLog->num);
  39. }
  40. $trans->commit();
  41. return true;
  42. } catch (\Exception $e) {
  43. $trans->rollBack();
  44. \Yii::$app->custom->logs("退款失败", $e->getMessage());
  45. return false;
  46. }
  47. }
  48. }