OrderController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. namespace hd\controllers;
  3. use biz\sj\services\MerchantAssetService;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\merchant\services\ShopService;
  7. use bizHd\order\classes\OrderClass;
  8. use bizHd\order\classes\OrderSendClass;
  9. use bizHd\order\services\OrderService;
  10. use bizHd\saas\services\RegionService;
  11. use bizHd\user\services\UserService;
  12. use bizGhs\order\classes\OrderItemClass;
  13. use common\components\dict;
  14. use common\components\dateUtil;
  15. use common\services\xhPayToolService;
  16. use Yii;
  17. use common\components\util;
  18. use common\components\stringUtil;
  19. use bizHd\goods\services\GoodsService;
  20. use bizHd\merchant\services\ExpressService;
  21. use bizHd\order\services\OrderGoodsService;
  22. use bizHd\promote\services\CouponService;
  23. use common\components\httpUtil;
  24. class OrderController extends BaseController
  25. {
  26. public $guestAccessAction = ['order-relate', 'fast-pay', 'create-order', 'list'];
  27. //商城下单操作 ssh 2019.12.3
  28. public function actionCreateOrder()
  29. {
  30. $post = Yii::$app->request->post();
  31. $needSend = isset($post['needSend']) && is_numeric($post['needSend']) ? $post['needSend'] : 1;
  32. $orderType = $post['orderType'] ?? 1;
  33. $defaultShopId = MerchantService::getDefaultShopId($this->sj);
  34. if (empty($defaultShopId)) {
  35. util::fail('没有找到门店');
  36. }
  37. $post['merchantId'] = $this->sjId;
  38. $post['shopId'] = $defaultShopId;
  39. $post['payWay'] = $this->isWx == true ? 0 : 1;
  40. $now = time();
  41. $expireTime = $now + 1800;//订单30分钟后过期
  42. $post['deadline'] = $expireTime;
  43. $post['fromType'] = 1;//商城
  44. $customId = $post['customId'] ?? 0;
  45. if (!empty($customId)) {
  46. $customId = $this->customId;
  47. }
  48. $post['customId'] = $customId;
  49. //非门店订单
  50. $store = 0;
  51. $post['store'] = $store;
  52. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  53. //$orderType = 1 成品订单
  54. if ($orderType == 1) {
  55. $goodsInfo = $post['goodsInfo'] ?? '';
  56. $goodsInfoArr = json_decode($goodsInfo, true);
  57. if (empty($goodsInfoArr)) {
  58. util::fail('请选择商品');
  59. }
  60. //总的商品金额
  61. $totalGoodsPrice = 0;
  62. $orderGoods = [];
  63. foreach ($goodsInfoArr as $key => $currentGoods) {
  64. $currentGoodsId = $currentGoods['goodsId'];
  65. $currentGoodsInfo = GoodsService::getGoodsInfo($currentGoodsId);
  66. if (empty($currentGoodsInfo)) {
  67. util::fail('没有找到商品');
  68. }
  69. $goodsStyleId = $currentGoods['goodsStyleId'];
  70. $goodsNum = $currentGoods['goodsNum'];
  71. //计算总金额
  72. $unitPrice = $currentGoodsInfo['price'];
  73. $goodsStyleList = isset($currentGoodsInfo['goodsStyleList']) ? $currentGoodsInfo['goodsStyleList'] : [];
  74. if (!empty($goodsStyleId)) {
  75. if (empty($goodsStyleList)) {
  76. util::fail('商品款式没有找到');
  77. }
  78. $styleIdList = array_keys($goodsStyleList);
  79. if (in_array($goodsStyleId, $styleIdList) == false) {
  80. util::fail('商品款式没有找到!');
  81. }
  82. $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
  83. }
  84. $goodsPrice = $unitPrice * $goodsNum;
  85. $totalGoodsPrice = stringUtil::calcAdd($totalGoodsPrice, $goodsPrice);
  86. $orderGoods[] = [
  87. 'goodsId' => $currentGoodsId,
  88. 'userId' => $this->adminId,
  89. 'merchantId' => $this->sjId,
  90. 'title' => $currentGoodsInfo['goodsName'],
  91. 'cover' => isset($goodsInfo['shortImgList']) && !empty($currentGoodsInfo['shortImgList']) ? current($currentGoodsInfo['shortImgList']) : '',
  92. 'unitPrice' => $unitPrice,
  93. 'num' => $goodsNum,
  94. 'goodsStyleName' => isset($currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
  95. 'goodsStyleId' => $goodsStyleId,
  96. 'createTime' => date("Y-m-d H:i:s"),
  97. ];
  98. }
  99. $user = $this->custom;
  100. $post['bookName'] = isset($user['name']) ? $user['name'] : '';
  101. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  102. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  103. $post['bookMobile'] = $bookMobile;
  104. //计算运费
  105. $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
  106. $sendCost = 0;
  107. //按距离计算运费并且客户要求送的
  108. //运费计算方式
  109. $freightType = 0;
  110. if ($freightType == 0 && $needSend == 1) {
  111. $lat = $post['latitude'];//收货人纬度
  112. $lng = $post['longitude'];//收货人经度
  113. $respond = ExpressService::getUserDistance($lng, $lat, $defaultShopId, $this->sjExtend);
  114. $sendCost = isset($respond['fee']) ? $respond['fee'] : 0;
  115. }
  116. $post['sendCost'] = $sendCost;
  117. $prePrice = stringUtil::calcAdd($sendCost, $totalGoodsPrice);
  118. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  119. $sourceType = $this->isWx ? 0 : 1;
  120. if (httpUtil::isMiniProgram()) {
  121. $sourceType = 2;
  122. }
  123. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  124. $actPrice = $discountData['price'];
  125. $post['discountType'] = $discountData['discountType'];
  126. $post['discountAmount'] = $discountData['discountAmount'];
  127. $post['prePrice'] = $prePrice;
  128. $post['actPrice'] = $actPrice;
  129. $order = OrderClass::addOrder($post);
  130. $orderId = $order['id'];
  131. $orderSn = $order['orderSn'];
  132. foreach ($orderGoods as $key => $val) {
  133. $val['orderId'] = $orderId;
  134. $val['orderSn'] = $orderSn;
  135. OrderGoodsService::add($val);//创建订单商品列表
  136. }
  137. } else {
  138. //$orderType = 2 花材订单
  139. $productData = $post['product'] ?? '';
  140. $product = json_decode($productData, true);
  141. if (empty($product)) {
  142. util::fail('请选择花材');
  143. }
  144. $actPrice = 0;
  145. $orderProduct = [];
  146. foreach ($product as $key => $val) {
  147. $productId = $val['productId'];
  148. $bigNum = $val['bigNum'];
  149. $smallNum = $val['smallNum'];
  150. $actPrice += 2;
  151. $orderProduct[] = [
  152. 'productId' => $productId,
  153. 'bigNum' => $bigNum,
  154. 'smallNum' => $smallNum,
  155. ];
  156. }
  157. $post['prePrice'] = $actPrice;
  158. $post['actPrice'] = $actPrice;
  159. $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
  160. $order = OrderClass::addOrder($post);
  161. $orderId = $order['id'];
  162. $orderSn = $order['orderSn'];
  163. foreach ($orderProduct as $key => $val) {
  164. $val['orderSn'] = $orderSn;
  165. OrderItemClass::add($val);
  166. }
  167. }
  168. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'orderId' => $orderId]);
  169. }
  170. //下单要用到的相关信息 ssh 2019.12.6
  171. public function actionOrderRelate()
  172. {
  173. $regionTree = RegionService::tree();
  174. $shop = ShopService::getDefaultShop($this->sj);
  175. $freight = MerchantExtendService::getFreight($this->sjExtend);
  176. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  177. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->sj];
  178. util::success($out);
  179. }
  180. //余额支付 ssh 2019.12.6
  181. public function actionBalancePay()
  182. {
  183. $post = Yii::$app->request->post();
  184. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  185. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  186. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  187. //验证优惠券是否还有效
  188. if (!empty($couponId)) {
  189. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  190. }
  191. $order = OrderService::getByOrderSn($orderSn);
  192. $userId = $this->adminId;
  193. $user = UserService::getUserInfo($userId, false);
  194. //验证支付密码是否正确
  195. UserService::validPayPassword($payPassword, $user);
  196. //支付前验证订单有效性
  197. OrderService::checkBeforePay($order, $this->adminId);
  198. $typeList = dict::getConfig('capitalType');
  199. $capitalType = $typeList['xhOrder']['id'];
  200. $totalFee = $order['prePrice'];
  201. $sourceType = 0;
  202. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  203. $actPrice = $discountData['price'];
  204. $callbackParams = [];
  205. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  206. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  207. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  208. }
  209. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  210. public function actionWxPay()
  211. {
  212. ini_set('date.timezone', 'Asia/Shanghai');
  213. $post = Yii::$app->request->post();
  214. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  215. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  216. $order = OrderService::getByOrderSn($orderSn);
  217. $orderId = $order['id'];
  218. //验证优惠券是否还有效
  219. if (!empty($couponId)) {
  220. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  221. }
  222. //支付前验证订单有效性
  223. OrderService::checkBeforePay($order, $this->adminId);
  224. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  225. $totalFee = $order['actPrice'];
  226. //小程序使用miniOpenId
  227. if (httpUtil::isMiniProgram()) {
  228. $openId = $this->user['miniOpenId'];
  229. } else {
  230. $openId = $this->user['openId'];
  231. }
  232. $typeList = dict::getConfig('capitalType');
  233. $capitalType = $typeList['xhOrder']['id'];
  234. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  235. $wxPayType = 0;
  236. if (httpUtil::isMiniProgram()) {
  237. $wxPayType = 1;
  238. }
  239. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  240. //订单30分钟后过期
  241. $now = time();
  242. $expireTime = $now + 1800;
  243. $wx = Yii::getAlias("@vendor/weixin");
  244. require_once($wx . '/lib/WxPay.Api.php');
  245. require_once($wx . '/example/WxPay.JsApiPay.php');
  246. $input = new \WxPayUnifiedOrder();
  247. $input->SetBody($name);
  248. $input->SetOut_trade_no($orderSn);
  249. $input->SetTotal_fee($totalFee * 100);
  250. $input->SetTime_start(date("YmdHis", $now));
  251. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  252. $input->SetTime_expire(date("YmdHis", $expireTime));
  253. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  254. $input->SetTrade_type("JSAPI");
  255. //花卉宝代为申请的微信支付
  256. $merchantExtend = $this->sjExtend;
  257. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  258. $input->SetSub_openid($openId);
  259. } else {
  260. $input->SetOpenid($openId);
  261. }
  262. //小程序使用miniAppId
  263. if (httpUtil::isMiniProgram()) {
  264. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  265. }
  266. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  267. $tools = new \JsApiPay();
  268. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  269. $newParams = json_decode($jsApiParameters, true);
  270. //已请求微信不能修改价格
  271. $updateData = [];
  272. $updateData['modPrice'] = 0;
  273. if (empty($order['deadline'])) {
  274. $updateData['deadline'] = $expireTime;
  275. }
  276. OrderService::updateById($orderId, $updateData);
  277. util::success($newParams);
  278. }
  279. //快捷付款订单
  280. public function actionFastPay()
  281. {
  282. ini_set('date.timezone', 'Asia/Shanghai');
  283. $post = Yii::$app->request->post();
  284. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  285. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  286. //验证门店是否有效
  287. $shopInfo = ShopService::getById($shopId);
  288. ShopService::valid($shopInfo, $this->sjId);
  289. $post['shopId'] = $shopId;
  290. //默认门店订单
  291. $store = isset($post['store']) ? $post['store'] : 1;
  292. $post['store'] = $store;
  293. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  294. $sourceType = $this->isWx ? 0 : 1;
  295. if (httpUtil::isMiniProgram()) {
  296. $sourceType = 2;
  297. }
  298. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  299. //兼容旧系统sourceType=3表示朋友圈来源
  300. $fromType = $sourceType == 3 ? 2 : 0;
  301. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  302. $post['userId'] = $this->adminId;
  303. $custom = $this->custom;
  304. $post['bookName'] = $custom['userName'] ?? '';
  305. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  306. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  307. $post['bookMobile'] = $bookMobile;
  308. $prePrice = round($post['prePrice'], 2);
  309. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  310. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  311. $actPrice = $discountData['price'];
  312. $post['discountType'] = $discountData['discountType'];
  313. $post['discountAmount'] = $discountData['discountAmount'];
  314. $post['actPrice'] = $actPrice;
  315. $post['payStyle'] = 0;
  316. $post['merchantId'] = $this->sjId;
  317. $now = time();
  318. $expireTime = $now + 300;//订单5分钟后过期
  319. $post['createTime'] = date("Y-m-d H:i:s", $now);
  320. $post['deadline'] = $expireTime;
  321. if ($actPrice <= 0) {
  322. util::fail('请填写正确的金额');
  323. }
  324. //微信支付订单提交不能修改价格
  325. $post['modPrice'] = $this->isWx ? 0 : 1;
  326. $post['sourceType'] = $sourceType;
  327. $post['goodsNum'] = 1;
  328. $post['fromType'] = $fromType;
  329. $order = OrderService::addOrder($post);
  330. $orderId = $order['id'];
  331. $orderSn = $order['orderSn'];
  332. $merchantId = $this->sjId;
  333. if ($payWay == 0) {
  334. $orderId = $order['id'];
  335. $name = '购买商品';
  336. $totalFee = $actPrice;
  337. $user = UserService::getById($this->adminId);
  338. $openId = isset($user['openId']) ? $user['openId'] : 0;
  339. if (httpUtil::isMiniProgram()) {
  340. //小程序使用miniOpenId
  341. $openId = $user['miniOpenId'];
  342. }
  343. if (empty($openId)) {
  344. util::fail('没有找到客户的openId');
  345. }
  346. $typeList = dict::getConfig('capitalType');
  347. $capitalType = $typeList['xhOrder']['id'];
  348. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  349. $wxPayType = 0;
  350. if (httpUtil::isMiniProgram()) {
  351. $wxPayType = 1;
  352. }
  353. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  354. $wx = Yii::getAlias("@vendor/weixin");
  355. require_once($wx . '/lib/WxPay.Api.php');
  356. require_once($wx . '/example/WxPay.JsApiPay.php');
  357. $input = new \WxPayUnifiedOrder();
  358. $input->SetBody($name);
  359. $input->SetOut_trade_no($orderSn);
  360. $input->SetTotal_fee($totalFee * 100);
  361. $input->SetTime_start(date("YmdHis", $now));
  362. $input->SetAttach($attach);
  363. //设置订单有效期5分钟
  364. $input->SetTime_expire(date("YmdHis", $expireTime));
  365. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  366. $input->SetTrade_type("JSAPI");
  367. //花卉宝代为申请的微信支付
  368. $merchantExtend = $this->sjExtend;
  369. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  370. $input->SetSub_openid($openId);
  371. } else {
  372. $input->SetOpenid($openId);
  373. }
  374. //小程序使用miniAppId
  375. if (httpUtil::isMiniProgram()) {
  376. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  377. }
  378. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  379. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  380. $tools = new \JsApiPay();
  381. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  382. $newParams = json_decode($jsApiParameters, true);
  383. $newParams['orderId'] = $orderId;
  384. util::success($newParams);
  385. } elseif ($payWay == 1) {
  386. $extend = MerchantExtendService::getByMerchantId($merchantId);
  387. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  388. util::fail('支付宝付款即将开通...');
  389. }
  390. $config = [
  391. //应用ID,您的APPID。
  392. 'app_id' => $extend['alipayPId'],
  393. //商户私钥,您的原始格式RSA私钥
  394. 'merchant_private_key' => $extend['alipayKey'],
  395. //异步通知地址
  396. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/alipay-callback",
  397. //同步跳转
  398. 'return_url' => "",
  399. //编码格式
  400. 'charset' => "UTF-8",
  401. //签名方式
  402. 'sign_type' => "RSA2",
  403. //支付宝网关
  404. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  405. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  406. 'alipay_public_key' => $extend['alipayPublicKey'],
  407. ];
  408. Yii::info(json_encode($config) . ' aplipay config');
  409. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  410. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  411. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  412. $totalFee = $order['actPrice'];
  413. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  414. $subject = '购买商品';//订单名称,必填
  415. $total_amount = $totalFee;//付款金额,必填
  416. $body = '';//商品描述,可空
  417. $timeout_express = "1m";//超时时间
  418. $typeList = dict::getConfig('capitalType');
  419. $capitalType = $typeList['xhOrder']['id'];
  420. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  421. $passbackParams = urlencode($passbackParams);
  422. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  423. $payRequestBuilder->setBody($body);
  424. $payRequestBuilder->setSubject($subject);
  425. $payRequestBuilder->setOutTradeNo($out_trade_no);
  426. $payRequestBuilder->setTotalAmount($total_amount);
  427. $payRequestBuilder->setTimeExpress($timeout_express);
  428. //在异步通知时将该参数原样返回
  429. $payRequestBuilder->setPassbackParams($passbackParams);
  430. //同步通知
  431. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$merchantId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  432. //异步通知
  433. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/alipay-callback";
  434. $payResponse = new \AlipayTradeService($config);
  435. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  436. //直接将支付宝的html返回给前端
  437. echo $result;
  438. } elseif ($payWay == 2) {
  439. //余额支付,验证支付密码是否正确
  440. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  441. $user = UserService::getUserInfo($this->adminId, false);
  442. UserService::validPayPassword($payPassword, $user);
  443. $typeList = dict::getConfig('capitalType');
  444. $capitalType = $typeList['xhOrder']['id'];
  445. $callbackParams = [];
  446. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  447. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  448. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  449. } else {
  450. util::fail('无效的支付方式');
  451. }
  452. }
  453. //订单评价
  454. public function actionComment()
  455. {
  456. $post = Yii::$app->request->post();
  457. $id = $post['id'];
  458. $order = OrderService::getById($id);
  459. OrderService::valid($order, $this->sjId);
  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. $orderSn = Yii::$app->request->get('orderSn', '');
  471. $detail = [];
  472. if (!empty($id)) {
  473. $detail = OrderService::getOrderById($id);
  474. }
  475. if (!empty($orderSn)) {
  476. $detail = OrderService::getOrderBySn($orderSn);
  477. }
  478. OrderService::valid($detail, $this->sjId);
  479. util::success($detail);
  480. }
  481. public function actionList()
  482. {
  483. $get = Yii::$app->request->get();
  484. $search = isset($get['search']) ? $get['search'] : '';
  485. $where = [];
  486. $where['merchantId'] = $this->sjId;
  487. if (!empty($get['status'])) {
  488. $where['status'] = $get['status'];
  489. }
  490. if (!empty($search)) {
  491. if (stringUtil::isMobile($search)) {
  492. $where['receiveMobile'] = $search;
  493. } else {
  494. $where['id'] = $search;
  495. }
  496. }
  497. $searchTime = $get['searchTime'] ?? '';
  498. if (!empty($searchTime)) {
  499. $startTime = $get['startTime'] ?? '';
  500. $endTime = $get['endTime'] ?? '';
  501. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  502. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  503. }
  504. $list = OrderService::getOrderList($where);
  505. $list['asset'] = MerchantAssetService::getByMerchantId($this->sjId);
  506. util::success($list);
  507. }
  508. //订单归类 ssh 2019.12.17
  509. public function actionClassify()
  510. {
  511. $post = Yii::$app->request->post();
  512. $id = isset($post['id']) ? $post['id'] : 0;
  513. $order = OrderService::getById($id);
  514. OrderService::valid($order, $this->sjId);
  515. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  516. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  517. OrderService::classify($id, $categoryId, $usageId);
  518. util::complete();
  519. }
  520. //更新配送单 ssh 2019.12.17
  521. public function actionUpdateSheet()
  522. {
  523. $post = Yii::$app->request->post();
  524. $id = isset($post['id']) ? $post['id'] : 0;
  525. unset($post['id']);
  526. $order = OrderService::getById($id);
  527. OrderService::valid($order, $this->sjId);
  528. OrderService::updateSheet($order, $post);
  529. util::complete('提交成功');
  530. }
  531. //商家收款与发货 ssh 2019.12.18
  532. public function actionGathering()
  533. {
  534. $post = Yii::$app->request->post();
  535. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  536. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  537. $userId = isset($post['userId']) ? $post['userId'] : 0;
  538. $userInfo = UserService::getById($userId);
  539. if (empty($userInfo) || $userInfo['merchantId'] != $this->sjId) {
  540. util::fail('您选择的客户无效');
  541. }
  542. if ($price <= 0) {
  543. util::fail('请输入金额');
  544. }
  545. $post['prePrice'] = $price;
  546. $post['actPrice'] = $price;
  547. $post['payWay'] = $payWay;
  548. $post['sourceType'] = 1;
  549. $post['userId'] = $userId;
  550. $post['goodsNum'] = 1;
  551. $post['orderName'] = '商品';
  552. $shopId = MerchantService::getDefaultShopId($this->sj);
  553. $post['shopId'] = $shopId;
  554. $order = OrderService::addOrder($post);
  555. $id = $order['id'];
  556. $orderSn = $order['orderSn'];
  557. //流水类型列表
  558. $capitalTypeList = dict::getConfig('capitalType');
  559. $capitalType = $capitalTypeList['xhOrder']['id'];
  560. $callbackParams = [];
  561. switch ($payWay) {
  562. case 0:
  563. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  564. break;
  565. case 1:
  566. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  567. break;
  568. case 2:
  569. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  570. break;
  571. default:
  572. util::fail('无效支付方式');
  573. }
  574. util::success($order);
  575. }
  576. //免发货管理 ssh 2020.1.6
  577. public function actionWithoutSend()
  578. {
  579. $id = Yii::$app->request->get('id');
  580. $order = OrderService::getById($id);
  581. OrderService::valid($order, $this->sjId);
  582. //配送流程变更
  583. $currentFlow = OrderSendClass::withoutSend($order);
  584. util::success(['currentFlow' => $currentFlow]);
  585. }
  586. //修改价格 ssh 2020.1.6
  587. public function actionUpdatePrice()
  588. {
  589. $id = Yii::$app->request->get('id');
  590. $price = Yii::$app->request->get('price', 1);
  591. $order = OrderService::getById($id);
  592. OrderService::valid($order, $this->sjId);
  593. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  594. util::fail('已发起支付,不能修改价格');
  595. }
  596. OrderService::updateById($id, ['actPrice' => $price]);
  597. util::complete('修改成功');
  598. }
  599. //配送单 ssh 2020.2.29
  600. public function actionGetDeliverDetail()
  601. {
  602. $id = Yii::$app->request->get('id', 0);
  603. $order = OrderService::getById($id);
  604. OrderService::valid($order, $this->sjId);
  605. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  606. $reachPeriodId = $order['reachPeriod'];
  607. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  608. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  609. $shopId = $order['shopId'];
  610. $telephone = '';
  611. if (!empty($shopId)) {
  612. $shop = ShopService::getById($shopId);
  613. }
  614. $telephone = isset($shop['telephone']) ? $shop['telephone'] : '';
  615. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  616. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  617. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  618. $bookName = '--';
  619. $bookMobile = '--';
  620. }
  621. $data = [
  622. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  623. 'orderSn' => $order['orderSn'],
  624. 'receiveUserName' => $order['receiveUserName'],
  625. 'receiveMobile' => $order['receiveMobile'],
  626. 'fullAddress' => $order['address'] . $order['floor'] . "(" . $order['fullAddress'] . ")",
  627. 'cardInfo' => $order['cardInfo'],
  628. 'bookMobile' => $bookMobile,
  629. 'bookName' => $bookName,
  630. 'remark' => $order['remark'],
  631. 'sendNum' => $order['sendNum'],
  632. 'telephone' => 13600903070,
  633. 'printTime' => '',
  634. 'img' => '',
  635. ];
  636. util::success($data);
  637. }
  638. }