OrderController.php 56 KB

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