OrderController.php 25 KB

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