RechargeController.php 13 KB

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