OrderController.php 52 KB

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