RechargeController.php 14 KB

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