RechargeController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\custom\classes\HdClass;
  5. use bizHd\merchant\classes\ShopClass;
  6. use bizHd\recharge\classes\RechargeClass;
  7. use bizHd\wx\classes\WxOpenClass;
  8. use common\components\dict;
  9. use common\components\noticeUtil;
  10. use common\components\orderSn;
  11. use common\components\util;
  12. use Yii;
  13. use common\components\lakala\Lakala;
  14. class RechargeController extends BaseController
  15. {
  16. public $guestAccess = [];
  17. //取消充值订单 ssh 20250721
  18. public function actionCancel()
  19. {
  20. $get = Yii::$app->request->get();
  21. $id = $get['id'] ?? 0;
  22. $recharge = \bizMall\recharge\classes\RechargeClass::getLockById($id);
  23. if (empty($recharge)) {
  24. util::fail('没有找到充值记录');
  25. }
  26. if ($recharge->userId != $this->userId) {
  27. util::fail('不是你的充值单');
  28. }
  29. if ($recharge->shopId != $this->shopId) {
  30. util::fail('不是你的充值记录');
  31. }
  32. if ($recharge->status == 1) {
  33. util::fail('已充值成功,不能取消');
  34. }
  35. if ($recharge->status == 2) {
  36. util::fail('已经取消过了');
  37. }
  38. $shop = $this->shop;
  39. \bizMall\recharge\classes\RechargeClass::cancelMyRecharge($recharge, $shop);
  40. util::complete('取消成功');
  41. }
  42. /**
  43. * 商城充值单列表:分页数据 + 各状态数量(供顶部 Tab 展示)。
  44. * 数量字段:all / waitPay(待付款) / recharged(已充值) / cancelled(已取消);
  45. * 统计条件与列表同范围,但不带当前 status,避免切 Tab 后其它数量变 0。
  46. */
  47. public function actionList()
  48. {
  49. $get = Yii::$app->request->get();
  50. $hdId = $get['hdId'] ?? 0;
  51. $status = $get['status'] ?? -1;
  52. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  53. $where = ['mainId' => $this->mainId];
  54. if (!empty($hd)) {
  55. if ($hd->shopId != $this->shopId) {
  56. util::fail('不是你的门店');
  57. }
  58. $customId = $hd->customId ?? 0;
  59. $where['customId'] = $customId;
  60. }
  61. // Tab 数量与列表共用业务范围,统计时去掉 status
  62. $countWhere = $where;
  63. if ($status > -1) {
  64. $where['status'] = $status;
  65. }
  66. $list = \bizMall\recharge\classes\RechargeClass::getRechargeList($where);
  67. $statusCounts = \bizMall\recharge\classes\RechargeClass::getStatusCounts($countWhere);
  68. $list['all'] = $statusCounts['all'];
  69. $list['waitPay'] = $statusCounts['waitPay'];
  70. $list['recharged'] = $statusCounts['recharged'];
  71. $list['cancelled'] = $statusCounts['cancelled'];
  72. util::success($list);
  73. }
  74. //用微信支付进行充值 ssh 20250709
  75. public function actionWxPayRecharge()
  76. {
  77. $post = Yii::$app->request->post();
  78. $id = $post['id'] ?? '';
  79. //避免重复提交
  80. util::checkRepeatCommit($id, 8);
  81. $recharge = RechargeClass::getById($id, true);
  82. if (empty($recharge)) {
  83. util::fail('没有充值单');
  84. }
  85. $userId = $this->userId;
  86. $shopId = $this->shopId;
  87. if ($recharge->userId != $userId || $recharge->shopId != $shopId) {
  88. util::fail('不是你的充值单');
  89. }
  90. $shop = $this->shop;
  91. $user = $this->user;
  92. if (empty($shop)) {
  93. util::fail('没有门店信息');
  94. }
  95. $shop = $this->shop;
  96. $orderSn = $recharge->orderSn;
  97. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  98. if (empty($openId)) {
  99. $openId = !empty($user['miniOpenId']) ? $user['miniOpenId'] : '';
  100. }
  101. if (empty($openId)) {
  102. util::fail('没有找到openId');
  103. }
  104. //已经发起过支付,需要更换单号
  105. if ($recharge->payRequest == 1 || $recharge->payRequest == 2) {
  106. $closeOrderSn = $orderSn;
  107. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  108. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  109. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  110. $params = [
  111. 'appid' => 'OP00002119',
  112. 'serial_no' => '018b08cfddbd',
  113. 'merchant_no' => $shop->lklSjNo,
  114. 'term_no' => $shop->lklScanTermNo,
  115. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  116. 'lklCertificatePath' => $lklCertificatePath,
  117. ];
  118. $laResource = new Lakala($params);
  119. $closeParams = ['orderSn' => $closeOrderSn];
  120. if ($recharge->payRequest == 1) {
  121. //聚合支付关单
  122. $response = $laResource->close($closeParams);
  123. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  124. $msg = $response['msg'] ?? '';
  125. noticeUtil::push('重点注意,充值换单号没有成功,单号:' . $closeOrderSn . ',关单没有成功,' . $msg, '15280215347');
  126. util::fail('出错了哦,请重新操作哈');
  127. }
  128. } else {
  129. //收银台关单
  130. $response = $laResource->cashClose($closeParams);
  131. if (!isset($response['code']) || $response['code'] != '000000') {
  132. $msg = $response['msg'] ?? '';
  133. noticeUtil::push('重点注意,充值换单,收银台的单号:' . $closeOrderSn . ',关单没有成功,' . $msg, '15280215347');
  134. util::fail('出错了哦,请重新操作呢');
  135. }
  136. }
  137. //生成新的订单,避免重复
  138. $orderSn = orderSn::getRechargeSn();
  139. $recharge->orderSn = $orderSn;
  140. $recharge->save();
  141. }
  142. $name = '充值销账,单号' . $orderSn;
  143. $totalFee = $recharge->amount ?? 0;
  144. $orderId = $recharge->id;
  145. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  146. $sjExtend = WxOpenClass::getMallWxInfo();
  147. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  148. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  149. $params = [
  150. 'appid' => 'OP00002119',
  151. 'serial_no' => '018b08cfddbd',
  152. 'merchant_no' => $shop->lklSjNo,
  153. 'term_no' => $shop->lklScanTermNo,
  154. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  155. 'lklCertificatePath' => $lklCertificatePath,
  156. ];
  157. $laResource = new Lakala($params);
  158. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  159. $wxParams = [
  160. 'orderSn' => $orderSn,
  161. 'amount' => $totalFee,
  162. 'capitalType' => $capitalType,
  163. 'notifyUrl' => $notifyUrl,
  164. 'wxAppId' => $sjExtend['miniAppId'],
  165. 'openId' => $openId,
  166. 'subject' => $name,
  167. 'couponId' => 0,
  168. ];
  169. $response = $laResource->driveWxPay($wxParams);
  170. //0没有唤起过第三方支付,1唤起过聚合支付,2唤起过收银台。主要针对拉卡拉支付。
  171. $recharge->payRequest = 1;
  172. $recharge->save();
  173. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  174. $errMsg = $response['msg'] ?? '';
  175. noticeUtil::push($errMsg, '15280215347');
  176. util::fail('充值失败');
  177. }
  178. $respData = $response['resp_data'] ?? [];
  179. $accRespFields = $respData['acc_resp_fields'] ?? [];
  180. if (empty($accRespFields)) {
  181. util::fail('充值失败,没有找到支付数据');
  182. }
  183. $appId = $accRespFields['app_id'] ?? '';
  184. $nonceStr = $accRespFields['nonce_str'] ?? '';
  185. $paySign = $accRespFields['pay_sign'] ?? '';
  186. $package = $accRespFields['package'] ?? '';
  187. $signType = $accRespFields['sign_type'] ?? '';
  188. $timeStamp = $accRespFields['time_stamp'] ?? '';
  189. $new = [
  190. 'id' => $orderId,
  191. 'appId' => $appId,
  192. 'nonceStr' => $nonceStr,
  193. 'paySign' => $paySign,
  194. 'package' => $package,
  195. 'signType' => $signType,
  196. 'timeStamp' => $timeStamp,
  197. 'orderSn' => $orderSn,
  198. ];
  199. util::success($new);
  200. }
  201. //获取充值订单的详情 ssh 20250709
  202. public function actionGetDetail()
  203. {
  204. $get = Yii::$app->request->get();
  205. $id = $get['id'] ?? 0;
  206. $recharge = RechargeClass::getById($id, true);
  207. if (empty($recharge)) {
  208. util::fail('没有找到充值订单');
  209. }
  210. $userId = $this->userId;
  211. $shopId = $this->shopId;
  212. if ($recharge->userId != $userId || $recharge->shopId != $shopId) {
  213. util::fail('不是你的单');
  214. }
  215. util::success($recharge);
  216. }
  217. //充值 ssh 20240507
  218. public function actionRecharge()
  219. {
  220. ini_set('date.timezone', 'Asia/Shanghai');
  221. $post = Yii::$app->request->post();
  222. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  223. $actPrice = $post['actPrice'] ?? 0;
  224. $totalFee = $actPrice;
  225. $shopId = $this->shopId;
  226. if (empty($shopId)) {
  227. util::fail('没有找到花店');
  228. }
  229. $shop = ShopClass::getById($shopId, true);
  230. if (empty($shop)) {
  231. util::fail('没有找到花店呢');
  232. }
  233. $mainId = $this->mainId;
  234. $userId = $this->userId;
  235. $salt = $post['salt'] ?? '';
  236. $hd = $this->hd;
  237. if (empty($hd)) {
  238. util::fail('没有找到花店哦!');
  239. }
  240. if (empty($salt)) {
  241. } else {
  242. if ($hd->salt != $salt) {
  243. util::fail('不是你的花店哦');
  244. }
  245. }
  246. $hdId = $hd->id;
  247. $customId = $hd->customId ?? 0;
  248. $custom = CustomClass::getById($customId, true);
  249. if (empty($custom)) {
  250. util::fail('花店客户缺失');
  251. }
  252. $customName = $custom->name;
  253. $hdName = $hd->name;
  254. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  255. $orderSn = orderSn::getRechargeSn();
  256. $rechargeData = [
  257. 'orderSn' => $orderSn,
  258. 'modPrice' => 0,
  259. 'payWay' => $payWay,
  260. 'returnCode' => '',
  261. 'shopId' => $shopId,
  262. 'mainId' => $mainId,
  263. 'hdId' => $hdId,
  264. 'hdName' => $hdName,
  265. 'amount' => $actPrice,
  266. 'onlinePay' => 1,
  267. 'payStatus' => 0,
  268. 'status' => 0,
  269. 'customId' => $customId,
  270. 'customName' => $customName,
  271. 'userId' => $userId,
  272. ];
  273. $respond = RechargeClass::addRecharge($rechargeData);
  274. $rechargeId = $respond->id;
  275. $respond->payRequest = 1;
  276. $respond->save();
  277. $subject = '线上充值,单号' . $orderSn;
  278. if ($payWay == 0) {
  279. $openId = !empty($post['currentMiniOpenId']) ? $post['currentMiniOpenId'] : '';
  280. if (empty($openId)) {
  281. if (!empty($this->admin)) {
  282. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  283. $openId = $this->admin->miniOpenId;
  284. }
  285. }
  286. }
  287. $merchantExtend = WxOpenClass::getMallWxInfo();
  288. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  289. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  290. $params = [
  291. 'appid' => 'OP00002119',
  292. 'serial_no' => '018b08cfddbd',
  293. 'merchant_no' => $shop->lklSjNo,
  294. 'term_no' => $shop->lklScanTermNo,
  295. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  296. 'lklCertificatePath' => $lklCertificatePath,
  297. ];
  298. $laResource = new Lakala($params);
  299. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  300. $wxParams = [
  301. 'orderSn' => $orderSn,
  302. 'amount' => $totalFee,
  303. 'capitalType' => $capitalType,
  304. 'notifyUrl' => $notifyUrl,
  305. 'wxAppId' => $merchantExtend['miniAppId'],
  306. 'openId' => $openId,
  307. 'subject' => $subject,
  308. 'couponId' => 0,
  309. ];
  310. $response = $laResource->driveWxPay($wxParams);
  311. if ($response['code'] != 'BBS00000') {
  312. $errMsg = $response['msg'] ?? '';
  313. noticeUtil::push('编号666993:' . $errMsg, '15280215347');
  314. util::fail('支付失败');
  315. }
  316. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  317. $appId = $newParams['app_id'] ?? '';
  318. $nonceStr = $newParams['nonce_str'] ?? '';
  319. $paySign = $newParams['pay_sign'] ?? '';
  320. $package = $newParams['package'] ?? '';
  321. $signType = $newParams['sign_type'] ?? '';
  322. $timeStamp = $newParams['time_stamp'] ?? '';
  323. $new = [
  324. 'id' => $rechargeId,
  325. 'appId' => $appId,
  326. 'nonceStr' => $nonceStr,
  327. 'paySign' => $paySign,
  328. 'package' => $package,
  329. 'signType' => $signType,
  330. 'timeStamp' => $timeStamp,
  331. 'orderSn' => $orderSn,
  332. ];
  333. util::success($new);
  334. } elseif ($payWay == 1) {
  335. util::fail('开发中');
  336. } else {
  337. util::fail('暂不支持的付款方式');
  338. }
  339. }
  340. }