RechargeController.php 12 KB

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