RechargeController.php 13 KB

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