RenewController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. $unitPrice = $pfLevel == 0 ? 980 : 1980;
  40. $price = bcmul($unitPrice, $year, 2);
  41. $data = [
  42. 'orderSn' => $orderSn,
  43. 'prePrice' => $price,
  44. 'actPrice' => $price,
  45. 'shopAdminId' => $staffId,
  46. 'shopAdminName' => $shopAdminName,
  47. 'sjId' => $sjId,
  48. 'mainId' => $mainId,
  49. 'shopId' => $shopId,
  50. 'pfLevel' => $pfLevel,
  51. 'year' => $year,
  52. 'deadline' => $deadline,
  53. 'beforeDeadline' => $beforeDeadline,
  54. 'status' => 0,
  55. ];
  56. $ret = RenewClass::add($data, true);
  57. $id = $ret->id ?? 0;
  58. $now = time();
  59. $expireTime = $now + 300;//订单5分钟后过期
  60. $totalFee = $price;
  61. $name = "续费{$year}年";
  62. $admin = $this->admin;
  63. $openId = $admin->ghsMiniOpenId;
  64. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  65. $attach = 'capitalType=' . $capitalType . '&couponId=0&shopId=' . $shopId;
  66. $wx = Yii::getAlias("@vendor/weixin");
  67. require_once($wx . '/lib/WxPay.Api.php');
  68. require_once($wx . '/example/WxPay.JsApiPay.php');
  69. $input = new \WxPayUnifiedOrder();
  70. $input->SetBody($name);
  71. $input->SetOut_trade_no($orderSn);
  72. $input->SetTotal_fee($totalFee * 100);
  73. $input->SetTime_start(date("YmdHis", $now));
  74. $input->SetAttach($attach);
  75. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  76. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  77. $input->SetTrade_type("JSAPI");
  78. //花卉宝代为申请的微信支付
  79. $sjExtend = WxOpenClass::getGhsWxInfo();
  80. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  81. $input->SetSub_openid($openId);
  82. } else {
  83. $input->SetOpenid($openId);
  84. }
  85. //小程序使用miniAppId
  86. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  87. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  88. $tools = new \JsApiPay();
  89. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  90. $newParams = json_decode($jsApiParameters, true);
  91. $newParams['id'] = $id;
  92. util::success($newParams);
  93. }
  94. //微信支付购买 ssh 2020.1.11
  95. public function actionWxPay()
  96. {
  97. ini_set('date.timezone', 'Asia/Shanghai');
  98. $shopAdminId = $this->shopAdminId;
  99. $addData['shopAdminId'] = $shopAdminId;
  100. $shopAdmin = $this->shopAdmin->attributes;
  101. $adminName = $shopAdmin['name'] ?? '';
  102. $sjId = $shopAdmin['sjId'];
  103. $addData['shopAdminName'] = $adminName;
  104. $set = SetClass::getByCondition(['style' => SetClass::SET_STYLE_GHS], true, 'id asc');
  105. if (empty($set)) {
  106. util::fail('没有找到套餐');
  107. }
  108. $price = $set->price;
  109. $prePrice = $price;
  110. $actPrice = $price;
  111. $setId = $set->id;
  112. $addData['actPrice'] = $actPrice;
  113. $addData['prePrice'] = $prePrice;
  114. $addData['sjId'] = $this->sjId;
  115. $now = time();
  116. $expireTime = $now + 300;//订单5分钟后过期
  117. $addData['deadline'] = date("Y-m-d H:i:s", $expireTime);
  118. $order = RenewService::addOrder($addData);
  119. $orderId = $order['id'];
  120. $orderSn = $order['orderSn'];
  121. $couponId = 0;
  122. $name = $set->name . '开通及续费';
  123. $totalFee = $actPrice;
  124. $admin = $this->admin->attributes;
  125. //小程序使用miniOpenId
  126. $openId = $admin['ghsMiniOpenId'];
  127. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  128. //将流水类型、优惠卷传过去
  129. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&setId=' . $setId . '&sjId=' . $sjId;
  130. $wx = Yii::getAlias("@vendor/weixin");
  131. require_once($wx . '/lib/WxPay.Api.php');
  132. require_once($wx . '/example/WxPay.JsApiPay.php');
  133. $input = new \WxPayUnifiedOrder();
  134. $input->SetBody($name);
  135. $input->SetOut_trade_no($orderSn);
  136. $input->SetTotal_fee($totalFee * 100);
  137. $input->SetTime_start(date("YmdHis", $now));
  138. $input->SetAttach($attach);
  139. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  140. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  141. $input->SetTrade_type("JSAPI");
  142. //花卉宝代为申请的微信支付
  143. $sjExtend = WxOpenClass::getGhsWxInfo();
  144. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  145. $input->SetSub_openid($openId);
  146. } else {
  147. $input->SetOpenid($openId);
  148. }
  149. //小程序使用miniAppId
  150. if (httpUtil::isMiniProgram()) {
  151. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  152. }
  153. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  154. $tools = new \JsApiPay();
  155. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  156. $newParams = json_decode($jsApiParameters, true);
  157. $newParams['orderId'] = $orderId;
  158. $newParams['sjId'] = $this->sjId;
  159. util::success($newParams);
  160. }
  161. }