RechargeController.php 12 KB

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