OrderController.php 54 KB

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