OrderController.php 26 KB

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