RenewController.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 bizHd\wx\classes\WxOpenClass;
  9. use common\components\dict;
  10. use common\components\httpUtil;
  11. use common\components\util;
  12. use Yii;
  13. class RenewController extends BaseController
  14. {
  15. public function actionList()
  16. {
  17. $where = [];
  18. $list = RenewService::getRenewList($where);
  19. util::success($list);
  20. }
  21. //微信支付购买 ssh 2020.1.11
  22. public function actionWxPay()
  23. {
  24. ini_set('date.timezone', 'Asia/Shanghai');
  25. $sj = $this->sj;
  26. $setId = isset($sj['setId']) ? $sj['setId'] : 0;
  27. $set = SetMealService::getById($setId);
  28. if (empty($set)) {
  29. util::fail('没有找到套餐');
  30. }
  31. $shopAdminId = $this->shopAdminId;
  32. $addData['shopAdminId'] = $shopAdminId;
  33. $shopAdmin = $this->shopAdmin;
  34. $adminName = $shopAdmin['name'] ?? '';
  35. $addData['shopAdminName'] = $adminName;
  36. $price = $set['price'];
  37. $prePrice = $price;
  38. $actPrice = $price;
  39. $addData['actPrice'] = $actPrice;
  40. $addData['prePrice'] = $prePrice;
  41. $addData['merchantId'] = $this->sjId;
  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. $openId = $user['miniOpenId'];
  55. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  56. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId;//将流水类型、优惠卷传过去
  57. $wx = Yii::getAlias("@vendor/weixin");
  58. require_once($wx . '/lib/WxPay.Api.php');
  59. require_once($wx . '/example/WxPay.JsApiPay.php');
  60. $input = new \WxPayUnifiedOrder();
  61. $input->SetBody($name);
  62. $input->SetOut_trade_no($orderSn);
  63. $input->SetTotal_fee($totalFee * 100);
  64. $input->SetTime_start(date("YmdHis", $now));
  65. $input->SetAttach($attach);
  66. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  67. $input->SetNotify_url($this->frontUrl . '/notice/weixin-callback/');
  68. $input->SetTrade_type("JSAPI");
  69. //花卉宝代为申请的微信支付
  70. $merchantExtend = WxOpenClass::getWxInfo();
  71. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  72. $input->SetSub_openid($openId);
  73. } else {
  74. $input->SetOpenid($openId);
  75. }
  76. //小程序使用miniAppId
  77. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  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. $newParams['merchantId'] = $this->sjId;
  84. util::success($newParams);
  85. }
  86. }