RechargeController.php 12 KB

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