RechargeController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. 'returnCode' => '',
  70. 'shopId' => $shopId,
  71. 'mainId' => $mainId,
  72. 'hdId' => $hdId,
  73. 'hdName' => $hdName,
  74. 'onlinePay' => 1,
  75. 'amount' => $amount,
  76. 'balance' => '',
  77. 'staffId' => $staffId,
  78. 'staffName' => $staffName,
  79. 'payStatus' => 0,
  80. 'payTime' => '0000-00-00 00:00:00',
  81. 'status' => 0,
  82. 'remark' => $remark,
  83. 'customId' => $customId,
  84. 'customName' => $customName,
  85. ];
  86. $respond = \bizHd\recharge\classes\RechargeClass::addRecharge($rechargeData);
  87. util::success($post);
  88. }
  89. //充值记录 ssh 2021.2.21
  90. public function actionList()
  91. {
  92. $where = ['shopId' => $this->shopId, 'payStatus' => RechargeClass::PAY_STATUS_HAS_PAY];
  93. $list = RechargeClass::getRechargeList($where);
  94. util::success($list);
  95. }
  96. //免费续期活动 ssh 2021.2.21
  97. public function actionRenew()
  98. {
  99. $sj = $this->sj->attributes;
  100. $shopId = $sj['parentShopId'] ?? 0;
  101. $shopName = '';
  102. $rechargeNum = getenv('RECHARGE_NUM') == false ? 268 : getenv('RECHARGE_NUM');
  103. if (!empty($shopId)) {
  104. $shop = ShopClass::getShopInfo($shopId);
  105. if (!empty($shop)) {
  106. $shopName = $shop['shopName'] ?? '';
  107. $num = $shop['rechargeNum'] ?? 0;
  108. $rechargeNum += $num;
  109. }
  110. }
  111. $data = [
  112. 'renew' => getenv('RENEW_PRICE') == false ? 1980 : getenv('RENEW_PRICE'),
  113. 'recharge' => getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE'),
  114. 'shopName' => $shopName,
  115. 'userNum' => $rechargeNum
  116. ];
  117. util::success($data);
  118. }
  119. //续期充值付款 ssh 2021.2.21
  120. public function actionRenewPay()
  121. {
  122. ini_set('date.timezone', 'Asia/Shanghai');
  123. $wxPay = dict::getDict('payWay', 'wxPay');
  124. $price = getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE');
  125. $shopAdmin = $this->shopAdmin->attributes;
  126. $shopAdminName = $shopAdmin['name'] ?? '';
  127. $data = [
  128. 'amount' => $price,
  129. 'payWay' => $wxPay,
  130. 'sjId' => $this->sjId,
  131. 'sjStyle' => SjClass::STYLE_RETAIL,
  132. 'shopId' => $this->shopId,
  133. 'modPrice' => 0,
  134. 'remark' => '免费续期充值',
  135. 'shopAdminId' => $this->shopAdminId,
  136. 'shopAdminName' => $shopAdminName,
  137. ];
  138. $order = RechargeClass::addOrder($data, true);
  139. $orderSn = $order->orderSn;
  140. $name = $order->orderName ?? '充值';
  141. $totalFee = $order->amount;
  142. if (isset($this->shopAdmin['super']) == false || $this->shopAdmin['super'] != 1) {
  143. util::fail('超级管理员才有权限操作');
  144. }
  145. //强制使用小程序的miniOpenId
  146. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  147. if (empty($openId)) {
  148. util::fail('没有找到openId');
  149. }
  150. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  151. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  152. $wxPayType = 1;
  153. $ghsId = Yii::$app->request->get('ghsId', 0);
  154. $ghs = GhsClass::getById($ghsId);
  155. GhsClass::valid($ghs, $this->shopId);
  156. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1&ghsId=' . $ghsId;
  157. //订单30分钟后过期
  158. $now = time();
  159. $expireTime = $now + 1800;
  160. $wx = Yii::getAlias("@vendor/weixin");
  161. require_once($wx . '/lib/WxPay.Api.php');
  162. require_once($wx . '/example/WxPay.JsApiPay.php');
  163. $input = new \WxPayUnifiedOrder();
  164. $input->SetBody($name);
  165. $input->SetOut_trade_no($orderSn);
  166. $input->SetTotal_fee($totalFee * 100);
  167. $input->SetTime_start(date("YmdHis", $now));
  168. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  169. $input->SetTime_expire(date("YmdHis", $expireTime));
  170. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  171. $input->SetTrade_type("JSAPI");
  172. $merchantExtend = WxOpenClass::getWxInfo();
  173. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  174. $input->SetSub_openid($openId);
  175. } else {
  176. $input->SetOpenid($openId);
  177. }
  178. //强制使用小程序的AppId
  179. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  180. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  181. $tools = new \JsApiPay();
  182. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  183. $newParams = json_decode($jsApiParameters, true);
  184. util::success($newParams);
  185. }
  186. //充值送红包 ssh 2021.2.21
  187. public function actionBalance()
  188. {
  189. ini_set('date.timezone', 'Asia/Shanghai');
  190. $get = Yii::$app->request->get();
  191. $hbId = $get['id'] ?? 0;
  192. $ghsId = $get['ghsId'] ?? 0;
  193. $ghsInfo = GhsClass::getById($ghsId, true);
  194. if (empty($ghsInfo)) {
  195. util::fail('没有找到供货商');
  196. }
  197. GhsClass::valid($ghsInfo, $this->shopId);
  198. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  199. $hbInfo = RechargeHbClass::getById($hbId, true);
  200. if (empty($hbInfo)) {
  201. util::fail('无效活动');
  202. }
  203. if ($hbInfo->shopId != $ghsShopId) {
  204. util::fail('供货商与充值红包活动不一致');
  205. }
  206. if ($hbInfo->delStatus == 1) {
  207. util::fail('红包已更新,请重新发起充值');
  208. }
  209. $amount = $hbInfo->amount ?? 0;
  210. if ($amount <= 0) {
  211. util::fail('充值金额有问题');
  212. }
  213. $customId = $ghsInfo['customId'] ?? 0;
  214. $custom = CustomClass::getById($customId, true);
  215. $customName = $custom->name ?? '';
  216. $wxPay = dict::getDict('payWay', 'wxPay');
  217. $shopAdmin = $this->shopAdmin->attributes;
  218. $shopAdminName = $shopAdmin['name'] ?? '';
  219. $data = [
  220. 'amount' => $amount,
  221. 'payWay' => $wxPay,
  222. 'sjId' => $this->sjId,
  223. 'sjStyle' => SjClass::STYLE_RETAIL,
  224. 'shopId' => $this->shopId,
  225. 'modPrice' => 0,
  226. 'remark' => '',
  227. 'shopAdminId' => $this->shopAdminId,
  228. 'shopAdminName' => $shopAdminName,
  229. 'hbId' => $hbInfo->id ?? 0,
  230. 'hbAmount' => $hbInfo->hbAmount ?? 0,
  231. 'hbNum' => $hbInfo->num,
  232. 'totalHb' => $hbInfo->totalHb,
  233. 'valid' => $hbInfo->valid,
  234. 'miniExpend' => $hbInfo->miniExpend,
  235. 'ghsId' => $ghsId,
  236. 'ghsShopId' => $ghsShopId,
  237. 'customId' => $customId,
  238. 'customName' => $customName,
  239. ];
  240. $order = RechargeClass::addOrder($data, true);
  241. $orderSn = $order->orderSn;
  242. $name = $order->orderName ?? '充值';
  243. $totalFee = $order->amount;
  244. //强制使用小程序的miniOpenId
  245. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  246. if (empty($openId)) {
  247. util::fail('没有找到openId');
  248. }
  249. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  250. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  251. $wxPayType = 1;
  252. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  253. //订单30分钟后过期
  254. $now = time();
  255. $expireTime = $now + 1800;
  256. $wx = Yii::getAlias("@vendor/weixin");
  257. require_once($wx . '/lib/WxPay.Api.php');
  258. require_once($wx . '/example/WxPay.JsApiPay.php');
  259. $input = new \WxPayUnifiedOrder();
  260. $input->SetBody($name);
  261. $input->SetOut_trade_no($orderSn);
  262. $input->SetTotal_fee($totalFee * 100);
  263. $input->SetTime_start(date("YmdHis", $now));
  264. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  265. $input->SetTime_expire(date("YmdHis", $expireTime));
  266. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  267. $input->SetTrade_type("JSAPI");
  268. $merchantExtend = WxOpenClass::getWxInfo();
  269. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  270. $input->SetSub_openid($openId);
  271. } else {
  272. $input->SetOpenid($openId);
  273. }
  274. //强制使用小程序的AppId
  275. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  276. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  277. $tools = new \JsApiPay();
  278. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  279. $newParams = json_decode($jsApiParameters, true);
  280. util::success($newParams);
  281. }
  282. //续期成功 ssh 2021.2.21
  283. public function actionRenewSuccess()
  284. {
  285. $sj = $this->sj->attributes;
  286. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  287. util::success(['deadline' => $deadline]);
  288. }
  289. }