ClearController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizGhs\clear\classes\ClearClass;
  5. use bizGhs\clear\services\ClearService;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\order\classes\OrderClearClass;
  8. use bizHd\shop\classes\ShopClass;
  9. use common\components\dict;
  10. use common\components\imgUtil;
  11. use common\components\lakala\Lakala;
  12. use common\components\noticeUtil;
  13. use common\components\util;
  14. use common\components\qrCodeUtil;
  15. use Yii;
  16. class ClearController extends BaseController
  17. {
  18. public $guestAccess = ['get-full-info', 'detail', 'get-ali-pay-clear-code'];
  19. //取消结账单 ssh 20220509
  20. public function actionCancel()
  21. {
  22. $id = Yii::$app->request->get('id');
  23. $clear = ClearClass::getLockById($id);
  24. if (empty($clear)) {
  25. util::fail('没有结账信息哈');
  26. }
  27. $customId = $clear->customId ?? 0;
  28. $custom = CustomClass::getById($customId, true);
  29. $customShopId = $custom->shopId ?? 0;
  30. if ($customShopId != $this->shopId) {
  31. util::fail('没有权限');
  32. }
  33. if ($clear->status == ClearClass::STATUS_HAS_PAY) {
  34. util::fail('账单已完成');
  35. }
  36. if ($clear->status == ClearClass::STATUS_EXPIRE) {
  37. util::fail('账单已取消');
  38. }
  39. $clear->status = ClearClass::STATUS_EXPIRE;
  40. $clear->remark = "客户手动取消 " . date('Y-m-d H:i:s');
  41. $clear->save();
  42. $orderSn = $clear->orderSn ?? '';
  43. $ghsShopId = $clear->ghsShopId ?? 0;
  44. $ghsShop = ShopClass::getById($ghsShopId, true);
  45. if (!empty($ghsShop)) {
  46. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  47. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  48. $params = [
  49. 'appid' => 'OP00002119',
  50. 'serial_no' => '018b08cfddbd',
  51. 'merchant_no' => $ghsShop->lklSjNo,
  52. 'term_no' => $ghsShop->lklScanTermNo,
  53. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  54. 'lklCertificatePath' => $lklCertificatePath,
  55. ];
  56. $laResource = new Lakala($params);
  57. $closeParams = [
  58. 'orderSn' => $orderSn,
  59. ];
  60. $response = $laResource->close($closeParams);
  61. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  62. $msg = $response['msg'] ?? '';
  63. $code = $response['code'] ?? 0;
  64. if (!in_array($code, ['BBS11114'])) {
  65. noticeUtil::push("花店结账:" . $orderSn . " 关单没有成功 {$msg} {$code}", '15280215347');
  66. }
  67. }
  68. //收银台关单
  69. $closeParams = ['orderSn' => $orderSn];
  70. $response = $laResource->cashClose($closeParams);
  71. if (isset($response['code']) == false || $response['code'] != '000000') {
  72. $msg = $response['msg'] ?? '';
  73. $code = $response['code'] ?? 0;
  74. if (!in_array($code, ['000091'])) {
  75. noticeUtil::push('花店结账:' . $orderSn . ',关单没有成功(收银台-支付宝),' . $msg . $code, '15280215347');
  76. }
  77. }
  78. }
  79. util::complete();
  80. }
  81. //结款单 ssh 20210625
  82. public function actionList()
  83. {
  84. $get = Yii::$app->request->get();
  85. $where = [];
  86. if (isset($get['status']) && is_numeric($get['status'])) {
  87. $where['status'] = $get['status'];
  88. }
  89. $ghsId = $get['ghsId'] ?? 0;
  90. if (!empty($ghsId)) {
  91. $ghs = GhsClass::getById($ghsId, true);
  92. GhsClass::valid($ghs, $this->shopId);
  93. $where['ghsId'] = $ghsId;
  94. }
  95. $where['customShopId'] = $this->shopId ?? 0;
  96. $list = ClearService::getClearList($where);
  97. util::success($list);
  98. }
  99. //获取基本信息 ssh 20210625
  100. public function actionGetFullInfo()
  101. {
  102. $id = Yii::$app->request->get('id');
  103. $info = ClearClass::getById($id);
  104. $respond = ClearClass::getCgInfo($info);
  105. util::success($respond);
  106. }
  107. //获取支付宝付款码 ssh 20240713
  108. public function actionGetAliPayClearCode()
  109. {
  110. $get = Yii::$app->request->get();
  111. $orderSn = $get['orderSn'] ?? 0;
  112. $info = ClearClass::getByCondition(['orderSn' => $orderSn], true);
  113. if (empty($info)) {
  114. util::fail('没有找到结账单');
  115. }
  116. if (getenv('YII_ENV') == 'production') {
  117. $url = "https://mall.huahb.cn/#/admin/clear/alipay?orderSn={$orderSn}";
  118. } else {
  119. $url = "https://mall.huaml.com/#/admin/clear/alipay?orderSn={$orderSn}";
  120. }
  121. $unique = "order_clear_ali_pay_" . $orderSn;
  122. $imgUrl = qrCodeUtil::generateRechargeQrCode($url, $unique);
  123. $imageUrl = imgUtil::groupImg($imgUrl);
  124. util::success(['imgUrl' => $imageUrl]);
  125. }
  126. //获取结账单详情 ssh 20220509
  127. public function actionDetail()
  128. {
  129. $id = Yii::$app->request->get('id');
  130. $clear = ClearClass::getById($id);
  131. if (empty($clear)) {
  132. util::fail('没有结账信息哦');
  133. }
  134. $deadline = isset($clear['deadline']) && !empty($clear['deadline']) ? strtotime($clear['deadline']) : 0;
  135. $now = time();
  136. $remainSecond = bcsub($deadline, $now);
  137. $remainSecond = bcsub($remainSecond, 100);
  138. $remainSecond = $remainSecond > 0 ? $remainSecond : 0;
  139. $clear['remainSecond'] = $remainSecond;
  140. $clear['hasRenew'] = 1;
  141. //如果是虚拟关系,暂时允许花店一键结账
  142. $ghsId = $clear['ghsId'] ?? 0;
  143. $ghs = GhsClass::getById($ghsId, true);
  144. $live = $ghs->live ?? 1;
  145. $clear['live'] = $live;
  146. util::success($clear);
  147. }
  148. public function actionHasWxPay()
  149. {
  150. $id = Yii::$app->request->get('id');
  151. $clear = ClearClass::getById($id, true);
  152. if (empty($clear)) {
  153. util::fail('没有结账信息哦');
  154. }
  155. if ($clear->customShopId != $this->shopId) {
  156. util::fail('不是你的结账单');
  157. }
  158. util::checkRepeatCommit($this->adminId, 4);
  159. //暂时用批发端的写法来顶替一下
  160. $connection = Yii::$app->db;
  161. $transaction = $connection->beginTransaction();
  162. try {
  163. $payWay = 0;
  164. $amount = $clear->actPrice ?? 0;
  165. $customId = $clear->customId ?? 0;
  166. $custom = CustomClass::getLockById($customId);
  167. $shop = $this->shop;
  168. $staff = $this->shopAdmin;
  169. //充值
  170. $return = CustomClass::rechargeBalance($custom, $amount, $shop, $staff, $payWay);
  171. $custom = $return['custom'];
  172. $ghs = $return['ghs'];
  173. //结账(充值销账链路,现金/余额已在 rechargeBalance 处理)
  174. OrderClearClass::confirmClear($clear, $payWay, ['skipCashMoney' => true]);
  175. $clearInfo = OrderClearClass::getLockById($id);
  176. //结账消余额
  177. CustomClass::clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clearInfo);
  178. $transaction->commit();
  179. util::complete();
  180. } catch (\Exception $exception) {
  181. $transaction->rollBack();
  182. Yii::info("失败:" . $exception->getMessage());
  183. util::fail('操作失败');
  184. }
  185. }
  186. }