OrderController.php 32 KB

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