OrderController.php 54 KB

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