OrderController.php 52 KB

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