OrderController.php 30 KB

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