OrderController.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. <?php
  2. namespace hd\controllers;
  3. use Yii;
  4. use biz\shop\classes\ShopExtClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use biz\sj\services\MerchantService;
  7. use bizHd\custom\classes\CustomClass;
  8. use bizHd\custom\classes\HdClass;
  9. use bizHd\goods\classes\GoodsClass;
  10. use bizHd\merchant\services\ShopService;
  11. use bizHd\order\classes\OrderClass;
  12. use bizHd\order\classes\OrderForwardClass;
  13. use bizHd\order\classes\OrderItemClass;
  14. use bizHd\order\classes\OrderSendClass;
  15. use bizHd\order\services\OrderService;
  16. use bizHd\product\classes\ProductClass;
  17. use bizHd\saas\services\RegionService;
  18. use bizHd\shop\classes\ShopClass;
  19. use bizHd\user\services\UserService;
  20. use bizHd\work\classes\WorkClass;
  21. use common\components\dict;
  22. use common\components\dateUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use common\components\payUtil;
  26. use common\services\xhPayToolService;
  27. use common\components\util;
  28. use common\components\stringUtil;
  29. use bizHd\promote\services\CouponService;
  30. use common\components\httpUtil;
  31. use biz\wx\classes\WxMessageClass;
  32. use common\components\lakala\Lakala;
  33. class OrderController extends BaseController
  34. {
  35. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list', 'info'];
  36. public function actionDebt()
  37. {
  38. $get = Yii::$app->request->get();
  39. $id = $get['id'] ?? 0;
  40. $info = OrderClass::getById($id, true);
  41. if (empty($info)) {
  42. util::fail('没有找到订单');
  43. }
  44. if ($info->mainId != $this->mainId) {
  45. util::fail('不是你的订单');
  46. }
  47. //4秒内不允许重复提交
  48. util::checkRepeatCommit($id, 4);
  49. if ($info->status != 1) {
  50. util::fail('订单已付款或取消');
  51. }
  52. $payWay = dict::getDict('payWay', 'debtPay');
  53. $connection = Yii::$app->db;
  54. $transaction = $connection->beginTransaction();
  55. try {
  56. $actPrice = $info->actPrice ?? 0;
  57. $info->debtPrice = $actPrice;
  58. $info->remainDebtPrice = $actPrice;
  59. $info->debt = 1;
  60. $info->save();
  61. OrderClass::payAfter($info, $payWay);
  62. $transaction->commit();
  63. util::complete('赊账成功');
  64. } catch (\Exception $exception) {
  65. $transaction->rollBack();
  66. Yii::info("赊账失败原因:" . $exception->getMessage());
  67. util::fail('操作失败');
  68. }
  69. }
  70. //取消订单 ssh 20221231
  71. public function actionCancel()
  72. {
  73. //避免重复提交
  74. $adminId = $this->adminId;
  75. util::checkRepeatCommit($adminId, 8);
  76. $get = Yii::$app->request->get();
  77. $id = $get['id'] ?? 0;
  78. $order = OrderClass::getById($id, true);
  79. if ($order->mainId != $this->mainId) {
  80. util::fail('不是你的订单');
  81. }
  82. $staff = $this->shopAdmin;
  83. $connection = Yii::$app->db;
  84. $transaction = $connection->beginTransaction();
  85. try {
  86. OrderClass::setExpire($order, true, $staff);
  87. $transaction->commit();
  88. util::complete();
  89. } catch (\Exception $exception) {
  90. $transaction->rollBack();
  91. Yii::info("取消原因:" . $exception->getMessage());
  92. util::fail('取消失败');
  93. }
  94. }
  95. //修改订单 ssh 20220926
  96. public function actionUpdate()
  97. {
  98. $post = Yii::$app->request->post();
  99. $id = $post['id'] ?? 0;
  100. $order = OrderClass::getById($id, true);
  101. OrderClass::valid($order, $this->mainId);
  102. unset($post['id']);
  103. if ($order->status == 3) {
  104. util::fail('订单已配送');
  105. }
  106. if ($order->status == 4) {
  107. util::fail('订单已完成');
  108. }
  109. if ($order->status == 5) {
  110. util::fail('订单已取消');
  111. }
  112. if ($order->fromType == 4) {
  113. util::fail('请在美团APP上修改');
  114. }
  115. $address = $post['address'] ?? '';
  116. $floor = $post['floor'] ?? '';
  117. $post['fullAddress'] = $address . $floor;
  118. OrderClass::updateById($id, $post);
  119. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  120. $remark = $post['remark'] ?? '';
  121. $reachDate = $post['reachDate'] ?? '';
  122. $reachPeriod = $post['reachPeriod'] ?? '';
  123. $cardInfo = $post['cardInfo'] ?? '';
  124. $anonymity = $post['anonymity'] ?? 0;
  125. $bookName = $post['bookName'] ?? '';
  126. if (!empty($workList)) {
  127. foreach ($workList as $work) {
  128. $work->remark = $remark;
  129. $work->reachDate = $reachDate;
  130. $work->reachPeriod = $reachPeriod;
  131. $work->cardInfo = $cardInfo;
  132. $work->anonymity = $anonymity;
  133. $work->bookName = $bookName;
  134. $work->save();
  135. }
  136. }
  137. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  138. ShopExtClass::orderUpdateRemind($shopExt, $order);
  139. util::complete();
  140. }
  141. //客户欠款的订单 ssh 2021.2.4
  142. public function actionDebtList()
  143. {
  144. $get = Yii::$app->request->get();
  145. $id = $get['id'] ?? 0;
  146. $info = CustomClass::getById($id, true);
  147. CustomClass::valid($info, $this->shopId);
  148. $where = ['customId' => $id, 'debt' => 1];
  149. $searchTime = $get['searchTime'] ?? '';
  150. if (!empty($searchTime)) {
  151. $startTime = $get['startTime'] ?? '';
  152. $endTime = $get['endTime'] ?? '';
  153. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  154. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  155. }
  156. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice', 'debtPrice']);
  157. if (!empty($list)) {
  158. foreach ($list as $key => $value) {
  159. $debtPrice = $value['debtPrice'] ?? 0;
  160. $remainDebtPrice = $value['remainDebtPrice'] ?? 0;
  161. $hasPayDebt = 0;
  162. if ($debtPrice > $remainDebtPrice) {
  163. $hasPayDebt = bcsub($debtPrice, $remainDebtPrice, 2);
  164. }
  165. $list[$key]['hasPayDebt'] = floatval($hasPayDebt);
  166. }
  167. }
  168. $result = ['customInfo' => $info, 'list' => $list];
  169. util::success($result);
  170. }
  171. //确认送达 ssh 20250902
  172. public function actionReach()
  173. {
  174. $get = Yii::$app->request->get();
  175. $id = $get['id'] ?? 0;
  176. $order = OrderClass::getById($id, true);
  177. if (empty($order)) {
  178. util::fail('没有找到订单');
  179. }
  180. OrderClass::valid($order, $this->mainId);
  181. if ($order->status == 5) {
  182. util::fail('订单已取消');
  183. }
  184. if ($order->status == 1) {
  185. util::fail('订单待付款');
  186. }
  187. if ($order->status == 2) {
  188. util::fail('订单未发货');
  189. }
  190. if ($order->status == 4) {
  191. util::fail('订单已经送到了');
  192. }
  193. if ($order->sendStatus == 1 || $order->sendStatus == 2) {
  194. util::fail('由跑腿送货,无需确认');
  195. }
  196. $order->status = 4;
  197. $order->save();
  198. util::complete('操作成功');
  199. }
  200. //确认取货
  201. public function actionFetch()
  202. {
  203. $get = Yii::$app->request->get();
  204. $id = $get['id'] ?? 0;
  205. $order = OrderClass::getById($id, true);
  206. if (empty($order)) {
  207. util::fail('没有找到订单');
  208. }
  209. OrderClass::valid($order, $this->mainId);
  210. if ($order->status == 5) {
  211. util::fail('订单已取消');
  212. }
  213. if ($order->status == 1) {
  214. util::fail('订单待付款');
  215. }
  216. if ($order->status == 3 || $order->status == 4) {
  217. util::fail('订单已取过了');
  218. }
  219. $order->status = 4;
  220. $order->save();
  221. util::complete('操作成功');
  222. }
  223. //确认发货
  224. public function actionSend()
  225. {
  226. $get = Yii::$app->request->get();
  227. $id = isset($get['id']) ? $get['id'] : 0;
  228. $order = OrderClass::getById($id, true);
  229. if (empty($order)) {
  230. util::fail('没有找到订单');
  231. }
  232. OrderClass::valid($order, $this->mainId);
  233. if ($order->status != 2) {
  234. util::fail('订单不是待发货状态');
  235. }
  236. // $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  237. // if (!empty($workList)) {
  238. // $unComplete = false;
  239. // foreach ($workList as $work) {
  240. // if ($work->status != 1) {
  241. // $unComplete = true;
  242. // }
  243. // }
  244. // if ($unComplete == true) {
  245. // util::fail('还有制作单没有完成');
  246. // }
  247. // }
  248. $order->status = 3;
  249. $order->save();
  250. util::complete('已发货');
  251. }
  252. //打印订单 ssh 20220601
  253. public function actionPrintOrder()
  254. {
  255. $get = Yii::$app->request->get();
  256. $id = isset($get['id']) ? $get['id'] : 0;
  257. $order = OrderClass::getById($id, true);
  258. if (empty($order)) {
  259. util::fail('没有找到订单');
  260. }
  261. if ($order->forward == 1) {
  262. util::fail('售后付款单不能打印');
  263. }
  264. OrderClass::valid($order, $this->mainId);
  265. OrderClass::onlinePrint($order, true);
  266. util::complete();
  267. }
  268. //微信和支付宝付款码支付复查 ssh 20220422
  269. public function actionCodePayCheck()
  270. {
  271. ini_set('date.timezone', 'Asia/Shanghai');
  272. header("Content-type: text/html; charset=utf-8");
  273. $get = Yii::$app->request->get();
  274. $id = isset($get['id']) ? $get['id'] : 0;
  275. $order = OrderClass::getById($id, true);
  276. OrderClass::valid($order, $this->mainId);
  277. $orderSn = $order->orderSn ?? '';
  278. $totalFee = $order->mainPay ?? 0;
  279. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  280. util::success(['returnStatus' => 'SUCCESS']);
  281. }
  282. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  283. util::complete('订单已取消');
  284. }
  285. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  286. util::complete('订单不是待付款状态');
  287. }
  288. $connection = Yii::$app->db;
  289. $transaction = $connection->beginTransaction();
  290. try {
  291. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  292. $shop = $this->shop;
  293. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  294. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  295. $params = [
  296. 'appid' => 'OP00002119',
  297. 'serial_no' => '018b08cfddbd',
  298. 'merchant_no' => $shop->lklSjNo,
  299. 'term_no' => $shop->lklScanTermNo,
  300. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  301. 'lklCertificatePath' => $lklCertificatePath,
  302. ];
  303. $laResource = new Lakala($params);
  304. $scanParams = ['orderSn' => $orderSn,];
  305. $response = $laResource->query($scanParams);
  306. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  307. if (isset($response['resp_data']['trade_state']) && $response['resp_data']['trade_state'] == 'SUCCESS') {
  308. $payWayType = dict::getDict('payWay', 'wxPay');
  309. $account_type = $response['resp_data']['account_type'] ?? '';
  310. if ($account_type == 'WECHAT') {
  311. $payWayType = dict::getDict('payWay', 'wxPay');
  312. }
  313. if ($account_type == 'ALIPAY') {
  314. $payWayType = dict::getDict('payWay', 'alipay');
  315. }
  316. $transactionId = $response['resp_data']['trade_no'] ?? '';
  317. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  318. $order->save();
  319. $attach = '';
  320. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  321. $transaction->commit();
  322. //打印小票和语音播报
  323. $newOrder = OrderClass::getById($id, true);
  324. OrderClass::onlinePrint($newOrder);
  325. ShopExtClass::hdGatheringReport($newOrder);
  326. $shopId = $newOrder->shopId ?? 0;
  327. $shop = ShopClass::getById($shopId, true);
  328. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  329. util::success(['returnStatus' => 'SUCCESS']);
  330. } else {
  331. util::complete('未知状态..');
  332. }
  333. }
  334. util::complete('未知状态..');
  335. } catch (\Exception $e) {
  336. $transaction->rollBack();
  337. util::complete('支付失败');
  338. }
  339. }
  340. //微信和支付宝付款码支付 ssh 2021.4.11
  341. public function actionCodePay()
  342. {
  343. ini_set('date.timezone', 'Asia/Shanghai');
  344. header("Content-type: text/html; charset=utf-8");
  345. $get = Yii::$app->request->get();
  346. $id = isset($get['id']) ? $get['id'] : 0;
  347. $version = $get['version'] ?? 0;
  348. $order = OrderClass::getById($id, true);
  349. OrderClass::valid($order, $this->mainId);
  350. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  351. $msg = '订单已取消';
  352. util::success(['returnStatus' => 'FAILURE'], $msg);
  353. }
  354. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  355. $msg = '订单不是待付款状态';
  356. util::success(['returnStatus' => 'FAILURE'], $msg);
  357. }
  358. $shop = $this->shop;
  359. $sj = $shop->merchantName;
  360. $shopName = $shop->shopName;
  361. $name = $shopName == '首店' ? $sj : $sj . '-' . $shopName;
  362. $authCode = (string)$get['authCode'] ?? '';
  363. if ($version > 0) {
  364. if (empty($authCode)) {
  365. $msg = '没有扫到付款码';
  366. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  367. noticeUtil::push($remind . ' 编号005', '15280215347');
  368. util::success(['returnStatus' => 'FAILURE'], $msg);
  369. }
  370. //微信文档说明 https://pay.weixin.qq.com/doc/v2/merchant/4011936234
  371. $wxPattern = '/^(10|11|12|13|14|15)\d{16}$/';
  372. //支付宝文档说明 https://opendocs.alipay.com/open/194/106039/
  373. $aliPattern = '/^(25|26|27|28|29|30)\d{14,22}$/';
  374. if (!preg_match($wxPattern, $authCode) && !preg_match($aliPattern, $authCode)) {
  375. $msg = '付款码错误,请使用微信或支付宝付款码';
  376. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  377. //noticeUtil::push($remind . ' 编号009', '15280215347');
  378. util::success(['returnStatus' => 'FAILURE'], $msg);
  379. }
  380. } else {
  381. if (empty($authCode)) {
  382. util::fail('没有获取到支付码');
  383. }
  384. }
  385. $orderSn = $order->orderSn ?? '';
  386. $lsCodePayErrorKey = 'ls_code_pay_error_' . $id;
  387. $hasFailPay = Yii::$app->redis->executeCommand('GET', [$lsCodePayErrorKey]);
  388. //前面有付失败过,要重新生成单号
  389. if (!empty($hasFailPay)) {
  390. $oldOrderSn = $order->orderSn;
  391. $orderSn = orderSn::getOrderSn();
  392. $order->orderSn = $orderSn;
  393. $order->save();
  394. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  395. }
  396. $subject = '购买花材 ' . $orderSn;
  397. $totalFee = $order->mainPay ?? 0;
  398. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  399. $connection = Yii::$app->db;
  400. $transaction = $connection->beginTransaction();
  401. try {
  402. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  403. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  404. $params = [
  405. 'appid' => 'OP00002119',
  406. 'serial_no' => '018b08cfddbd',
  407. 'merchant_no' => $shop->lklSjNo,
  408. 'term_no' => $shop->lklScanTermNo,
  409. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  410. 'lklCertificatePath' => $lklCertificatePath,
  411. ];
  412. $laResource = new Lakala($params);
  413. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  414. $scanParams = [
  415. 'orderSn' => $orderSn,
  416. 'amount' => $totalFee,
  417. 'capitalType' => $capitalType,
  418. 'notifyUrl' => $notifyUrl,
  419. 'subject' => $subject,
  420. 'authCode' => $authCode,
  421. ];
  422. $response = $laResource->scanPay($scanParams);
  423. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  424. $account_type = $response['resp_data']['account_type'] ?? '';
  425. $payWayType = dict::getDict('payWay', 'wxPay');
  426. if ($account_type == 'WECHAT') {
  427. $payWayType = dict::getDict('payWay', 'wxPay');
  428. }
  429. if ($account_type == 'ALIPAY') {
  430. $payWayType = dict::getDict('payWay', 'alipay');
  431. }
  432. $order->payWay = $payWayType;
  433. $transactionId = $response['resp_data']['trade_no'] ?? '';
  434. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  435. $order->save();
  436. $attach = '';
  437. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  438. $transaction->commit();
  439. //解决重复通知
  440. $cacheKey = 'hd_shop_order_pay_' . $orderSn;
  441. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  442. if (empty($has)) {
  443. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  444. $newOrder = OrderClass::getById($id, true);
  445. //打印小票
  446. OrderClass::onlinePrint($newOrder);
  447. //语音播报
  448. ShopExtClass::hdGatheringReport($newOrder);
  449. $shopId = $newOrder->shopId ?? 0;
  450. $shop = ShopClass::getById($shopId, true);
  451. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  452. }
  453. util::success(['returnStatus' => 'SUCCESS']);
  454. } else {
  455. //标记有没付成功的次数,原因有可能是余额不足等
  456. Yii::$app->redis->executeCommand('SETEX', [$lsCodePayErrorKey, 1200, 'hasFail']);
  457. $msg = $response['msg'] ?? '';
  458. $retCode = $response['code'] ?? '';
  459. $fullMsg = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  460. if ($retCode == 'BBS10000' || $retCode == 'BBS11105') {
  461. $shopId = $order->shopId;
  462. ShopExtClass::remindInputPassword($shopId);
  463. }
  464. //noticeUtil::push($fullMsg . ' ' . $retCode . ' 编号008', '15280215347');
  465. util::success(['returnStatus' => 'FAILURE', 'returnCode' => $retCode], $msg);
  466. }
  467. } catch (\Exception $e) {
  468. $transaction->rollBack();
  469. util::fail('支付失败');
  470. }
  471. }
  472. //开单操作 ssh 20220316
  473. public function actionCreateOrder()
  474. {
  475. $post = Yii::$app->request->post();
  476. $post['sjId'] = $this->sjId;
  477. $post['shopId'] = $this->shopId;
  478. $post['mainId'] = $this->mainId ?? 0;
  479. $shop = $this->shop;
  480. $now = time();
  481. $orderValidTime = 600;
  482. $expireTime = $now + $orderValidTime;
  483. $post['deadline'] = $expireTime;
  484. $post['staffId'] = $this->shopAdminId ?? 0;
  485. $post['staffName'] = $this->shopAdmin->name ?? '';
  486. $post['flowerNum'] = isset($post['flowerNum']) && $post['flowerNum'] > 0 ? $post['flowerNum'] : 0;
  487. $version = $post['version'] ?? 0;
  488. if ($version < 3) {
  489. util::fail('请升级版本,或用小程序');
  490. }
  491. //默认情况开单员工和收款员工是同个人
  492. $staffId = $this->shopAdminId ?? 0;
  493. $staffName = $this->shopAdmin->name ?? '';
  494. if (empty($post['shopAdminId'])) {
  495. $post['shopAdminId'] = $staffId;
  496. $post['shopAdminName'] = $staffName;
  497. }
  498. if (empty($post['getStaffId'])) {
  499. $post['getStaffId'] = $staffId;
  500. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  501. }
  502. $groupId = !empty($post['groupId']) ? $post['groupId'] : 0;
  503. $post['fromType'] = $post['fromType'] ?? 1;
  504. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  505. if (empty($post['bookName'])) {
  506. util::fail('请填写订花人姓名');
  507. }
  508. }
  509. $customId = $post['customId'] ?? 0;
  510. $custom = CustomClass::getById($customId, true);
  511. if (empty($custom)) {
  512. util::fail('没有找到客户哦');
  513. }
  514. $hdId = $custom->hdId ?? 0;
  515. $hd = HdClass::getById($hdId, true);
  516. if (empty($hd)) {
  517. util::fail('客户对应的花店信息缺失,编号869');
  518. }
  519. $hdName = $hd->name ?? '';
  520. $post['hdId'] = $hdId;
  521. $post['hdName'] = $hdName;
  522. $customName = $custom->name ?? '';
  523. $post['customName'] = $customName;
  524. $post['customNamePy'] = stringUtil::py($customName);
  525. if (!empty($post['receiveMobile'])) {
  526. if (!stringUtil::isMobile($post['receiveMobile'])) {
  527. util::fail('请填写正确的收花人手机号');
  528. }
  529. }
  530. if (!empty($post['bookMobile'])) {
  531. if (!stringUtil::isMobile($post['bookMobile'])) {
  532. util::fail('请填写正确的订花人手机号');
  533. }
  534. }
  535. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  536. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  537. //收银台开单默认到店自取
  538. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  539. }
  540. $forward = $post['forward'] ?? 0;
  541. if ($forward == 1) {
  542. if ($hasPay != 1) {
  543. util::fail('售后付款单只能走线下转账');
  544. }
  545. $historyDate = $post['historyDate'] ?? '';
  546. if (!empty($historyDate)) {
  547. if ($historyDate != date("Y-m-d")) {
  548. util::fail('售后付款单不能修改账单日期,只能今天');
  549. }
  550. }
  551. } else {
  552. //如果不是售后付款单,并且选的不是余额支付,余额又够付当前订单,则自动切到余额支付
  553. $balance = $custom->balance ?? 0;
  554. $modifyPrice = $post['modifyPrice'] ?? 0;
  555. if ($hasPay != 4 && $balance > $modifyPrice) {
  556. $post['hasPay'] = 4;
  557. $hasPay = 4;
  558. }
  559. }
  560. $productJson = $post['product'] ?? '';
  561. $productList = [];
  562. if (!empty($productJson)) {
  563. $productList = json_decode($productJson, true);
  564. }
  565. if (empty($productList) && empty($groupId)) {
  566. //商家手机上开单并生成制作单
  567. if (!empty($post['unitGoodsPrice'])) {
  568. $unitGoodsPrice = $post['unitGoodsPrice'];
  569. if (!is_numeric($unitGoodsPrice)) {
  570. util::fail('请输入正确的单价');
  571. }
  572. $productList = GoodsClass::orderCreateGoods($post);
  573. }
  574. }
  575. //直接收款
  576. $zjGathering = $post['zjGathering'] ?? 0;
  577. if ($zjGathering == 1) {
  578. $lsGoodsPrice = $post['lsGoodsPrice'] ?? 0;
  579. if (!is_numeric($lsGoodsPrice) || $lsGoodsPrice <= 0) {
  580. util::fail('请填写价格');
  581. }
  582. //直接收款不需要打印小票
  583. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  584. $shopId = $this->shopId;
  585. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  586. $zjGatheringItem = !empty($ext) ? $ext->zjGatheringItem : 0;
  587. if (empty($zjGatheringItem)) {
  588. util::fail('请设置直接收款的商品');
  589. }
  590. $zjGatheringItemInfo = ProductClass::getById($zjGatheringItem, true);
  591. if (empty($zjGatheringItemInfo)) {
  592. util::fail('请设置直接收款的商品哈');
  593. }
  594. $productList = [['productId' => $zjGatheringItem, 'unitType' => 0, 'num' => 1, 'property' => '1', 'unitPrice' => $lsGoodsPrice]];
  595. }
  596. $connection = Yii::$app->db;
  597. $transaction = $connection->beginTransaction();
  598. try {
  599. $post['product'] = $productList;
  600. //解决重复提交
  601. util::checkRepeatCommit($this->adminId, 2);
  602. $reduceStock = $post['reduceStock'] ?? 0;
  603. if ($reduceStock == 1) {
  604. //如果扣除库,则走另外一条路
  605. $return = OrderClass::reduceMyStock($post, $this->shop, $custom);
  606. $transaction->commit();
  607. util::success($return);
  608. }
  609. //解决开单提交订单时,别人修改价格,造成提交价格不是真实卖价问题!!
  610. $dealPrice = $post['dealPrice'] ?? 0;
  611. if ($dealPrice == 0) {
  612. $productIds = [];
  613. $hsIds = [];
  614. foreach ($productList as $pv) {
  615. $property = $pv['property'] ?? 0;
  616. $puId = $pv['productId'] ?? 0;
  617. if ($property == 0) {
  618. $hsIds[] = $puId;
  619. } else {
  620. $productIds[] = $puId;
  621. }
  622. }
  623. $itemInfo = [];
  624. if (!empty($productIds)) {
  625. $itemInfo = ProductClass::getByIds($productIds, null, 'id');
  626. }
  627. $hsInfo = [];
  628. if (!empty($hsIds)) {
  629. $hsInfo = GoodsClass::getByIds($hsIds, null, 'id');
  630. }
  631. $diff = [];
  632. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  633. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  634. $level = 0;
  635. foreach ($productList as $currentProduct) {
  636. $property = $currentProduct['property'] ?? 0;
  637. $currentPrice = $currentProduct['unitPrice'] ?? 0;
  638. $currentId = $currentProduct['productId'] ?? 0;
  639. if ($property == 1) {
  640. $systemInfo = $itemInfo[$currentId] ?? [];
  641. if (!empty($systemInfo)) {
  642. $name = $systemInfo['name'] ?? '';
  643. $systemPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  644. if (floatval($currentPrice) != floatval($systemPrice)) {
  645. $diff[] = [
  646. 'name' => $name,
  647. 'price' => $systemPrice,
  648. 'kdPrice' => $currentPrice,
  649. ];
  650. }
  651. }
  652. } else {
  653. $systemInfo = $hsInfo[$currentId] ?? [];
  654. if (!empty($systemInfo)) {
  655. $params = [];
  656. $name = $systemInfo['name'] ?? '';
  657. $hsData = GoodsClass::getFinalPrice($systemInfo, $shop, $custom, $params);
  658. $systemPrice = $hsData['price'] ?? 0;
  659. if ($systemInfo['priceType'] == 1) {
  660. if (floatval($currentPrice) != floatval($systemPrice)) {
  661. $diff[] = [
  662. 'name' => $name,
  663. 'price' => $systemPrice,
  664. 'kdPrice' => $currentPrice,
  665. ];
  666. }
  667. }
  668. }
  669. }
  670. }
  671. if (!empty($diff)) {
  672. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  673. }
  674. }
  675. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  676. $return = OrderService::createHdOrder($post, $custom, $hasPay);
  677. $staff = $this->shopAdmin;
  678. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  679. OrderClass::shPayChange($return, $shop, $staff);
  680. //打印小票和语音播报等使用
  681. $id = $return->id ?? 0;
  682. $order = OrderClass::getById($id, true);
  683. //如果需要生成制作单
  684. $needWork = $post['needWork'] ?? 0;
  685. if ($needWork == 1) {
  686. $main = $this->main;
  687. WorkClass::toDoWork($order, $main);
  688. }
  689. $transaction->commit();
  690. if ($order->fromType == 3 || $order->fromType == 4) {
  691. //除了客服号下单和美团下单,其它情况允许打小票
  692. } else {
  693. //前台打小票
  694. OrderClass::onlinePrint($order);
  695. }
  696. //前台提示收款多少钱
  697. ShopExtClass::hdGatheringReport($order);
  698. //前台提醒出示付款码
  699. if (isset($order->status) && $order->status == 1) {
  700. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  701. ShopExtClass::pleasePayReport($order);
  702. }
  703. }
  704. $shopId = $order->shopId ?? 0;
  705. $shop = ShopClass::getById($shopId, true);
  706. WxMessageClass::gatheringIncomeInform($shop, $order);
  707. if ($needWork == 1) {
  708. //新制作单提示
  709. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  710. ShopExtClass::newWorkRemind($shopExt, $order);
  711. }
  712. util::success($return);
  713. } catch (\Exception $e) {
  714. $transaction->rollBack();
  715. Yii::error("失败原因:" . $e->getMessage());
  716. util::fail('下单失败');
  717. }
  718. }
  719. //下单要用到的相关信息 ssh 2019.12.6
  720. public function actionOrderRelate()
  721. {
  722. $regionTree = RegionService::tree();
  723. $shop = $this->shop->attributes;
  724. $freight = MerchantExtendService::getFreight($this->sjExtend);
  725. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  726. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  727. util::success($out);
  728. }
  729. //余额支付 ssh 2019.12.6
  730. public function actionBalancePay()
  731. {
  732. $post = Yii::$app->request->post();
  733. $orderSn = $post['orderSn'] ?? 0;
  734. $payPassword = $post['payPassword'] ?? 0;
  735. $couponId = $post['couponId'] ?? 0;
  736. $order = OrderService::getByOrderSn($orderSn);
  737. $userId = $this->adminId;
  738. $user = UserService::getUserInfo($userId);
  739. //验证支付密码是否正确
  740. UserService::validPayPassword($payPassword, $user);
  741. //支付前验证订单有效性
  742. OrderService::checkBeforePay($order);
  743. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  744. $totalFee = $order['prePrice'];
  745. $sourceType = 0;
  746. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  747. $actPrice = $discountData['price'];
  748. $callbackParams = [];
  749. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  750. $balance = $respond['balance'] ?? 0;
  751. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  752. }
  753. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  754. public function actionWxPay()
  755. {
  756. ini_set('date.timezone', 'Asia/Shanghai');
  757. $post = Yii::$app->request->post();
  758. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  759. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  760. $order = OrderService::getByOrderSn($orderSn);
  761. $orderId = $order['id'];
  762. //验证优惠券是否还有效
  763. if (!empty($couponId)) {
  764. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  765. }
  766. //支付前验证订单有效性
  767. OrderService::checkBeforePay($order);
  768. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  769. $totalFee = $order['realPrice'];
  770. //小程序使用miniOpenId
  771. if (httpUtil::isMiniProgram()) {
  772. $openId = $this->user['miniOpenId'];
  773. } else {
  774. $openId = $this->user['openId'];
  775. }
  776. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  777. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  778. $wxPayType = 0;
  779. if (httpUtil::isMiniProgram()) {
  780. $wxPayType = 1;
  781. }
  782. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  783. //订单30分钟后过期
  784. $now = time();
  785. $expireTime = $now + 1800;
  786. $wx = Yii::getAlias("@vendor/weixin");
  787. require_once($wx . '/lib/WxPay.Api.php');
  788. require_once($wx . '/example/WxPay.JsApiPay.php');
  789. $input = new \WxPayUnifiedOrder();
  790. $input->SetBody($name);
  791. $input->SetOut_trade_no($orderSn);
  792. $input->SetTotal_fee($totalFee * 100);
  793. $input->SetTime_start(date("YmdHis", $now));
  794. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  795. $input->SetTime_expire(date("YmdHis", $expireTime));
  796. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  797. $input->SetTrade_type("JSAPI");
  798. //花卉宝代为申请的微信支付
  799. $merchantExtend = $this->sjExtend->attributes;
  800. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  801. $input->SetSub_openid($openId);
  802. } else {
  803. $input->SetOpenid($openId);
  804. }
  805. //小程序使用miniAppId
  806. if (httpUtil::isMiniProgram()) {
  807. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  808. }
  809. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  810. $tools = new \JsApiPay();
  811. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  812. $newParams = json_decode($jsApiParameters, true);
  813. //已请求微信不能修改价格
  814. $updateData = [];
  815. $updateData['modPrice'] = 0;
  816. if (empty($order['deadline'])) {
  817. $updateData['deadline'] = $expireTime;
  818. }
  819. OrderService::updateById($orderId, $updateData);
  820. util::success($newParams);
  821. }
  822. //快捷付款订单
  823. public function actionFastPay()
  824. {
  825. ini_set('date.timezone', 'Asia/Shanghai');
  826. $post = Yii::$app->request->post();
  827. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  828. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  829. //验证门店是否有效
  830. $shopInfo = ShopService::getById($shopId);
  831. ShopService::valid($shopInfo, $this->sjId);
  832. $post['shopId'] = $shopId;
  833. //默认门店订单
  834. $store = isset($post['store']) ? $post['store'] : 1;
  835. $post['store'] = $store;
  836. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  837. $sourceType = $this->isWx ? 0 : 1;
  838. if (httpUtil::isMiniProgram()) {
  839. $sourceType = 2;
  840. }
  841. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  842. //兼容旧系统sourceType=3表示朋友圈来源
  843. $fromType = $sourceType == 3 ? 2 : 0;
  844. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  845. $post['userId'] = $this->adminId;
  846. $custom = $this->custom;
  847. $post['bookName'] = $custom['userName'] ?? '';
  848. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  849. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  850. $post['bookMobile'] = $bookMobile;
  851. $prePrice = round($post['prePrice'], 2);
  852. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  853. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  854. $actPrice = $discountData['price'];
  855. $post['discountType'] = $discountData['discountType'];
  856. $post['discountAmount'] = $discountData['discountAmount'];
  857. $post['actPrice'] = $actPrice;
  858. $post['realPrice'] = $actPrice;
  859. $post['payStyle'] = 0;
  860. $post['sjId'] = $this->sjId;
  861. $now = time();
  862. $expireTime = $now + 300;//订单5分钟后过期
  863. $post['createTime'] = date("Y-m-d H:i:s", $now);
  864. $post['deadline'] = $expireTime;
  865. if ($actPrice <= 0) {
  866. util::fail('请填写正确的金额');
  867. }
  868. //微信支付订单提交不能修改价格
  869. $post['modPrice'] = $this->isWx ? 0 : 1;
  870. $post['sourceType'] = $sourceType;
  871. $post['goodsNum'] = 1;
  872. $post['fromType'] = $fromType;
  873. $order = OrderService::addOrder($post);
  874. $orderId = $order['id'];
  875. $orderSn = $order['orderSn'];
  876. $sjId = $this->sjId;
  877. if ($payWay == 0) {
  878. $orderId = $order['id'];
  879. $name = '购买商品';
  880. $totalFee = $actPrice;
  881. $user = UserService::getById($this->adminId);
  882. $openId = isset($user['openId']) ? $user['openId'] : 0;
  883. if (httpUtil::isMiniProgram()) {
  884. //小程序使用miniOpenId
  885. $openId = $user['miniOpenId'];
  886. }
  887. if (empty($openId)) {
  888. util::fail('没有找到客户的openId');
  889. }
  890. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  891. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  892. $wxPayType = 0;
  893. if (httpUtil::isMiniProgram()) {
  894. $wxPayType = 1;
  895. }
  896. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  897. $wx = Yii::getAlias("@vendor/weixin");
  898. require_once($wx . '/lib/WxPay.Api.php');
  899. require_once($wx . '/example/WxPay.JsApiPay.php');
  900. $input = new \WxPayUnifiedOrder();
  901. $input->SetBody($name);
  902. $input->SetOut_trade_no($orderSn);
  903. $input->SetTotal_fee($totalFee * 100);
  904. $input->SetTime_start(date("YmdHis", $now));
  905. $input->SetAttach($attach);
  906. //设置订单有效期5分钟
  907. $input->SetTime_expire(date("YmdHis", $expireTime));
  908. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  909. $input->SetTrade_type("JSAPI");
  910. //花卉宝代为申请的微信支付
  911. $merchantExtend = $this->sjExtend->attributes;
  912. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  913. $input->SetSub_openid($openId);
  914. } else {
  915. $input->SetOpenid($openId);
  916. }
  917. //小程序使用miniAppId
  918. if (httpUtil::isMiniProgram()) {
  919. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  920. }
  921. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  922. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  923. $tools = new \JsApiPay();
  924. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  925. $newParams = json_decode($jsApiParameters, true);
  926. $newParams['orderId'] = $orderId;
  927. util::success($newParams);
  928. } elseif ($payWay == 1) {
  929. $extend = MerchantExtendService::getBySjId($sjId);
  930. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  931. util::fail('支付宝付款即将开通...');
  932. }
  933. $config = [
  934. //应用ID,您的APPID。
  935. 'app_id' => $extend['alipayPId'],
  936. //商户私钥,您的原始格式RSA私钥
  937. 'merchant_private_key' => $extend['alipayKey'],
  938. //异步通知地址
  939. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  940. //同步跳转
  941. 'return_url' => "",
  942. //编码格式
  943. 'charset' => "UTF-8",
  944. //签名方式
  945. 'sign_type' => "RSA2",
  946. //支付宝网关
  947. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  948. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  949. 'alipay_public_key' => $extend['alipayPublicKey'],
  950. ];
  951. Yii::info(json_encode($config) . ' aplipay config');
  952. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  953. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  954. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  955. $totalFee = $order['realPrice'];
  956. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  957. $subject = '购买商品';//订单名称,必填
  958. $total_amount = $totalFee;//付款金额,必填
  959. $body = '';//商品描述,可空
  960. $timeout_express = "1m";//超时时间
  961. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  962. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  963. $passBackParams = urlencode($passBackParams);
  964. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  965. $payRequestBuilder->setBody($body);
  966. $payRequestBuilder->setSubject($subject);
  967. $payRequestBuilder->setOutTradeNo($out_trade_no);
  968. $payRequestBuilder->setTotalAmount($total_amount);
  969. $payRequestBuilder->setTimeExpress($timeout_express);
  970. //在异步通知时将该参数原样返回
  971. $payRequestBuilder->setPassbackParams($passBackParams);
  972. //同步通知
  973. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  974. //异步通知
  975. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  976. $payResponse = new \AlipayTradeService($config);
  977. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  978. //直接将支付宝的html返回给前端
  979. echo $result;
  980. } elseif ($payWay == 2) {
  981. //余额支付
  982. util::fail('暂不支持');
  983. } else {
  984. util::fail('无效的支付方式');
  985. }
  986. }
  987. //订单评价
  988. public function actionComment()
  989. {
  990. $post = Yii::$app->request->post();
  991. $id = $post['id'];
  992. $order = OrderService::getById($id);
  993. OrderService::valid($order, $this->mainId);
  994. if ($order['grade'] > 0) {
  995. util::fail('您已经评过了');
  996. }
  997. OrderService::comment($post);
  998. util::complete('提交成功');
  999. }
  1000. //订单详情,不需要登陆可以查看,要有盐,给跑腿上查看订单信息用的
  1001. public function actionInfo()
  1002. {
  1003. $get = Yii::$app->request->get();
  1004. $id = $get['id'] ?? 0;
  1005. $salt = $get['salt'] ?? '';
  1006. $detail = OrderClass::getFullInfo($id);
  1007. if (empty($detail)) {
  1008. util::fail('没有找到订单');
  1009. }
  1010. if (empty($detail['salt'])) {
  1011. util::fail('没有找到订单信息');
  1012. }
  1013. if (empty($salt)) {
  1014. util::fail('请求参数错误');
  1015. }
  1016. if ($detail['salt'] != $salt) {
  1017. util::fail('不是你的订单');
  1018. }
  1019. util::success($detail);
  1020. }
  1021. //订单详情
  1022. public function actionDetail()
  1023. {
  1024. $id = Yii::$app->request->get('id', 0);
  1025. $detail = OrderClass::getFullInfo($id);
  1026. OrderService::valid($detail, $this->mainId);
  1027. $forwardMap = [];
  1028. if ($detail['forward'] == 1) {
  1029. //当前单如果已经是售后付款单,则取出其对应的原单
  1030. $map = OrderForwardClass::getByCondition(['shOrderId' => $id], true);
  1031. if (!empty($map)) {
  1032. $originOrderId = $map->orderId ?? 0;
  1033. $forwardMap['orderId'] = $originOrderId;
  1034. }
  1035. }
  1036. $detail['forwardMap'] = $forwardMap;
  1037. $shOrderMap = [];
  1038. if ($detail['hasForward'] == 1) {
  1039. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1040. }
  1041. $detail['shOrderMap'] = $shOrderMap;
  1042. $main = $this->main;
  1043. //是否走售后付款单模式判断 ssh
  1044. $ret = OrderClass::ifShPay($detail, $main, 0);
  1045. $shAddPay = $ret['shAddPay'];
  1046. $shAddPayReason = $ret['shAddPayReason'];
  1047. $detail['shAddPay'] = $shAddPay;
  1048. $detail['shAddPayReason'] = $shAddPayReason;
  1049. util::success($detail);
  1050. }
  1051. public function actionList()
  1052. {
  1053. $get = Yii::$app->request->get();
  1054. $searchText = $get['searchText'] ?? '';
  1055. $searchStyle = $get['searchStyle'] ?? 0;
  1056. $shopId = $this->shopId ?? 0;
  1057. if (empty($shopId)) {
  1058. //没有登录不显示数据
  1059. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  1060. }
  1061. $where = [];
  1062. $where['shopId'] = $shopId;
  1063. if (!empty($get['customId'])) {
  1064. $where['customId'] = $get['customId'];
  1065. }
  1066. if (!empty($get['ids'])) {
  1067. $stringIds = $get['ids'];
  1068. $newIds = explode(',', $stringIds);
  1069. $where['id'] = ['in', $newIds];
  1070. }
  1071. $status = $get['status'] ?? 0;
  1072. if (!empty($status)) {
  1073. $where['status'] = $get['status'];
  1074. }
  1075. $debt = $get['debt'] ?? -1;
  1076. if ($debt > -1) {
  1077. $where['debt'] = $debt;
  1078. }
  1079. if (!empty($get['shopAdminId'])) {
  1080. $where['shopAdminId'] = $get['shopAdminId'];
  1081. }
  1082. if ($searchStyle == 1 && !empty($searchText)) {
  1083. $where['sendNum'] = $searchText;
  1084. }
  1085. if (isset($get['repeat']) && $get['repeat'] > -1) {
  1086. $where['repeat'] = $get['repeat'];
  1087. }
  1088. if (!empty($get['searchTime'])) {
  1089. $startTime = $get['startTime'] ?? '';
  1090. $endTime = $get['endTime'] ?? '';
  1091. $period = dateUtil::formatTime($get['searchTime'], $startTime, $endTime);
  1092. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1093. }
  1094. $list = OrderService::getOrderList($where);
  1095. $list['shop'] = $this->shop->attributes ?? [];
  1096. util::success($list);
  1097. }
  1098. //订单归类 ssh 2019.12.17
  1099. public function actionClassify()
  1100. {
  1101. $post = Yii::$app->request->post();
  1102. $id = $post['id'] ?? 0;
  1103. $order = OrderService::getById($id);
  1104. OrderService::valid($order, $this->mainId);
  1105. $categoryId = $post['categoryId'];
  1106. $usageId = $post['usageId'];
  1107. OrderService::classify($id, $categoryId, $usageId);
  1108. util::complete();
  1109. }
  1110. //更新配送单 ssh 2019.12.17
  1111. public function actionUpdateSheet()
  1112. {
  1113. $post = Yii::$app->request->post();
  1114. $id = $post['id'] ?? 0;
  1115. unset($post['id']);
  1116. $order = OrderService::getById($id);
  1117. OrderService::valid($order, $this->mainId);
  1118. OrderService::updateSheet($order, $post);
  1119. util::complete('提交成功');
  1120. }
  1121. //商家收款与发货 ssh 2019.12.18
  1122. public function actionGathering()
  1123. {
  1124. $post = Yii::$app->request->post();
  1125. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  1126. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  1127. $userId = $post['userId'] ?? 0;
  1128. $userInfo = UserService::getById($userId);
  1129. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  1130. util::fail('您选择的客户无效');
  1131. }
  1132. if ($price <= 0) {
  1133. util::fail('请输入金额');
  1134. }
  1135. $post['prePrice'] = $price;
  1136. $post['actPrice'] = $price;
  1137. $post['payWay'] = $payWay;
  1138. $post['sourceType'] = 1;
  1139. $post['userId'] = $userId;
  1140. $post['goodsNum'] = 1;
  1141. $post['orderName'] = '商品';
  1142. $shopId = MerchantService::getDefaultShopId($this->sj);
  1143. $post['shopId'] = $shopId;
  1144. $order = OrderService::addOrder($post);
  1145. $id = $order['id'];
  1146. $orderSn = $order['orderSn'];
  1147. //流水类型列表
  1148. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1149. $callbackParams = [];
  1150. switch ($payWay) {
  1151. case 0:
  1152. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  1153. break;
  1154. case 1:
  1155. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  1156. break;
  1157. case 2:
  1158. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  1159. break;
  1160. default:
  1161. util::fail('无效支付方式');
  1162. }
  1163. util::success($order);
  1164. }
  1165. //免发货管理 ssh 2020.1.6
  1166. public function actionWithoutSend()
  1167. {
  1168. $id = Yii::$app->request->get('id');
  1169. $order = OrderService::getById($id);
  1170. OrderService::valid($order, $this->mainId);
  1171. //配送流程变更
  1172. $currentFlow = OrderSendClass::withoutSend($order);
  1173. util::success(['currentFlow' => $currentFlow]);
  1174. }
  1175. //修改价格 ssh 2020.1.6
  1176. public function actionUpdatePrice()
  1177. {
  1178. $id = Yii::$app->request->get('id');
  1179. $price = Yii::$app->request->get('price', 1);
  1180. $order = OrderService::getById($id);
  1181. OrderService::valid($order, $this->mainId);
  1182. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1183. util::fail('已发起支付,不能修改价格');
  1184. }
  1185. OrderService::updateById($id, ['actPrice' => $price]);
  1186. util::complete('修改成功');
  1187. }
  1188. //配送单 ssh 2020.2.29
  1189. public function actionGetDeliverDetail()
  1190. {
  1191. $id = Yii::$app->request->get('id', 0);
  1192. $order = OrderService::getById($id);
  1193. OrderService::valid($order, $this->mainId);
  1194. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1195. $reachPeriodId = $order['reachPeriod'];
  1196. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1197. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1198. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1199. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1200. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1201. $bookName = '--';
  1202. $bookMobile = '--';
  1203. }
  1204. $data = [
  1205. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1206. 'orderSn' => $order['orderSn'],
  1207. 'receiveUserName' => $order['receiveUserName'],
  1208. 'receiveMobile' => $order['receiveMobile'],
  1209. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1210. 'cardInfo' => $order['cardInfo'],
  1211. 'bookMobile' => $bookMobile,
  1212. 'bookName' => $bookName,
  1213. 'remark' => $order['remark'],
  1214. 'sendNum' => $order['sendNum'],
  1215. 'telephone' => 18030142050,
  1216. 'printTime' => '',
  1217. 'img' => '',
  1218. ];
  1219. util::success($data);
  1220. }
  1221. //运费计算 lqh 2021.4.12 暂反回固定
  1222. public function actionFreight()
  1223. {
  1224. util::success(['sedCost' => 10]);
  1225. }
  1226. }