RechargeController.php 8.6 KB

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