RenewClass.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace biz\renew\classes;
  3. use biz\base\classes\BaseClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\sj\classes\SjClass;
  6. use bizHd\saas\classes\ApplyClass;
  7. use bizHd\shop\classes\MainInviteClass;
  8. use common\components\dict;
  9. use common\components\noticeUtil;
  10. use common\components\util;
  11. use Yii;
  12. class RenewClass extends BaseClass
  13. {
  14. public static $baseFile = '\biz\renew\models\Renew';
  15. public static function getRenewList($where)
  16. {
  17. $data = self::getList('*', $where, 'addTime DESC');
  18. return $data;
  19. }
  20. /**
  21. * 是否已有 xhRenew 付费成功记录(同一 main/shop 仅首次付费享受邀请减免与佣金)
  22. */
  23. public static function hasPaidMemberRenew(int $mainId, int $shopId = 0): bool
  24. {
  25. $ptStyle = dict::getDict('ptStyle', 'hd');
  26. if ($mainId > 0) {
  27. $row = self::getByCondition(['mainId' => $mainId, 'status' => 1, 'ptStyle' => $ptStyle], false);
  28. if (!empty($row)) {
  29. return true;
  30. }
  31. }
  32. if ($shopId > 0) {
  33. $row = self::getByCondition(['shopId' => $shopId, 'status' => 1, 'ptStyle' => $ptStyle], false);
  34. return !empty($row);
  35. }
  36. return false;
  37. }
  38. /**
  39. * 除当前订单外是否已有付费成功的 xhRenew(回调写佣金时用)
  40. */
  41. public static function hasOtherPaidMemberRenew(int $mainId, string $excludeOrderSn = ''): bool
  42. {
  43. if ($mainId <= 0) {
  44. return false;
  45. }
  46. $ptStyle = dict::getDict('ptStyle', 'hd');
  47. $list = self::getAllByCondition(['mainId' => $mainId, 'status' => 1, 'ptStyle' => $ptStyle]);
  48. if (empty($list)) {
  49. return false;
  50. }
  51. foreach ($list as $item) {
  52. $sn = is_array($item) ? (string) ($item['orderSn'] ?? '') : (string) ($item->orderSn ?? '');
  53. if ($excludeOrderSn !== '' && $sn !== $excludeOrderSn) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
  60. {
  61. $renew = self::getByCondition(['orderSn' => $orderSn], true);
  62. if (empty($renew)) {
  63. $msg = "没有找到续费订单 orderSn:{$orderSn}";
  64. noticeUtil::push($msg, '15280215347');
  65. util::fail($msg);
  66. }
  67. if ($renew->actPrice != $totalFee) {
  68. $msg = "续费订单金额与回调通知金额不一致 orderSn:{$orderSn} {$renew->actPrice} {$totalFee}";
  69. noticeUtil::push($msg, '15280215347');
  70. util::fail($msg);
  71. }
  72. if ($renew->status == 1) {
  73. $msg = '续费订单已付款,重复通知 orderSn:' . $orderSn;
  74. noticeUtil::push($msg, '15280215347');
  75. util::fail($msg);
  76. }
  77. $attachParams = [];
  78. if (!empty($attach)) {
  79. parse_str($attach, $attachParams);
  80. }
  81. $applyId = (int) ($attachParams['applyId'] ?? 0);
  82. $isRegisterMember = $applyId > 0;
  83. $shopId = $renew->shopId ?? 0;
  84. $shop = ShopClass::getById($shopId, true);
  85. if (empty($shop)) {
  86. $msg = '续费订单没有找到门店 orderSn:' . $orderSn;
  87. noticeUtil::push($msg, '15280215347');
  88. util::fail($msg);
  89. }
  90. $mainId = $shop->mainId ?? 0;
  91. $before = $shop->deadline ?? '';
  92. $beforeTime = strtotime($before);
  93. if ($renew->type == 2) {
  94. //购买设备,不需要有操作
  95. } else {
  96. //每次续费一年
  97. $time = $beforeTime + 31536000;
  98. $newDate = date("Y-m-d H:i:s", $time);
  99. $renew->deadline = $newDate;
  100. $renew->beforeDeadline = $before;
  101. $shopList = ShopClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  102. if (!empty($shopList)) {
  103. foreach ($shopList as $myShop) {
  104. $myShop->beforeDeadline = $before;
  105. $myShop->deadline = $newDate;
  106. $myShop->hasRenew = 1;
  107. $myShop->needRenew = 0;
  108. $myShop->save();
  109. }
  110. }
  111. }
  112. $renew->returnCode = $transactionId;
  113. $renew->payTime = date("Y-m-d H:i:s");
  114. $renew->status = 1;
  115. $renew->save();
  116. if ($isRegisterMember && !self::hasOtherPaidMemberRenew((int) $mainId, (string) $orderSn)) {
  117. $apply = ApplyClass::getById($applyId);
  118. if (!empty($apply)) {
  119. try {
  120. MainInviteClass::grantInviterCommission(
  121. $apply,
  122. (int) $mainId,
  123. trim((string) ($apply['name'] ?? '')),
  124. trim((string) ($apply['mobile'] ?? '')),
  125. round((float) ($renew->prePrice ?? 0), 2),
  126. round((float) ($renew->actPrice ?? 0), 2)
  127. );
  128. } catch (\Throwable $e) {
  129. $msg = '邀请发佣失败 orderSn:' . $orderSn . ' ' . $e->getMessage();
  130. noticeUtil::push($msg, '15280215347');
  131. }
  132. }
  133. }
  134. return true;
  135. }
  136. }