RenewController.php 2.8 KB

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