RenewController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace hd\controllers;
  3. use biz\sj\services\MerchantExtendService;
  4. use biz\sj\services\MerchantService;
  5. use bizHd\saas\services\RenewService;
  6. use bizHd\saas\services\SetMealService;
  7. use bizHd\user\services\UserService;
  8. use common\components\configDict;
  9. use common\components\httpUtil;
  10. use common\components\util;
  11. use Yii;
  12. class RenewController extends BaseController
  13. {
  14. public function actionList()
  15. {
  16. $where = [];
  17. $list = RenewService::getRenewList($where);
  18. util::success($list);
  19. }
  20. //微信支付购买 shish 2020.1.11
  21. public function actionWxPay()
  22. {
  23. util::fail('即将开通...');
  24. ini_set('date.timezone', 'Asia/Shanghai');
  25. $merchantId = Yii::$app->request->get('merchantId', 0);
  26. $merchant = MerchantService::getMerchantById($merchantId);
  27. if (empty($merchant)) {
  28. util::fail('没有找到商家');
  29. }
  30. $setId = isset($merchant['setId']) ? $merchant['setId'] : 0;
  31. $set = SetMealService::getById($setId);
  32. if (empty($set)) {
  33. util::fail('没有找到套餐');
  34. }
  35. $userId = $this->adminId;
  36. $addData['userId'] = $userId;
  37. $price = $set['price'];
  38. $prePrice = $price;
  39. $actPrice = $price;
  40. $addData['actPrice'] = $actPrice;
  41. $addData['prePrice'] = $prePrice;
  42. $addData['merchantId'] = $merchantId;
  43. $now = time();
  44. $expireTime = $now + 300;//订单5分钟后过期
  45. $addData['deadline'] = $expireTime;
  46. $order = RenewService::addOrder($addData);
  47. $orderId = $order['id'];
  48. $orderSn = $order['orderSn'];
  49. $couponId = 0;
  50. $name = $set['name'] . '开通续费';
  51. $totalFee = $actPrice;
  52. $userId = $addData['userId'];
  53. $user = UserService::getById($userId);
  54. //小程序使用miniOpenId
  55. if (httpUtil::isMiniProgram()) {
  56. $openId = $user['miniOpenId'];
  57. } else {
  58. $openId = $user['openId'];
  59. }
  60. $typeList = configDict::getConfig('capitalType');
  61. $capitalType = $typeList['xhRenew']['id'];
  62. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId;//将流水类型、优惠卷传过去
  63. $wx = Yii::getAlias("@vendor/weixin");
  64. require_once($wx . '/lib/WxPay.Api.php');
  65. require_once($wx . '/example/WxPay.JsApiPay.php');
  66. $input = new \WxPayUnifiedOrder();
  67. $input->SetBody($name);
  68. $input->SetOut_trade_no($orderSn);
  69. $input->SetTotal_fee($totalFee * 100);
  70. $input->SetTime_start(date("YmdHis", $now));
  71. $input->SetAttach($attach);
  72. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  73. $input->SetNotify_url($this->frontUrl . '/notice/weixin-callback/');
  74. $input->SetTrade_type("JSAPI");
  75. //花卉宝代为申请的微信支付
  76. $merchantExtend = MerchantExtendService::getByMerchantId($this->sjId);
  77. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  78. $input->SetSub_openid($openId);
  79. } else {
  80. $input->SetOpenid($openId);
  81. }
  82. //小程序使用miniAppId
  83. if (httpUtil::isMiniProgram()) {
  84. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  85. }
  86. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  87. $tools = new \JsApiPay();
  88. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  89. $newParams = json_decode($jsApiParameters, true);
  90. $newParams['orderId'] = $orderId;
  91. $newParams['merchantId'] = $merchantId;
  92. util::success($newParams);
  93. }
  94. }