OrderController.php 22 KB

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