RenewController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace hd\controllers;
  3. use biz\set\classes\SetClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\saas\classes\ApplyClass;
  7. use bizHd\saas\services\RenewService;
  8. use bizHd\saas\services\SetMealService;
  9. use bizHd\shop\classes\MainInviteClass;
  10. use bizHd\user\services\UserService;
  11. use bizHd\wx\classes\WxOpenClass;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  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 2020.1.11
  25. public function actionWxPay()
  26. {
  27. ini_set('date.timezone', 'Asia/Shanghai');
  28. $eId = $this->shopAdminId;
  29. $addData['shopAdminId'] = $eId;
  30. $shopAdmin = $this->shopAdmin->attributes;
  31. $adminName = $shopAdmin['name'] ?? '';
  32. $sjId = $shopAdmin['sjId'] ?? 0;
  33. $addData['shopAdminName'] = $adminName;
  34. $set = SetClass::getByCondition(['style' => SetClass::SET_STYLE_GHS], true, 'id asc');
  35. if (empty($set)) {
  36. util::fail('没有找到套餐');
  37. }
  38. $price = $set->price;
  39. $prePrice = $price;
  40. $actPrice = $price;
  41. $setId = $set->id;
  42. $addData['actPrice'] = $actPrice;
  43. $addData['prePrice'] = $prePrice;
  44. $addData['sjId'] = $this->sjId;
  45. $now = time();
  46. $expireTime = $now + 300;//订单5分钟后过期
  47. $addData['deadline'] = date("Y-m-d H:i:s", $expireTime);
  48. $order = RenewService::addOrder($addData);
  49. $orderId = $order['id'];
  50. $orderSn = $order['orderSn'];
  51. $couponId = 0;
  52. $name = $set->name . '开通及续费';
  53. $totalFee = $actPrice;
  54. $admin = $this->admin->attributes;
  55. //小程序使用miniOpenId
  56. $openId = $admin['miniOpenId'];
  57. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  58. //将流水类型、优惠卷传过去
  59. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&setId=' . $setId . '&sjId=' . $sjId;
  60. $wx = Yii::getAlias("@vendor/weixin");
  61. require_once($wx . '/lib/WxPay.Api.php');
  62. require_once($wx . '/example/WxPay.JsApiPay.php');
  63. $input = new \WxPayUnifiedOrder();
  64. $input->SetBody($name);
  65. $input->SetOut_trade_no($orderSn);
  66. $input->SetTotal_fee($totalFee * 100);
  67. $input->SetTime_start(date("YmdHis", $now));
  68. $input->SetAttach($attach);
  69. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  70. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  71. $input->SetTrade_type("JSAPI");
  72. //花卉宝代为申请的微信支付
  73. $merchantExtend = WxOpenClass::getWxInfo();
  74. $input->SetOpenid($openId);
  75. //小程序使用miniAppId
  76. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  77. Yii::info('renew:' . json_encode($merchantExtend));
  78. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  79. $tools = new \JsApiPay();
  80. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  81. $newParams = json_decode($jsApiParameters, true);
  82. $newParams['orderId'] = $orderId;
  83. $newParams['sjId'] = $this->sjId;
  84. util::success($newParams);
  85. }
  86. /**
  87. * 花店邀请注册:创建年度会员 xhRenew 订单并唤起微信支付(注册成功后调用,applyId 可选)
  88. */
  89. public function actionCreate()
  90. {
  91. ini_set('date.timezone', 'Asia/Shanghai');
  92. $post = Yii::$app->request->post();
  93. $applyId = (int) ($post['applyId'] ?? 0);
  94. $registerConfig = dict::getDict('hdRegisterConfig');
  95. $prePrice = round((float) ($registerConfig['price'] ?? 0), 2);
  96. $hdDiscountAmount = 0;
  97. if ($applyId > 0) {
  98. $apply = ApplyClass::getById($applyId);
  99. if (empty($apply)) {
  100. util::fail('申请不存在');
  101. }
  102. if ((int) ($apply['adminId'] ?? 0) !== (int) $this->adminId) {
  103. util::fail('申请与当前账号不匹配');
  104. }
  105. $hdDiscountAmount = round((float) ($apply['hdDiscountAmount'] ?? 0), 2);
  106. }
  107. $actPrice = round(max(0, $prePrice - $hdDiscountAmount), 2);
  108. $shopAdmin = $this->shopAdmin->attributes;
  109. $adminName = $shopAdmin['name'] ?? '';
  110. $shop = $this->shop;
  111. $shopName = $shop['shopName'] ?? ($shop['name'] ?? '');
  112. $sjName = is_array($this->sj) ? ($this->sj['name'] ?? '') : ($this->sj->name ?? '');
  113. $now = time();
  114. $expireTime = $now + 300;
  115. $addData = [
  116. 'shopAdminId' => $this->shopAdminId,
  117. 'shopAdminName' => $adminName,
  118. 'prePrice' => $prePrice,
  119. 'actPrice' => $actPrice,
  120. 'sjId' => $this->sjId,
  121. 'sjName' => $sjName,
  122. 'mainId' => $this->mainId,
  123. 'shopId' => $this->shopId,
  124. 'shopName' => $shopName,
  125. 'deadline' => date('Y-m-d H:i:s', $expireTime),
  126. 'status' => 0,
  127. 'type' => 1,
  128. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  129. 'remark' => $applyId > 0 ? 'applyId=' . $applyId : '',
  130. 'beforeDeadline'=>$shop->beforeDeadline ?? '',
  131. ];
  132. $order = RenewService::addOrder($addData);
  133. $orderId = (int) ($order['id'] ?? 0);
  134. $orderSn = (string) ($order['orderSn'] ?? '');
  135. if ($orderId <= 0 || $orderSn === '') {
  136. util::fail('创建订单失败');
  137. }
  138. if ($actPrice <= 0) {
  139. RenewService::completeRegisterRenewPay(
  140. dict::getDict('payWay', 'wxPay'),
  141. $orderSn,
  142. $actPrice,
  143. 'capitalType=' . dict::getDict('capitalType', 'xhRenew', 'id') . '&applyId=' . $applyId,
  144. ''
  145. );
  146. util::success([
  147. 'needPay' => 0,
  148. 'paid' => 1,
  149. 'applyId' => $applyId,
  150. 'orderId' => $orderId,
  151. 'actPrice' => $actPrice,
  152. ]);
  153. }
  154. $admin = $this->admin->attributes;
  155. $openId = trim((string) ($post['miniOpenId'] ?? ($admin['miniOpenId'] ?? '')));
  156. if ($openId === '') {
  157. util::fail('请先授权微信登录');
  158. }
  159. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  160. $attach = 'capitalType=' . $capitalType . '&applyId=' . $applyId . '&sjId=' . $this->sjId;
  161. $wx = Yii::getAlias('@vendor/weixin');
  162. require_once $wx . '/lib/WxPay.Api.php';
  163. require_once $wx . '/example/WxPay.JsApiPay.php';
  164. $input = new \WxPayUnifiedOrder();
  165. $input->SetBody('花店年度会员');
  166. $input->SetOut_trade_no($orderSn);
  167. $input->SetTotal_fee(round($actPrice * 100));
  168. $input->SetTime_start(date('YmdHis', $now));
  169. $input->SetAttach($attach);
  170. $input->SetTime_expire(date('YmdHis', $expireTime));
  171. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  172. $input->SetTrade_type('JSAPI');
  173. $merchantExtend = WxOpenClass::getWxInfo();
  174. $input->SetOpenid($openId);
  175. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  176. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  177. $tools = new \JsApiPay();
  178. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  179. $newParams = json_decode($jsApiParameters, true) ?: [];
  180. $newParams['orderId'] = $orderId;
  181. $newParams['applyId'] = $applyId;
  182. $newParams['needPay'] = 1;
  183. $newParams['paid'] = 0;
  184. $newParams['actPrice'] = $actPrice;
  185. util::success($newParams);
  186. }
  187. }