OrderController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. namespace mall\controllers;
  3. use biz\goods\services\CategoryService;
  4. use biz\goods\services\GoodsCategoryService;
  5. use biz\goods\services\GoodsService;
  6. use biz\merchant\services\MerchantExtendService;
  7. use biz\merchant\services\MerchantService;
  8. use biz\merchant\services\ShopService;
  9. use biz\order\services\OrderGoodsService;
  10. use biz\order\services\OrderService;
  11. use biz\promote\services\CouponService;
  12. use biz\saas\services\RegionService;
  13. use biz\user\services\UserAssetService;
  14. use biz\user\services\UserService;
  15. use common\components\configDict;
  16. use common\components\httpUtil;
  17. use common\components\stringUtil;
  18. use common\services\xhPayToolService;
  19. use Yii;
  20. use yii\web\Controller;
  21. use common\components\util;
  22. class OrderController extends BaseController
  23. {
  24. public $withoutLogin = ['order-relate', 'fast-pay'];
  25. //商城下单操作 shish 2019.12.3
  26. public function actionCreateOrder()
  27. {
  28. $post = Yii::$app->request->post();
  29. $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
  30. $goodsStyleId = isset($post['goodsStyleId']) ? $post['goodsStyleId'] : 0;
  31. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  32. if (empty($goodsId)) {
  33. util::fail('请选择商品');
  34. }
  35. $goodsInfo = GoodsService::getGoodsInfo($goodsId);
  36. if (empty($goodsInfo)) {
  37. util::fail('没有找到商品');
  38. }
  39. //确认是否要配送的商品
  40. $needSend = isset($goodsInfo['needSend']) && $goodsInfo['needSend'] == 1 ? 1 : 0;
  41. $post['userId'] = $this->userId;
  42. $user = $this->user;
  43. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  44. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  45. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  46. $post['bookMobile'] = $bookMobile;
  47. $post['payWay'] = $this->isWx == true ? 0 : 1;
  48. $defaultShopId = MerchantService::getDefaultShopId($this->merchant);
  49. if (empty($defaultShopId)) {
  50. util::fail('没有找到门店');
  51. }
  52. $shopInfo = ShopService::getById($defaultShopId);
  53. //计算运费
  54. $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
  55. $sendCost = 0;
  56. if ($needSend == 1) {
  57. $lat = $post['receiveLat'];//收货人纬度
  58. $lng = $post['receiveLong'];//收货人经度
  59. $jsStatDistance = $post['sendDistance'];
  60. $sendCost = ShopService::getSendCost($lat, $lng, $jsStatDistance, $shopInfo);
  61. }
  62. $post['sendCost'] = $sendCost;
  63. $post['merchantId'] = $this->merchantId;
  64. $post['shopId'] = $defaultShopId;
  65. //计算总金额
  66. $unitPrice = $goodsInfo['price'];
  67. $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : [];
  68. if (!empty($goodsStyleId)) {
  69. if (empty($goodsStyleList)) {
  70. util::fail('商品款式没有找到');
  71. }
  72. $styleIdList = array_keys($goodsStyleList);
  73. if (in_array($goodsStyleId, $styleIdList) == false) {
  74. util::fail('商品款式没有找到!');
  75. }
  76. $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
  77. }
  78. $goodsPrice = $unitPrice * $goodsNum;
  79. $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  80. $showPrice = $prePrice;
  81. //非门店订单
  82. $store = 0;
  83. $post['store'] = $store;
  84. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  85. $sourceType = $this->isWx ? 0 : 1;
  86. if (httpUtil::isMiniProgram()) {
  87. $sourceType = 2;
  88. }
  89. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  90. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  91. $actPrice = $discountData['price'];
  92. $post['discountType'] = $discountData['discountType'];
  93. $post['discountAmount'] = $discountData['discountAmount'];
  94. $now = time();
  95. $expireTime = $now + 1800;//订单30分钟后过期
  96. $post['deadline'] = $expireTime;
  97. $post['prePrice'] = $prePrice;
  98. $post['actPrice'] = $actPrice;
  99. $order = OrderService::addOrder($post);
  100. $orderId = $order['id'];
  101. $orderSn = $order['orderSn'];
  102. $data = [
  103. 'orderId' => $orderId,
  104. 'goodsId' => $goodsId,
  105. 'userId' => $this->userId,
  106. 'merchantId' => $this->merchantId,
  107. 'title' => $goodsInfo['goodsName'],
  108. 'cover' => isset($goodsInfo['shortImgList']) && !empty($goodsInfo['shortImgList']) ? current($goodsInfo['shortImgList']) : '',
  109. 'unitPrice' => $unitPrice,
  110. 'num' => $goodsNum,
  111. 'goodsStyleName' => isset($goodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $goodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
  112. 'goodsStyleId' => $goodsStyleId,
  113. 'createTime' => date("Y-m-d H:i:s"),
  114. ];
  115. OrderGoodsService::add($data);//创建订单商品列表
  116. util::success(['orderSn' => $orderSn, 'totalPrice' => $showPrice, 'couponId' => $couponId]);
  117. }
  118. //下单要用到的相关信息 shish 2019.12.6
  119. public function actionOrderRelate()
  120. {
  121. $regionTree = RegionService::tree();
  122. $shop = ShopService::getDefaultShop($this->merchant);
  123. $freight = MerchantExtendService::getFreight($this->merchantExtend);
  124. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  125. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->merchant];
  126. util::success($out);
  127. }
  128. //余额支付 shish 2019.12.6
  129. public function actionBalancePay()
  130. {
  131. $post = Yii::$app->request->post();
  132. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  133. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  134. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  135. $order = OrderService::getByOrderSn($orderSn);
  136. $userId = $this->userId;
  137. $user = UserService::getUserInfo($userId, false);
  138. //验证支付密码是否正确
  139. UserService::validPayPassword($payPassword, $user);
  140. //验证订单有效性
  141. OrderService::check($order, $this->userId);
  142. $typeList = configDict::getConfig('capitalType');
  143. $capitalType = $typeList['xhOrder']['id'];
  144. $totalFee = $order['actPrice'];
  145. $sourceType = 0;
  146. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  147. $actPrice = $discountData['price'];
  148. $respond = xhPayToolService::balancePay($orderSn, $actPrice, $capitalType, $couponId);
  149. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  150. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  151. }
  152. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  153. public function actionWxPay()
  154. {
  155. ini_set('date.timezone', 'Asia/Shanghai');
  156. $post = Yii::$app->request->post();
  157. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  158. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  159. $order = OrderService::getByOrderSn($orderSn);
  160. $orderId = $order['id'];
  161. //验证订单有效性
  162. OrderService::check($order, $this->userId);
  163. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  164. $totalFee = $order['actPrice'];
  165. //小程序使用miniOpenId
  166. if (httpUtil::isMiniProgram()) {
  167. $openId = $this->user['miniOpenId'];
  168. } else {
  169. $openId = $this->user['openId'];
  170. }
  171. $typeList = configDict::getConfig('capitalType');
  172. $capitalType = $typeList['xhOrder']['id'];
  173. //将流水类型、代金劵传过去
  174. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType;
  175. //订单30分钟后过期
  176. $now = time();
  177. $expireTime = $now + 1800;
  178. $wx = Yii::getAlias("@vendor/weixin");
  179. require_once($wx . '/lib/WxPay.Api.php');
  180. require_once($wx . '/example/WxPay.JsApiPay.php');
  181. $input = new \WxPayUnifiedOrder();
  182. $input->SetBody($name);
  183. $input->SetOut_trade_no($orderSn);
  184. $input->SetTotal_fee($totalFee * 100);
  185. $input->SetTime_start(date("YmdHis", $now));
  186. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  187. $input->SetTime_expire(date("YmdHis", $expireTime));
  188. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  189. $input->SetTrade_type("JSAPI");
  190. //花卉宝代为申请的微信支付
  191. $merchantExtend = $this->merchantExtend;
  192. if ($merchantExtend['wxPayApply'] == 1) {
  193. $input->SetSub_openid($openId);
  194. } else {
  195. $input->SetOpenid($openId);
  196. }
  197. //小程序使用miniAppId
  198. if (httpUtil::isMiniProgram()) {
  199. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  200. }
  201. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  202. $tools = new \JsApiPay();
  203. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  204. $newParams = json_decode($jsApiParameters, true);
  205. //已请求微信不能修改价格
  206. $updateData = [];
  207. $updateData['modPrice'] = 0;
  208. if (empty($order['deadline'])) {
  209. $updateData['deadline'] = $expireTime;
  210. }
  211. OrderService::updateById($orderId, $updateData);
  212. util::success($newParams);
  213. }
  214. //快捷付款订单
  215. public function actionFastPay()
  216. {
  217. ini_set('date.timezone', 'Asia/Shanghai');
  218. $post = Yii::$app->request->post();
  219. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  220. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->merchant);
  221. //验证门店是否有效
  222. $shopInfo = ShopService::getById($shopId);
  223. ShopService::valid($shopInfo, $this->merchantId);
  224. //默认门店订单
  225. $store = isset($post['store']) ? $post['store'] : 1;
  226. $post['store'] = $store;
  227. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  228. $sourceType = $this->isWx ? 0 : 1;
  229. if (httpUtil::isMiniProgram()) {
  230. $sourceType = 2;
  231. }
  232. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  233. $post['userId'] = $this->userId;
  234. $user = $this->user;
  235. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  236. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  237. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  238. $post['bookMobile'] = $bookMobile;
  239. $prePrice = round($post['prePrice'], 2);
  240. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  241. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  242. $actPrice = $discountData['price'];
  243. $post['discountType'] = $discountData['discountType'];
  244. $post['discountAmount'] = $discountData['discountAmount'];
  245. $post['actPrice'] = $actPrice;
  246. $post['payStyle'] = 0;
  247. $post['merchantId'] = $this->merchantId;
  248. $now = time();
  249. $expireTime = $now + 300;//订单5分钟后过期
  250. $post['createTime'] = date("Y-m-d H:i:s", $now);
  251. $post['deadline'] = $expireTime;
  252. if ($actPrice <= 0) {
  253. util::fail('请填写正确的金额');
  254. }
  255. //微信支付订单提交不能修改价格
  256. $post['modPrice'] = $this->isWx ? 0 : 1;
  257. $post['sourceType'] = $sourceType;
  258. $order = OrderService::addOrder($post);
  259. $orderId = $order['id'];
  260. $orderSn = $order['orderSn'];
  261. $merchantId = $this->merchantId;
  262. if ($payWay == 0) {
  263. $orderId = $order['id'];
  264. $name = '购买商品';
  265. $totalFee = $actPrice;
  266. $user = UserService::getById($this->userId);
  267. $openId = isset($user['openId']) ? $user['openId'] : 0;
  268. if (httpUtil::isMiniProgram()) {
  269. //小程序使用miniOpenId
  270. $openId = $user['miniOpenId'];
  271. }
  272. if (empty($openId)) {
  273. util::fail('没有找到客户的openId');
  274. }
  275. $typeList = configDict::getConfig('capitalType');
  276. $capitalType = $typeList['xhOrder']['id'];
  277. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId;//将流水类型、优惠卷传过去
  278. $wx = Yii::getAlias("@vendor/weixin");
  279. require_once($wx . '/lib/WxPay.Api.php');
  280. require_once($wx . '/example/WxPay.JsApiPay.php');
  281. $input = new \WxPayUnifiedOrder();
  282. $input->SetBody($name);
  283. $input->SetOut_trade_no($orderSn);
  284. $input->SetTotal_fee($totalFee * 100);
  285. $input->SetTime_start(date("YmdHis", $now));
  286. $input->SetAttach($attach);
  287. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  288. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  289. $input->SetTrade_type("JSAPI");
  290. //花卉宝代为申请的微信支付
  291. $merchantExtend = $this->merchantExtend;
  292. if ($merchantExtend['wxPayApply'] == 1) {
  293. $input->SetSub_openid($openId);
  294. } else {
  295. $input->SetOpenid($openId);
  296. }
  297. //小程序使用miniAppId
  298. if (httpUtil::isMiniProgram()) {
  299. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  300. }
  301. Yii::log('支付发起使用小程序信息' . json_encode($merchantExtend));
  302. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  303. $tools = new \JsApiPay();
  304. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  305. $newParams = json_decode($jsApiParameters, true);
  306. $newParams['orderId'] = $orderId;
  307. util::success($newParams);
  308. } elseif ($payWay == 1) {
  309. $extend = MerchantExtendService::getByMerchantId($merchantId);
  310. $config = [
  311. //应用ID,您的APPID。
  312. 'app_id' => $extend['alipayPId'],
  313. //商户私钥,您的原始格式RSA私钥
  314. 'merchant_private_key' => $extend['alipayKey'],
  315. //异步通知地址
  316. 'notify_url' => Yii::$app->params['frontUrl'] . "/notice/alipay-async",
  317. //同步跳转
  318. 'return_url' => "",
  319. //编码格式
  320. 'charset' => "UTF-8",
  321. //签名方式
  322. 'sign_type' => "RSA2",
  323. //支付宝网关
  324. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  325. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  326. 'alipay_public_key' => $extend['alipayPublicKey'],
  327. ];
  328. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  329. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  330. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  331. $totalFee = $order['actPrice'];
  332. $out_trade_no = $orderId;//商户订单号,商户网站订单系统中唯一订单号,必填
  333. $subject = '购买商品';//订单名称,必填
  334. $total_amount = $totalFee;//付款金额,必填
  335. $body = '';//商品描述,可空
  336. $timeout_express = "1m";//超时时间
  337. $typeList = configDict::getConfig('capitalType');
  338. $capitalType = $typeList['xhOrder']['id'];
  339. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  340. $passbackParams = urlencode($passbackParams);
  341. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  342. $payRequestBuilder->setBody($body);
  343. $payRequestBuilder->setSubject($subject);
  344. $payRequestBuilder->setOutTradeNo($out_trade_no);
  345. $payRequestBuilder->setTotalAmount($total_amount);
  346. $payRequestBuilder->setTimeExpress($timeout_express);
  347. //在异步通知时将该参数原样返回
  348. $payRequestBuilder->setPassbackParams($passbackParams);
  349. //同步通知
  350. $returnUrl = Yii::$app->params['frontUrl'] . "/notice/pay-alipay-sync";
  351. //异步通知
  352. $notifyUrl = Yii::$app->params['frontUrl'] . "/notice/alipay-async";
  353. $payResponse = new \AlipayTradeService($config);
  354. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  355. //直接将支付宝的html返回给前端
  356. echo $result;
  357. } elseif ($payWay == 2) {
  358. //余额支付,验证支付密码是否正确
  359. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  360. $user = UserService::getUserInfo($this->userId, false);
  361. UserService::validPayPassword($payPassword, $user);
  362. $typeList = configDict::getConfig('capitalType');
  363. $capitalType = $typeList['xhOrder']['id'];
  364. $respond = xhPayToolService::balancePay($orderSn, $actPrice, $capitalType, $couponId);
  365. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  366. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  367. } else {
  368. util::fail('无效的支付方式');
  369. }
  370. }
  371. //订单列表 shish 2019.12.12
  372. public function actionList()
  373. {
  374. $where = ['userId' => $this->userId];
  375. $list = OrderService::getOrderList($where);
  376. util::success($list);
  377. }
  378. //订单评价
  379. public function actionComment()
  380. {
  381. $post = Yii::$app->request->post();
  382. $id = $post['id'];
  383. $order = OrderService::getById($id);
  384. if ($this->userId != $order['userId']) {
  385. util::fail('您没有权限操作');
  386. }
  387. OrderService::comment($post);
  388. util::complete('提交成功');
  389. }
  390. //订单详情 shish 2019.12.16
  391. public function actionDetail()
  392. {
  393. $id = Yii::$app->request->get('id', 0);
  394. $orderSn = Yii::$app->request->get('orderSn', '');
  395. $detail = [];
  396. if (!empty($id)) {
  397. $detail = OrderService::getOrderById($id);
  398. }
  399. if (!empty($orderSn)) {
  400. $detail = OrderService::getOrderBySn($orderSn);
  401. }
  402. OrderService::valid($detail, $this->merchantId);
  403. util::success($detail);
  404. }
  405. }