RechargeController.php 8.9 KB

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