OrderController.php 53 KB

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