OrderController.php 26 KB

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