OrderRefundRejectService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace addons\WangDianTong\common\service;
  3. use addons\WangDianTong\common\traits\CompensateTrait;
  4. use addons\WangDianTong\common\traits\CreateRefundSendTrait;
  5. use addons\WangDianTong\common\traits\RequestRetryTrait;
  6. use common\enums\RabbitMqEnum;
  7. use common\models\order\OrderRefund;
  8. use Yii;
  9. use yii\helpers\Json;
  10. class OrderRefundRejectService
  11. {
  12. use RequestRetryTrait, CreateRefundSendTrait, CompensateTrait;
  13. /**
  14. * @param int $mall_id
  15. * @param int $refund_id
  16. * @return void
  17. * @throws \Exception
  18. */
  19. public function sync(int $mall_id, int $refund_id)
  20. {
  21. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, [
  22. 'mall_id' => $mall_id,
  23. 'id' => $refund_id,
  24. ],
  25. self::class, 'execute');
  26. }
  27. /**
  28. * @param array $data
  29. * @return void|null
  30. * @throws \Exception
  31. */
  32. public function execute(array $data)
  33. {
  34. \Yii::error(__CLASS__ . "->async_handle: " . Json::encode($data));
  35. if (empty($data)) {
  36. Yii::error('OrderRefundRejectService error:售后处理失败,内容为空');
  37. return null;
  38. }
  39. try {
  40. // 退款申请
  41. $refund = OrderRefund::getOne([
  42. ['id' => $data['id']]
  43. ], true, ['orderDetail', 'base_user', 'order']);
  44. if (empty($refund) || $refund['order']['store_id'] > 0 || $refund['order']['mch_id'] > 0) return;
  45. $data['mall_id'] = $refund['mall_id'];
  46. $config = (new SettingService())->get($refund['mall_id']);
  47. if (empty($config['is_enable'])) {
  48. \Yii::error("{$refund['mall_id']} 未开启旺店通插件");
  49. return;
  50. }
  51. $trade_list[] = (new CreateRefundService())->assembleData($refund, $config);
  52. $flag = $this->retry(function () use ($config, $trade_list) {
  53. return $this->send($config, $trade_list);
  54. });
  55. if (is_string($flag)) throw new \Exception($flag);
  56. $this->compensateFinish($data['compensate_id']??0);
  57. } catch (\Exception $e) {
  58. $this->compensate($data, __CLASS__, __FUNCTION__, $e);
  59. }
  60. }
  61. }