RenewController.php 8.1 KB

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