OrderController.php 56 KB

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