OrderController.php 54 KB

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