RenewController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace www\controllers;
  3. use biz\merchant\services\MerchantExtendService;
  4. use biz\merchant\services\MerchantService;
  5. use biz\saas\services\RenewService;
  6. use biz\saas\services\SetMealService;
  7. use biz\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. ini_set('date.timezone', 'Asia/Shanghai');
  24. $merchantId = Yii::$app->request->get('merchantId', 0);
  25. $merchant = MerchantService::getMerchantById($merchantId);
  26. if (empty($merchant)) {
  27. util::fail('没有找到商家');
  28. }
  29. $setId = isset($merchant['setId']) ? $merchant['setId'] : 0;
  30. $set = SetMealService::getById($setId);
  31. if (empty($set)) {
  32. util::fail('没有找到套餐');
  33. }
  34. $userId = $this->userId;
  35. $addData['userId'] = $userId;
  36. $price = $set['price'];
  37. $prePrice = $price;
  38. $actPrice = $price;
  39. $addData['actPrice'] = $actPrice;
  40. $addData['prePrice'] = $prePrice;
  41. $addData['merchantId'] = $merchantId;
  42. $now = time();
  43. $expireTime = $now + 300;//订单5分钟后过期
  44. $addData['deadline'] = $expireTime;
  45. $order = RenewService::addOrder($addData);
  46. $orderId = $order['id'];
  47. $orderSn = $order['orderSn'];
  48. $couponId = 0;
  49. $name = $set['name'] . '开通续费';
  50. $totalFee = $actPrice;
  51. $userId = $addData['userId'];
  52. $user = UserService::getById($userId);
  53. //小程序使用miniOpenId
  54. if (httpUtil::isMiniProgram()) {
  55. $openId = $user['miniOpenId'];
  56. } else {
  57. $openId = $user['openId'];
  58. }
  59. $typeList = configDict::getConfig('capitalType');
  60. $capitalType = $typeList['xhRenew']['id'];
  61. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId;//将流水类型、优惠卷传过去
  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($this->frontUrl . '/notice/weixin-callback/');
  73. $input->SetTrade_type("JSAPI");
  74. //花卉宝代为申请的微信支付
  75. $merchantExtend = MerchantExtendService::getByMerchantId($this->merchantId);
  76. if ($merchantExtend['generalMerchant'] == 1) {
  77. $input->SetSub_openid($openId);
  78. } else {
  79. $input->SetOpenid($openId);
  80. }
  81. //小程序使用miniAppId
  82. if (httpUtil::isMiniProgram()) {
  83. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  84. }
  85. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  86. $tools = new \JsApiPay();
  87. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  88. $newParams = json_decode($jsApiParameters, true);
  89. $newParams['orderId'] = $orderId;
  90. $newParams['merchantId'] = $merchantId;
  91. util::success($newParams);
  92. }
  93. }