| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace addons\HuijuBill\common\service;
- use addons\HuijuBill\common\enums\StatusEnum;
- use addons\HuijuBill\common\models\OrderRefundModel;
- use addons\HuijuBill\common\logic\PayApiLogic;
- use common\helpers\Url;
- use common\models\common\MiddleMch;
- use common\models\common\PaymentTradeOrder;
- use Exception;
- use yii\base\Model;
- use yii\data\Pagination;
- class RefundService extends Model
- {
- /**
- * 商户ID
- *
- * @var integer
- */
- public $mall_id;
- /**
- * 报错
- *
- * @var string
- */
- public $error;
- /**
- * 获取汇聚分账支付列表
- *
- * @param array $params
- * @return array
- */
- public function getRefundOrderList($params)
- {
- $params = array_filter($params, function ($val) {return ($val === '' || $val === null) ? false : true;});
- $limit = $params['limit'] ?? 30;
- $page = $params['page'] ?? 1;
- $model = OrderRefundModel::find();
- $model->where(['mall_id' => $this->mall_id]);
- $model->with(['mallOrderRefund' => function ($query) {
- $query->with(['orderDetail' => function ($query) {
- return $query->select('id, goods_name, num, order_id, pic_url');
- }]);
- return $query->select('id, is_refund, order_detail_id, order_no, reason, reality_refund_price');
- }]);
- // 查询
- !empty($params['no']) && $model->andWhere(['like', 'refund_order_no', $params['no']]);
- !empty($params['status']) && $model->andWhere(['=', 'status', $params['status']]);
- $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
- $model->limit($pagination->limit)->offset($pagination->offset);
-
- $list = $model->asArray()
- ->orderBy('id desc')
- ->all();
- foreach ($list as $k => $v) {
- $list[$k]['status_text'] = StatusEnum::getValue($v['status']);
- }
- $data['list'] = $list;
- $data['status'] = StatusEnum::getMap();
- $data['page_count'] = $pagination->pageCount;
- $data['current_page'] = $page + 1;
- return $data;
- }
- /**
- * 退款失败时重新手动发起退款请求
- *
- * @param array $ids [1, 2, ...] 退款订单ID
- * @return boolean
- */
- public function reissueRefund($ids)
- {
- try {
- if (empty($ids)) throw new Exception('ID不能为空');
- $order_list = OrderRefundModel::find()->where(['id' => $ids, 'status' => [
- StatusEnum::STATUS_101,
- StatusEnum::STATUS_102,
- ]])->all();
- if (empty($order_list)) throw new Exception('补单列表为空');
-
- $mch = MiddleMch::getMch($this->mall_id);
- $api = new PayApiLogic($mch);
- foreach ($order_list as $order) {
- $res = $api->order_refund([
- 'order_no' => $order->out_trade_no,
- 'refund_order_no' => $order->refund_order_no,
- 'refund_amount' => $order->refund_amount,
- 'reason' => '订单售后退款',
- 'notify_url' => Url::toApi(['notify/huiju_bill_refund'], true), // 退款结果通知网址
- ]);
- }
- } catch (Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- return true;
- }
- }
|