RenewController.php 8.4 KB

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