RechargeController.php 13 KB

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