RechargeController.php 15 KB

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