RechargeController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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;
  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. $data = [
  50. 'amount' => $price,
  51. 'payWay' => $wxPay,
  52. 'sjId' => $this->sjId,
  53. 'sjStyle' => SjClass::STYLE_RETAIL,
  54. 'shopId' => $this->shopId,
  55. 'modPrice' => 0,
  56. 'remark' => '免费续期充值',
  57. ];
  58. $order = RechargeClass::addOrder($data, true);
  59. $orderSn = $order->orderSn;
  60. $name = $order->orderName ?? '充值';
  61. $totalFee = $order->amount;
  62. //强制使用小程序的miniOpenId
  63. $openId = $this->admin['miniOpenId'];
  64. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  65. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  66. $wxPayType = 1;
  67. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType . '&rechargeRenew=1';
  68. //订单30分钟后过期
  69. $now = time();
  70. $expireTime = $now + 1800;
  71. $wx = Yii::getAlias("@vendor/weixin");
  72. require_once($wx . '/lib/WxPay.Api.php');
  73. require_once($wx . '/example/WxPay.JsApiPay.php');
  74. $input = new \WxPayUnifiedOrder();
  75. $input->SetBody($name);
  76. $input->SetOut_trade_no($orderSn);
  77. $input->SetTotal_fee($totalFee * 100);
  78. $input->SetTime_start(date("YmdHis", $now));
  79. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  80. $input->SetTime_expire(date("YmdHis", $expireTime));
  81. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  82. $input->SetTrade_type("JSAPI");
  83. $merchantExtend = WxOpenClass::getWxInfo();
  84. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  85. $input->SetSub_openid($openId);
  86. } else {
  87. $input->SetOpenid($openId);
  88. }
  89. //强制使用小程序的AppId
  90. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  91. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  92. $tools = new \JsApiPay();
  93. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  94. $newParams = json_decode($jsApiParameters, true);
  95. util::success($newParams);
  96. }
  97. //充值余额 ssh 2021.2.21
  98. public function actionBalance()
  99. {
  100. ini_set('date.timezone', 'Asia/Shanghai');
  101. $get = Yii::$app->request->get();
  102. $amount = $get['amount'] ?? 500;
  103. $wxPay = dict::getDict('payWay', 'wxPay');
  104. $data = [
  105. 'amount' => $amount,
  106. 'payWay' => $wxPay,
  107. 'sjId' => $this->sjId,
  108. 'sjStyle' => SjClass::STYLE_RETAIL,
  109. 'shopId' => $this->shopId,
  110. 'modPrice' => 0,
  111. 'remark' => '',
  112. ];
  113. $order = RechargeClass::addOrder($data, true);
  114. $orderSn = $order->orderSn;
  115. $name = $order->orderName ?? '充值';
  116. $totalFee = $order->amount;
  117. //强制使用小程序的miniOpenId
  118. $openId = $this->admin['miniOpenId'];
  119. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  120. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  121. $wxPayType = 1;
  122. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  123. //订单30分钟后过期
  124. $now = time();
  125. $expireTime = $now + 1800;
  126. $wx = Yii::getAlias("@vendor/weixin");
  127. require_once($wx . '/lib/WxPay.Api.php');
  128. require_once($wx . '/example/WxPay.JsApiPay.php');
  129. $input = new \WxPayUnifiedOrder();
  130. $input->SetBody($name);
  131. $input->SetOut_trade_no($orderSn);
  132. $input->SetTotal_fee($totalFee * 100);
  133. $input->SetTime_start(date("YmdHis", $now));
  134. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  135. $input->SetTime_expire(date("YmdHis", $expireTime));
  136. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  137. $input->SetTrade_type("JSAPI");
  138. $merchantExtend = WxOpenClass::getWxInfo();
  139. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  140. $input->SetSub_openid($openId);
  141. } else {
  142. $input->SetOpenid($openId);
  143. }
  144. //强制使用小程序的AppId
  145. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  146. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  147. $tools = new \JsApiPay();
  148. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  149. $newParams = json_decode($jsApiParameters, true);
  150. util::success($newParams);
  151. }
  152. //续期成功 ssh 2021.2.21
  153. public function actionRenewSuccess()
  154. {
  155. $sj = $this->sj;
  156. $deadline = date("Y-m-d H:i", strtotime($sj['deadline']));
  157. util::success(['deadline' => $deadline]);
  158. }
  159. }