RechargeController.php 15 KB

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