OrderController.php 24 KB

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