RenewController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\set\classes\SetClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizGhs\shop\classes\RenewClass;
  7. use bizHd\saas\services\RenewService;
  8. use bizHd\saas\services\SetMealService;
  9. use bizHd\user\services\UserService;
  10. use bizHd\wx\classes\WxOpenClass;
  11. use common\components\dict;
  12. use common\components\httpUtil;
  13. use common\components\orderSn;
  14. use common\components\util;
  15. use Yii;
  16. class RenewController extends BaseController
  17. {
  18. public function actionList()
  19. {
  20. $where = [];
  21. $list = RenewService::getRenewList($where);
  22. util::success($list);
  23. }
  24. //创建续费的订单 ssh 20240425
  25. public function actionCreate()
  26. {
  27. ini_set('date.timezone', 'Asia/Shanghai');
  28. $get = Yii::$app->request->get();
  29. $year = $get['year'] ?? 1;
  30. $orderSn = orderSn::getRenewSn();
  31. $staff = $this->shopAdmin;
  32. $staffId = $staff->id ?? 0;
  33. $shopAdminName = $staff->name ?? '';
  34. $shop = $this->shop;
  35. $shopId = $shop->id ?? 0;
  36. $sjId = $shop->sjId ?? 0;
  37. $mainId = $shop->mainId ?? 0;
  38. $pfLevel = $shop->pfLevel ?? 0;
  39. $deadline = $shop->deadline ?? '';
  40. $beforeDeadline = $shop->beforeDeadline ?? '';
  41. if (getenv('YII_ENV') == 'production') {
  42. $unitPrice = $pfLevel == 0 ? 980 : 1980;
  43. } else {
  44. $unitPrice = $pfLevel == 0 ? 0.1 : 0.2;
  45. }
  46. $price = bcmul($unitPrice, $year, 2);
  47. $data = [
  48. 'orderSn' => $orderSn,
  49. 'prePrice' => $price,
  50. 'actPrice' => $price,
  51. 'shopAdminId' => $staffId,
  52. 'shopAdminName' => $shopAdminName,
  53. 'sjId' => $sjId,
  54. 'mainId' => $mainId,
  55. 'shopId' => $shopId,
  56. 'pfLevel' => $pfLevel,
  57. 'year' => $year,
  58. 'deadline' => $deadline,
  59. 'beforeDeadline' => $beforeDeadline,
  60. 'status' => 0,
  61. ];
  62. $ret = RenewClass::add($data, true);
  63. $id = $ret->id ?? 0;
  64. $now = time();
  65. $expireTime = $now + 300;//订单5分钟后过期
  66. $totalFee = $price;
  67. $name = "续费{$year}年";
  68. $admin = $this->admin;
  69. $openId = $admin->ghsMiniOpenId;
  70. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  71. $attach = 'capitalType=' . $capitalType;
  72. $wx = Yii::getAlias("@vendor/weixin");
  73. require_once($wx . '/lib/WxPay.Api.php');
  74. require_once($wx . '/example/WxPay.JsApiPay.php');
  75. $input = new \WxPayUnifiedOrder();
  76. $input->SetBody($name);
  77. $input->SetOut_trade_no($orderSn);
  78. $input->SetTotal_fee($totalFee * 100);
  79. $input->SetTime_start(date("YmdHis", $now));
  80. $input->SetAttach($attach);
  81. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  82. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  83. $input->SetTrade_type("JSAPI");
  84. //花卉宝代为申请的微信支付
  85. $sjExtend = WxOpenClass::getGhsWxInfo();
  86. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  87. $input->SetSub_openid($openId);
  88. } else {
  89. $input->SetOpenid($openId);
  90. }
  91. //小程序使用miniAppId
  92. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  93. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  94. $tools = new \JsApiPay();
  95. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  96. $newParams = json_decode($jsApiParameters, true);
  97. $newParams['id'] = $id;
  98. util::success($newParams);
  99. }
  100. //微信支付购买 ssh 2020.1.11
  101. public function actionWxPay()
  102. {
  103. ini_set('date.timezone', 'Asia/Shanghai');
  104. $shopAdminId = $this->shopAdminId;
  105. $addData['shopAdminId'] = $shopAdminId;
  106. $shopAdmin = $this->shopAdmin->attributes;
  107. $adminName = $shopAdmin['name'] ?? '';
  108. $sjId = $shopAdmin['sjId'];
  109. $addData['shopAdminName'] = $adminName;
  110. $set = SetClass::getByCondition(['style' => SetClass::SET_STYLE_GHS], true, 'id asc');
  111. if (empty($set)) {
  112. util::fail('没有找到套餐');
  113. }
  114. $price = $set->price;
  115. $prePrice = $price;
  116. $actPrice = $price;
  117. $setId = $set->id;
  118. $addData['actPrice'] = $actPrice;
  119. $addData['prePrice'] = $prePrice;
  120. $addData['sjId'] = $this->sjId;
  121. $now = time();
  122. $expireTime = $now + 300;//订单5分钟后过期
  123. $addData['deadline'] = date("Y-m-d H:i:s", $expireTime);
  124. $order = RenewService::addOrder($addData);
  125. $orderId = $order['id'];
  126. $orderSn = $order['orderSn'];
  127. $couponId = 0;
  128. $name = $set->name . '开通及续费';
  129. $totalFee = $actPrice;
  130. $admin = $this->admin->attributes;
  131. //小程序使用miniOpenId
  132. $openId = $admin['ghsMiniOpenId'];
  133. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  134. //将流水类型、优惠卷传过去
  135. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&setId=' . $setId . '&sjId=' . $sjId;
  136. $wx = Yii::getAlias("@vendor/weixin");
  137. require_once($wx . '/lib/WxPay.Api.php');
  138. require_once($wx . '/example/WxPay.JsApiPay.php');
  139. $input = new \WxPayUnifiedOrder();
  140. $input->SetBody($name);
  141. $input->SetOut_trade_no($orderSn);
  142. $input->SetTotal_fee($totalFee * 100);
  143. $input->SetTime_start(date("YmdHis", $now));
  144. $input->SetAttach($attach);
  145. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  146. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  147. $input->SetTrade_type("JSAPI");
  148. //花卉宝代为申请的微信支付
  149. $sjExtend = WxOpenClass::getGhsWxInfo();
  150. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  151. $input->SetSub_openid($openId);
  152. } else {
  153. $input->SetOpenid($openId);
  154. }
  155. //小程序使用miniAppId
  156. if (httpUtil::isMiniProgram()) {
  157. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  158. }
  159. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  160. $tools = new \JsApiPay();
  161. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  162. $newParams = json_decode($jsApiParameters, true);
  163. $newParams['orderId'] = $orderId;
  164. $newParams['sjId'] = $this->sjId;
  165. util::success($newParams);
  166. }
  167. }