OrderController.php 30 KB

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