OrderController.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\custom\classes\CustomClass;
  7. use bizHd\goods\classes\GoodsClass;
  8. use bizHd\merchant\services\ShopService;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\classes\OrderSendClass;
  11. use bizHd\order\services\OrderService;
  12. use bizHd\product\classes\ProductClass;
  13. use bizHd\saas\services\RegionService;
  14. use bizHd\shop\classes\ShopClass;
  15. use bizHd\user\services\UserService;
  16. use bizHd\work\classes\WorkClass;
  17. use bizHd\wx\classes\WxOpenClass;
  18. use common\components\dict;
  19. use common\components\dateUtil;
  20. use common\components\payUtil;
  21. use common\services\xhPayToolService;
  22. use Yii;
  23. use common\components\util;
  24. use common\components\stringUtil;
  25. use bizHd\promote\services\CouponService;
  26. use common\components\httpUtil;
  27. use biz\wx\classes\WxMessageClass;
  28. use common\components\lakala\Lakala;
  29. class OrderController extends BaseController
  30. {
  31. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  32. //取消订单 ssh 20221231
  33. public function actionCancel()
  34. {
  35. $get = Yii::$app->request->get();
  36. $id = $get['id'] ?? 0;
  37. $order = OrderClass::getById($id, true);
  38. OrderClass::valid($order, $this->mainId);
  39. $deadTime = $order->deadline ?? 0;
  40. $current = time();
  41. $diff = bcsub($deadTime, $current);
  42. if ($diff < 60) {
  43. util::fail('60秒后将自动取消');
  44. }
  45. $connection = Yii::$app->db;
  46. $transaction = $connection->beginTransaction();
  47. try {
  48. OrderClass::setExpire($order, true);
  49. $transaction->commit();
  50. util::complete();
  51. } catch (\Exception $exception) {
  52. $transaction->rollBack();
  53. Yii::info("取消原因:" . $exception->getMessage());
  54. util::fail('取消失败');
  55. }
  56. }
  57. //修改订单 ssh 20220926
  58. public function actionUpdate()
  59. {
  60. $post = Yii::$app->request->post();
  61. $id = $post['id'] ?? 0;
  62. $order = OrderClass::getById($id, true);
  63. OrderClass::valid($order, $this->mainId);
  64. unset($post['id']);
  65. if ($order->status == 3) {
  66. util::fail('订单已配送');
  67. }
  68. if ($order->status == 4) {
  69. util::fail('订单已完成');
  70. }
  71. if ($order->status == 5) {
  72. util::fail('订单已取消');
  73. }
  74. if ($order->fromType == 4) {
  75. util::fail('请在美团APP上修改');
  76. }
  77. $address = $post['address'] ?? '';
  78. $floor = $post['floor'] ?? '';
  79. $post['fullAddress'] = $address . $floor;
  80. OrderClass::updateById($id, $post);
  81. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  82. $remark = $post['remark'] ?? '';
  83. $reachDate = $post['reachDate'] ?? '';
  84. $reachPeriod = $post['reachPeriod'] ?? '';
  85. $cardInfo = $post['cardInfo'] ?? '';
  86. $anonymity = $post['anonymity'] ?? 0;
  87. $bookName = $post['bookName'] ?? '';
  88. if (!empty($workList)) {
  89. foreach ($workList as $work) {
  90. $work->remark = $remark;
  91. $work->reachDate = $reachDate;
  92. $work->reachPeriod = $reachPeriod;
  93. $work->cardInfo = $cardInfo;
  94. $work->anonymity = $anonymity;
  95. $work->bookName = $bookName;
  96. $work->save();
  97. }
  98. }
  99. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  100. ShopExtClass::orderUpdateRemind($shopExt, $order);
  101. util::complete();
  102. }
  103. //客户欠款的订单 ssh 2021.2.4
  104. public function actionDebtList()
  105. {
  106. $get = Yii::$app->request->get();
  107. $id = $get['id'] ?? 0;
  108. $info = CustomClass::getById($id, true);
  109. CustomClass::valid($info, $this->shopId);
  110. $where = ['customId' => $id, 'debt' => 1];
  111. $searchTime = $get['searchTime'] ?? '';
  112. if (!empty($searchTime)) {
  113. $startTime = $get['startTime'] ?? '';
  114. $endTime = $get['endTime'] ?? '';
  115. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  116. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  117. }
  118. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice']);
  119. $result = ['customInfo' => $info, 'list' => $list];
  120. util::success($result);
  121. }
  122. //发货
  123. public function actionSend()
  124. {
  125. $get = Yii::$app->request->get();
  126. $id = isset($get['id']) ? $get['id'] : 0;
  127. $order = OrderClass::getById($id, true);
  128. if (empty($order)) {
  129. util::fail('没有找到订单');
  130. }
  131. OrderClass::valid($order, $this->mainId);
  132. if ($order->status != 2) {
  133. util::fail('订单不是待发货状态');
  134. }
  135. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  136. if (!empty($workList)) {
  137. $unComplete = false;
  138. foreach ($workList as $work) {
  139. if ($work->status != 1) {
  140. $unComplete = true;
  141. }
  142. }
  143. if ($unComplete == true) {
  144. util::fail('还有制作单没有完成');
  145. }
  146. }
  147. $order->status = 4;
  148. $order->save();
  149. util::complete('操作成功');
  150. }
  151. //打印订单 ssh 20220601
  152. public function actionPrintOrder()
  153. {
  154. $get = Yii::$app->request->get();
  155. $id = isset($get['id']) ? $get['id'] : 0;
  156. $order = OrderClass::getById($id, true);
  157. if (empty($order)) {
  158. util::fail('没有找到订单');
  159. }
  160. OrderClass::valid($order, $this->mainId);
  161. OrderClass::onlinePrint($order, true);
  162. util::complete();
  163. }
  164. //微信和支付宝付款码支付复查 ssh 20220422
  165. public function actionCodePayCheck()
  166. {
  167. ini_set('date.timezone', 'Asia/Shanghai');
  168. header("Content-type: text/html; charset=utf-8");
  169. $get = Yii::$app->request->get();
  170. $id = isset($get['id']) ? $get['id'] : 0;
  171. $order = OrderClass::getById($id, true);
  172. OrderClass::valid($order, $this->mainId);
  173. $orderSn = $order->orderSn ?? '';
  174. $totalFee = $order->mainPay ?? 0;
  175. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  176. util::success(['returnStatus' => 'SUCCESS']);
  177. }
  178. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  179. util::complete('订单不是待付款状态');
  180. }
  181. $connection = Yii::$app->db;
  182. $transaction = $connection->beginTransaction();
  183. try {
  184. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  185. $shop = $this->shop;
  186. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  187. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  188. $params = [
  189. 'appid' => 'OP00002119',
  190. 'serial_no' => '018b08cfddbd',
  191. 'merchant_no' => $shop->lklSjNo,
  192. 'term_no' => $shop->lklScanTermNo,
  193. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  194. 'lklCertificatePath' => $lklCertificatePath,
  195. ];
  196. $laResource = new Lakala($params);
  197. $scanParams = ['orderSn' => $orderSn,];
  198. $response = $laResource->query($scanParams);
  199. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  200. if (isset($response['resp_data']['trade_state']) && $response['resp_data']['trade_state'] == 'SUCCESS') {
  201. $payWayType = dict::getDict('payWay', 'wxPay');
  202. $account_type = $response['resp_data']['account_type'] ?? '';
  203. if ($account_type == 'WECHAT') {
  204. $payWayType = dict::getDict('payWay', 'wxPay');
  205. }
  206. if ($account_type == 'ALIPAY') {
  207. $payWayType = dict::getDict('payWay', 'alipay');
  208. }
  209. $transactionId = $response['resp_data']['trade_no'] ?? '';
  210. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  211. $order->save();
  212. $attach = '';
  213. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  214. $transaction->commit();
  215. //打印小票和语音播报
  216. $newOrder = OrderClass::getById($id, true);
  217. OrderClass::onlinePrint($newOrder);
  218. ShopExtClass::hdGatheringReport($newOrder);
  219. $shopId = $newOrder->shopId ?? 0;
  220. $shop = ShopClass::getById($shopId, true);
  221. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  222. util::success(['returnStatus' => 'SUCCESS']);
  223. } else {
  224. util::complete('未知状态..');
  225. }
  226. }
  227. } catch (\Exception $e) {
  228. $transaction->rollBack();
  229. util::complete('支付失败');
  230. }
  231. }
  232. //微信和支付宝付款码支付 ssh 2021.4.11
  233. public function actionCodePay()
  234. {
  235. ini_set('date.timezone', 'Asia/Shanghai');
  236. header("Content-type: text/html; charset=utf-8");
  237. $get = Yii::$app->request->get();
  238. $id = isset($get['id']) ? $get['id'] : 0;
  239. $order = OrderClass::getById($id, true);
  240. OrderClass::valid($order, $this->mainId);
  241. $authCode = (string)$get['authCode'] ?? '';
  242. if (empty($authCode)) {
  243. util::fail('没有获取到支付码');
  244. }
  245. $orderSn = $order->orderSn ?? '';
  246. $subject = '购买花材';
  247. $totalFee = $order->mainPay ?? 0;
  248. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  249. $connection = Yii::$app->db;
  250. $transaction = $connection->beginTransaction();
  251. try {
  252. $shop = $this->shop;
  253. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  254. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  255. $params = [
  256. 'appid' => 'OP00002119',
  257. 'serial_no' => '018b08cfddbd',
  258. 'merchant_no' => $shop->lklSjNo,
  259. 'term_no' => $shop->lklScanTermNo,
  260. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  261. 'lklCertificatePath' => $lklCertificatePath,
  262. ];
  263. $laResource = new Lakala($params);
  264. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  265. $scanParams = [
  266. 'orderSn' => $orderSn,
  267. 'amount' => $totalFee,
  268. 'capitalType' => $capitalType,
  269. 'notifyUrl' => $notifyUrl,
  270. 'subject' => $subject,
  271. 'authCode' => $authCode,
  272. ];
  273. $response = $laResource->scanPay($scanParams);
  274. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  275. $account_type = $response['resp_data']['account_type'] ?? '';
  276. $payWayType = dict::getDict('payWay', 'wxPay');
  277. if ($account_type == 'WECHAT') {
  278. $payWayType = dict::getDict('payWay', 'wxPay');
  279. }
  280. if ($account_type == 'ALIPAY') {
  281. $payWayType = dict::getDict('payWay', 'alipay');
  282. }
  283. $order->payWay = $payWayType;
  284. $transactionId = $response['resp_data']['trade_no'] ?? '';
  285. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  286. $order->save();
  287. $attach = '';
  288. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  289. $transaction->commit();
  290. //解决重复通知
  291. $cacheKey = 'hd_shop_order_pay_' . $orderSn;
  292. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  293. if (empty($has)) {
  294. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  295. $newOrder = OrderClass::getById($id, true);
  296. //打印小票
  297. OrderClass::onlinePrint($newOrder);
  298. //语音播报
  299. ShopExtClass::hdGatheringReport($newOrder);
  300. $shopId = $newOrder->shopId ?? 0;
  301. $shop = ShopClass::getById($shopId, true);
  302. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  303. }
  304. util::success(['returnStatus' => 'SUCCESS']);
  305. } else {
  306. $msg = $response['msg'] ?? '';
  307. util::success(['returnStatus' => 'FAILURE'], $msg);
  308. }
  309. } catch (\Exception $e) {
  310. $transaction->rollBack();
  311. util::fail('支付失败');
  312. }
  313. }
  314. //开单操作 ssh 20220316
  315. public function actionCreateOrder()
  316. {
  317. $post = Yii::$app->request->post();
  318. $post['sjId'] = $this->sjId;
  319. $post['shopId'] = $this->shopId;
  320. $post['mainId'] = $this->mainId ?? 0;
  321. $now = time();
  322. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  323. $expireTime = $now + $orderValidTime;
  324. $post['deadline'] = $expireTime;
  325. $post['staffId'] = $this->shopAdminId ?? 0;
  326. $post['staffName'] = $this->shopAdmin->name ?? '';
  327. $post['flowerNum'] = isset($post['flowerNum']) && $post['flowerNum'] > 0 ? $post['flowerNum'] : 0;
  328. //默认情况开单员工和收款员工是同个人
  329. $staffId = $this->shopAdminId ?? 0;
  330. $staffName = $this->shopAdmin->name ?? '';
  331. if (isset($post['shopAdminId']) == false || empty($post['shopAdminId'])) {
  332. $post['shopAdminId'] = $staffId;
  333. $post['shopAdminName'] = $staffName;
  334. }
  335. if (isset($post['getStaffId']) == false || empty($post['getStaffId'])) {
  336. $post['getStaffId'] = $staffId;
  337. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  338. }
  339. $groupId = isset($post['groupId']) && !empty($post['groupId']) ? $post['groupId'] : 0;
  340. $post['fromType'] = $post['fromType'] ?? 1;
  341. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  342. if (isset($post['bookName']) == false || empty($post['bookName'])) {
  343. util::fail('请填写订花人姓名');
  344. }
  345. }
  346. $customId = isset($post['customId']) && $post['customId'] > 0 ? $post['customId'] : 0;
  347. if (empty($customId)) {
  348. $customId = $this->shop->defaultCustomId ?? 0;
  349. }
  350. $custom = CustomClass::getById($customId);
  351. if (empty($custom)) {
  352. util::fail('没有找到客户哦');
  353. }
  354. $post['customId'] = $customId;
  355. $customName = $custom['name'] ?? '';
  356. $post['customName'] = $customName;
  357. $post['customNamePy'] = stringUtil::py($customName);
  358. if (isset($post['receiveMobile']) && !empty($post['receiveMobile'])) {
  359. if (stringUtil::isMobile($post['receiveMobile']) == false) {
  360. util::fail('请填写正确的收花人手机号');
  361. }
  362. }
  363. if (isset($post['bookMobile']) && !empty($post['bookMobile'])) {
  364. if (stringUtil::isMobile($post['bookMobile']) == false) {
  365. util::fail('请填写正确的订花人手机号');
  366. }
  367. }
  368. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  369. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  370. //收银台开单默认到店自取
  371. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  372. }
  373. $productJson = $post['product'] ?? '';
  374. $productList = [];
  375. if (!empty($productJson)) {
  376. $productList = json_decode($productJson, true);
  377. }
  378. if (empty($productList) && empty($groupId)) {
  379. //商家手机上开单并生成制作单
  380. if (isset($post['unitGoodsPrice']) && !empty($post['unitGoodsPrice'])) {
  381. $unitGoodsPrice = $post['unitGoodsPrice'];
  382. if (is_numeric($unitGoodsPrice) == false) {
  383. util::fail('请输入正确的单价');
  384. }
  385. $productList = GoodsClass::orderCreateGoods($post);
  386. }
  387. }
  388. if (empty($productList) && empty($groupId)) {
  389. if (isset($post['lsGoodsPrice']) == false || $post['lsGoodsPrice'] <= 0) {
  390. $lsGoodsPrice = $post['lsGoodsPrice'];
  391. if (is_numeric($lsGoodsPrice) == false) {
  392. util::fail('请输入正确的金额');
  393. }
  394. util::fail('请填写金额或选商品');
  395. }
  396. }
  397. $connection = Yii::$app->db;
  398. $transaction = $connection->beginTransaction();
  399. try {
  400. $post['product'] = $productList;
  401. //阿东开5支老是会出现5扎情况跟踪
  402. ProductClass::adStockCheck($this->shop, $productList);
  403. //3秒内不允许重复开单
  404. $staffId = $this->shopAdminId ?? 0;
  405. $cacheKey = 'hd_create_order_' . $staffId . '_' . $customId;
  406. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  407. if (!empty($has)) {
  408. util::fail('稍等5秒再开单');
  409. }
  410. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 3, 'has']);
  411. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  412. $transaction->commit();
  413. //打印小票和语音播报
  414. $id = $return->id ?? 0;
  415. $order = OrderClass::getById($id, true);
  416. if ($order->fromType == 3 || $order->fromType == 4) {
  417. //除了客服号下单和美团下单,其它情况允许打小票
  418. } else {
  419. //前台打小票
  420. OrderClass::onlinePrint($order);
  421. }
  422. //前台提示收款多少钱
  423. ShopExtClass::hdGatheringReport($order);
  424. //前台提醒出示付款码
  425. if (isset($order->status) && $order->status == 1) {
  426. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  427. ShopExtClass::pleasePayReport($order);
  428. }
  429. }
  430. $shopId = $order->shopId ?? 0;
  431. $shop = ShopClass::getById($shopId, true);
  432. WxMessageClass::gatheringIncomeInform($shop, $order);
  433. //新制作单提示
  434. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  435. ShopExtClass::newWorkRemind($shopExt, $order);
  436. util::success($return);
  437. } catch (\Exception $e) {
  438. $transaction->rollBack();
  439. Yii::error("失败原因:" . $e->getMessage());
  440. util::fail('下单失败');
  441. }
  442. }
  443. //下单要用到的相关信息 ssh 2019.12.6
  444. public function actionOrderRelate()
  445. {
  446. $regionTree = RegionService::tree();
  447. $shop = $this->shop->attributes;
  448. $freight = MerchantExtendService::getFreight($this->sjExtend);
  449. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  450. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  451. util::success($out);
  452. }
  453. //余额支付 ssh 2019.12.6
  454. public function actionBalancePay()
  455. {
  456. $post = Yii::$app->request->post();
  457. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  458. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  459. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  460. $order = OrderService::getByOrderSn($orderSn);
  461. $userId = $this->adminId;
  462. $user = UserService::getUserInfo($userId);
  463. //验证支付密码是否正确
  464. UserService::validPayPassword($payPassword, $user);
  465. //支付前验证订单有效性
  466. OrderService::checkBeforePay($order);
  467. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  468. $totalFee = $order['prePrice'];
  469. $sourceType = 0;
  470. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  471. $actPrice = $discountData['price'];
  472. $callbackParams = [];
  473. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  474. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  475. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  476. }
  477. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  478. public function actionWxPay()
  479. {
  480. ini_set('date.timezone', 'Asia/Shanghai');
  481. $post = Yii::$app->request->post();
  482. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  483. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  484. $order = OrderService::getByOrderSn($orderSn);
  485. $orderId = $order['id'];
  486. //验证优惠券是否还有效
  487. if (!empty($couponId)) {
  488. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  489. }
  490. //支付前验证订单有效性
  491. OrderService::checkBeforePay($order);
  492. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  493. $totalFee = $order['realPrice'];
  494. //小程序使用miniOpenId
  495. if (httpUtil::isMiniProgram()) {
  496. $openId = $this->user['miniOpenId'];
  497. } else {
  498. $openId = $this->user['openId'];
  499. }
  500. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  501. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  502. $wxPayType = 0;
  503. if (httpUtil::isMiniProgram()) {
  504. $wxPayType = 1;
  505. }
  506. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  507. //订单30分钟后过期
  508. $now = time();
  509. $expireTime = $now + 1800;
  510. $wx = Yii::getAlias("@vendor/weixin");
  511. require_once($wx . '/lib/WxPay.Api.php');
  512. require_once($wx . '/example/WxPay.JsApiPay.php');
  513. $input = new \WxPayUnifiedOrder();
  514. $input->SetBody($name);
  515. $input->SetOut_trade_no($orderSn);
  516. $input->SetTotal_fee($totalFee * 100);
  517. $input->SetTime_start(date("YmdHis", $now));
  518. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  519. $input->SetTime_expire(date("YmdHis", $expireTime));
  520. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  521. $input->SetTrade_type("JSAPI");
  522. //花卉宝代为申请的微信支付
  523. $merchantExtend = $this->sjExtend->attributes;
  524. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  525. $input->SetSub_openid($openId);
  526. } else {
  527. $input->SetOpenid($openId);
  528. }
  529. //小程序使用miniAppId
  530. if (httpUtil::isMiniProgram()) {
  531. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  532. }
  533. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  534. $tools = new \JsApiPay();
  535. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  536. $newParams = json_decode($jsApiParameters, true);
  537. //已请求微信不能修改价格
  538. $updateData = [];
  539. $updateData['modPrice'] = 0;
  540. if (empty($order['deadline'])) {
  541. $updateData['deadline'] = $expireTime;
  542. }
  543. OrderService::updateById($orderId, $updateData);
  544. util::success($newParams);
  545. }
  546. //快捷付款订单
  547. public function actionFastPay()
  548. {
  549. ini_set('date.timezone', 'Asia/Shanghai');
  550. $post = Yii::$app->request->post();
  551. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  552. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  553. //验证门店是否有效
  554. $shopInfo = ShopService::getById($shopId);
  555. ShopService::valid($shopInfo, $this->sjId);
  556. $post['shopId'] = $shopId;
  557. //默认门店订单
  558. $store = isset($post['store']) ? $post['store'] : 1;
  559. $post['store'] = $store;
  560. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  561. $sourceType = $this->isWx ? 0 : 1;
  562. if (httpUtil::isMiniProgram()) {
  563. $sourceType = 2;
  564. }
  565. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  566. //兼容旧系统sourceType=3表示朋友圈来源
  567. $fromType = $sourceType == 3 ? 2 : 0;
  568. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  569. $post['userId'] = $this->adminId;
  570. $custom = $this->custom;
  571. $post['bookName'] = $custom['userName'] ?? '';
  572. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  573. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  574. $post['bookMobile'] = $bookMobile;
  575. $prePrice = round($post['prePrice'], 2);
  576. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  577. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  578. $actPrice = $discountData['price'];
  579. $post['discountType'] = $discountData['discountType'];
  580. $post['discountAmount'] = $discountData['discountAmount'];
  581. $post['actPrice'] = $actPrice;
  582. $post['realPrice'] = $actPrice;
  583. $post['payStyle'] = 0;
  584. $post['sjId'] = $this->sjId;
  585. $now = time();
  586. $expireTime = $now + 300;//订单5分钟后过期
  587. $post['createTime'] = date("Y-m-d H:i:s", $now);
  588. $post['deadline'] = $expireTime;
  589. if ($actPrice <= 0) {
  590. util::fail('请填写正确的金额');
  591. }
  592. //微信支付订单提交不能修改价格
  593. $post['modPrice'] = $this->isWx ? 0 : 1;
  594. $post['sourceType'] = $sourceType;
  595. $post['goodsNum'] = 1;
  596. $post['fromType'] = $fromType;
  597. $order = OrderService::addOrder($post);
  598. $orderId = $order['id'];
  599. $orderSn = $order['orderSn'];
  600. $sjId = $this->sjId;
  601. if ($payWay == 0) {
  602. $orderId = $order['id'];
  603. $name = '购买商品';
  604. $totalFee = $actPrice;
  605. $user = UserService::getById($this->adminId);
  606. $openId = isset($user['openId']) ? $user['openId'] : 0;
  607. if (httpUtil::isMiniProgram()) {
  608. //小程序使用miniOpenId
  609. $openId = $user['miniOpenId'];
  610. }
  611. if (empty($openId)) {
  612. util::fail('没有找到客户的openId');
  613. }
  614. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  615. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  616. $wxPayType = 0;
  617. if (httpUtil::isMiniProgram()) {
  618. $wxPayType = 1;
  619. }
  620. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  621. $wx = Yii::getAlias("@vendor/weixin");
  622. require_once($wx . '/lib/WxPay.Api.php');
  623. require_once($wx . '/example/WxPay.JsApiPay.php');
  624. $input = new \WxPayUnifiedOrder();
  625. $input->SetBody($name);
  626. $input->SetOut_trade_no($orderSn);
  627. $input->SetTotal_fee($totalFee * 100);
  628. $input->SetTime_start(date("YmdHis", $now));
  629. $input->SetAttach($attach);
  630. //设置订单有效期5分钟
  631. $input->SetTime_expire(date("YmdHis", $expireTime));
  632. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  633. $input->SetTrade_type("JSAPI");
  634. //花卉宝代为申请的微信支付
  635. $merchantExtend = $this->sjExtend->attributes;
  636. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  637. $input->SetSub_openid($openId);
  638. } else {
  639. $input->SetOpenid($openId);
  640. }
  641. //小程序使用miniAppId
  642. if (httpUtil::isMiniProgram()) {
  643. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  644. }
  645. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  646. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  647. $tools = new \JsApiPay();
  648. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  649. $newParams = json_decode($jsApiParameters, true);
  650. $newParams['orderId'] = $orderId;
  651. util::success($newParams);
  652. } elseif ($payWay == 1) {
  653. $extend = MerchantExtendService::getBySjId($sjId);
  654. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  655. util::fail('支付宝付款即将开通...');
  656. }
  657. $config = [
  658. //应用ID,您的APPID。
  659. 'app_id' => $extend['alipayPId'],
  660. //商户私钥,您的原始格式RSA私钥
  661. 'merchant_private_key' => $extend['alipayKey'],
  662. //异步通知地址
  663. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  664. //同步跳转
  665. 'return_url' => "",
  666. //编码格式
  667. 'charset' => "UTF-8",
  668. //签名方式
  669. 'sign_type' => "RSA2",
  670. //支付宝网关
  671. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  672. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  673. 'alipay_public_key' => $extend['alipayPublicKey'],
  674. ];
  675. Yii::info(json_encode($config) . ' aplipay config');
  676. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  677. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  678. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  679. $totalFee = $order['realPrice'];
  680. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  681. $subject = '购买商品';//订单名称,必填
  682. $total_amount = $totalFee;//付款金额,必填
  683. $body = '';//商品描述,可空
  684. $timeout_express = "1m";//超时时间
  685. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  686. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  687. $passBackParams = urlencode($passBackParams);
  688. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  689. $payRequestBuilder->setBody($body);
  690. $payRequestBuilder->setSubject($subject);
  691. $payRequestBuilder->setOutTradeNo($out_trade_no);
  692. $payRequestBuilder->setTotalAmount($total_amount);
  693. $payRequestBuilder->setTimeExpress($timeout_express);
  694. //在异步通知时将该参数原样返回
  695. $payRequestBuilder->setPassbackParams($passBackParams);
  696. //同步通知
  697. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  698. //异步通知
  699. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  700. $payResponse = new \AlipayTradeService($config);
  701. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  702. //直接将支付宝的html返回给前端
  703. echo $result;
  704. } elseif ($payWay == 2) {
  705. //余额支付,验证支付密码是否正确
  706. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  707. $user = UserService::getUserInfo($this->adminId);
  708. UserService::validPayPassword($payPassword, $user);
  709. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  710. $callbackParams = [];
  711. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  712. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  713. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  714. } else {
  715. util::fail('无效的支付方式');
  716. }
  717. }
  718. //订单评价
  719. public function actionComment()
  720. {
  721. $post = Yii::$app->request->post();
  722. $id = $post['id'];
  723. $order = OrderService::getById($id);
  724. OrderService::valid($order, $this->mainId);
  725. if ($order['grade'] > 0) {
  726. util::fail('您已经评过了');
  727. }
  728. OrderService::comment($post);
  729. util::complete('提交成功');
  730. }
  731. //订单详情 ssh 2019.12.16
  732. public function actionDetail()
  733. {
  734. $id = Yii::$app->request->get('id', 0);
  735. $detail = OrderClass::getFullInfo($id);
  736. OrderService::valid($detail, $this->mainId);
  737. util::success($detail);
  738. }
  739. public function actionList()
  740. {
  741. $get = Yii::$app->request->get();
  742. $search = isset($get['searchText']) ? $get['searchText'] : '';
  743. $searchType = isset($get['searchType']) && is_numeric($get['searchType']) ? $get['searchType'] : '';
  744. $shopId = $this->shopId ?? 0;
  745. if (empty($shopId)) {
  746. //没有登录不显示数据
  747. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  748. }
  749. $where = [];
  750. $where['shopId'] = $shopId;
  751. $status = $get['status'] ?? 0;
  752. if (!empty($status)) {
  753. $where['status'] = $get['status'];
  754. }
  755. $debt = $get['debt'] ?? 2;
  756. if ($debt != 2) {
  757. $where['debt'] = $debt;
  758. }
  759. if (isset($get['shopAdminId']) && !empty($get['shopAdminId'])) {
  760. $where['shopAdminId'] = $get['shopAdminId'];
  761. }
  762. if (!empty($search)) {
  763. if (is_numeric($searchType)) {
  764. if ($searchType == 0) {
  765. $where['actPrice'] = $search;
  766. } elseif ($searchType == 1) {
  767. $where['orderSn'] = $search;
  768. } elseif ($searchType == 2) {
  769. $where['bookMobile'] = $search;
  770. } else {
  771. $where['actPrice'] = $search;
  772. }
  773. } else {
  774. //兼容旧版本
  775. if (stringUtil::isMobile($search)) {
  776. $where['bookMobile'] = $search;
  777. } else {
  778. $where['orderSn'] = strtoupper($search);
  779. }
  780. }
  781. }
  782. $searchTime = $get['searchTime'] ?? '';
  783. if (!empty($searchTime)) {
  784. $startTime = $get['startTime'] ?? '';
  785. $endTime = $get['endTime'] ?? '';
  786. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  787. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  788. }
  789. $list = OrderService::getOrderList($where);
  790. $list['shop'] = $this->shop->attributes ?? [];
  791. util::success($list);
  792. }
  793. //订单归类 ssh 2019.12.17
  794. public function actionClassify()
  795. {
  796. $post = Yii::$app->request->post();
  797. $id = isset($post['id']) ? $post['id'] : 0;
  798. $order = OrderService::getById($id);
  799. OrderService::valid($order, $this->mainId);
  800. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  801. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  802. OrderService::classify($id, $categoryId, $usageId);
  803. util::complete();
  804. }
  805. //更新配送单 ssh 2019.12.17
  806. public function actionUpdateSheet()
  807. {
  808. $post = Yii::$app->request->post();
  809. $id = isset($post['id']) ? $post['id'] : 0;
  810. unset($post['id']);
  811. $order = OrderService::getById($id);
  812. OrderService::valid($order, $this->mainId);
  813. OrderService::updateSheet($order, $post);
  814. util::complete('提交成功');
  815. }
  816. //商家收款与发货 ssh 2019.12.18
  817. public function actionGathering()
  818. {
  819. $post = Yii::$app->request->post();
  820. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  821. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  822. $userId = isset($post['userId']) ? $post['userId'] : 0;
  823. $userInfo = UserService::getById($userId);
  824. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  825. util::fail('您选择的客户无效');
  826. }
  827. if ($price <= 0) {
  828. util::fail('请输入金额');
  829. }
  830. $post['prePrice'] = $price;
  831. $post['actPrice'] = $price;
  832. $post['payWay'] = $payWay;
  833. $post['sourceType'] = 1;
  834. $post['userId'] = $userId;
  835. $post['goodsNum'] = 1;
  836. $post['orderName'] = '商品';
  837. $shopId = MerchantService::getDefaultShopId($this->sj);
  838. $post['shopId'] = $shopId;
  839. $order = OrderService::addOrder($post);
  840. $id = $order['id'];
  841. $orderSn = $order['orderSn'];
  842. //流水类型列表
  843. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  844. $callbackParams = [];
  845. switch ($payWay) {
  846. case 0:
  847. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  848. break;
  849. case 1:
  850. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  851. break;
  852. case 2:
  853. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  854. break;
  855. default:
  856. util::fail('无效支付方式');
  857. }
  858. util::success($order);
  859. }
  860. //免发货管理 ssh 2020.1.6
  861. public function actionWithoutSend()
  862. {
  863. $id = Yii::$app->request->get('id');
  864. $order = OrderService::getById($id);
  865. OrderService::valid($order, $this->mainId);
  866. //配送流程变更
  867. $currentFlow = OrderSendClass::withoutSend($order);
  868. util::success(['currentFlow' => $currentFlow]);
  869. }
  870. //修改价格 ssh 2020.1.6
  871. public function actionUpdatePrice()
  872. {
  873. $id = Yii::$app->request->get('id');
  874. $price = Yii::$app->request->get('price', 1);
  875. $order = OrderService::getById($id);
  876. OrderService::valid($order, $this->mainId);
  877. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  878. util::fail('已发起支付,不能修改价格');
  879. }
  880. OrderService::updateById($id, ['actPrice' => $price]);
  881. util::complete('修改成功');
  882. }
  883. //配送单 ssh 2020.2.29
  884. public function actionGetDeliverDetail()
  885. {
  886. $id = Yii::$app->request->get('id', 0);
  887. $order = OrderService::getById($id);
  888. OrderService::valid($order, $this->mainId);
  889. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  890. $reachPeriodId = $order['reachPeriod'];
  891. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  892. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  893. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  894. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  895. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  896. $bookName = '--';
  897. $bookMobile = '--';
  898. }
  899. $data = [
  900. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  901. 'orderSn' => $order['orderSn'],
  902. 'receiveUserName' => $order['receiveUserName'],
  903. 'receiveMobile' => $order['receiveMobile'],
  904. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  905. 'cardInfo' => $order['cardInfo'],
  906. 'bookMobile' => $bookMobile,
  907. 'bookName' => $bookName,
  908. 'remark' => $order['remark'],
  909. 'sendNum' => $order['sendNum'],
  910. 'telephone' => 18030142050,
  911. 'printTime' => '',
  912. 'img' => '',
  913. ];
  914. util::success($data);
  915. }
  916. //运费计算 lqh 2021.4.12 暂反回固定
  917. public function actionFreight()
  918. {
  919. util::success(['sedCost' => 10]);
  920. }
  921. }