RechargeController.php 12 KB

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