OrderController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. namespace mall\controllers;
  3. use biz\product\classes\ProductClass;
  4. use bizHd\custom\classes\HdClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizMall\custom\classes\CustomClass;
  7. use bizMall\goods\classes\GoodsClass;
  8. use bizMall\order\classes\OrderClass;
  9. use bizMall\order\classes\OrderGoodsClass;
  10. use bizMall\order\services\OrderService;
  11. use bizMall\promote\services\CouponService;
  12. use bizMall\saas\services\RegionService;
  13. use bizMall\user\services\UserService;
  14. use common\components\dict;
  15. use common\components\httpUtil;
  16. use common\components\orderSn;
  17. use common\components\stringUtil;
  18. use common\services\xhPayToolService;
  19. use Yii;
  20. use common\components\util;
  21. class OrderController extends BaseController
  22. {
  23. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  24. public function actionFreight()
  25. {
  26. $get = Yii::$app->request->get();
  27. $shop = $this->shop;
  28. $shopLat = isset($shop->lat) ? $shop->lat : '';
  29. $shopLong = isset($shop->long) ? $shop->long : '';
  30. if (empty($shopLat) || empty($shopLong)) {
  31. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  32. }
  33. $userLat = $get['lat'] ?? '';
  34. $userLong = $get['long'] ?? '';
  35. if (empty($userLat) || empty($userLong)) {
  36. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  37. }
  38. $weight = $get['weight'] ?? 0;
  39. if (empty($weight)) {
  40. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  41. }
  42. $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
  43. util::success($respond);
  44. }
  45. //购买花材 ssh 20220511
  46. public function actionBuyItem()
  47. {
  48. $post = Yii::$app->request->post();
  49. $post['sjId'] = $this->sjId;
  50. $post['shopId'] = $this->shopId;
  51. $post['payWay'] = $this->isWx == true ? 0 : 1;
  52. $post['mainId'] = $this->mainId ?? 0;
  53. $post['userId'] = $this->userId ?? 0;
  54. $custom = CustomClass::getByCondition(['shopId' => $this->shopId, 'userId' => $this->userId], true);
  55. $customId = $custom->id ?? 0;
  56. if (!empty($custom)) {
  57. $post['customId'] = $customId;
  58. $customName = $custom->name ?? '';
  59. $post['customName'] = $customName;
  60. $post['customNamePy'] = stringUtil::py($customName);
  61. $hdId = $custom->hdId ?? 0;
  62. $hd = HdClass::getById($hdId, true);
  63. if (!empty($hd)) {
  64. $post['hdId'] = $hd->id ?? 0;
  65. $post['hdName'] = $hd->name ?? '';
  66. }
  67. }
  68. $now = time();
  69. //订单30分钟后过期
  70. $expireTime = $now + 1800;
  71. $post['deadline'] = $expireTime;
  72. //商城
  73. $post['fromType'] = dict::getDict('fromType', 'mall');
  74. $productJson = $post['product'] ?? '';
  75. if (empty($productJson) && empty($groupId)) {
  76. util::fail('请选择商品');
  77. }
  78. $productList = json_decode($productJson, true);
  79. if (empty($productList)) {
  80. util::fail('请选择商品');
  81. }
  82. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  83. $productInfoList = productClass::getByIds($ids, null, 'id');
  84. if (!empty($productInfoList)) {
  85. $presellData = [];
  86. $nowTime = strtotime(date("Y-m-d"));
  87. foreach ($productInfoList as $currentInfo) {
  88. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  89. util::fail('只能选择同一家的商品哦');
  90. }
  91. $currentName = $currentInfo['name'] ?? '';
  92. $presell = $currentInfo['presell'] ?? 0;
  93. $presellData[] = $presell;
  94. if ($presell == 1) {
  95. if (isset($post['reachDate']) == false || empty($post['reachDate'])) {
  96. util::fail('预售花材,请选择配送日期');
  97. }
  98. $sendTimeWant = $post['reachDate'];
  99. if (strtotime($sendTimeWant) < $nowTime) {
  100. util::fail('不能选择过去时间');
  101. }
  102. $hasDateStr = $currentInfo['presellDate'] ?? [];
  103. $hasDate = explode(',', trim($hasDateStr));
  104. if (empty($hasDate)) {
  105. util::fail('预售日期有问题,请提醒门店');
  106. }
  107. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  108. util::fail("{$currentName} 在{$sendTimeWant}没有预售");
  109. }
  110. $post['presell'] = 1;
  111. }
  112. }
  113. if (count($productInfoList) != count($ids)) {
  114. util::fail('存在无效商品');
  115. }
  116. $presellData = array_unique($presellData);
  117. if (count($presellData) > 1) {
  118. util::fail('预售和非预售花材不能同时下单');
  119. }
  120. } else {
  121. util::fail('花材不存在');
  122. }
  123. $connection = Yii::$app->db;
  124. $transaction = $connection->beginTransaction();
  125. try {
  126. $post['product'] = $productList;
  127. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  128. $post['deadline'] = time() + $orderValidTime;
  129. $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  130. $post['needPrint'] = dict::getDict('needPrint', 'need');
  131. $user = $this->user;
  132. $post['bookMobile'] = $user->mobile ?? '';
  133. $post['bookName'] = $user->name ?? '';
  134. $return = OrderService::createOrder($post);
  135. $transaction->commit();
  136. $orderSn = $return->orderSn ?? '';
  137. $orderPrice = $return->orderPrice ?? 0;
  138. $id = $return->id ?? 0;
  139. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id]);
  140. } catch (\Exception $e) {
  141. $transaction->rollBack();
  142. Yii::error("失败原因:" . $e->getMessage());
  143. util::fail('下单失败');
  144. }
  145. }
  146. //商城下单操作 ssh 2019.12.3
  147. public function actionCreateOrder()
  148. {
  149. $post = Yii::$app->request->post();
  150. $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
  151. $goodsStyleId = isset($post['goodsStyleId']) ? $post['goodsStyleId'] : 0;
  152. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  153. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  154. if (empty($goodsId)) {
  155. util::fail('请选择商品');
  156. }
  157. $goodsInfo = GoodsClass::getGoodsInfo($goodsId);
  158. if (empty($goodsInfo)) {
  159. util::fail('没有找到商品');
  160. }
  161. $stock = $goodsInfo['stock'] && is_numeric($goodsInfo['stock']) ? $goodsInfo['stock'] : 0;
  162. $stockSet = $goodsInfo['stockSet'] && is_numeric($goodsInfo['stockSet']) ? $goodsInfo['stockSet'] : 0;
  163. if ($stock <= 0 && $stockSet == 0) {
  164. util::fail('库存不足');
  165. }
  166. //事务处理
  167. $connection = Yii::$app->db;
  168. $transaction = $connection->beginTransaction();
  169. try {
  170. $custom = CustomClass::getByCondition(['shopId' => $this->shopId, 'userId' => $this->userId], true);
  171. $customId = $custom->id ?? 0;
  172. if (!empty($custom)) {
  173. $post['customId'] = $customId;
  174. $customName = $custom->name ?? '';
  175. $post['customName'] = $customName;
  176. $post['customNamePy'] = stringUtil::py($customName);
  177. $hdId = $custom->hdId ?? 0;
  178. $hd = HdClass::getById($hdId, true);
  179. if (!empty($hd)) {
  180. $post['hdId'] = $hd->id ?? 0;
  181. $post['hdName'] = $hd->name ?? '';
  182. }
  183. }
  184. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  185. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  186. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  187. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  188. $post['bookMobile'] = $bookMobile;
  189. $post['payWay'] = $this->isWx == true ? 0 : 1;
  190. //计算运费
  191. $sendDistance = 0;
  192. $sendCost = 0;
  193. $shop = $this->shop;
  194. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  195. $lat = $post['lat'] ?? '';
  196. $lng = $post['long'] ?? '';
  197. if (empty($lat) || empty($lng)) {
  198. util::fail('请填写收花地址');
  199. }
  200. $address = $post['address'] ?? '';
  201. $floor = $post['floor'] ?? '';
  202. $fullAddress = $address . $floor;
  203. $post['fullAddress'] = $fullAddress;
  204. $shopLat = $shop->lat ?? '';
  205. $shopLong = $shop->long ?? '';
  206. if (empty($shopLat) || empty($shopLong)) {
  207. util::fail('请完成门店地址');
  208. }
  209. //花束重量默认1.5
  210. $weight = 1.5;
  211. $disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
  212. $sendCost = $disRespond['fee'] ?? 0;
  213. $sendDistance = $disRespond['distance'] ?? 0;
  214. }
  215. $post['sendDistance'] = $sendDistance;
  216. $post['sendCost'] = $sendCost;
  217. $post['sjId'] = $this->sjId;
  218. $post['shopId'] = $this->shopId;
  219. $mainId = $this->mainId;
  220. $post['mainId'] = $mainId;
  221. $post['userId'] = $this->userId;
  222. //计算总金额
  223. $unitPrice = $goodsInfo['price'];
  224. $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : [];
  225. if (!empty($goodsStyleId)) {
  226. if (empty($goodsStyleList)) {
  227. util::fail('商品款式没有找到');
  228. }
  229. $styleIdList = array_keys($goodsStyleList);
  230. if (in_array($goodsStyleId, $styleIdList) == false) {
  231. util::fail('商品款式没有找到!');
  232. }
  233. $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
  234. }
  235. $goodsPrice = $unitPrice * $goodsNum;
  236. $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  237. //非门店订单
  238. $post['store'] = 0;
  239. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  240. $sourceType = $this->isWx ? 0 : 1;
  241. if (httpUtil::isMiniProgram()) {
  242. $sourceType = 2;
  243. }
  244. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  245. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  246. $actPrice = $discountData['price'];
  247. $post['discountType'] = $discountData['discountType'];
  248. $post['discountAmount'] = $discountData['discountAmount'];
  249. //非自取订单考虑配送时间
  250. $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  251. $sendType = $post['sendType'] ?? 0;
  252. if ($sendType != 1) {
  253. if (isset($post['reachPeriod']) == false || empty($post['reachPeriod'])) {
  254. util::fail('请选择配送时间');
  255. }
  256. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  257. }
  258. $now = time();
  259. $expireTime = $now + 1800;//订单30分钟后过期
  260. $post['deadline'] = $expireTime;
  261. $orderSn = orderSn::getOrderSn();
  262. $post['orderSn'] = $orderSn;
  263. $post['fromType'] = dict::getDict('fromType', 'mall');
  264. $post['prePrice'] = $prePrice;
  265. $post['orderPrice'] = $actPrice;
  266. $post['actPrice'] = $actPrice;
  267. $post['realPrice'] = $actPrice;
  268. $post['mainPay'] = $actPrice;
  269. $post['cash'] = 0;
  270. $post['mainId'] = $mainId;
  271. $post['needPrint'] = dict::getDict('needPrint', 'need');
  272. $order = OrderClass::addOrder($post);
  273. $orderId = $order['id'];
  274. $orderSn = $order['orderSn'];
  275. $currentCover = isset($goodsInfo['shortImgList']) && !empty($goodsInfo['shortImgList']) ? current($goodsInfo['shortImgList']) : '';
  276. $flower = $goodsInfo['flower'] ?? 0;
  277. $price = bcmul($goodsNum, $unitPrice, 2);
  278. $data = [
  279. 'orderId' => $orderId,
  280. 'goodsId' => $goodsId,
  281. 'flower' => $flower,
  282. 'userId' => $this->userId,
  283. 'sjId' => $this->sjId,
  284. 'mainId' => $mainId,
  285. 'name' => $goodsInfo['name'] ?? '',
  286. 'cover' => $currentCover,
  287. 'unitPrice' => $unitPrice,
  288. 'num' => $goodsNum,
  289. 'orderSn' => $orderSn,
  290. 'price' => $price,
  291. 'createTime' => date("Y-m-d H:i:s"),
  292. ];
  293. OrderGoodsClass::addData($data);
  294. $transaction->commit();
  295. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'id' => $orderId]);
  296. } catch (Exception $e) {
  297. $transaction->rollBack();
  298. Yii::info("失败原因:" . $e->getMessage());
  299. util::fail('下单失败');
  300. }
  301. }
  302. //下单要用到的相关信息 ssh 2019.12.6
  303. public function actionOrderRelate()
  304. {
  305. $regionTree = RegionService::tree();
  306. //门店名称
  307. $shop = isset($this->shop) && !empty($this->shop) ? $this->shop->attributes : [];
  308. $sjName = $shop['merchantName'] ?? '';
  309. $shopName = $shop['shopName'] ?? '';
  310. $default = $shop['default'] ?? 0;
  311. $shop['showName'] = $default == 1 && $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  312. $freight = ['first' => 5, 'add' => 2];
  313. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  314. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  315. util::success($out);
  316. }
  317. //余额支付 ssh 2019.12.6
  318. public function actionBalancePay()
  319. {
  320. $post = Yii::$app->request->post();
  321. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  322. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  323. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  324. //验证优惠券是否还有效
  325. if (!empty($couponId)) {
  326. //CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  327. }
  328. $order = OrderService::getByOrderSn($orderSn);
  329. $userId = $this->userId;
  330. $user = UserService::getUserInfo($userId);
  331. //验证支付密码是否正确
  332. UserService::validPayPassword($payPassword, $user);
  333. //支付前验证订单有效性
  334. OrderService::checkBeforePay($order);
  335. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  336. $totalFee = $order['prePrice'];
  337. $sourceType = 0;
  338. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  339. $actPrice = $discountData['price'];
  340. $callbackParams = [];
  341. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  342. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  343. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  344. }
  345. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  346. public function actionWxPay()
  347. {
  348. ini_set('date.timezone', 'Asia/Shanghai');
  349. $post = Yii::$app->request->post();
  350. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  351. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  352. $order = OrderService::getByOrderSn($orderSn);
  353. $orderId = $order['id'];
  354. //验证优惠券是否还有效
  355. if (!empty($couponId)) {
  356. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  357. }
  358. //支付前验证订单有效性
  359. OrderService::checkBeforePay($order);
  360. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  361. $totalFee = $order['mainPay'];
  362. //小程序使用miniOpenId
  363. $openId = $this->user['miniOpenId'] ?? '';
  364. if (empty($openId)) {
  365. $openId = $post['miniOpenId'] ?? '';
  366. }
  367. if (empty($openId)) {
  368. util::fail('没有openId');
  369. }
  370. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  371. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  372. $wxPayType = 0;
  373. if (httpUtil::isMiniProgram()) {
  374. $wxPayType = 1;
  375. }
  376. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  377. //订单30分钟后过期
  378. $now = time();
  379. $expireTime = $now + 1800;
  380. $wx = Yii::getAlias("@vendor/weixin");
  381. require_once($wx . '/lib/WxPay.Api.php');
  382. require_once($wx . '/example/WxPay.JsApiPay.php');
  383. $input = new \WxPayUnifiedOrder();
  384. $input->SetBody($name);
  385. $input->SetOut_trade_no($orderSn);
  386. $input->SetTotal_fee($totalFee * 100);
  387. $input->SetTime_start(date("YmdHis", $now));
  388. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  389. $input->SetTime_expire(date("YmdHis", $expireTime));
  390. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  391. $input->SetTrade_type("JSAPI");
  392. $sjExtend = WxOpenClass::getMallWxInfo();
  393. $input->SetOpenid($openId);
  394. //小程序使用miniAppId
  395. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  396. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  397. $tools = new \JsApiPay();
  398. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  399. $newParams = json_decode($jsApiParameters, true);
  400. //已请求微信不能修改价格
  401. $updateData = [];
  402. $updateData['modPrice'] = 0;
  403. if (empty($order['deadline'])) {
  404. $updateData['deadline'] = $expireTime;
  405. }
  406. OrderService::updateById($orderId, $updateData);
  407. util::success($newParams);
  408. }
  409. //快捷付款订单
  410. public function actionFastPay()
  411. {
  412. ini_set('date.timezone', 'Asia/Shanghai');
  413. $post = Yii::$app->request->post();
  414. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  415. $shopId = $this->shopId;
  416. $post['shopId'] = $shopId;
  417. $post['mainId'] = $this->mainId;
  418. $post['sjId'] = $this->sjId;
  419. $post['store'] = 0;
  420. $post['customId'] = $this->customId;
  421. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  422. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  423. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  424. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  425. $post['bookMobile'] = $bookMobile;
  426. $prePrice = round($post['prePrice'], 2);
  427. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  428. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => 0, 'couponId' => $couponId, 'userId' => $this->userId]);
  429. $actPrice = $discountData['price'];
  430. $post['discountType'] = $discountData['discountType'];
  431. $post['discountAmount'] = $discountData['discountAmount'];
  432. $post['orderPrice'] = $actPrice;
  433. $post['actPrice'] = $actPrice;
  434. $post['realPrice'] = $actPrice;
  435. $post['mainPay'] = $actPrice;
  436. $post['payStyle'] = 0;
  437. $post['sjId'] = $this->sjId;
  438. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  439. $now = time();
  440. $expireTime = $now + 300;//订单5分钟后过期
  441. $post['createTime'] = date("Y-m-d H:i:s", $now);
  442. $post['deadline'] = $expireTime;
  443. if ($actPrice <= 0) {
  444. util::fail('请填写正确的金额');
  445. }
  446. //微信支付订单提交不能修改价格
  447. $post['modPrice'] = $this->isWx ? 0 : 1;
  448. $post['goodsNum'] = 1;
  449. $post['fromType'] = $post['fromType'] ?? 1;
  450. $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : '00-00-00';
  451. $post['onlinePay'] = dict::getDict('onlinePay', 'yes');
  452. //前端未修改时这边做兼容
  453. if (isset($post['receiveAddress']) && !empty($post['receiveAddress'])) {
  454. $post['address'] = $post['receiveAddress'];
  455. if (isset($post['receiveProvince']) && !empty($post['receiveProvince'])) {
  456. $post['province'] = $post['receiveProvince'];
  457. }
  458. if (isset($post['receiveCity']) && !empty($post['receiveCity'])) {
  459. $post['city'] = $post['receiveCity'];
  460. }
  461. if (isset($post['receiveFloor']) && !empty($post['receiveFloor'])) {
  462. $post['floor'] = $post['receiveFloor'];
  463. }
  464. if (isset($post['latitude']) && !empty($post['latitude'])) {
  465. $post['lat'] = $post['latitude'];
  466. }
  467. if (isset($post['longitude']) && !empty($post['longitude'])) {
  468. $post['long'] = $post['longitude'];
  469. }
  470. $city = $post['receiveCity'] ?? '';
  471. $address = $post['receiveAddress'] ?? '';
  472. $floor = $post['receiveFloor'] ?? '';
  473. $post['fullAddress'] = $city . $address . $floor;
  474. }
  475. $order = OrderClass::addOrder($post);
  476. $orderId = $order['id'];
  477. $orderSn = $order['orderSn'];
  478. $sjId = $this->sjId;
  479. if ($payWay == 0) {
  480. $orderId = $order['id'];
  481. $name = '购买商品';
  482. $totalFee = $actPrice;
  483. $user = UserService::getById($this->userId);
  484. //小程序使用miniOpenId
  485. $openId = $user['miniOpenId'];
  486. if (empty($openId)) {
  487. $openId = isset($post['miniOpenId']) && !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  488. }
  489. if (empty($openId)) {
  490. util::fail('没有微信信息');
  491. }
  492. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  493. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  494. $wxPayType = 0;
  495. if (httpUtil::isMiniProgram()) {
  496. $wxPayType = 1;
  497. }
  498. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  499. $wx = Yii::getAlias("@vendor/weixin");
  500. require_once($wx . '/lib/WxPay.Api.php');
  501. require_once($wx . '/example/WxPay.JsApiPay.php');
  502. $input = new \WxPayUnifiedOrder();
  503. $input->SetBody($name);
  504. $input->SetOut_trade_no($orderSn);
  505. $input->SetTotal_fee($totalFee * 100);
  506. $input->SetTime_start(date("YmdHis", $now));
  507. $input->SetAttach($attach);
  508. //设置订单有效期5分钟
  509. $input->SetTime_expire(date("YmdHis", $expireTime));
  510. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  511. $input->SetTrade_type("JSAPI");
  512. $sjExtend = WxOpenClass::getMallWxInfo();
  513. $input->SetOpenid($openId);
  514. //小程序使用miniAppId
  515. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  516. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  517. $tools = new \JsApiPay();
  518. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  519. $newParams = json_decode($jsApiParameters, true);
  520. $newParams['orderId'] = $orderId;
  521. $newParams['orderSn'] = $orderSn;
  522. $newParams['totalPrice'] = $actPrice;
  523. util::success($newParams);
  524. } elseif ($payWay == 1) {
  525. $aliPId = '2021003122664465';
  526. $aliKey = 'MIIEpAIBAAKCAQEAnefrChYa5FUbGTlZNMTDNG531WrVP+TBPw3/hSmycRlBE/1sg9EryM0S3a9OQKQenRdYOoalqkkBuzt41ygJUC5hfNeaiV2OzphIiAyxUaiL1mSftP5B8joBN3HGG2AKbcvKlWcFoeiGY4ZmV/K0t3fqj/uPP6N/pEtnx6X4Ifgz4WGCDKUXKqT1Pyk2rqyIpel2EYk+HtC+9iicfac9kKQQiksDopb7sYosttdk64Dmjp2nMLDSpR+KOZUaqB39MEeHcJGeTzdVrp+Z9wW/NcJzSkSUb9juA0YjmogdbiKltykVHTbP6J0uPyByvzfyyY22QPqw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/CJRQbahz6B3Sc4NOiFUQioM30qDKbDibzUq95u5PtQOx69+/h68tA2nj7ILZOXoh6/KF6LM9WK0QctY0HR5Uy4Uz8YkY/Y0L/LExSkNdpYqbpBc8DdIsKYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  527. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  528. if (getenv('YII_ENV') == 'production') {
  529. if ($shopId == 1) {
  530. //线上国兰使用老的支付宝账号
  531. $aliPId = '2017041906821571';
  532. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNN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Wiij1oCZF+iJdKkonHoDbDd+Q0ykcpQ+5Gv07iiT8wJYFR/0j8g2kfql7bVQhGSBeBMS5sawKR3CvkfW73WX+CNnf0PklTY0kTyt6WRXDnKSeyxFVwSZ7g==';
  533. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsz3m9juAR1xew4DY6c34gvbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  534. }
  535. }
  536. $config = [
  537. //应用ID,您的APPID。
  538. 'app_id' => $aliPId,
  539. //商户私钥,您的原始格式RSA私钥
  540. 'merchant_private_key' => $aliKey,
  541. //异步通知地址
  542. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  543. //同步跳转
  544. 'return_url' => "",
  545. //编码格式
  546. 'charset' => "UTF-8",
  547. //签名方式
  548. 'sign_type' => "RSA2",
  549. //支付宝网关
  550. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  551. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  552. 'alipay_public_key' => $aliPublicKey,
  553. ];
  554. Yii::info(json_encode($config) . ' aplipay config');
  555. $aliWap = Yii::getAlias("@vendor/alipayWap");
  556. require_once($aliWap . '/wappay/service/AlipayTradeService.php');
  557. require_once($aliWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  558. $totalFee = $order['realPrice'];
  559. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  560. $subject = '购买商品';//订单名称,必填
  561. $total_amount = $totalFee;//付款金额,必填
  562. $body = '';//商品描述,可空
  563. $timeout_express = "1m";//超时时间
  564. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  565. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  566. $passBackParams = urlencode($passBackParams);
  567. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  568. $payRequestBuilder->setBody($body);
  569. $payRequestBuilder->setSubject($subject);
  570. $payRequestBuilder->setOutTradeNo($out_trade_no);
  571. $payRequestBuilder->setTotalAmount($total_amount);
  572. $payRequestBuilder->setTimeExpress($timeout_express);
  573. //在异步通知时将该参数原样返回
  574. $payRequestBuilder->setPassbackParams($passBackParams);
  575. //同步通知
  576. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  577. //异步通知
  578. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  579. $payResponse = new \AlipayTradeService($config);
  580. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  581. //直接将支付宝的html返回给前端
  582. echo $result;
  583. } elseif ($payWay == 2) {
  584. //余额支付,验证支付密码是否正确
  585. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  586. $user = UserService::getUserInfo($this->userId);
  587. UserService::validPayPassword($payPassword, $user);
  588. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  589. $callbackParams = [];
  590. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  591. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  592. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  593. } else {
  594. util::fail('无效的支付方式');
  595. }
  596. }
  597. //订单列表 ssh 2019.12.12
  598. public function actionList()
  599. {
  600. $userId = $this->userId;
  601. if (empty($userId)) {
  602. util::success(['list' => [], 'moreData' => 0, 'totalNum' => 0, 'totalPage' => 0]);
  603. }
  604. $where = ['userId' => $userId];
  605. $list = OrderService::getOrderList($where);
  606. util::success($list);
  607. }
  608. //订单评价
  609. public function actionComment()
  610. {
  611. $post = Yii::$app->request->post();
  612. $id = $post['id'];
  613. $order = OrderClass::getById($id);
  614. if ($order['grade'] > 0) {
  615. util::fail('您已经评过了');
  616. }
  617. OrderService::comment($post);
  618. util::complete('提交成功');
  619. }
  620. //订单详情 ssh 2019.12.16
  621. public function actionDetail()
  622. {
  623. $id = Yii::$app->request->get('id', 0);
  624. $detail = OrderClass::getOrderById($id);
  625. util::success($detail);
  626. }
  627. //获取详情 ssh 20220512
  628. public function actionInfo()
  629. {
  630. $sn = Yii::$app->request->get('orderSn', '');
  631. $detail = OrderClass::getOrderBySn($sn);
  632. util::success($detail);
  633. }
  634. }