OrderController.php 25 KB

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