| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- /**
- * 用途:零售售后主表业务类
- * 谁用:hdApp 售后列表/详情、mallApp 顾客申请归属校验、资金统计
- * 解决:统一 status 审核四态;顾客申请按 userId/orderId 查待审核与最新记录
- */
- namespace bizHd\refund\classes;
- use biz\shop\classes\ShopExtClass;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use bizHd\base\classes\BaseClass;
- use common\components\lakala\Lakala;
- class HdRefundClass extends BaseClass
- {
- public static $baseFile = '\bizHd\refund\models\HdRefund';
- const REFUND_TYPE_MONEY_GOOD = 1; //退款退货
- const REFUND_TYPE_MONEY = 2; // 仅退款
- /** 待审核(商城顾客申请;旧别名 STATUS_UN_COMPLETE) */
- const STATUS_PENDING = 0;
- /** 已通过 / 已完成 */
- const STATUS_PASS = 1;
- /** 已驳回 */
- const STATUS_REJECT = 2;
- /** 已取消 */
- const STATUS_CANCEL = 3;
- /** @deprecated 兼容旧调用,等价 STATUS_PENDING */
- const STATUS_UN_COMPLETE = 0;
- /** @deprecated 兼容旧调用,等价 STATUS_PASS */
- const STATUS_COMPLETE = 1;
- /** 1当天售后(默认) */
- const SAME_DAY_YES = 1;
- /** 0跨天售后(与有无原单无关;无原单看 orderId/orderSn) */
- const SAME_DAY_NO = 0;
- public static function onlyRefundAmount($shop, $order)
- {
- $refundSn = orderSn::getHdRefundSn();
- $xsOrderSn = $order['orderSn'] ?? '';
- $refundPrice = $order['actPrice'] ?? 0;
- $refundFee = bcmul($refundPrice, 100);
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundReason = '';
- $orderSn = '';
- $thirdNo = $order['thirdNo'] ?? '';
- //竟然会出现thirdNo为空的情况,好像客户付款时有优惠金额时
- if (empty($thirdNo)) {
- $orderSn = $order['orderSn'] ?? '';
- }
- $backParams = [
- 'refundSn' => $refundSn,
- 'thirdNo' => $thirdNo,
- 'orderSn' => $orderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => $refundReason,
- ];
- $response = $laResource->refund($backParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push("钱没有退回 {$xsOrderSn} {$errMsg}", '15280215347');
- return ['status' => 0, 'msg' => $errMsg];
- } else {
- $shopId = $shop->id;
- ShopExtClass::orderCancelBackMoneyReport($shopId);
- $sjName = $shop->merchantName;
- $shopName = $shop->shopName;
- $name = $shopName == '首店' ? $sjName : $sjName . '-' . $shopName;
- noticeUtil::push("钱已经原路退回 success {$xsOrderSn} {$name}", '15280215347');
- return ['status' => 1, 'msg' => '退款成功'];
- }
- }
- // 订单列表
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- $data['totalRefundPrice'] = self::sum($where, 'refundPrice');
- $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
- if (empty($list)) {
- return $data;
- }
- $data['list'] = $list;
- return $data;
- }
- //验证信息 ssh 20220401
- public static function valid($refund, $mainId)
- {
- if (empty($refund)) {
- util::fail('没有找到退款信息');
- }
- if (isset($refund->mainId) == false || $refund->mainId != $mainId) {
- util::fail('无法操作');
- }
- return true;
- }
- /**
- * 校验售后单归属当前登录商城用户
- * 列表/详情不带当前店 hdId 时 customId 可能为 0,需同时认 userId
- * @param object|null $refund
- * @param int $customId 当前请求店铺的客户 id,可能为 0
- * @param int $userId 商城登录用户 id
- * @return bool
- */
- public static function validByCustom($refund, $customId, $userId = 0)
- {
- if (empty($refund)) {
- util::fail('没有找到售后申请');
- }
- $userId = (int)$userId;
- $customId = (int)$customId;
- $refundUserId = (int)($refund->userId ?? 0);
- $refundCustomId = (int)($refund->customId ?? 0);
- if ($userId > 0 && $refundUserId === $userId) {
- return true;
- }
- if ($customId > 0 && $refundCustomId === $customId) {
- return true;
- }
- util::fail('不是你的售后申请');
- return false;
- }
- /**
- * 按订单查最新一条商城顾客申请(优先待审核)
- * 商家即时售后 userId=0,这里只认顾客申请,避免把商家退款当成申请进度
- * @param int $orderId
- * @return array|null
- */
- public static function getLatestByOrderId($orderId)
- {
- $orderId = (int)$orderId;
- if ($orderId <= 0) {
- return null;
- }
- $pending = self::getByCondition(['orderId' => $orderId, 'status' => self::STATUS_PENDING]);
- if (!empty($pending)) {
- return $pending;
- }
- $list = self::getLimitList('*', ['orderId' => $orderId, 'userId>' => 0], 1, 'id DESC');
- if (empty($list)) {
- return null;
- }
- return $list[0] ?? null;
- }
- /**
- * 订单是否存在待审核申请
- * 待审核只会由 mallApp 顾客提交,商家即时售后创建时即 status=1
- * @param int $orderId
- * @return bool
- */
- public static function hasPendingByOrderId($orderId)
- {
- $row = self::getByCondition(['orderId' => (int)$orderId, 'status' => self::STATUS_PENDING]);
- return !empty($row);
- }
- }
|