| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace addons\YunfastPay\common\services;
- use addons\YunfastPay\common\enums\YunfastPayEnum;
- use addons\YunfastPay\common\models\PaymentOrder;
- use addons\YunfastPay\common\models\RefundOrder;
- use addons\YunfastPay\common\models\User;
- use addons\YunfastPay\common\services\trans\nocash\RefundTrans;
- use common\enums\RabbitMqEnum;
- use common\logic\order\OrderRefundLogic;
- use common\models\order\OrderRefund;
- use Yii;
- use yii\data\Pagination;
- use yii\helpers\Json;
- /**
- * 退款
- * @package addons\YunfastPay\common\services
- */
- class RefundService extends BaseService
- {
- /**
- * @var RefundTrans
- */
- protected $refundTrans;
- /**
- * @var OrderRefund
- */
- protected $refund;
- /**
- * @var PaymentOrder
- */
- protected $payment;
- /**
- * @var YunPayService
- */
- protected $yunPay;
- /**
- * @var string
- */
- protected $orderNo;
- /**
- * 订单退款
- *
- * @param string $order_no
- * @param string $refund_source
- * @return boolean
- */
- public function orderRefund(OrderRefund $refund, array $refund_params, string $order_no = '', string $refund_source = YunfastPayEnum::MALL_ORDER)
- {
- $this->refund = $refund;
- $this->payment = $this->getPayment($refund->order_id, $order_no, $refund_source);
- $this->yunPay = new YunPayService(['mall_id' => $this->mall_id]);
- $this->getRefundTrans();
- $res = $this->yunPay->sendPayRq($this->refundTrans);
- // 添加日志
- $this->addRefundLog($res, $refund_params, $refund_source);
- if ($this->getTransStatus($res) == '100') {
- // 返回100直接售后
- return true;
- }
- if ($this->getTransStatus($res) == '101') {
- // 返回101等候售后
- return $this->error('订单处理中');
- }
- return $this->error($res['respMsg'] ?? '');
- }
- /**
- * 当前订单是否属于插件
- *
- * @param OrderRefund $refund
- * @param string $order_no
- * @param string $refund_source
- * @return boolean
- */
- public function isAddonsOrder(OrderRefund $refund, string $order_no = '', string $refund_source = YunfastPayEnum::MALL_ORDER)
- {
- $this->refund = $refund;
- $this->payment = $this->getPayment($refund->order_id, $order_no, $refund_source);
-
- return !empty($this->payment) ? true : false;
- }
- /**
- * 是否已退款
- *
- * @param OrderRefund $refund
- * @param string $refund_source
- * @return boolean
- */
- public function isRefund(OrderRefund $refund, string $refund_source = YunfastPayEnum::MALL_ORDER)
- {
- $order = RefundOrder::getOrderByRefundId($refund->id, $refund_source);
- if (!empty($order)) {
- return $order->isRefundOk();
- }
- return false;
- }
- /**
- * @param integer $order_id
- * @param string $order_no
- * @param string $refund_source
- * @return PaymentOrder
- */
- protected function getPayment(int $order_id, string $order_no = '', string $refund_source = YunfastPayEnum::MALL_ORDER)
- {
- return !empty($this->payment)
- ? $this->payment
- : PaymentOrder::getPaymentByOrderId($order_id, $order_no, $refund_source);
- }
- /**
- * @return RefundTrans
- */
- protected function getRefundTrans()
- {
- return $this->refundTrans = new RefundTrans(
- $this->getOrderNo(),
- $this->payment->order_no,
- date('Ymd', $this->payment->created_at),
- $this->refund->refund_price,
- );
- }
- /**
- * @return string
- */
- protected function getOrderNo()
- {
- return !empty($this->orderNo)
- ? $this->orderNo
- : $this->refund->order_no;
- }
- /**
- * 添加退款日志
- *
- * @param array $result
- * @return void
- */
- protected function addRefundLog(array $result, array $refund_params, string $refund_source = YunfastPayEnum::MALL_ORDER)
- {
- // 异步执行
- Yii::$app->services->rabbitMq->push(
- RabbitMqEnum::EXCHANGE_ORDER,
- RabbitMqEnum::ORDER_QUEUE,
- [
- 'mall_id' => $this->mall_id,
- 'user_id' => $this->refund->user_id,
- 'refund_id' => $this->refund->id,
- 'order_id' => $this->refund->order_id,
- 'order_no' => $this->getOrderNo(),
- 'refund_amount' => $this->refund->refund_price,
- 'req_data' => $this->yunPay->getReqData(),
- 'res_data' => $result,
- 'refund_params' => $refund_params,
- 'status' => $this->getTransStatus($result),
- 'refund_source' => $refund_source,
- ],
- RefundOrder::class,
- 'addLog'
- );
- // RefundOrder::addLog([
- // 'mall_id' => $this->mall_id,
- // 'user_id' => $this->refund->user_id,
- // 'refund_id' => $this->refund->id,
- // 'order_id' => $this->refund->order_id,
- // 'order_no' => $this->getOrderNo(),
- // 'refund_amount' => $this->refund->refund_price,
- // 'req_data' => $this->yunPay->getReqData(),
- // 'res_data' => $result,
- // 'status' => $this->getTransStatus($result),
- // ]);
- }
- /**
- * 回调
- *
- * @param array $respData
- * @return boolean
- */
- public function notify(array $respData)
- {
- /**
- * @var RefundOrder
- */
- $order = RefundOrder::getOrderByNo($this->mall_id, $respData['outOrderId']);
- if ($order->status == '100') return true;
- // 交易成功
- if ('100' == $respData['transStatus']) {
- // 云库存取消自提的退款会传refund_source,其他的都是 YunfastPayEnum::MALL_ORDER,云库存自提不走主商城售后
- if ($order->refund_source === YunfastPayEnum::MALL_ORDER) {
- // 退款
- $logic = new OrderRefundLogic();
- $params = Json::decode($order->refund_params);
- $res = $logic->execute($this->mall_id, array_merge($params, [
- 'is_notify' => true // 设置为回调模式
- ]));
- if ($res == false) {
- Yii::error('订单回调退款订单失败%s', $respData['outOrderId']);
- }
- }
- return true;
- } else if ('102' == $respData['transStatus']) {
- // 交易失败
- $order->changeStatus('102');
- return true;
- } else {
- // 未知结果,这种情况不会有。
- return $this->error('未知情况');
- }
- }
- /**
- * 获取订单分页列表
- *
- * @param array $params
- * @return array
- */
- public function getPageList(array $params)
- {
- $limit = $params['limit'] ?? 20;
- $page = $params['page'] ?? 1;
- $model = RefundOrder::find();
- $model->andWhere(['mall_id' => $this->mall_id]);
- $model->with(['user']);
- if (!empty($params['user_id'])) {
- $model->andWhere(['user_id' => $params['user_id']]);
- }
- if (!empty($params['user_keyword'])) {
- $model->andWhere([
- 'user_id' => User::getUserIdsByKeyword($this->mall_id, $params['user_keyword'])
- ]);
- }
- if (!empty($params['order_no'])) {
- $model->andWhere(['order_no' => $params['order_no']]);
- }
- if (!empty($params['time'])) {
- $model->andFilterWhere([
- 'between',
- 'created_at',
- strtotime($params['time'][0]),
- strtotime($params['time'][1])
- ]);
- }
- $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
- $model->limit($pagination->limit)->offset($pagination->offset);
- $list = $model->orderBy('id desc')
- ->asArray()
- ->all();
- $data['list'] = $list;
- $data['page_count'] = $pagination->pageCount;
- $data['current_page'] = $page + 1;
- return $data;
- }
- }
|