RechargeController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\wx\classes\WxOpenClass;
  10. use common\components\dict;
  11. use common\components\util;
  12. use Yii;
  13. class RechargeController extends BaseController
  14. {
  15. //手动给客户充值 ssh 20250301
  16. public function actionManRecharge()
  17. {
  18. $post = Yii::$app->request->post();
  19. print_r($post);
  20. }
  21. //充值记录 ssh 2021.2.21
  22. public function actionList()
  23. {
  24. $where = ['shopId' => $this->shopId, 'payStatus' => RechargeClass::PAY_STATUS_HAS_PAY];
  25. $list = RechargeClass::getRechargeList($where);
  26. util::success($list);
  27. }
  28. //免费续期活动 ssh 2021.2.21
  29. public function actionRenew()
  30. {
  31. $sj = $this->sj->attributes;
  32. $shopId = $sj['parentShopId'] ?? 0;
  33. $shopName = '';
  34. $rechargeNum = getenv('RECHARGE_NUM') == false ? 268 : getenv('RECHARGE_NUM');
  35. if (!empty($shopId)) {
  36. $shop = ShopClass::getShopInfo($shopId);
  37. if (!empty($shop)) {
  38. $shopName = $shop['shopName'] ?? '';
  39. $num = $shop['rechargeNum'] ?? 0;
  40. $rechargeNum += $num;
  41. }
  42. }
  43. $data = [
  44. 'renew' => getenv('RENEW_PRICE') == false ? 1980 : getenv('RENEW_PRICE'),
  45. 'recharge' => getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE'),
  46. 'shopName' => $shopName,
  47. 'userNum' => $rechargeNum
  48. ];
  49. util::success($data);
  50. }
  51. //续期充值付款 ssh 2021.2.21
  52. public function actionRenewPay()
  53. {
  54. ini_set('date.timezone', 'Asia/Shanghai');
  55. $wxPay = dict::getDict('payWay', 'wxPay');
  56. $price = getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE');
  57. $shopAdmin = $this->shopAdmin->attributes;
  58. $shopAdminName = $shopAdmin['name'] ?? '';
  59. $data = [
  60. 'amount' => $price,
  61. 'payWay' => $wxPay,
  62. 'sjId' => $this->sjId,
  63. 'sjStyle' => SjClass::STYLE_RETAIL,
  64. 'shopId' => $this->shopId,
  65. 'modPrice' => 0,
  66. 'remark' => '免费续期充值',
  67. 'shopAdminId' => $this->shopAdminId,
  68. 'shopAdminName' => $shopAdminName,
  69. ];
  70. $order = RechargeClass::addOrder($data, true);
  71. $orderSn = $order->orderSn;
  72. $name = $order->orderName ?? '充值';
  73. $totalFee = $order->amount;
  74. if (isset($this->shopAdmin['super']) == false || $this->shopAdmin['super'] != 1) {
  75. util::fail('超级管理员才有权限操作');
  76. }
  77. //强制使用小程序的miniOpenId
  78. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  79. if (empty($openId)) {
  80. util::fail('没有找到openId');
  81. }
  82. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  83. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  84. $wxPayType = 1;
  85. $ghsId = Yii::$app->request->get('ghsId', 0);
  86. $ghs = GhsClass::getById($ghsId);
  87. GhsClass::valid($ghs, $this->shopId);
  88. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1&ghsId=' . $ghsId;
  89. //订单30分钟后过期
  90. $now = time();
  91. $expireTime = $now + 1800;
  92. $wx = Yii::getAlias("@vendor/weixin");
  93. require_once($wx . '/lib/WxPay.Api.php');
  94. require_once($wx . '/example/WxPay.JsApiPay.php');
  95. $input = new \WxPayUnifiedOrder();
  96. $input->SetBody($name);
  97. $input->SetOut_trade_no($orderSn);
  98. $input->SetTotal_fee($totalFee * 100);
  99. $input->SetTime_start(date("YmdHis", $now));
  100. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  101. $input->SetTime_expire(date("YmdHis", $expireTime));
  102. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  103. $input->SetTrade_type("JSAPI");
  104. $merchantExtend = WxOpenClass::getWxInfo();
  105. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  106. $input->SetSub_openid($openId);
  107. } else {
  108. $input->SetOpenid($openId);
  109. }
  110. //强制使用小程序的AppId
  111. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  112. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  113. $tools = new \JsApiPay();
  114. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  115. $newParams = json_decode($jsApiParameters, true);
  116. util::success($newParams);
  117. }
  118. //充值送红包 ssh 2021.2.21
  119. public function actionBalance()
  120. {
  121. ini_set('date.timezone', 'Asia/Shanghai');
  122. $get = Yii::$app->request->get();
  123. $hbId = $get['id'] ?? 0;
  124. $ghsId = $get['ghsId'] ?? 0;
  125. $ghsInfo = GhsClass::getById($ghsId, true);
  126. if (empty($ghsInfo)) {
  127. util::fail('没有找到供货商');
  128. }
  129. GhsClass::valid($ghsInfo, $this->shopId);
  130. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  131. $hbInfo = RechargeHbClass::getById($hbId, true);
  132. if (empty($hbInfo)) {
  133. util::fail('无效活动');
  134. }
  135. if ($hbInfo->shopId != $ghsShopId) {
  136. util::fail('供货商与充值红包活动不一致');
  137. }
  138. if ($hbInfo->delStatus == 1) {
  139. util::fail('红包已更新,请重新发起充值');
  140. }
  141. $amount = $hbInfo->amount ?? 0;
  142. if ($amount <= 0) {
  143. util::fail('充值金额有问题');
  144. }
  145. $customId = $ghsInfo['customId'] ?? 0;
  146. $custom = CustomClass::getById($customId, true);
  147. $customName = $custom->name ?? '';
  148. $wxPay = dict::getDict('payWay', 'wxPay');
  149. $shopAdmin = $this->shopAdmin->attributes;
  150. $shopAdminName = $shopAdmin['name'] ?? '';
  151. $data = [
  152. 'amount' => $amount,
  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. 'hbId' => $hbInfo->id ?? 0,
  162. 'hbAmount' => $hbInfo->hbAmount ?? 0,
  163. 'hbNum' => $hbInfo->num,
  164. 'totalHb' => $hbInfo->totalHb,
  165. 'valid' => $hbInfo->valid,
  166. 'miniExpend' => $hbInfo->miniExpend,
  167. 'ghsId' => $ghsId,
  168. 'ghsShopId' => $ghsShopId,
  169. 'customId' => $customId,
  170. 'customName' => $customName,
  171. ];
  172. $order = RechargeClass::addOrder($data, true);
  173. $orderSn = $order->orderSn;
  174. $name = $order->orderName ?? '充值';
  175. $totalFee = $order->amount;
  176. //强制使用小程序的miniOpenId
  177. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  178. if (empty($openId)) {
  179. util::fail('没有找到openId');
  180. }
  181. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  182. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  183. $wxPayType = 1;
  184. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  185. //订单30分钟后过期
  186. $now = time();
  187. $expireTime = $now + 1800;
  188. $wx = Yii::getAlias("@vendor/weixin");
  189. require_once($wx . '/lib/WxPay.Api.php');
  190. require_once($wx . '/example/WxPay.JsApiPay.php');
  191. $input = new \WxPayUnifiedOrder();
  192. $input->SetBody($name);
  193. $input->SetOut_trade_no($orderSn);
  194. $input->SetTotal_fee($totalFee * 100);
  195. $input->SetTime_start(date("YmdHis", $now));
  196. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  197. $input->SetTime_expire(date("YmdHis", $expireTime));
  198. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  199. $input->SetTrade_type("JSAPI");
  200. $merchantExtend = WxOpenClass::getWxInfo();
  201. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  202. $input->SetSub_openid($openId);
  203. } else {
  204. $input->SetOpenid($openId);
  205. }
  206. //强制使用小程序的AppId
  207. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  208. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  209. $tools = new \JsApiPay();
  210. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  211. $newParams = json_decode($jsApiParameters, true);
  212. util::success($newParams);
  213. }
  214. //续期成功 ssh 2021.2.21
  215. public function actionRenewSuccess()
  216. {
  217. $sj = $this->sj->attributes;
  218. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  219. util::success(['deadline' => $deadline]);
  220. }
  221. }