OrderController.php 31 KB

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