OrderController.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. $now = time();
  490. $orderValidTime = 600;
  491. $expireTime = $now + $orderValidTime;
  492. $post['deadline'] = $expireTime;
  493. $post['staffId'] = $this->shopAdminId ?? 0;
  494. $post['staffName'] = $this->shopAdmin->name ?? '';
  495. $post['flowerNum'] = isset($post['flowerNum']) && $post['flowerNum'] > 0 ? $post['flowerNum'] : 0;
  496. $version = $post['version'] ?? 0;
  497. if ($version < 3) {
  498. util::fail('请升级版本,或用小程序');
  499. }
  500. //默认情况开单员工和收款员工是同个人
  501. $staffId = $this->shopAdminId ?? 0;
  502. $staffName = $this->shopAdmin->name ?? '';
  503. if (empty($post['shopAdminId'])) {
  504. $post['shopAdminId'] = $staffId;
  505. $post['shopAdminName'] = $staffName;
  506. }
  507. if (empty($post['getStaffId'])) {
  508. $post['getStaffId'] = $staffId;
  509. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  510. }
  511. $groupId = !empty($post['groupId']) ? $post['groupId'] : 0;
  512. $post['fromType'] = $post['fromType'] ?? 1;
  513. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  514. if (empty($post['bookName'])) {
  515. util::fail('请填写订花人姓名');
  516. }
  517. }
  518. $customId = $post['customId'] ?? 0;
  519. $custom = CustomClass::getById($customId, true);
  520. if (empty($custom)) {
  521. util::fail('没有找到客户哦');
  522. }
  523. $hdId = $custom->hdId ?? 0;
  524. $hd = HdClass::getById($hdId, true);
  525. if (empty($hd)) {
  526. util::fail('客户对应的花店信息缺失,编号869');
  527. }
  528. $hdName = $hd->name ?? '';
  529. $post['hdId'] = $hdId;
  530. $post['hdName'] = $hdName;
  531. $customName = $custom->name ?? '';
  532. $post['customName'] = $customName;
  533. $post['customNamePy'] = stringUtil::py($customName);
  534. if (!empty($post['receiveMobile'])) {
  535. if (!stringUtil::isMobile($post['receiveMobile'])) {
  536. util::fail('请填写正确的收花人手机号');
  537. }
  538. }
  539. if (!empty($post['bookMobile'])) {
  540. if (!stringUtil::isMobile($post['bookMobile'])) {
  541. util::fail('请填写正确的订花人手机号');
  542. }
  543. }
  544. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  545. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  546. //收银台开单默认到店自取
  547. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  548. }
  549. $forward = $post['forward'] ?? 0;
  550. if ($forward == 1) {
  551. if ($hasPay != 1) {
  552. util::fail('售后付款单只能走线下转账');
  553. }
  554. $historyDate = $post['historyDate'] ?? '';
  555. if (!empty($historyDate)) {
  556. if ($historyDate != date("Y-m-d")) {
  557. util::fail('售后付款单不能修改账单日期,只能今天');
  558. }
  559. }
  560. } else {
  561. //如果不是售后付款单,并且选的不是余额支付,余额又够付当前订单,则自动切到余额支付
  562. $balance = $custom->balance ?? 0;
  563. $modifyPrice = $post['modifyPrice'] ?? 0;
  564. if ($hasPay != 4 && $balance > $modifyPrice) {
  565. $post['hasPay'] = 4;
  566. $hasPay = 4;
  567. }
  568. }
  569. $productJson = $post['product'] ?? '';
  570. $productList = [];
  571. if (!empty($productJson)) {
  572. $productList = json_decode($productJson, true);
  573. }
  574. if (empty($productList) && empty($groupId)) {
  575. //商家手机上开单并生成制作单
  576. if (!empty($post['unitGoodsPrice'])) {
  577. $unitGoodsPrice = $post['unitGoodsPrice'];
  578. if (!is_numeric($unitGoodsPrice)) {
  579. util::fail('请输入正确的单价');
  580. }
  581. $productList = GoodsClass::orderCreateGoods($post);
  582. }
  583. }
  584. //直接收款
  585. $zjGathering = $post['zjGathering'] ?? 0;
  586. if ($zjGathering == 1) {
  587. $lsGoodsPrice = $post['lsGoodsPrice'] ?? 0;
  588. if (!is_numeric($lsGoodsPrice) || $lsGoodsPrice <= 0) {
  589. util::fail('请填写价格');
  590. }
  591. //直接收款不需要打印小票
  592. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  593. $shopId = $this->shopId;
  594. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  595. $zjGatheringItem = !empty($ext) ? $ext->zjGatheringItem : 0;
  596. if (empty($zjGatheringItem)) {
  597. util::fail('请设置直接收款的商品');
  598. }
  599. $zjGatheringItemInfo = ProductClass::getById($zjGatheringItem, true);
  600. if (empty($zjGatheringItemInfo)) {
  601. util::fail('请设置直接收款的商品哈');
  602. }
  603. $productList = [['productId' => $zjGatheringItem, 'unitType' => 0, 'num' => 1, 'property' => '1', 'unitPrice' => $lsGoodsPrice]];
  604. }
  605. $connection = Yii::$app->db;
  606. $transaction = $connection->beginTransaction();
  607. try {
  608. $post['product'] = $productList;
  609. $reduceStock = $post['reduceStock'] ?? 0;
  610. if ($reduceStock == 1) {
  611. //如果扣除库,则走另外一条路
  612. $return = OrderClass::reduceMyStock($post, $this->shop, $custom);
  613. $transaction->commit();
  614. util::success($return);
  615. }
  616. //解决开单提交订单时,别人修改价格,造成提交价格不是真实卖价问题!!
  617. $dealPrice = $post['dealPrice'] ?? 0;
  618. //解决重复提交
  619. util::checkRepeatCommit($this->adminId . '_' . $dealPrice, 3);
  620. if ($dealPrice == 0) {
  621. $productIds = [];
  622. $hsIds = [];
  623. foreach ($productList as $pv) {
  624. $property = $pv['property'] ?? 0;
  625. $puId = $pv['productId'] ?? 0;
  626. if ($property == 0) {
  627. $hsIds[] = $puId;
  628. } else {
  629. $productIds[] = $puId;
  630. }
  631. }
  632. $itemInfo = [];
  633. if (!empty($productIds)) {
  634. $itemInfo = ProductClass::getByIds($productIds, null, 'id');
  635. }
  636. $hsInfo = [];
  637. if (!empty($hsIds)) {
  638. $hsInfo = GoodsClass::getByIds($hsIds, null, 'id');
  639. }
  640. $diff = [];
  641. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  642. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  643. $level = 0;
  644. foreach ($productList as $currentProduct) {
  645. $property = $currentProduct['property'] ?? 0;
  646. $currentPrice = $currentProduct['unitPrice'] ?? 0;
  647. $currentId = $currentProduct['productId'] ?? 0;
  648. if ($property == 1) {
  649. $systemInfo = $itemInfo[$currentId] ?? [];
  650. if (!empty($systemInfo)) {
  651. $name = $systemInfo['name'] ?? '';
  652. $systemPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  653. if (floatval($currentPrice) != floatval($systemPrice)) {
  654. $diff[] = [
  655. 'name' => $name,
  656. 'price' => $systemPrice,
  657. 'kdPrice' => $currentPrice,
  658. ];
  659. }
  660. }
  661. } else {
  662. $systemInfo = $hsInfo[$currentId] ?? [];
  663. if (!empty($systemInfo)) {
  664. $params = [];
  665. $name = $systemInfo['name'] ?? '';
  666. $hsData = GoodsClass::getFinalPrice($systemInfo, $shop, $custom, $params);
  667. $systemPrice = $hsData['price'] ?? 0;
  668. if ($systemInfo['priceType'] == 1) {
  669. if (floatval($currentPrice) != floatval($systemPrice)) {
  670. $diff[] = [
  671. 'name' => $name,
  672. 'price' => $systemPrice,
  673. 'kdPrice' => $currentPrice,
  674. ];
  675. }
  676. }
  677. }
  678. }
  679. }
  680. if (!empty($diff)) {
  681. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  682. }
  683. }
  684. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  685. $return = OrderService::createHdOrder($post, $custom, $hasPay);
  686. $staff = $this->shopAdmin;
  687. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  688. OrderClass::shPayChange($return, $shop, $staff);
  689. //打印小票和语音播报等使用
  690. $id = $return->id ?? 0;
  691. $order = OrderClass::getById($id, true);
  692. //如果需要生成制作单
  693. $needWork = $post['needWork'] ?? 0;
  694. if ($needWork == 1) {
  695. $main = $this->main;
  696. WorkClass::needWork($order, $main);
  697. $has = WorkClass::getByCondition(['orderId' => $id], true);
  698. if (!empty($has)) {
  699. $return->hasWork = 1;
  700. $return->save();
  701. }
  702. }
  703. $transaction->commit();
  704. if ($order->fromType == 3 || $order->fromType == 4) {
  705. //除了客服号下单和美团下单,其它情况允许打小票
  706. } else {
  707. //前台打小票
  708. OrderClass::onlinePrint($order);
  709. }
  710. //前台提示收款多少钱
  711. ShopExtClass::hdGatheringReport($order);
  712. //前台提醒出示付款码
  713. if (isset($order->status) && $order->status == 1) {
  714. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  715. ShopExtClass::pleasePayReport($order);
  716. }
  717. }
  718. $shopId = $order->shopId ?? 0;
  719. $shop = ShopClass::getById($shopId, true);
  720. WxMessageClass::gatheringIncomeInform($shop, $order);
  721. if ($needWork == 1) {
  722. //新制作单提示
  723. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  724. ShopExtClass::newWorkRemind($shopExt, $order);
  725. }
  726. util::success($return);
  727. } catch (\Exception $e) {
  728. $transaction->rollBack();
  729. Yii::error("失败原因:" . $e->getMessage());
  730. util::fail('下单失败');
  731. }
  732. }
  733. //下单要用到的相关信息 ssh 2019.12.6
  734. public function actionOrderRelate()
  735. {
  736. $regionTree = RegionService::tree();
  737. $shop = $this->shop->attributes;
  738. $freight = MerchantExtendService::getFreight($this->sjExtend);
  739. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  740. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  741. util::success($out);
  742. }
  743. //余额支付 ssh 2019.12.6
  744. public function actionBalancePay()
  745. {
  746. $post = Yii::$app->request->post();
  747. $orderSn = $post['orderSn'] ?? 0;
  748. $payPassword = $post['payPassword'] ?? 0;
  749. $couponId = $post['couponId'] ?? 0;
  750. $order = OrderService::getByOrderSn($orderSn);
  751. $userId = $this->adminId;
  752. $user = UserService::getUserInfo($userId);
  753. //验证支付密码是否正确
  754. UserService::validPayPassword($payPassword, $user);
  755. //支付前验证订单有效性
  756. OrderService::checkBeforePay($order);
  757. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  758. $totalFee = $order['prePrice'];
  759. $sourceType = 0;
  760. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  761. $actPrice = $discountData['price'];
  762. $callbackParams = [];
  763. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  764. $balance = $respond['balance'] ?? 0;
  765. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  766. }
  767. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  768. public function actionWxPay()
  769. {
  770. ini_set('date.timezone', 'Asia/Shanghai');
  771. $post = Yii::$app->request->post();
  772. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  773. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  774. $order = OrderService::getByOrderSn($orderSn);
  775. $orderId = $order['id'];
  776. //验证优惠券是否还有效
  777. if (!empty($couponId)) {
  778. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  779. }
  780. //支付前验证订单有效性
  781. OrderService::checkBeforePay($order);
  782. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  783. $totalFee = $order['realPrice'];
  784. //小程序使用miniOpenId
  785. if (httpUtil::isMiniProgram()) {
  786. $openId = $this->user['miniOpenId'];
  787. } else {
  788. $openId = $this->user['openId'];
  789. }
  790. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  791. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  792. $wxPayType = 0;
  793. if (httpUtil::isMiniProgram()) {
  794. $wxPayType = 1;
  795. }
  796. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  797. //订单30分钟后过期
  798. $now = time();
  799. $expireTime = $now + 1800;
  800. $wx = Yii::getAlias("@vendor/weixin");
  801. require_once($wx . '/lib/WxPay.Api.php');
  802. require_once($wx . '/example/WxPay.JsApiPay.php');
  803. $input = new \WxPayUnifiedOrder();
  804. $input->SetBody($name);
  805. $input->SetOut_trade_no($orderSn);
  806. $input->SetTotal_fee($totalFee * 100);
  807. $input->SetTime_start(date("YmdHis", $now));
  808. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  809. $input->SetTime_expire(date("YmdHis", $expireTime));
  810. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  811. $input->SetTrade_type("JSAPI");
  812. //花卉宝代为申请的微信支付
  813. $merchantExtend = $this->sjExtend->attributes;
  814. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  815. $input->SetSub_openid($openId);
  816. } else {
  817. $input->SetOpenid($openId);
  818. }
  819. //小程序使用miniAppId
  820. if (httpUtil::isMiniProgram()) {
  821. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  822. }
  823. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  824. $tools = new \JsApiPay();
  825. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  826. $newParams = json_decode($jsApiParameters, true);
  827. //已请求微信不能修改价格
  828. $updateData = [];
  829. $updateData['modPrice'] = 0;
  830. if (empty($order['deadline'])) {
  831. $updateData['deadline'] = $expireTime;
  832. }
  833. OrderService::updateById($orderId, $updateData);
  834. util::success($newParams);
  835. }
  836. //快捷付款订单
  837. public function actionFastPay()
  838. {
  839. ini_set('date.timezone', 'Asia/Shanghai');
  840. $post = Yii::$app->request->post();
  841. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  842. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  843. //验证门店是否有效
  844. $shopInfo = ShopService::getById($shopId);
  845. ShopService::valid($shopInfo, $this->sjId);
  846. $post['shopId'] = $shopId;
  847. //默认门店订单
  848. $store = isset($post['store']) ? $post['store'] : 1;
  849. $post['store'] = $store;
  850. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  851. $sourceType = $this->isWx ? 0 : 1;
  852. if (httpUtil::isMiniProgram()) {
  853. $sourceType = 2;
  854. }
  855. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  856. //兼容旧系统sourceType=3表示朋友圈来源
  857. $fromType = $sourceType == 3 ? 2 : 0;
  858. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  859. $post['userId'] = $this->adminId;
  860. $custom = $this->custom;
  861. $post['bookName'] = $custom['userName'] ?? '';
  862. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  863. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  864. $post['bookMobile'] = $bookMobile;
  865. $prePrice = round($post['prePrice'], 2);
  866. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  867. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  868. $actPrice = $discountData['price'];
  869. $post['discountType'] = $discountData['discountType'];
  870. $post['discountAmount'] = $discountData['discountAmount'];
  871. $post['actPrice'] = $actPrice;
  872. $post['realPrice'] = $actPrice;
  873. $post['payStyle'] = 0;
  874. $post['sjId'] = $this->sjId;
  875. $now = time();
  876. $expireTime = $now + 300;//订单5分钟后过期
  877. $post['createTime'] = date("Y-m-d H:i:s", $now);
  878. $post['deadline'] = $expireTime;
  879. if ($actPrice <= 0) {
  880. util::fail('请填写正确的金额');
  881. }
  882. //微信支付订单提交不能修改价格
  883. $post['modPrice'] = $this->isWx ? 0 : 1;
  884. $post['sourceType'] = $sourceType;
  885. $post['goodsNum'] = 1;
  886. $post['fromType'] = $fromType;
  887. $order = OrderService::addOrder($post);
  888. $orderId = $order['id'];
  889. $orderSn = $order['orderSn'];
  890. $sjId = $this->sjId;
  891. if ($payWay == 0) {
  892. $orderId = $order['id'];
  893. $name = '购买商品';
  894. $totalFee = $actPrice;
  895. $user = UserService::getById($this->adminId);
  896. $openId = isset($user['openId']) ? $user['openId'] : 0;
  897. if (httpUtil::isMiniProgram()) {
  898. //小程序使用miniOpenId
  899. $openId = $user['miniOpenId'];
  900. }
  901. if (empty($openId)) {
  902. util::fail('没有找到客户的openId');
  903. }
  904. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  905. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  906. $wxPayType = 0;
  907. if (httpUtil::isMiniProgram()) {
  908. $wxPayType = 1;
  909. }
  910. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  911. $wx = Yii::getAlias("@vendor/weixin");
  912. require_once($wx . '/lib/WxPay.Api.php');
  913. require_once($wx . '/example/WxPay.JsApiPay.php');
  914. $input = new \WxPayUnifiedOrder();
  915. $input->SetBody($name);
  916. $input->SetOut_trade_no($orderSn);
  917. $input->SetTotal_fee($totalFee * 100);
  918. $input->SetTime_start(date("YmdHis", $now));
  919. $input->SetAttach($attach);
  920. //设置订单有效期5分钟
  921. $input->SetTime_expire(date("YmdHis", $expireTime));
  922. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  923. $input->SetTrade_type("JSAPI");
  924. //花卉宝代为申请的微信支付
  925. $merchantExtend = $this->sjExtend->attributes;
  926. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  927. $input->SetSub_openid($openId);
  928. } else {
  929. $input->SetOpenid($openId);
  930. }
  931. //小程序使用miniAppId
  932. if (httpUtil::isMiniProgram()) {
  933. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  934. }
  935. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  936. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  937. $tools = new \JsApiPay();
  938. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  939. $newParams = json_decode($jsApiParameters, true);
  940. $newParams['orderId'] = $orderId;
  941. util::success($newParams);
  942. } elseif ($payWay == 1) {
  943. $extend = MerchantExtendService::getBySjId($sjId);
  944. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  945. util::fail('支付宝付款即将开通...');
  946. }
  947. $config = [
  948. //应用ID,您的APPID。
  949. 'app_id' => $extend['alipayPId'],
  950. //商户私钥,您的原始格式RSA私钥
  951. 'merchant_private_key' => $extend['alipayKey'],
  952. //异步通知地址
  953. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  954. //同步跳转
  955. 'return_url' => "",
  956. //编码格式
  957. 'charset' => "UTF-8",
  958. //签名方式
  959. 'sign_type' => "RSA2",
  960. //支付宝网关
  961. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  962. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  963. 'alipay_public_key' => $extend['alipayPublicKey'],
  964. ];
  965. Yii::info(json_encode($config) . ' aplipay config');
  966. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  967. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  968. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  969. $totalFee = $order['realPrice'];
  970. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  971. $subject = '购买商品';//订单名称,必填
  972. $total_amount = $totalFee;//付款金额,必填
  973. $body = '';//商品描述,可空
  974. $timeout_express = "1m";//超时时间
  975. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  976. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  977. $passBackParams = urlencode($passBackParams);
  978. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  979. $payRequestBuilder->setBody($body);
  980. $payRequestBuilder->setSubject($subject);
  981. $payRequestBuilder->setOutTradeNo($out_trade_no);
  982. $payRequestBuilder->setTotalAmount($total_amount);
  983. $payRequestBuilder->setTimeExpress($timeout_express);
  984. //在异步通知时将该参数原样返回
  985. $payRequestBuilder->setPassbackParams($passBackParams);
  986. //同步通知
  987. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  988. //异步通知
  989. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  990. $payResponse = new \AlipayTradeService($config);
  991. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  992. //直接将支付宝的html返回给前端
  993. echo $result;
  994. } elseif ($payWay == 2) {
  995. //余额支付
  996. util::fail('暂不支持');
  997. } else {
  998. util::fail('无效的支付方式');
  999. }
  1000. }
  1001. //订单评价
  1002. public function actionComment()
  1003. {
  1004. $post = Yii::$app->request->post();
  1005. $id = $post['id'];
  1006. $order = OrderService::getById($id);
  1007. OrderService::valid($order, $this->mainId);
  1008. if ($order['grade'] > 0) {
  1009. util::fail('您已经评过了');
  1010. }
  1011. OrderService::comment($post);
  1012. util::complete('提交成功');
  1013. }
  1014. //订单详情,不需要登陆可以查看,要有盐,给跑腿上查看订单信息用的
  1015. public function actionInfo()
  1016. {
  1017. $get = Yii::$app->request->get();
  1018. $id = $get['id'] ?? 0;
  1019. $salt = $get['salt'] ?? '';
  1020. $detail = OrderClass::getFullInfo($id);
  1021. if (empty($detail)) {
  1022. util::fail('没有找到订单');
  1023. }
  1024. if (empty($detail['salt'])) {
  1025. util::fail('没有找到订单信息');
  1026. }
  1027. if (empty($salt)) {
  1028. util::fail('请求参数错误');
  1029. }
  1030. if ($detail['salt'] != $salt) {
  1031. util::fail('不是你的订单');
  1032. }
  1033. util::success($detail);
  1034. }
  1035. //订单详情
  1036. public function actionDetail()
  1037. {
  1038. $id = Yii::$app->request->get('id', 0);
  1039. $detail = OrderClass::getFullInfo($id);
  1040. OrderService::valid($detail, $this->mainId);
  1041. $forwardMap = [];
  1042. if ($detail['forward'] == 1) {
  1043. //当前单如果已经是售后付款单,则取出其对应的原单
  1044. $map = OrderForwardClass::getByCondition(['shOrderId' => $id], true);
  1045. if (!empty($map)) {
  1046. $originOrderId = $map->orderId ?? 0;
  1047. $forwardMap['orderId'] = $originOrderId;
  1048. }
  1049. }
  1050. $detail['forwardMap'] = $forwardMap;
  1051. $shOrderMap = [];
  1052. if ($detail['hasForward'] == 1) {
  1053. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1054. }
  1055. $detail['shOrderMap'] = $shOrderMap;
  1056. $main = $this->main;
  1057. //是否走售后付款单模式判断 ssh
  1058. $ret = OrderClass::ifShPay($detail, $main, 0);
  1059. $shAddPay = $ret['shAddPay'];
  1060. $shAddPayReason = $ret['shAddPayReason'];
  1061. $detail['shAddPay'] = $shAddPay;
  1062. $detail['shAddPayReason'] = $shAddPayReason;
  1063. util::success($detail);
  1064. }
  1065. public function actionList()
  1066. {
  1067. $get = Yii::$app->request->get();
  1068. $searchText = $get['searchText'] ?? '';
  1069. $searchStyle = $get['searchStyle'] ?? 0;
  1070. $shopId = $this->shopId ?? 0;
  1071. if (empty($shopId)) {
  1072. //没有登录不显示数据
  1073. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  1074. }
  1075. $where = [];
  1076. $where['shopId'] = $shopId;
  1077. if (!empty($get['customId'])) {
  1078. $where['customId'] = $get['customId'];
  1079. }
  1080. if (!empty($get['ids'])) {
  1081. $stringIds = $get['ids'];
  1082. $newIds = explode(',', $stringIds);
  1083. $where['id'] = ['in', $newIds];
  1084. }
  1085. $status = $get['status'] ?? 0;
  1086. if (!empty($status)) {
  1087. $where['status'] = $get['status'];
  1088. }
  1089. $debt = $get['debt'] ?? -1;
  1090. if ($debt > -1) {
  1091. $where['debt'] = $debt;
  1092. }
  1093. if (!empty($get['shopAdminId'])) {
  1094. $where['shopAdminId'] = $get['shopAdminId'];
  1095. }
  1096. if ($searchStyle == 1 && !empty($searchText)) {
  1097. $where['sendNum'] = $searchText;
  1098. }
  1099. if (isset($get['repeat']) && $get['repeat'] > -1) {
  1100. $where['repeat'] = $get['repeat'];
  1101. }
  1102. if (!empty($get['searchTime'])) {
  1103. $startTime = $get['startTime'] ?? '';
  1104. $endTime = $get['endTime'] ?? '';
  1105. $period = dateUtil::formatTime($get['searchTime'], $startTime, $endTime);
  1106. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1107. }
  1108. $list = OrderService::getOrderList($where);
  1109. $list['shop'] = $this->shop->attributes ?? [];
  1110. util::success($list);
  1111. }
  1112. //订单归类 ssh 2019.12.17
  1113. public function actionClassify()
  1114. {
  1115. $post = Yii::$app->request->post();
  1116. $id = $post['id'] ?? 0;
  1117. $order = OrderService::getById($id);
  1118. OrderService::valid($order, $this->mainId);
  1119. $categoryId = $post['categoryId'];
  1120. $usageId = $post['usageId'];
  1121. OrderService::classify($id, $categoryId, $usageId);
  1122. util::complete();
  1123. }
  1124. //更新配送单 ssh 2019.12.17
  1125. public function actionUpdateSheet()
  1126. {
  1127. $post = Yii::$app->request->post();
  1128. $id = $post['id'] ?? 0;
  1129. unset($post['id']);
  1130. $order = OrderService::getById($id);
  1131. OrderService::valid($order, $this->mainId);
  1132. OrderService::updateSheet($order, $post);
  1133. util::complete('提交成功');
  1134. }
  1135. //商家收款与发货 ssh 2019.12.18
  1136. public function actionGathering()
  1137. {
  1138. $post = Yii::$app->request->post();
  1139. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  1140. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  1141. $userId = $post['userId'] ?? 0;
  1142. $userInfo = UserService::getById($userId);
  1143. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  1144. util::fail('您选择的客户无效');
  1145. }
  1146. if ($price <= 0) {
  1147. util::fail('请输入金额');
  1148. }
  1149. $post['prePrice'] = $price;
  1150. $post['actPrice'] = $price;
  1151. $post['payWay'] = $payWay;
  1152. $post['sourceType'] = 1;
  1153. $post['userId'] = $userId;
  1154. $post['goodsNum'] = 1;
  1155. $post['orderName'] = '商品';
  1156. $shopId = MerchantService::getDefaultShopId($this->sj);
  1157. $post['shopId'] = $shopId;
  1158. $order = OrderService::addOrder($post);
  1159. $id = $order['id'];
  1160. $orderSn = $order['orderSn'];
  1161. //流水类型列表
  1162. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1163. $callbackParams = [];
  1164. switch ($payWay) {
  1165. case 0:
  1166. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  1167. break;
  1168. case 1:
  1169. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  1170. break;
  1171. case 2:
  1172. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  1173. break;
  1174. default:
  1175. util::fail('无效支付方式');
  1176. }
  1177. util::success($order);
  1178. }
  1179. //免发货管理 ssh 2020.1.6
  1180. public function actionWithoutSend()
  1181. {
  1182. $id = Yii::$app->request->get('id');
  1183. $order = OrderService::getById($id);
  1184. OrderService::valid($order, $this->mainId);
  1185. //配送流程变更
  1186. $currentFlow = OrderSendClass::withoutSend($order);
  1187. util::success(['currentFlow' => $currentFlow]);
  1188. }
  1189. //修改价格 ssh 2020.1.6
  1190. public function actionUpdatePrice()
  1191. {
  1192. $id = Yii::$app->request->get('id');
  1193. $price = Yii::$app->request->get('price', 1);
  1194. $order = OrderService::getById($id);
  1195. OrderService::valid($order, $this->mainId);
  1196. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1197. util::fail('已发起支付,不能修改价格');
  1198. }
  1199. OrderService::updateById($id, ['actPrice' => $price]);
  1200. util::complete('修改成功');
  1201. }
  1202. //配送单 ssh 2020.2.29
  1203. public function actionGetDeliverDetail()
  1204. {
  1205. $id = Yii::$app->request->get('id', 0);
  1206. $order = OrderService::getById($id);
  1207. OrderService::valid($order, $this->mainId);
  1208. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1209. $reachPeriodId = $order['reachPeriod'];
  1210. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1211. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1212. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1213. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1214. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1215. $bookName = '--';
  1216. $bookMobile = '--';
  1217. }
  1218. $data = [
  1219. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1220. 'orderSn' => $order['orderSn'],
  1221. 'receiveUserName' => $order['receiveUserName'],
  1222. 'receiveMobile' => $order['receiveMobile'],
  1223. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1224. 'cardInfo' => $order['cardInfo'],
  1225. 'bookMobile' => $bookMobile,
  1226. 'bookName' => $bookName,
  1227. 'remark' => $order['remark'],
  1228. 'sendNum' => $order['sendNum'],
  1229. 'telephone' => 18030142050,
  1230. 'printTime' => '',
  1231. 'img' => '',
  1232. ];
  1233. util::success($data);
  1234. }
  1235. //运费计算 lqh 2021.4.12 暂反回固定
  1236. public function actionFreight()
  1237. {
  1238. util::success(['sedCost' => 10]);
  1239. }
  1240. }