OrderController.php 51 KB

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