RechargeController.php 13 KB

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