RechargeController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\recharge\classes\RechargeClass;
  5. use biz\shop\classes\ShopClass;
  6. use biz\sj\classes\SjClass;
  7. use bizHd\wx\classes\WxOpenClass;
  8. use common\components\dict;
  9. use common\components\httpUtil;
  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. $amount = $get['amount'] ?? 500;
  117. $wxPay = dict::getDict('payWay', 'wxPay');
  118. $shopAdmin = $this->shopAdmin->attributes;
  119. $shopAdminName = $shopAdmin['name'] ?? '';
  120. $data = [
  121. 'amount' => $amount,
  122. 'payWay' => $wxPay,
  123. 'sjId' => $this->sjId,
  124. 'sjStyle' => SjClass::STYLE_RETAIL,
  125. 'shopId' => $this->shopId,
  126. 'modPrice' => 0,
  127. 'remark' => '',
  128. 'shopAdminId' => $this->shopAdminId,
  129. 'shopAdminName' => $shopAdminName,
  130. ];
  131. $order = RechargeClass::addOrder($data, true);
  132. $orderSn = $order->orderSn;
  133. $name = $order->orderName ?? '充值';
  134. $totalFee = $order->amount;
  135. //强制使用小程序的miniOpenId
  136. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  137. if (empty($openId)) {
  138. util::fail('没有找到openId');
  139. }
  140. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  141. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  142. $wxPayType = 1;
  143. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  144. //订单30分钟后过期
  145. $now = time();
  146. $expireTime = $now + 1800;
  147. $wx = Yii::getAlias("@vendor/weixin");
  148. require_once($wx . '/lib/WxPay.Api.php');
  149. require_once($wx . '/example/WxPay.JsApiPay.php');
  150. $input = new \WxPayUnifiedOrder();
  151. $input->SetBody($name);
  152. $input->SetOut_trade_no($orderSn);
  153. $input->SetTotal_fee($totalFee * 100);
  154. $input->SetTime_start(date("YmdHis", $now));
  155. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  156. $input->SetTime_expire(date("YmdHis", $expireTime));
  157. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  158. $input->SetTrade_type("JSAPI");
  159. $merchantExtend = WxOpenClass::getWxInfo();
  160. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  161. $input->SetSub_openid($openId);
  162. } else {
  163. $input->SetOpenid($openId);
  164. }
  165. //强制使用小程序的AppId
  166. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  167. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  168. $tools = new \JsApiPay();
  169. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  170. $newParams = json_decode($jsApiParameters, true);
  171. util::success($newParams);
  172. }
  173. //续期成功 ssh 2021.2.21
  174. public function actionRenewSuccess()
  175. {
  176. $sj = $this->sj->attributes;
  177. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  178. util::success(['deadline' => $deadline]);
  179. }
  180. }