HdRefundClass.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * 用途:零售售后主表业务类
  4. * 谁用:hdApp 售后列表/详情、mallApp 顾客申请归属校验、资金统计
  5. * 解决:统一 status 审核四态;顾客申请按 userId/orderId 查待审核与最新记录
  6. */
  7. namespace bizHd\refund\classes;
  8. use biz\shop\classes\ShopExtClass;
  9. use common\components\noticeUtil;
  10. use common\components\orderSn;
  11. use common\components\util;
  12. use Yii;
  13. use bizHd\base\classes\BaseClass;
  14. use common\components\lakala\Lakala;
  15. class HdRefundClass extends BaseClass
  16. {
  17. public static $baseFile = '\bizHd\refund\models\HdRefund';
  18. const REFUND_TYPE_MONEY_GOOD = 1; //退款退货
  19. const REFUND_TYPE_MONEY = 2; // 仅退款
  20. /** 待审核(商城顾客申请;旧别名 STATUS_UN_COMPLETE) */
  21. const STATUS_PENDING = 0;
  22. /** 已通过 / 已完成 */
  23. const STATUS_PASS = 1;
  24. /** 已驳回 */
  25. const STATUS_REJECT = 2;
  26. /** 已取消 */
  27. const STATUS_CANCEL = 3;
  28. /** @deprecated 兼容旧调用,等价 STATUS_PENDING */
  29. const STATUS_UN_COMPLETE = 0;
  30. /** @deprecated 兼容旧调用,等价 STATUS_PASS */
  31. const STATUS_COMPLETE = 1;
  32. /** 1当天售后(默认) */
  33. const SAME_DAY_YES = 1;
  34. /** 0跨天售后(与有无原单无关;无原单看 orderId/orderSn) */
  35. const SAME_DAY_NO = 0;
  36. public static function onlyRefundAmount($shop, $order)
  37. {
  38. $refundSn = orderSn::getHdRefundSn();
  39. $xsOrderSn = $order['orderSn'] ?? '';
  40. $refundPrice = $order['actPrice'] ?? 0;
  41. $refundFee = bcmul($refundPrice, 100);
  42. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  43. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  44. $params = [
  45. 'appid' => 'OP00002119',
  46. 'serial_no' => '018b08cfddbd',
  47. 'merchant_no' => $shop->lklSjNo,
  48. 'term_no' => $shop->lklScanTermNo,
  49. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  50. 'lklCertificatePath' => $lklCertificatePath,
  51. ];
  52. $laResource = new Lakala($params);
  53. $refundReason = '';
  54. $orderSn = '';
  55. $thirdNo = $order['thirdNo'] ?? '';
  56. //竟然会出现thirdNo为空的情况,好像客户付款时有优惠金额时
  57. if (empty($thirdNo)) {
  58. $orderSn = $order['orderSn'] ?? '';
  59. }
  60. $backParams = [
  61. 'refundSn' => $refundSn,
  62. 'thirdNo' => $thirdNo,
  63. 'orderSn' => $orderSn,
  64. 'refundAmount' => $refundFee,
  65. 'refundReason' => $refundReason,
  66. ];
  67. $response = $laResource->refund($backParams);
  68. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  69. $errMsg = $response['msg'] ?? '';
  70. noticeUtil::push("钱没有退回 {$xsOrderSn} {$errMsg}", '15280215347');
  71. return ['status' => 0, 'msg' => $errMsg];
  72. } else {
  73. $shopId = $shop->id;
  74. ShopExtClass::orderCancelBackMoneyReport($shopId);
  75. $sjName = $shop->merchantName;
  76. $shopName = $shop->shopName;
  77. $name = $shopName == '首店' ? $sjName : $sjName . '-' . $shopName;
  78. noticeUtil::push("钱已经原路退回 success {$xsOrderSn} {$name}", '15280215347');
  79. return ['status' => 1, 'msg' => '退款成功'];
  80. }
  81. }
  82. // 订单列表
  83. public static function getOrderList($where)
  84. {
  85. $data = self::getList('*', $where, 'addTime DESC');
  86. $data['totalRefundPrice'] = self::sum($where, 'refundPrice');
  87. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  88. if (empty($list)) {
  89. return $data;
  90. }
  91. $data['list'] = $list;
  92. return $data;
  93. }
  94. //验证信息 ssh 20220401
  95. public static function valid($refund, $mainId)
  96. {
  97. if (empty($refund)) {
  98. util::fail('没有找到退款信息');
  99. }
  100. if (isset($refund->mainId) == false || $refund->mainId != $mainId) {
  101. util::fail('无法操作');
  102. }
  103. return true;
  104. }
  105. /**
  106. * 校验售后单归属当前登录商城用户
  107. * 列表/详情不带当前店 hdId 时 customId 可能为 0,需同时认 userId
  108. * @param object|null $refund
  109. * @param int $customId 当前请求店铺的客户 id,可能为 0
  110. * @param int $userId 商城登录用户 id
  111. * @return bool
  112. */
  113. public static function validByCustom($refund, $customId, $userId = 0)
  114. {
  115. if (empty($refund)) {
  116. util::fail('没有找到售后申请');
  117. }
  118. $userId = (int)$userId;
  119. $customId = (int)$customId;
  120. $refundUserId = (int)($refund->userId ?? 0);
  121. $refundCustomId = (int)($refund->customId ?? 0);
  122. if ($userId > 0 && $refundUserId === $userId) {
  123. return true;
  124. }
  125. if ($customId > 0 && $refundCustomId === $customId) {
  126. return true;
  127. }
  128. util::fail('不是你的售后申请');
  129. return false;
  130. }
  131. /**
  132. * 按订单查最新一条商城顾客申请(优先待审核)
  133. * 商家即时售后 userId=0,这里只认顾客申请,避免把商家退款当成申请进度
  134. * @param int $orderId
  135. * @return array|null
  136. */
  137. public static function getLatestByOrderId($orderId)
  138. {
  139. $orderId = (int)$orderId;
  140. if ($orderId <= 0) {
  141. return null;
  142. }
  143. $pending = self::getByCondition(['orderId' => $orderId, 'status' => self::STATUS_PENDING]);
  144. if (!empty($pending)) {
  145. return $pending;
  146. }
  147. $list = self::getLimitList('*', ['orderId' => $orderId, 'userId>' => 0], 1, 'id DESC');
  148. if (empty($list)) {
  149. return null;
  150. }
  151. return $list[0] ?? null;
  152. }
  153. /**
  154. * 订单是否存在待审核申请
  155. * 待审核只会由 mallApp 顾客提交,商家即时售后创建时即 status=1
  156. * @param int $orderId
  157. * @return bool
  158. */
  159. public static function hasPendingByOrderId($orderId)
  160. {
  161. $row = self::getByCondition(['orderId' => (int)$orderId, 'status' => self::STATUS_PENDING]);
  162. return !empty($row);
  163. }
  164. }