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