ClearController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. $salt = Yii::$app->request->get('salt');
  104. $info = ClearClass::getById($id);
  105. if (empty($info)) {
  106. util::fail('没有结账单');
  107. }
  108. if (empty($salt)) {
  109. if (!isset($info['customShopId']) || $info['customShopId'] != $this->shopId) {
  110. //util::fail('不是你的结账信息');
  111. }
  112. } else {
  113. if (!isset($info['salt']) || $info['salt'] != $salt) {
  114. util::fail('不是你的结账信息哦');
  115. }
  116. }
  117. $respond = ClearClass::getCgInfo($info);
  118. util::success($respond);
  119. }
  120. //获取支付宝付款码 ssh 20240713
  121. public function actionGetAliPayClearCode()
  122. {
  123. $get = Yii::$app->request->get();
  124. $orderSn = $get['orderSn'] ?? 0;
  125. $info = ClearClass::getByCondition(['orderSn' => $orderSn], true);
  126. if (empty($info)) {
  127. util::fail('没有找到结账单');
  128. }
  129. if (getenv('YII_ENV') == 'production') {
  130. $url = "https://mall.huahb.cn/#/admin/clear/alipay?orderSn={$orderSn}";
  131. } else {
  132. $url = "https://mall.huaml.com/#/admin/clear/alipay?orderSn={$orderSn}";
  133. }
  134. $unique = "order_clear_ali_pay_" . $orderSn;
  135. $imgUrl = qrCodeUtil::generateRechargeQrCode($url, $unique);
  136. $imageUrl = imgUtil::groupImg($imgUrl);
  137. util::success(['imgUrl' => $imageUrl]);
  138. }
  139. //获取结账单详情 ssh 20220509
  140. public function actionDetail()
  141. {
  142. $id = Yii::$app->request->get('id');
  143. $clear = ClearClass::getById($id);
  144. if (empty($clear)) {
  145. util::fail('没有结账信息哦');
  146. }
  147. $deadline = isset($clear['deadline']) && !empty($clear['deadline']) ? strtotime($clear['deadline']) : 0;
  148. $now = time();
  149. $remainSecond = bcsub($deadline, $now);
  150. $remainSecond = bcsub($remainSecond, 100);
  151. $remainSecond = $remainSecond > 0 ? $remainSecond : 0;
  152. $clear['remainSecond'] = $remainSecond;
  153. $clear['hasRenew'] = 1;
  154. //如果是虚拟关系,暂时允许花店一键结账
  155. $ghsId = $clear['ghsId'] ?? 0;
  156. $ghs = GhsClass::getById($ghsId, true);
  157. $live = $ghs->live ?? 1;
  158. $clear['live'] = $live;
  159. util::success($clear);
  160. }
  161. public function actionHasWxPay()
  162. {
  163. $id = Yii::$app->request->get('id');
  164. $clear = ClearClass::getById($id, true);
  165. if (empty($clear)) {
  166. util::fail('没有结账信息哦');
  167. }
  168. if ($clear->customShopId != $this->shopId) {
  169. util::fail('不是你的结账单');
  170. }
  171. util::checkRepeatCommit($this->adminId, 4);
  172. //暂时用批发端的写法来顶替一下
  173. $connection = Yii::$app->db;
  174. $transaction = $connection->beginTransaction();
  175. try {
  176. $payWay = 0;
  177. $amount = $clear->actPrice ?? 0;
  178. $customId = $clear->customId ?? 0;
  179. $custom = CustomClass::getLockById($customId);
  180. $shop = $this->shop;
  181. $staff = $this->shopAdmin;
  182. //充值
  183. $return = CustomClass::rechargeBalance($custom, $amount, $shop, $staff, $payWay);
  184. $custom = $return['custom'];
  185. $ghs = $return['ghs'];
  186. //结账(充值销账链路,现金/余额已在 rechargeBalance 处理)
  187. OrderClearClass::confirmClear($clear, $payWay, ['skipCashMoney' => true]);
  188. $clearInfo = OrderClearClass::getLockById($id);
  189. //结账消余额
  190. CustomClass::clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clearInfo);
  191. $transaction->commit();
  192. util::complete();
  193. } catch (\Exception $exception) {
  194. $transaction->rollBack();
  195. Yii::info("失败:" . $exception->getMessage());
  196. util::fail('操作失败');
  197. }
  198. }
  199. }