RefundService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace addons\HuijuBill\common\service;
  3. use addons\HuijuBill\common\enums\StatusEnum;
  4. use addons\HuijuBill\common\models\OrderRefundModel;
  5. use addons\HuijuBill\common\logic\PayApiLogic;
  6. use common\helpers\Url;
  7. use common\models\common\MiddleMch;
  8. use common\models\common\PaymentTradeOrder;
  9. use Exception;
  10. use yii\base\Model;
  11. use yii\data\Pagination;
  12. class RefundService extends Model
  13. {
  14. /**
  15. * 商户ID
  16. *
  17. * @var integer
  18. */
  19. public $mall_id;
  20. /**
  21. * 报错
  22. *
  23. * @var string
  24. */
  25. public $error;
  26. /**
  27. * 获取汇聚分账支付列表
  28. *
  29. * @param array $params
  30. * @return array
  31. */
  32. public function getRefundOrderList($params)
  33. {
  34. $params = array_filter($params, function ($val) {return ($val === '' || $val === null) ? false : true;});
  35. $limit = $params['limit'] ?? 30;
  36. $page = $params['page'] ?? 1;
  37. $model = OrderRefundModel::find();
  38. $model->where(['mall_id' => $this->mall_id]);
  39. $model->with(['mallOrderRefund' => function ($query) {
  40. $query->with(['orderDetail' => function ($query) {
  41. return $query->select('id, goods_name, num, order_id, pic_url');
  42. }]);
  43. return $query->select('id, is_refund, order_detail_id, order_no, reason, reality_refund_price');
  44. }]);
  45. // 查询
  46. !empty($params['no']) && $model->andWhere(['like', 'refund_order_no', $params['no']]);
  47. !empty($params['status']) && $model->andWhere(['=', 'status', $params['status']]);
  48. $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  49. $model->limit($pagination->limit)->offset($pagination->offset);
  50. $list = $model->asArray()
  51. ->orderBy('id desc')
  52. ->all();
  53. foreach ($list as $k => $v) {
  54. $list[$k]['status_text'] = StatusEnum::getValue($v['status']);
  55. }
  56. $data['list'] = $list;
  57. $data['status'] = StatusEnum::getMap();
  58. $data['page_count'] = $pagination->pageCount;
  59. $data['current_page'] = $page + 1;
  60. return $data;
  61. }
  62. /**
  63. * 退款失败时重新手动发起退款请求
  64. *
  65. * @param array $ids [1, 2, ...] 退款订单ID
  66. * @return boolean
  67. */
  68. public function reissueRefund($ids)
  69. {
  70. try {
  71. if (empty($ids)) throw new Exception('ID不能为空');
  72. $order_list = OrderRefundModel::find()->where(['id' => $ids, 'status' => [
  73. StatusEnum::STATUS_101,
  74. StatusEnum::STATUS_102,
  75. ]])->all();
  76. if (empty($order_list)) throw new Exception('补单列表为空');
  77. $mch = MiddleMch::getMch($this->mall_id);
  78. $api = new PayApiLogic($mch);
  79. foreach ($order_list as $order) {
  80. $res = $api->order_refund([
  81. 'order_no' => $order->out_trade_no,
  82. 'refund_order_no' => $order->refund_order_no,
  83. 'refund_amount' => $order->refund_amount,
  84. 'reason' => '订单售后退款',
  85. 'notify_url' => Url::toApi(['notify/huiju_bill_refund'], true), // 退款结果通知网址
  86. ]);
  87. }
  88. } catch (Exception $e) {
  89. $this->error = $e->getMessage();
  90. return false;
  91. }
  92. return true;
  93. }
  94. }