RechargeController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace hd\controllers;
  3. use biz\recharge\classes\RechargeClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\sj\classes\SjClass;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use common\components\dict;
  8. use common\components\httpUtil;
  9. use common\components\util;
  10. use Yii;
  11. class RechargeController extends BaseController
  12. {
  13. //充值记录 ssh 2021.2.21
  14. public function actionList()
  15. {
  16. $where = ['shopId' => $this->shopId, 'payStatus' => RechargeClass::PAY_STATUS_HAS_PAY];
  17. $list = RechargeClass::getRechargeList($where);
  18. util::success($list);
  19. }
  20. //免费续期活动 ssh 2021.2.21
  21. public function actionRenew()
  22. {
  23. $sj = $this->sj->attributes;
  24. $shopId = $sj['parentShopId'] ?? 0;
  25. $shopName = '';
  26. $rechargeNum = getenv('RECHARGE_NUM') == false ? 268 : getenv('RECHARGE_NUM');
  27. if (!empty($shopId)) {
  28. $shop = ShopClass::getShopInfo($shopId);
  29. if (!empty($shop)) {
  30. $shopName = $shop['shopName'] ?? '';
  31. $num = $shop['rechargeNum'] ?? 0;
  32. $rechargeNum += $num;
  33. }
  34. }
  35. $data = [
  36. 'renew' => getenv('RENEW_PRICE') == false ? 3980 : getenv('RENEW_PRICE'),
  37. 'recharge' => getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE'),
  38. 'shopName' => $shopName,
  39. 'userNum' => $rechargeNum
  40. ];
  41. util::success($data);
  42. }
  43. //续期充值付款 ssh 2021.2.21
  44. public function actionRenewPay()
  45. {
  46. ini_set('date.timezone', 'Asia/Shanghai');
  47. $wxPay = dict::getDict('payWay', 'wxPay');
  48. $price = getenv('RECHARGE_PRICE') == false ? 5980 : getenv('RECHARGE_PRICE');
  49. $shopAdmin = $this->shopAdmin->attributes;
  50. $shopAdminName = $shopAdmin['name'] ?? '';
  51. $data = [
  52. 'amount' => $price,
  53. 'payWay' => $wxPay,
  54. 'sjId' => $this->sjId,
  55. 'sjStyle' => SjClass::STYLE_RETAIL,
  56. 'shopId' => $this->shopId,
  57. 'modPrice' => 0,
  58. 'remark' => '免费续期充值',
  59. 'shopAdminId' => $this->shopAdminId,
  60. 'shopAdminName' => $shopAdminName,
  61. ];
  62. $order = RechargeClass::addOrder($data, true);
  63. $orderSn = $order->orderSn;
  64. $name = $order->orderName ?? '充值';
  65. $totalFee = $order->amount;
  66. //强制使用小程序的miniOpenId
  67. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  68. if (empty($openId)) {
  69. util::fail('没有找到openId');
  70. }
  71. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  72. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  73. $wxPayType = 1;
  74. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1';
  75. //订单30分钟后过期
  76. $now = time();
  77. $expireTime = $now + 1800;
  78. $wx = Yii::getAlias("@vendor/weixin");
  79. require_once($wx . '/lib/WxPay.Api.php');
  80. require_once($wx . '/example/WxPay.JsApiPay.php');
  81. $input = new \WxPayUnifiedOrder();
  82. $input->SetBody($name);
  83. $input->SetOut_trade_no($orderSn);
  84. $input->SetTotal_fee($totalFee * 100);
  85. $input->SetTime_start(date("YmdHis", $now));
  86. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  87. $input->SetTime_expire(date("YmdHis", $expireTime));
  88. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  89. $input->SetTrade_type("JSAPI");
  90. $merchantExtend = WxOpenClass::getWxInfo();
  91. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  92. $input->SetSub_openid($openId);
  93. } else {
  94. $input->SetOpenid($openId);
  95. }
  96. //强制使用小程序的AppId
  97. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  98. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  99. $tools = new \JsApiPay();
  100. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  101. $newParams = json_decode($jsApiParameters, true);
  102. util::success($newParams);
  103. }
  104. //充值余额 ssh 2021.2.21
  105. public function actionBalance()
  106. {
  107. ini_set('date.timezone', 'Asia/Shanghai');
  108. $get = Yii::$app->request->get();
  109. $amount = $get['amount'] ?? 500;
  110. $wxPay = dict::getDict('payWay', 'wxPay');
  111. $shopAdmin = $this->shopAdmin->attributes;
  112. $shopAdminName = $shopAdmin['name'] ?? '';
  113. $data = [
  114. 'amount' => $amount,
  115. 'payWay' => $wxPay,
  116. 'sjId' => $this->sjId,
  117. 'sjStyle' => SjClass::STYLE_RETAIL,
  118. 'shopId' => $this->shopId,
  119. 'modPrice' => 0,
  120. 'remark' => '',
  121. 'shopAdminId' => $this->shopAdminId,
  122. 'shopAdminName' => $shopAdminName,
  123. ];
  124. $order = RechargeClass::addOrder($data, true);
  125. $orderSn = $order->orderSn;
  126. $name = $order->orderName ?? '充值';
  127. $totalFee = $order->amount;
  128. //强制使用小程序的miniOpenId
  129. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  130. if (empty($openId)) {
  131. util::fail('没有找到openId');
  132. }
  133. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  134. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  135. $wxPayType = 1;
  136. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  137. //订单30分钟后过期
  138. $now = time();
  139. $expireTime = $now + 1800;
  140. $wx = Yii::getAlias("@vendor/weixin");
  141. require_once($wx . '/lib/WxPay.Api.php');
  142. require_once($wx . '/example/WxPay.JsApiPay.php');
  143. $input = new \WxPayUnifiedOrder();
  144. $input->SetBody($name);
  145. $input->SetOut_trade_no($orderSn);
  146. $input->SetTotal_fee($totalFee * 100);
  147. $input->SetTime_start(date("YmdHis", $now));
  148. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  149. $input->SetTime_expire(date("YmdHis", $expireTime));
  150. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  151. $input->SetTrade_type("JSAPI");
  152. $merchantExtend = WxOpenClass::getWxInfo();
  153. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  154. $input->SetSub_openid($openId);
  155. } else {
  156. $input->SetOpenid($openId);
  157. }
  158. //强制使用小程序的AppId
  159. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  160. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  161. $tools = new \JsApiPay();
  162. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  163. $newParams = json_decode($jsApiParameters, true);
  164. util::success($newParams);
  165. }
  166. //续期成功 ssh 2021.2.21
  167. public function actionRenewSuccess()
  168. {
  169. $sj = $this->sj->attributes;
  170. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  171. util::success(['deadline' => $deadline]);
  172. }
  173. }