RechargeController.php 12 KB

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