RechargeController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. \bizHd\recharge\classes\RechargeClass::checkShOrderRecharge($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. $transaction->commit();
  156. util::success($post);
  157. } catch (\Exception $e) {
  158. $transaction->rollBack();
  159. Yii::error("充值失败原因:" . $e->getMessage());
  160. util::fail('充值失败');
  161. }
  162. }
  163. //充值记录 ssh 2021.2.21
  164. public function actionList()
  165. {
  166. $get = Yii::$app->request->get();
  167. $customId = $get['customId'] ?? 0;
  168. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  169. if (empty($custom)) {
  170. util::fail('没有找到客户');
  171. }
  172. if ($custom->shopId != $this->shopId) {
  173. util::fail('不是你的客户');
  174. }
  175. $where = ['shopId' => $this->shopId, 'customId' => $customId];
  176. $list = RechargeClass::getRechargeList($where);
  177. util::success($list);
  178. }
  179. //免费续期活动 ssh 2021.2.21
  180. public function actionRenew()
  181. {
  182. $sj = $this->sj->attributes;
  183. $shopId = $sj['parentShopId'] ?? 0;
  184. $shopName = '';
  185. $rechargeNum = getenv('RECHARGE_NUM') == false ? 268 : getenv('RECHARGE_NUM');
  186. if (!empty($shopId)) {
  187. $shop = ShopClass::getShopInfo($shopId);
  188. if (!empty($shop)) {
  189. $shopName = $shop['shopName'] ?? '';
  190. $num = $shop['rechargeNum'] ?? 0;
  191. $rechargeNum += $num;
  192. }
  193. }
  194. $data = [
  195. 'renew' => getenv('RENEW_PRICE') == false ? 1980 : getenv('RENEW_PRICE'),
  196. 'recharge' => getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE'),
  197. 'shopName' => $shopName,
  198. 'userNum' => $rechargeNum
  199. ];
  200. util::success($data);
  201. }
  202. //续期充值付款 ssh 2021.2.21
  203. public function actionRenewPay()
  204. {
  205. ini_set('date.timezone', 'Asia/Shanghai');
  206. $wxPay = dict::getDict('payWay', 'wxPay');
  207. $price = getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE');
  208. $shopAdmin = $this->shopAdmin->attributes;
  209. $shopAdminName = $shopAdmin['name'] ?? '';
  210. $data = [
  211. 'amount' => $price,
  212. 'payWay' => $wxPay,
  213. 'sjId' => $this->sjId,
  214. 'sjStyle' => SjClass::STYLE_RETAIL,
  215. 'shopId' => $this->shopId,
  216. 'modPrice' => 0,
  217. 'remark' => '免费续期充值',
  218. 'shopAdminId' => $this->shopAdminId,
  219. 'shopAdminName' => $shopAdminName,
  220. ];
  221. $order = RechargeClass::addOrder($data, true);
  222. $orderSn = $order->orderSn;
  223. $name = $order->orderName ?? '充值';
  224. $totalFee = $order->amount;
  225. if (isset($this->shopAdmin['super']) == false || $this->shopAdmin['super'] != 1) {
  226. util::fail('超级管理员才有权限操作');
  227. }
  228. //强制使用小程序的miniOpenId
  229. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  230. if (empty($openId)) {
  231. util::fail('没有找到openId');
  232. }
  233. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  234. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  235. $wxPayType = 1;
  236. $ghsId = Yii::$app->request->get('ghsId', 0);
  237. $ghs = GhsClass::getById($ghsId);
  238. GhsClass::valid($ghs, $this->shopId);
  239. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1&ghsId=' . $ghsId;
  240. //订单30分钟后过期
  241. $now = time();
  242. $expireTime = $now + 1800;
  243. $wx = Yii::getAlias("@vendor/weixin");
  244. require_once($wx . '/lib/WxPay.Api.php');
  245. require_once($wx . '/example/WxPay.JsApiPay.php');
  246. $input = new \WxPayUnifiedOrder();
  247. $input->SetBody($name);
  248. $input->SetOut_trade_no($orderSn);
  249. $input->SetTotal_fee($totalFee * 100);
  250. $input->SetTime_start(date("YmdHis", $now));
  251. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  252. $input->SetTime_expire(date("YmdHis", $expireTime));
  253. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  254. $input->SetTrade_type("JSAPI");
  255. $merchantExtend = WxOpenClass::getWxInfo();
  256. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  257. $input->SetSub_openid($openId);
  258. } else {
  259. $input->SetOpenid($openId);
  260. }
  261. //强制使用小程序的AppId
  262. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  263. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  264. $tools = new \JsApiPay();
  265. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  266. $newParams = json_decode($jsApiParameters, true);
  267. util::success($newParams);
  268. }
  269. //充值送红包 ssh 2021.2.21
  270. public function actionBalance()
  271. {
  272. ini_set('date.timezone', 'Asia/Shanghai');
  273. $get = Yii::$app->request->get();
  274. $hbId = $get['id'] ?? 0;
  275. $ghsId = $get['ghsId'] ?? 0;
  276. $ghsInfo = GhsClass::getById($ghsId, true);
  277. if (empty($ghsInfo)) {
  278. util::fail('没有找到供货商');
  279. }
  280. GhsClass::valid($ghsInfo, $this->shopId);
  281. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  282. $hbInfo = RechargeHbClass::getById($hbId, true);
  283. if (empty($hbInfo)) {
  284. util::fail('无效活动');
  285. }
  286. if ($hbInfo->shopId != $ghsShopId) {
  287. util::fail('供货商与充值红包活动不一致');
  288. }
  289. if ($hbInfo->delStatus == 1) {
  290. util::fail('红包已更新,请重新发起充值');
  291. }
  292. $amount = $hbInfo->amount ?? 0;
  293. if ($amount <= 0) {
  294. util::fail('充值金额有问题');
  295. }
  296. $customId = $ghsInfo['customId'] ?? 0;
  297. $custom = CustomClass::getById($customId, true);
  298. $customName = $custom->name ?? '';
  299. $wxPay = dict::getDict('payWay', 'wxPay');
  300. $shopAdmin = $this->shopAdmin->attributes;
  301. $shopAdminName = $shopAdmin['name'] ?? '';
  302. $data = [
  303. 'amount' => $amount,
  304. 'payWay' => $wxPay,
  305. 'sjId' => $this->sjId,
  306. 'sjStyle' => SjClass::STYLE_RETAIL,
  307. 'shopId' => $this->shopId,
  308. 'modPrice' => 0,
  309. 'remark' => '',
  310. 'shopAdminId' => $this->shopAdminId,
  311. 'shopAdminName' => $shopAdminName,
  312. 'hbId' => $hbInfo->id ?? 0,
  313. 'hbAmount' => $hbInfo->hbAmount ?? 0,
  314. 'hbNum' => $hbInfo->num,
  315. 'totalHb' => $hbInfo->totalHb,
  316. 'valid' => $hbInfo->valid,
  317. 'miniExpend' => $hbInfo->miniExpend,
  318. 'ghsId' => $ghsId,
  319. 'ghsShopId' => $ghsShopId,
  320. 'customId' => $customId,
  321. 'customName' => $customName,
  322. ];
  323. $order = RechargeClass::addOrder($data, true);
  324. $orderSn = $order->orderSn;
  325. $name = $order->orderName ?? '充值';
  326. $totalFee = $order->amount;
  327. //强制使用小程序的miniOpenId
  328. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  329. if (empty($openId)) {
  330. util::fail('没有找到openId');
  331. }
  332. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  333. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  334. $wxPayType = 1;
  335. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  336. //订单30分钟后过期
  337. $now = time();
  338. $expireTime = $now + 1800;
  339. $wx = Yii::getAlias("@vendor/weixin");
  340. require_once($wx . '/lib/WxPay.Api.php');
  341. require_once($wx . '/example/WxPay.JsApiPay.php');
  342. $input = new \WxPayUnifiedOrder();
  343. $input->SetBody($name);
  344. $input->SetOut_trade_no($orderSn);
  345. $input->SetTotal_fee($totalFee * 100);
  346. $input->SetTime_start(date("YmdHis", $now));
  347. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  348. $input->SetTime_expire(date("YmdHis", $expireTime));
  349. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  350. $input->SetTrade_type("JSAPI");
  351. $merchantExtend = WxOpenClass::getWxInfo();
  352. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  353. $input->SetSub_openid($openId);
  354. } else {
  355. $input->SetOpenid($openId);
  356. }
  357. //强制使用小程序的AppId
  358. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  359. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  360. $tools = new \JsApiPay();
  361. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  362. $newParams = json_decode($jsApiParameters, true);
  363. util::success($newParams);
  364. }
  365. //续期成功 ssh 2021.2.21
  366. public function actionRenewSuccess()
  367. {
  368. $sj = $this->sj->attributes;
  369. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  370. util::success(['deadline' => $deadline]);
  371. }
  372. }