RechargeController.php 13 KB

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