| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace addons\Mch\common\services;
- use addons\Mch\common\models\Mch;
- use addons\Mch\common\models\MchWithdrawLog;
- /**
- * 提现事务服务
- */
- class WithdrawTransferService
- {
- /**
- * @param array $data
- * @return bool
- */
- public function refund(array $data): bool
- {
- $trans = \Yii::$app->db->beginTransaction();
- try {
- // 根据流水号获取提现记录
- $withdrawLog = MchWithdrawLog::find()->where(['id' => $data['withdrawal_source_id'], 'status' => 1])->one();
- if (!$withdrawLog) {
- throw new \Exception("提现记录不存在");
- }
- $withdrawLog->withdraw_status = 2;
- if (!$withdrawLog->save()) {
- throw new \Exception("提现状态修改失败");
- }
- $mch = Mch::find()->where([
- 'id' => $withdrawLog->mch_id,
- 'mall_id' => $withdrawLog->mall_id,
- 'user_id' => $withdrawLog->user_id,
- 'status' => 1
- ])->one();
- if ($mch) {
- $mch->balance += $withdrawLog->num;
- if (!$mch->save()) {
- throw new \Exception("商户余额修改失败");
- }
- \Yii::$app->custom->logs("商户提现回退:" . $withdrawLog->user_id, '回退金额:' . $withdrawLog->num);
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- \Yii::$app->custom->logs("退款失败", $e->getMessage());
- return false;
- }
- }
- }
|