OrderController.php 30 KB

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