RechargeController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\recharge\classes\RechargeClass;
  5. use biz\recharge\classes\RechargeHbClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\sj\classes\SjClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizHd\custom\classes\HdClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\order\classes\SettleClass;
  12. use bizHd\wx\classes\WxOpenClass;
  13. use bizMall\wx\classes\WxMiniClass;
  14. use common\components\dict;
  15. use common\components\imgUtil;
  16. use common\components\miniUtil;
  17. use common\components\orderSn;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use Yii;
  21. class RechargeController extends BaseController
  22. {
  23. //获取商城端充值页和订单页小程序短链接
  24. public function actionGetMallRechargeShortLink()
  25. {
  26. $get = Yii::$app->request->get();
  27. if (getenv('YII_ENV') == 'dev') {
  28. $ip = Yii::$app->request->getUserIP();
  29. if ($ip == '127.0.0.1') {
  30. util::success(['rechargeAccountLink' => 'www.google.com', 'orderLink' => 'www.google.com']);
  31. }
  32. }
  33. $orderId = $get['orderId'] ?? 0;
  34. $account = $this->shopId;
  35. $page = 'pages/member/recharge?account=' . $account;
  36. $ret = WxMiniClass::getShortLink($page);
  37. $link = $ret['link'] ?? '';
  38. $orderLink = '';
  39. if (!empty($orderId)) {
  40. $order = OrderClass::getById($orderId, true);
  41. if ($order->mainId != $this->mainId) {
  42. util::fail('访问错误');
  43. }
  44. $orderPage = 'pages/order/detail?account=' . $account . '&id=' . $orderId;
  45. $res = WxMiniClass::getShortLink($orderPage);
  46. $orderLink = $res['link'] ?? '';
  47. }
  48. util::success(['rechargeAccountLink' => $link, 'orderLink' => $orderLink]);
  49. }
  50. //获取扫码充值的小程序码 ssh 20250307
  51. public function actionGetRechargeCode()
  52. {
  53. $merchant = WxOpenClass::getMallWxInfo();
  54. $page = 'pages/member/recharge';
  55. $ptStyle = dict::getDict('ptStyle', 'mall');
  56. $scene = 'a=' . $this->shopAdminId . '&s=' . $this->shopId;
  57. $imgUrl = miniUtil::generateUnlimitedRechargeMiniCode($merchant, $page, $scene, $ptStyle);
  58. $prefix = imgUtil::getPrefix();
  59. $titleBase64 = stringUtil::ossBase64('扫码充值');
  60. $memberUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  61. $memberMiniCodeBase64 = stringUtil::ossBase64($imgUrl);
  62. $memberUrl .= '/watermark,image_' . $memberMiniCodeBase64 . ',g_north,x_0,y_40';
  63. $memberUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  64. $respond = ['imgUrl' => $memberUrl];
  65. util::success($respond);
  66. }
  67. //手动给客户充值 ssh 20250301
  68. public function actionManRecharge()
  69. {
  70. $post = Yii::$app->request->post();
  71. $shopAdmin = $this->shopAdmin;
  72. if ($shopAdmin->super != 1) {
  73. util::fail('超管才能充值');
  74. }
  75. $staffId = $shopAdmin->id;
  76. $staffName = $shopAdmin->name;
  77. $connection = Yii::$app->db;
  78. $transaction = $connection->beginTransaction();
  79. try {
  80. $payWay = $post['payWay'] ?? 0;
  81. $amount = $post['amount'] ?? 0;
  82. $remark = $post['remark'] ?? '';
  83. $customId = $post['customId'] ?? 0;
  84. $shOrderId = $post['shOrderId'] ?? 0;
  85. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  86. if (empty($custom)) {
  87. util::fail('没有找到客户');
  88. }
  89. //避免重复提交
  90. util::checkRepeatCommit($customId, 4);
  91. $shOrderSn = '';
  92. if (!empty($shOrderId)) {
  93. $shOrder = OrderClass::getById($shOrderId, true);
  94. if ($shOrder->forward != 1) {
  95. util::fail('不是售后付款单');
  96. }
  97. if ($shOrder->mainId != $this->mainId) {
  98. util::fail('不是你的单');
  99. }
  100. //判断是否充值过了
  101. OrderClass::shOrderRecharge($shOrder);
  102. $shOrderSn = $shOrder->orderSn ?? '';
  103. }
  104. $refundOrderId = $post['refundOrderId'] ?? 0;
  105. $refundOrderSn = '';
  106. if (!empty($refundOrderId)) {
  107. //线下订单售后,充值
  108. $myOrder = OrderClass::getById($refundOrderId, true);
  109. if ($myOrder->mainId != $this->mainId) {
  110. util::fail('不是你的订单');
  111. }
  112. $refundOrderSn = $myOrder->orderSn ?? '';
  113. }
  114. $customName = $custom->name;
  115. $userId = $custom->userId ?? 0;
  116. $hdId = $custom->hdId;
  117. $hd = HdClass::getById($hdId, true);
  118. if (empty($hd)) {
  119. util::fail('花店信息缺失,编号9886');
  120. }
  121. $hdName = $hd->name;
  122. $shopId = $this->shopId;
  123. $mainId = $this->mainId;
  124. $orderSn = orderSn::getRechargeSn();
  125. $rechargeData = [
  126. 'orderSn' => $orderSn,
  127. 'modPrice' => 1,
  128. 'payWay' => $payWay,
  129. 'shopId' => $shopId,
  130. 'mainId' => $mainId,
  131. 'hdId' => $hdId,
  132. 'hdName' => $hdName,
  133. 'onlinePay' => 1,
  134. 'amount' => $amount,
  135. 'staffId' => $staffId,
  136. 'staffName' => $staffName,
  137. 'payStatus' => 0,
  138. 'payTime' => '0000-00-00 00:00:00',
  139. 'status' => 0,
  140. 'remark' => $remark,
  141. 'customId' => $customId,
  142. 'customName' => $customName,
  143. 'userId' => $userId,
  144. 'shOrderId' => $shOrderId,
  145. 'shOrderSn' => $shOrderSn,
  146. 'refundOrderId' => $refundOrderId,
  147. 'refundOrderSn' => $refundOrderSn,
  148. ];
  149. $respond = \bizHd\recharge\classes\RechargeClass::addRecharge($rechargeData);
  150. $id = $respond->id;
  151. $recharge = RechargeClass::getLockById($id);
  152. $params = ['onlinePay' => 1, 'side' => 0];
  153. //充值成功并且销账
  154. \bizHd\recharge\classes\RechargeClass::complete($recharge, $payWay, $params);
  155. if (!empty($shOrderId)) {
  156. //售后付款标记为充值了
  157. OrderClass::shOrderRecharge($shOrder, true);
  158. }
  159. $transaction->commit();
  160. util::success($post);
  161. } catch (\Exception $e) {
  162. $transaction->rollBack();
  163. Yii::error("充值失败原因:" . $e->getMessage());
  164. util::fail('充值失败');
  165. }
  166. }
  167. //充值记录 ssh 2021.2.21
  168. public function actionList()
  169. {
  170. $get = Yii::$app->request->get();
  171. $customId = $get['customId'] ?? 0;
  172. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  173. if (empty($custom)) {
  174. util::fail('没有找到客户');
  175. }
  176. if ($custom->shopId != $this->shopId) {
  177. util::fail('不是你的客户');
  178. }
  179. $where = ['shopId' => $this->shopId, 'customId' => $customId];
  180. $list = RechargeClass::getRechargeList($where);
  181. util::success($list);
  182. }
  183. //免费续期活动 ssh 2021.2.21
  184. public function actionRenew()
  185. {
  186. $sj = $this->sj->attributes;
  187. $shopId = $sj['parentShopId'] ?? 0;
  188. $shopName = '';
  189. $rechargeNum = getenv('RECHARGE_NUM') == false ? 268 : getenv('RECHARGE_NUM');
  190. if (!empty($shopId)) {
  191. $shop = ShopClass::getShopInfo($shopId);
  192. if (!empty($shop)) {
  193. $shopName = $shop['shopName'] ?? '';
  194. $num = $shop['rechargeNum'] ?? 0;
  195. $rechargeNum += $num;
  196. }
  197. }
  198. $data = [
  199. 'renew' => getenv('RENEW_PRICE') == false ? 1980 : getenv('RENEW_PRICE'),
  200. 'recharge' => getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE'),
  201. 'shopName' => $shopName,
  202. 'userNum' => $rechargeNum
  203. ];
  204. util::success($data);
  205. }
  206. //续期充值付款 ssh 2021.2.21
  207. public function actionRenewPay()
  208. {
  209. ini_set('date.timezone', 'Asia/Shanghai');
  210. $wxPay = dict::getDict('payWay', 'wxPay');
  211. $price = getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE');
  212. $shopAdmin = $this->shopAdmin->attributes;
  213. $shopAdminName = $shopAdmin['name'] ?? '';
  214. $data = [
  215. 'amount' => $price,
  216. 'payWay' => $wxPay,
  217. 'sjId' => $this->sjId,
  218. 'sjStyle' => SjClass::STYLE_RETAIL,
  219. 'shopId' => $this->shopId,
  220. 'modPrice' => 0,
  221. 'remark' => '免费续期充值',
  222. 'shopAdminId' => $this->shopAdminId,
  223. 'shopAdminName' => $shopAdminName,
  224. ];
  225. $order = RechargeClass::addOrder($data, true);
  226. $orderSn = $order->orderSn;
  227. $name = $order->orderName ?? '充值';
  228. $totalFee = $order->amount;
  229. if (isset($this->shopAdmin['super']) == false || $this->shopAdmin['super'] != 1) {
  230. util::fail('超级管理员才有权限操作');
  231. }
  232. //强制使用小程序的miniOpenId
  233. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  234. if (empty($openId)) {
  235. util::fail('没有找到openId');
  236. }
  237. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  238. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  239. $wxPayType = 1;
  240. $ghsId = Yii::$app->request->get('ghsId', 0);
  241. $ghs = GhsClass::getById($ghsId);
  242. GhsClass::valid($ghs, $this->shopId);
  243. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1&ghsId=' . $ghsId;
  244. //订单30分钟后过期
  245. $now = time();
  246. $expireTime = $now + 1800;
  247. $wx = Yii::getAlias("@vendor/weixin");
  248. require_once($wx . '/lib/WxPay.Api.php');
  249. require_once($wx . '/example/WxPay.JsApiPay.php');
  250. $input = new \WxPayUnifiedOrder();
  251. $input->SetBody($name);
  252. $input->SetOut_trade_no($orderSn);
  253. $input->SetTotal_fee($totalFee * 100);
  254. $input->SetTime_start(date("YmdHis", $now));
  255. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  256. $input->SetTime_expire(date("YmdHis", $expireTime));
  257. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  258. $input->SetTrade_type("JSAPI");
  259. $merchantExtend = WxOpenClass::getWxInfo();
  260. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  261. $input->SetSub_openid($openId);
  262. } else {
  263. $input->SetOpenid($openId);
  264. }
  265. //强制使用小程序的AppId
  266. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  267. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  268. $tools = new \JsApiPay();
  269. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  270. $newParams = json_decode($jsApiParameters, true);
  271. util::success($newParams);
  272. }
  273. //充值送红包 ssh 2021.2.21
  274. public function actionBalance()
  275. {
  276. ini_set('date.timezone', 'Asia/Shanghai');
  277. $get = Yii::$app->request->get();
  278. $hbId = $get['id'] ?? 0;
  279. $ghsId = $get['ghsId'] ?? 0;
  280. $ghsInfo = GhsClass::getById($ghsId, true);
  281. if (empty($ghsInfo)) {
  282. util::fail('没有找到供货商');
  283. }
  284. GhsClass::valid($ghsInfo, $this->shopId);
  285. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  286. $hbInfo = RechargeHbClass::getById($hbId, true);
  287. if (empty($hbInfo)) {
  288. util::fail('无效活动');
  289. }
  290. if ($hbInfo->shopId != $ghsShopId) {
  291. util::fail('供货商与充值红包活动不一致');
  292. }
  293. if ($hbInfo->delStatus == 1) {
  294. util::fail('红包已更新,请重新发起充值');
  295. }
  296. $amount = $hbInfo->amount ?? 0;
  297. if ($amount <= 0) {
  298. util::fail('充值金额有问题');
  299. }
  300. $customId = $ghsInfo['customId'] ?? 0;
  301. $custom = CustomClass::getById($customId, true);
  302. $customName = $custom->name ?? '';
  303. $wxPay = dict::getDict('payWay', 'wxPay');
  304. $shopAdmin = $this->shopAdmin->attributes;
  305. $shopAdminName = $shopAdmin['name'] ?? '';
  306. $data = [
  307. 'amount' => $amount,
  308. 'payWay' => $wxPay,
  309. 'sjId' => $this->sjId,
  310. 'sjStyle' => SjClass::STYLE_RETAIL,
  311. 'shopId' => $this->shopId,
  312. 'modPrice' => 0,
  313. 'remark' => '',
  314. 'shopAdminId' => $this->shopAdminId,
  315. 'shopAdminName' => $shopAdminName,
  316. 'hbId' => $hbInfo->id ?? 0,
  317. 'hbAmount' => $hbInfo->hbAmount ?? 0,
  318. 'hbNum' => $hbInfo->num,
  319. 'totalHb' => $hbInfo->totalHb,
  320. 'valid' => $hbInfo->valid,
  321. 'miniExpend' => $hbInfo->miniExpend,
  322. 'ghsId' => $ghsId,
  323. 'ghsShopId' => $ghsShopId,
  324. 'customId' => $customId,
  325. 'customName' => $customName,
  326. ];
  327. $order = RechargeClass::addOrder($data, true);
  328. $orderSn = $order->orderSn;
  329. $name = $order->orderName ?? '充值';
  330. $totalFee = $order->amount;
  331. //强制使用小程序的miniOpenId
  332. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  333. if (empty($openId)) {
  334. util::fail('没有找到openId');
  335. }
  336. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  337. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  338. $wxPayType = 1;
  339. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  340. //订单30分钟后过期
  341. $now = time();
  342. $expireTime = $now + 1800;
  343. $wx = Yii::getAlias("@vendor/weixin");
  344. require_once($wx . '/lib/WxPay.Api.php');
  345. require_once($wx . '/example/WxPay.JsApiPay.php');
  346. $input = new \WxPayUnifiedOrder();
  347. $input->SetBody($name);
  348. $input->SetOut_trade_no($orderSn);
  349. $input->SetTotal_fee($totalFee * 100);
  350. $input->SetTime_start(date("YmdHis", $now));
  351. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  352. $input->SetTime_expire(date("YmdHis", $expireTime));
  353. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  354. $input->SetTrade_type("JSAPI");
  355. $merchantExtend = WxOpenClass::getWxInfo();
  356. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  357. $input->SetSub_openid($openId);
  358. } else {
  359. $input->SetOpenid($openId);
  360. }
  361. //强制使用小程序的AppId
  362. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  363. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  364. $tools = new \JsApiPay();
  365. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  366. $newParams = json_decode($jsApiParameters, true);
  367. util::success($newParams);
  368. }
  369. //续期成功 ssh 2021.2.21
  370. public function actionRenewSuccess()
  371. {
  372. $sj = $this->sj->attributes;
  373. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  374. util::success(['deadline' => $deadline]);
  375. }
  376. }