OrderController.php 30 KB

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