OrderController.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\express\services\DadaExpressServices;
  4. use bizHd\purchase\classes\PurchaseClass;
  5. use bizHd\purchase\services\PurchaseService;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\services\OrderService;
  10. use bizHd\saas\services\RegionService;
  11. use bizGhs\product\classes\ProductClass;
  12. use common\components\baiduAip;
  13. use common\components\dict;
  14. use common\components\dateUtil;
  15. use common\components\freight;
  16. use common\components\httpUtil;
  17. use common\components\oss;
  18. use common\components\payUtil;
  19. use common\components\qrCodeUtil;
  20. use Yii;
  21. use common\components\util;
  22. use biz\shop\classes\ShopAdminClass;
  23. class OrderController extends BaseController
  24. {
  25. public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
  26. //预订单到货 ssh 20220321
  27. public function actionArrival()
  28. {
  29. $post = Yii::$app->request->post();
  30. $id = $post['id'] ?? 0;
  31. $order = OrderClass::getById($id, true);
  32. OrderClass::valid($order, $this->shopId);
  33. OrderService::arrival($order, $post);
  34. util::complete();
  35. }
  36. //到货获取订单详情 ssh 20220323
  37. public function actionArrivalDetail()
  38. {
  39. $get = Yii::$app->request->get();
  40. $id = $get['id'] ?? 0;
  41. $info = OrderClass::getById($id);
  42. OrderClass::valid($info, $this->shopId);
  43. $showItemData = OrderClass::getOriginalShowItem(true, $info);
  44. $itemList = $showItemData['list'] ?? [];
  45. $info['product'] = $itemList;
  46. $kiloFee = $this->shop->kiloFee ?? 0;
  47. $miniKilo = $this->shop->miniKilo ?? 0;
  48. $info['kiloFee'] = $kiloFee;
  49. $info['miniKilo'] = $miniKilo;
  50. util::success($info);
  51. }
  52. //支付宝扫码支付 shish 20210103
  53. public function actionScanPayCheck()
  54. {
  55. $get = Yii::$app->request->get();
  56. $id = isset($get['id']) ? $get['id'] : 0;
  57. $order = OrderClass::getById($id, true);
  58. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY && $order->status != OrderClass::ORDER_STATUS_CANCEL) {
  59. util::success(['returnStatus' => 'SUCCESS']);
  60. }
  61. util::success(['returnStatus' => 'FAILURE']);
  62. }
  63. //微信和支付宝扫码支付 shish 20210103
  64. public function actionScanPay()
  65. {
  66. ini_set('date.timezone', 'Asia/Shanghai');
  67. header("Content-type: text/html; charset=utf-8");
  68. $get = Yii::$app->request->get();
  69. $id = isset($get['id']) ? $get['id'] : 0;
  70. $order = OrderClass::getById($id, true);
  71. if (isset($order->status) == false || $order->status != 1) {
  72. util::success(['returnStatus' => 'FAILURE'], '不是待付款订单');
  73. }
  74. OrderClass::valid($order, $this->shopId);
  75. $cgId = $order->purchaseId ?? 0;
  76. $cg = PurchaseClass::getById($cgId, true);
  77. $orderSn = $cg->orderSn ?? '';
  78. if (empty($orderSn)) {
  79. util::success(['returnStatus' => 'FAILURE'], '没有找到采购单');
  80. }
  81. $deadline = $cg->deadline;
  82. $current = date("Y-m-d H:i:s");
  83. if ((strtotime($deadline) + 20) < strtotime($current)) {
  84. util::success(['returnStatus' => 'FAILURE'], '订单已经失效');
  85. }
  86. $totalFee = $cg->actPrice ?? 0;
  87. $outTradeNo = $orderSn;
  88. $subject = '购买商品';
  89. $totalAmount = $totalFee;
  90. $body = "到店订单";
  91. //标记为扫码支付 和 自取订单
  92. $order->codePay = 2;
  93. $order->sendType = OrderClass::SEND_TYPE_NO;
  94. $order->save();
  95. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  96. $cg->codePay = 2;
  97. $cg->save();
  98. $wxPayWay = dict::getDict('payWay', 'wxPay');
  99. $aliPayWay = dict::getDict('payWay', 'alipay');
  100. $payWay = Yii::$app->request->get('payWay', $wxPayWay);
  101. if ($payWay == $wxPayWay) {
  102. if (isset($order->wxPayUrl) && !empty($order->wxPayUrl)) {
  103. $url = $order->wxPayUrl;
  104. } else {
  105. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  106. require_once($wx . '/lib/WxPay.Api.php');
  107. require_once($wx . '/example/WxPay.NativePay.php');
  108. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  109. $wxPayType = 0;
  110. if (httpUtil::isMiniProgram()) {
  111. $wxPayType = 1;
  112. }
  113. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  114. $attach = "couponId=0&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  115. $input = new \WxPayUnifiedOrder();
  116. $input->SetBody($subject);
  117. $input->SetOut_trade_no($orderSn);
  118. $input->SetTotal_fee($totalFee * 100);
  119. $input->SetAttach($attach);
  120. $input->SetTime_start(date("YmdHis"));
  121. $input->SetTime_expire(date("YmdHis", time() + 600));
  122. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  123. $input->SetTrade_type("NATIVE");
  124. //native支付必传
  125. $input->SetProduct_id("hdCG_" . $cgId);
  126. //获取微信商户号等信息
  127. $sjExtend = WxOpenClass::getWxInfo();
  128. $notify = new \NativePay();
  129. $result = $notify->GetPayUrl($input, $sjExtend);
  130. $url = $result["code_url"] ?? '';
  131. if (empty($url)) {
  132. util::success(['returnStatus' => 'FAILURE'], '二维码生成失败');
  133. }
  134. $order->wxPayUrl = $url;
  135. $order->save();
  136. }
  137. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  138. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  139. util::success(['url' => $payUrl, 'returnStatus' => 'SUCCESS']);
  140. }
  141. if ($payWay == $aliPayWay) {
  142. if (isset($order->aliPayUrl) && !empty($order->aliPayUrl)) {
  143. $url = $order->aliPayUrl;
  144. } else {
  145. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  146. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  147. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  148. $aliPId = '2017041906821571';
  149. $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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nO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  151. $config = array(
  152. //签名方式,默认为RSA2(RSA2048)
  153. 'sign_type' => "RSA2",
  154. //支付宝公钥
  155. 'alipay_public_key' => $aliPublicKey,
  156. //商户私钥
  157. 'merchant_private_key' => $aliKey,
  158. //编码格式
  159. 'charset' => "UTF-8",
  160. //支付宝网关
  161. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  162. //应用ID
  163. 'app_id' => $aliPId,
  164. //异步通知地址,只有扫码支付预下单可用
  165. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/cg-ali-callback",
  166. //最大查询重试次数
  167. 'MaxQueryRetry' => "10",
  168. //查询间隔
  169. 'QueryDuration' => "3"
  170. );
  171. // 支付超时,线下扫码交易定义为5分钟
  172. $timeExpress = "5m";
  173. // 创建请求builder,设置请求参数
  174. $qrPayRequestBuilder = new \AlipayTradePayContentBuilder();
  175. $qrPayRequestBuilder->setOutTradeNo($outTradeNo);
  176. $qrPayRequestBuilder->setTotalAmount($totalAmount);
  177. $qrPayRequestBuilder->setTimeExpress($timeExpress);
  178. $qrPayRequestBuilder->setSubject($subject);
  179. $qrPayRequestBuilder->setBody($body);
  180. // 调用barPay方法获取当面付应答
  181. $qrPay = new \AlipayTradeService($config);
  182. $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);
  183. if ($qrPayResult->getTradeStatus() != 'SUCCESS') {
  184. util::success(['returnStatus' => 'FAILURE'], '二维码生成失败..');
  185. }
  186. $respond = $qrPayResult->getResponse();
  187. $url = $respond->qr_code ?? '';
  188. if (empty($url)) {
  189. util::success(['returnStatus' => 'FAILURE'], '二维码生成失败');
  190. }
  191. $order->aliPayUrl = $url;
  192. $order->save();
  193. }
  194. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  195. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  196. util::success(['url' => $payUrl, 'returnStatus' => 'SUCCESS']);
  197. }
  198. util::success(['returnStatus' => 'FAILURE'], '不支持的收款方式');
  199. }
  200. //微信和支付宝付款码支付复查 shish 20211231
  201. public function actionCodePayCheck()
  202. {
  203. ini_set('date.timezone', 'Asia/Shanghai');
  204. header("Content-type: text/html; charset=utf-8");
  205. $get = Yii::$app->request->get();
  206. $id = isset($get['id']) ? $get['id'] : 0;
  207. $order = OrderClass::getById($id, true);
  208. if (empty($order)) {
  209. util::complete('没有找到订单');
  210. }
  211. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  212. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  213. util::complete('订单已付款');
  214. }
  215. util::complete('订单不是待付款状态');
  216. }
  217. OrderClass::valid($order, $this->shopId);
  218. $cgId = $order->purchaseId ?? 0;
  219. $cg = PurchaseClass::getById($cgId, true);
  220. $orderSn = $cg->orderSn ?? '';
  221. $totalFee = $cg->actPrice ?? 0;
  222. if (empty($orderSn)) {
  223. util::complete('没有找到采购单');
  224. }
  225. $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
  226. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  227. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  228. //微信付款
  229. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  230. require_once($wx . '/lib/WxPay.Api.php');
  231. require_once($wx . '/example/WxPay.MicroPay.php');
  232. //获取微信商户号等信息
  233. $sjExtend = WxOpenClass::getWxInfo();
  234. $microPay = new \MicroPay();
  235. $payReturn = $microPay->query($orderSn, $sjExtend);
  236. if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
  237. if ($payReturn["trade_state"] == "SUCCESS") {
  238. //支付成功
  239. $transactionId = $payReturn['transaction_id'] ?? '';
  240. $openId = $payReturn['openid'] ?? '';
  241. $aliUserId = '';
  242. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  243. $cg->codePay = 1;
  244. $cg->payOpenId = $openId;
  245. $cg->aliUserId = $aliUserId;
  246. //自取订单
  247. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  248. $cg->save();
  249. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  250. $order->codePay = 1;
  251. //自取订单
  252. $order->sendType = OrderClass::SEND_TYPE_NO;
  253. $order->save();
  254. $attach = '';
  255. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  256. util::success(['returnStatus' => 'SUCCESS']);
  257. } else if ($payReturn["trade_state"] == "USERPAYING") {
  258. //用户支付中
  259. util::complete('正在付款,请稍候..');
  260. }
  261. }
  262. if ($payReturn["err_code"] == "ORDERNOTEXIST") {
  263. //交易订单号不存在
  264. util::complete('订单没有付款');
  265. } else {
  266. util::complete('系统错误,请稍候再试');
  267. }
  268. } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
  269. //支付宝付款
  270. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  271. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  272. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  273. $aliPId = '2017041906821571';
  274. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApi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ilnQcts5VLFaChx0GCvS2BZgy2W0QKBgQDjP2VtRq4aVSLUq2z1KIDI7GJBbOM5KSW1OEj4sfEdViozPh3Ar/FQWiij1oCZF+iJdKkonHoDbDd+Q0ykcpQ+5Gv07iiT8wJYFR/0j8g2kfql7bVQhGSBeBMS5sawKR3CvkfW73WX+CNnf0PklTY0kTyt6WRXDnKSeyxFVwSZ7g==';
  275. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsz3m9juAR1xew4DY6c34gvbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  276. $config = array(
  277. //签名方式,默认为RSA2(RSA2048)
  278. 'sign_type' => "RSA2",
  279. //支付宝公钥
  280. 'alipay_public_key' => $aliPublicKey,
  281. //商户私钥
  282. 'merchant_private_key' => $aliKey,
  283. //编码格式
  284. 'charset' => "UTF-8",
  285. //支付宝网关
  286. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  287. //应用ID
  288. 'app_id' => $aliPId,
  289. //异步通知地址,只有扫码支付预下单可用
  290. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  291. //最大查询重试次数
  292. 'MaxQueryRetry' => "10",
  293. //查询间隔
  294. 'QueryDuration' => "3"
  295. );
  296. //构造查询业务请求参数对象
  297. $queryContentBuilder = new \AlipayTradeQueryContentBuilder();
  298. $queryContentBuilder->setOutTradeNo($orderSn);
  299. //初始化类对象,调用queryTradeResult方法获取查询应答
  300. $queryResponse = new \AlipayTradeService($config);
  301. $queryResult = $queryResponse->queryTradeResult($queryContentBuilder);
  302. if ($queryResult->getTradeStatus() == 'SUCCESS') {
  303. $respond = $queryResult->getResponse();
  304. $openId = '';
  305. $aliUserId = $respond->buyer_user_id ?? '';
  306. $transactionId = $respond->trade_no ?? '';
  307. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  308. $cg->codePay = 1;
  309. $cg->payOpenId = $openId;
  310. $cg->aliUserId = $aliUserId;
  311. //自取订单
  312. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  313. $cg->save();
  314. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  315. $order->codePay = 1;
  316. //自取订单
  317. $order->sendType = OrderClass::SEND_TYPE_NO;
  318. $order->save();
  319. $attach = '';
  320. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  321. util::success(['returnStatus' => 'SUCCESS']);
  322. }
  323. } else {
  324. util::fail('还没有付款哦');
  325. }
  326. }
  327. //微信和支付宝付款码支付 shish 2021.4.11
  328. public function actionCodePay()
  329. {
  330. ini_set('date.timezone', 'Asia/Shanghai');
  331. header("Content-type: text/html; charset=utf-8");
  332. $get = Yii::$app->request->get();
  333. $id = isset($get['id']) ? $get['id'] : 0;
  334. $order = OrderClass::getById($id, true);
  335. if (isset($order->status) == false || $order->status != 1) {
  336. util::fail('不是待付款订单');
  337. }
  338. OrderClass::valid($order, $this->shopId);
  339. $cgId = $order->purchaseId ?? 0;
  340. $cg = PurchaseClass::getById($cgId, true);
  341. $orderSn = $cg->orderSn ?? '';
  342. if (empty($orderSn)) {
  343. util::fail('没有找到采购单');
  344. }
  345. //付款码
  346. $authCode = (string)$get['authCode'] ?? '';
  347. if (empty($authCode)) {
  348. util::fail('支付失败');
  349. }
  350. $wxPrefix = ['10', '11', '12', '13', '14', '15'];
  351. $isWx = false;
  352. foreach ($wxPrefix as $wxId) {
  353. if (strpos($authCode, $wxId) === 0) {
  354. $isWx = true;
  355. }
  356. }
  357. $deadline = $cg->deadline;
  358. $current = date("Y-m-d H:i:s");
  359. if ((strtotime($deadline) + 20) < strtotime($current)) {
  360. util::fail('订单已经失效');
  361. }
  362. $totalFee = $cg->actPrice ?? 0;
  363. $outTradeNo = $orderSn;
  364. $subject = '购买商品';
  365. $totalAmount = $totalFee;
  366. $body = "门店";
  367. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  368. if ($isWx) {
  369. $wxPayWay = dict::getDict('payWay', 'wxPay');
  370. $order->payWay = $wxPayWay;
  371. $order->save();
  372. $cg->payWay = $wxPayWay;
  373. $cg->save();
  374. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  375. require_once($wx . '/lib/WxPay.Api.php');
  376. require_once($wx . '/example/WxPay.MicroPay.php');
  377. $input = new \WxPayMicroPay();
  378. $input->SetAuth_code($authCode);
  379. $input->SetBody($subject);
  380. $input->SetTotal_fee($totalFee * 100);
  381. $input->SetOut_trade_no($orderSn);
  382. //获取微信商户号等信息
  383. $sjExtend = WxOpenClass::getWxInfo();
  384. $microPay = new \MicroPay();
  385. $res = $microPay->pay($input, $sjExtend);
  386. if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS" && isset($res["result_code"]) && $res["result_code"] == 'SUCCESS') {
  387. $transactionId = $res['transaction_id'] ?? '';
  388. $openId = $res['openid'] ?? '';
  389. $aliUserId = '';
  390. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  391. $cg->codePay = 1;
  392. $cg->payOpenId = $openId;
  393. $cg->aliUserId = $aliUserId;
  394. //自取订单
  395. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  396. $cg->save();
  397. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  398. $order->codePay = 1;
  399. //自取订单
  400. $order->sendType = OrderClass::SEND_TYPE_NO;
  401. $order->save();
  402. $attach = '';
  403. payUtil::thirdPay($wxPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  404. util::success(['returnStatus' => 'SUCCESS']);
  405. } else {
  406. Yii::info(json_encode($res));
  407. $errCode = $res['err_code'] ?? '';
  408. $msg = '';
  409. if ($errCode == 'NOTENOUGH') {
  410. $msg = '余额不足';
  411. }
  412. if ($errCode == 'USERPAYING') {
  413. $msg = '等待客户输入支付密码';
  414. }
  415. util::success(['returnStatus' => 'FAILURE'], $msg);
  416. }
  417. } else {
  418. $aliPayWay = dict::getDict('payWay', 'alipay');
  419. $order->payWay = $aliPayWay;
  420. $order->save();
  421. $cg->payWay = $aliPayWay;
  422. $cg->save();
  423. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  424. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  425. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  426. $aliPId = '2017041906821571';
  427. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDq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vbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  429. $config = array(
  430. //签名方式,默认为RSA2(RSA2048)
  431. 'sign_type' => "RSA2",
  432. //支付宝公钥
  433. 'alipay_public_key' => $aliPublicKey,
  434. //商户私钥
  435. 'merchant_private_key' => $aliKey,
  436. //编码格式
  437. 'charset' => "UTF-8",
  438. //支付宝网关
  439. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  440. //应用ID
  441. 'app_id' => $aliPId,
  442. //异步通知地址,只有扫码支付预下单可用
  443. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  444. //最大查询重试次数
  445. 'MaxQueryRetry' => "10",
  446. //查询间隔
  447. 'QueryDuration' => "3"
  448. );
  449. // 支付超时,线下扫码交易定义为5分钟
  450. $timeExpress = "5m";
  451. // 创建请求builder,设置请求参数
  452. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  453. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  454. $barPayRequestBuilder->setTotalAmount($totalAmount);
  455. $barPayRequestBuilder->setAuthCode($authCode);
  456. $barPayRequestBuilder->setTimeExpress($timeExpress);
  457. $barPayRequestBuilder->setSubject($subject);
  458. $barPayRequestBuilder->setBody($body);
  459. // 调用barPay方法获取当面付应答
  460. $barPay = new \AlipayTradeService($config);
  461. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  462. if ($barPayResult->getTradeStatus() == 'SUCCESS') {
  463. $respond = $barPayResult->getResponse();
  464. $openId = '';
  465. $aliUserId = $respond->buyer_user_id ?? '';
  466. $transactionId = $respond->trade_no ?? '';
  467. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  468. $cg->codePay = 1;
  469. $cg->payOpenId = $openId;
  470. $cg->aliUserId = $aliUserId;
  471. //自取订单
  472. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  473. $cg->save();
  474. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  475. $order->codePay = 1;
  476. //自取订单
  477. $order->sendType = OrderClass::SEND_TYPE_NO;
  478. $order->save();
  479. $attach = '';
  480. payUtil::thirdPay($aliPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  481. util::success(['returnStatus' => 'SUCCESS']);
  482. }
  483. util::success(['returnStatus' => 'FAILURE']);
  484. }
  485. }
  486. //订单列表 ssh 2021.1.20
  487. public function actionList()
  488. {
  489. $get = Yii::$app->request->get();
  490. $status = $get['status'] ?? 0;
  491. if (!empty($status)) {
  492. $where['status'] = $status;
  493. }
  494. $where['sjId'] = $this->sjId;
  495. if (isset($get['shopId'])) {
  496. $shopId = $get['shopId'] ?? 0;
  497. if (!empty($shopId)) {
  498. $where['shopId'] = $this->shopId;
  499. }
  500. } else {
  501. $where['shopId'] = $this->shopId;
  502. }
  503. $searchTime = $get['searchTime'] ?? '';
  504. if (!empty($searchTime)) {
  505. $startTime = $get['startTime'] ?? '';
  506. $endTime = $get['endTime'] ?? '';
  507. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  508. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  509. }
  510. $name = $get['name'] ?? '';
  511. if (!empty($name)) {
  512. $searchType = $get['searchType'] ?? 0;
  513. if (empty($searchType)) {
  514. //订单编号
  515. if (strpos($name, 'XSD') === 0) {
  516. $where['orderSn'] = $name;
  517. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  518. $where['customNamePy'] = ['like', $name];
  519. } else {
  520. $where['customName'] = ['like', $name];
  521. }
  522. } elseif ($searchType == 1) {
  523. $where['sendNum'] = $name;
  524. } else {
  525. }
  526. }
  527. $respond = OrderClass::getOrderList($where);
  528. $respond['shop'] = $this->shop;
  529. $respond['shopExt'] = $this->shopExt;
  530. util::success($respond);
  531. }
  532. //商城下单操作 ssh 2021.1.14
  533. public function actionCreateOrder()
  534. {
  535. $post = Yii::$app->request->post();
  536. $shopId = $this->shopId;
  537. $customId = $post['customId'] ?? 0;
  538. $post['customId'] = $customId;
  539. //开单必须选客户
  540. if (empty($customId)) {
  541. util::fail('请选择客户');
  542. }
  543. $custom = CustomClass::getCustom($customId);
  544. if (empty($custom)) {
  545. util::fail('请选客户哦');
  546. }
  547. CustomClass::valid($custom, $this->shopId);
  548. $post['ghsId'] = $custom['ghsId'] ?? 0;
  549. $post['customMobile'] = $custom['mobile'] ?? '';
  550. $customName = $custom['name'] ?? '';
  551. $post['customName'] = $customName;
  552. $post['customNamePy'] = $custom['py'] ?? '';
  553. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  554. $post['shopAdminId'] = $this->shopAdminId;
  555. $post['province'] = $custom['province'] ?? '';
  556. $post['city'] = $custom['city'] ?? '';
  557. $post['dist'] = $custom['dist'] ?? '';
  558. $post['address'] = $custom['address'] ?? '';
  559. $post['floor'] = $custom['floor'] ?? '';
  560. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  561. $post['showAddress'] = $custom['showAddress'] ?? '';
  562. $post['long'] = $custom['long'] ?? '';
  563. $post['lat'] = $custom['lat'] ?? '';
  564. $shopAdmin = $this->shopAdmin->attributes;
  565. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  566. $post['sjId'] = $this->sjId;
  567. $post['shopId'] = $shopId;
  568. $post['mainId'] = $this->mainId;
  569. $post['fromType'] = 1;
  570. $post['expressId'] = $post['expressId'] ?? 0;
  571. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  572. $post['book'] = $post['book'] ?? 0;
  573. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  574. $post['debt'] = OrderClass::DEBT_NO;
  575. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  576. $post['debt'] = OrderClass::DEBT_YES;
  577. }
  578. $shopAdmin = $this->shopAdmin;
  579. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  580. if ($hasPay == dict::getDict('hasPay', 'payed')) {
  581. util::fail('没有权限');
  582. }
  583. }
  584. $productJson = $post['product'] ?? '';
  585. if (empty($productJson)) {
  586. util::fail('请选择花材');
  587. }
  588. $productList = json_decode($productJson, true);
  589. if (empty($productList)) {
  590. util::fail('请选择花材');
  591. }
  592. $connection = Yii::$app->db;//事务处理
  593. $transaction = $connection->beginTransaction();
  594. try {
  595. //判断花材有效性
  596. ProductClass::valid($productList, $this->mainId);
  597. $post['product'] = $productList;
  598. //商家开单到时间会自动标记为欠款,所以360天内不过期,以保证足够时间标记欠款,具体参数查看dict local_gys_kd_auto_set_debt_time
  599. $post['deadline'] = date("Y-m-d H:i:s", strtotime('+360 day'));
  600. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  601. $transaction->commit();
  602. util::success($return);
  603. } catch (\Exception $e) {
  604. $transaction->rollBack();
  605. Yii::error("下单失败原因:" . $e->getMessage());
  606. util::fail('下单失败');
  607. }
  608. }
  609. //修改订单 ssh 20210810
  610. public function actionUpdateOrder()
  611. {
  612. $post = Yii::$app->request->post();
  613. //PC端开单
  614. if (isset($post['clearType'])) {
  615. $clearType = $post['clearType'];
  616. if ($clearType == OrderClass::CLEAR_DEBT) {
  617. $post['debt'] = OrderClass::DEBT_YES;
  618. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  619. } else {
  620. $post['debt'] = OrderClass::DEBT_NO;
  621. }
  622. }
  623. //员工不能开已收款订单
  624. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  625. if ($debt == OrderClass::DEBT_NO) {
  626. $shopAdmin = $this->shopAdmin;
  627. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  628. util::fail('不能开已收款订单');
  629. }
  630. }
  631. //PC端开单
  632. if (isset($post['getType'])) {
  633. util::fail('暂不支持PC端修改订单');
  634. $getType = $post['getType'];
  635. //自取
  636. if ($getType == 1) {
  637. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  638. } else {
  639. //选择配送时,在发货时让商家自己再确认一下用什么方式
  640. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  641. }
  642. }
  643. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  644. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  645. $post['debt'] = OrderClass::DEBT_NO;
  646. }
  647. $id = $post['id'] ?? 0;
  648. $order = OrderClass::getLockById($id);
  649. if (empty($order)) {
  650. util::fail('没有找到订单');
  651. }
  652. OrderClass::valid($order, $this->shopId);
  653. unset($post['id']);
  654. $shopAdmin = $this->shopAdmin;
  655. $post['shopAdminId'] = $this->shopAdminId;
  656. $post['shopAdminName'] = $shopAdmin->name ?? '';
  657. $post['fromType'] = 1;
  658. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  659. $productJson = $post['product'] ?? '';
  660. if (empty($productJson)) {
  661. util::fail('请选择花材');
  662. }
  663. $productList = json_decode($productJson, true);
  664. if (empty($productList)) {
  665. util::fail('请选择花材');
  666. }
  667. $connection = Yii::$app->db;//事务处理
  668. $transaction = $connection->beginTransaction();
  669. try {
  670. //判断花材有效性
  671. ProductClass::valid($productList, $this->mainId);
  672. $post['product'] = $productList;
  673. //商家开单订单有效期,到期记欠款
  674. $validTime = dict::getDict('order_pay_has_time');
  675. $current = time() + $validTime;
  676. $post['deadline'] = date("Y-m-d H:i:s", $current);
  677. $upData = [
  678. 'payWay' => $post['payWay'],
  679. 'sendType' => $post['sendType'],
  680. 'debt' => $post['debt'],
  681. 'needPrint' => $post['needPrint'],
  682. 'modifyPrice' => $post['modifyPrice'],
  683. 'shopAdminId' => $post['shopAdminId'],
  684. 'shopAdminName' => $post['shopAdminName'],
  685. 'fromType' => 1,
  686. 'sendCost' => $post['sendCost'],
  687. 'product' => $post['product'],
  688. 'deadline' => $post['deadline'],
  689. 'debt' => $post['debt'],
  690. ];
  691. $return = OrderService::updateOrder($order, $upData);
  692. $transaction->commit();
  693. util::success($return);
  694. } catch (\Exception $e) {
  695. $transaction->rollBack();
  696. Yii::error("下单失败原因:" . $e->getMessage());
  697. util::fail('下单失败');
  698. }
  699. }
  700. //延期支付 ssh 2021.1.19
  701. public function actionDebt()
  702. {
  703. $id = Yii::$app->request->get('id');
  704. $order = OrderClass::getById($id, true);
  705. OrderClass::valid($order, $this->shopId);
  706. $payWay = dict::getDict('payWay', 'debtPay');
  707. $cgId = $order->purchaseId ?? 0;
  708. $cg = PurchaseClass::getById($cgId, true);
  709. if (empty($cg)) {
  710. util::fail('没有找到采购单');
  711. }
  712. PurchaseService::payAfter($cg, $payWay);
  713. OrderService::payAfter($order, $payWay);
  714. util::complete();
  715. }
  716. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  717. public function actionWxPay()
  718. {
  719. ini_set('date.timezone', 'Asia/Shanghai');
  720. $post = Yii::$app->request->post();
  721. $couponId = $post['couponId'] ?? 0;
  722. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  723. $order = OrderClass::getByOrderSn($orderSn);
  724. if (empty($order)) {
  725. util::fail('订单号无效');
  726. }
  727. $id = $order['id'];
  728. //支付前验证订单有效性
  729. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  730. util::fail('没有权限操作');
  731. }
  732. $name = $order['orderName'] ?? '购买商品';
  733. $totalFee = $order['actPrice'];
  734. //强制使用小程序的miniOpenId
  735. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  736. if (empty($openId)) {
  737. util::fail('没有找到openId');
  738. }
  739. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  740. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  741. $wxPayType = 0;
  742. if (httpUtil::isMiniProgram()) {
  743. $wxPayType = 1;
  744. }
  745. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  746. //订单30分钟后过期
  747. $now = time();
  748. $expireTime = $now + 1800;
  749. $wx = Yii::getAlias("@vendor/weixin");
  750. require_once($wx . '/lib/WxPay.Api.php');
  751. require_once($wx . '/example/WxPay.JsApiPay.php');
  752. $input = new \WxPayUnifiedOrder();
  753. $input->SetBody($name);
  754. $input->SetOut_trade_no($orderSn);
  755. $input->SetTotal_fee($totalFee * 100);
  756. $input->SetTime_start(date("YmdHis", $now));
  757. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  758. $input->SetTime_expire(date("YmdHis", $expireTime));
  759. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  760. $input->SetTrade_type("JSAPI");
  761. //花卉宝代为申请的微信支付
  762. //$sjExtend = $this->sjExtend->attributes;
  763. $sjExtend = WxOpenClass::getGhsWxInfo();
  764. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  765. $input->SetSub_openid($openId);
  766. } else {
  767. $input->SetOpenid($openId);
  768. }
  769. //强制使用小程序的miniAppId
  770. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  771. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  772. $tools = new \JsApiPay();
  773. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  774. $newParams = json_decode($jsApiParameters, true);
  775. //微信不能修改价格
  776. $current = date("Y-m-d H:i:s");
  777. $updateData = ['deadline' => $current, 'modPrice' => 0];
  778. OrderClass::updateById($id, $updateData);
  779. util::success($newParams);
  780. }
  781. //获取订单详情 ssh 2021.1.21
  782. public function actionDetail()
  783. {
  784. $get = Yii::$app->request->get();
  785. $id = $get['id'] ?? 0;
  786. $info = OrderService::getOrderInfo($id, true, true, true, false);
  787. OrderClass::valid($info, $this->shopId);
  788. //是否有云打印判断
  789. $hasCloudPrint = 0;
  790. $shopExt = $this->shopExt;
  791. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  792. $hasCloudPrint = 1;
  793. }
  794. $info['hasCloudPrint'] = $hasCloudPrint;
  795. //店长信息
  796. $customShopId = $info['customShopId'] ?? 0;
  797. $superInfo = ShopAdminClass::getManager($customShopId);
  798. $info['customSuper'] = $superInfo;
  799. $purchaseId = $info['purchaseId'] ?? 0;
  800. $cg = PurchaseClass::getById($purchaseId);
  801. $info['cgInfo'] = $cg;
  802. util::success($info);
  803. }
  804. //自取免发货流程处理 ssh 2021.1.22
  805. public function actionWithoutSend()
  806. {
  807. $get = Yii::$app->request->get();
  808. $id = $get['id'] ?? 0;
  809. $info = OrderService::getById($id, true);
  810. OrderClass::valid($info, $this->shopId);
  811. $purchaseId = $info->purchaseId;
  812. $purchase = PurchaseClass::getById($purchaseId, true);
  813. OrderService::withoutSend($info, $purchase);
  814. util::complete();
  815. }
  816. //本店送 ssh 2021.1.24
  817. public function actionSelfSend()
  818. {
  819. $get = Yii::$app->request->get();
  820. $id = isset($get['id']) ? $get['id'] : 0;
  821. $info = OrderClass::getById($id, true);
  822. OrderClass::valid($info->attributes, $this->shopId);
  823. $connection = Yii::$app->db;
  824. $transaction = $connection->beginTransaction();
  825. try {
  826. OrderClass::selfSend($info);
  827. $transaction->commit();
  828. } catch (\Exception $exception) {
  829. $transaction->rollBack();
  830. Yii::info("本店送操作报错:" . $exception->getMessage());
  831. util::fail('操作失败');
  832. }
  833. util::complete();
  834. }
  835. //运费计算 ssh 2021.1.24
  836. public function actionFreight()
  837. {
  838. //2021.3.28 接入达达
  839. $get = Yii::$app->request->get();
  840. $post = Yii::$app->request->post();
  841. if (empty($get)) {
  842. $get = $post;
  843. }
  844. $orderId = $get['id'] ?? 0; //订单ID
  845. $customId = $get['customId'] ?? 0;
  846. if (empty($orderId)) {
  847. //自定义运费
  848. //freight::getCost();
  849. if (empty($customId)) {
  850. util::fail("请选择客户");
  851. }
  852. $customInfo = CustomClass::getById($customId);
  853. if (empty($customInfo)) {
  854. util::fail('没有找到客户');
  855. }
  856. $shop = $this->shop;
  857. //花材信息
  858. $productJson = $get['product'] ?? '';
  859. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  860. if (empty($productJson)) {
  861. util::fail('请选择花材');
  862. }
  863. $productList = json_decode($productJson, true);
  864. if (empty($productList)) {
  865. util::fail('请选择花材');
  866. }
  867. $productList = ProductClass::mergeItemInfo($productList);
  868. $weight = 0;
  869. $price = 0;
  870. $productData = ProductClass::getProductMapData($productList);
  871. $bigNum = 0;
  872. foreach ($productList as $key => $val) {
  873. $productId = $val['productId'];
  874. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  875. $bigNum += $val['bigNum'];
  876. $weight = bcadd($w, $weight, 2);
  877. $ratio = $productData[$productId]['ratio'] ?? 0;
  878. //大小数量合并多少扎
  879. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  880. //售卖价格
  881. $itemPrice = $productData[$productId]['price'] ?? 0;
  882. //合计价格
  883. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  884. }
  885. //没有经纬度的客户运费直接返回空 shish 20210612
  886. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  887. util::success(['sedCost' => '']);
  888. }
  889. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  890. util::success(['sedCost' => $cost]);
  891. }
  892. $province = $get['province'] ?? '';
  893. $city = $get['city'] ?? '';
  894. $address = $get['address'] ?? '';
  895. $floor = $get['floor'] ?? '';
  896. $customName = $get['customName'] ?? '';
  897. $customMobile = $get['customMobile'] ?? '';
  898. $fullAddress = $province . $city . $address . $floor;
  899. $sendTime = $get['sendTime'] ?? '';
  900. $lat = $get['lat'] ?? '';
  901. $lng = $get['long'] ?? '';
  902. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  903. util::fail('请填写客户地址');
  904. }
  905. ini_set('date.timezone', 'Asia/Shanghai');
  906. if (!empty($sendTime)) {
  907. //配送时间不为空
  908. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  909. $sendTimeInt = strtotime($sendTime);
  910. $now = time();
  911. $diff = $sendTimeInt - $now;
  912. //因服务器写入时间,改为至少提前6分钟
  913. if ($diff <= 360) {
  914. util::fail('配送时间至少提前6分钟');
  915. }
  916. } else {
  917. //默认立即发送
  918. $sendTime = 0;
  919. }
  920. //更新订单表
  921. $info = OrderService::getOrderInfo($orderId);
  922. OrderClass::valid($info, $this->shopId);
  923. if ($sendTime) {
  924. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  925. } else {
  926. $sendTime = '0000-00-00 00:00:00';
  927. }
  928. $expressAddInfo = [
  929. 'customName' => $customName,
  930. 'customMobile' => $customMobile,
  931. 'province' => $province,
  932. 'city' => $city,
  933. 'address' => $address,
  934. 'floor' => $floor,
  935. 'lat' => $lat,
  936. 'long' => $lng,
  937. 'sendTime' => $sendTime,
  938. ];
  939. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  940. //计算运费
  941. $info['province'] = $province;
  942. $info['city'] = $city;
  943. $info['address'] = $address;
  944. $info['floor'] = $floor;
  945. $info['fullAddress'] = $fullAddress;
  946. $info['lat'] = $lat;
  947. $info['long'] = $lng;
  948. $info['sendTime'] = $expressAddInfo['sendTime'];
  949. $res = DadaExpressServices::queryDeliverFee($info);
  950. util::success(['sedCost' => $res['fee']]);
  951. }
  952. //发快递和第三方配送 ssh 2021.1.24
  953. public function actionThirdSend()
  954. {
  955. $get = Yii::$app->request->get();
  956. $id = isset($get['id']) ? $get['id'] : 0;
  957. $info = OrderService::getById($id, true);
  958. OrderClass::valid($info->attributes, $this->shopId);
  959. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  960. util::fail('请填写客户地址');
  961. }
  962. $connection = Yii::$app->db;
  963. $transaction = $connection->beginTransaction();
  964. try {
  965. $respond = OrderClass::thirdSend($info, $get);
  966. $transaction->commit();
  967. util::success($respond);
  968. } catch (\Exception $exception) {
  969. $transaction->rollBack();
  970. Yii::info("发快递操作报错:" . $exception->getMessage());
  971. util::fail('操作失败' . $exception->getMessage());
  972. }
  973. }
  974. //确认送达 ssh 2021.1.24
  975. public function actionReach()
  976. {
  977. $get = Yii::$app->request->get();
  978. $id = isset($get['id']) ? $get['id'] : 0;
  979. $info = OrderClass::getById($id, true);
  980. OrderClass::valid($info->attributes, $this->shopId);
  981. $connection = Yii::$app->db;
  982. $transaction = $connection->beginTransaction();
  983. try {
  984. OrderService::reach($info);
  985. $transaction->commit();
  986. } catch (\Exception $exception) {
  987. $transaction->rollBack();
  988. Yii::info("送达操作报错:" . $exception->getMessage());
  989. util::fail('操作失败');
  990. }
  991. util::complete();
  992. }
  993. //下单要用到的相关信息 ssh 2019.12.6
  994. public function actionOrderRelate()
  995. {
  996. $regionTree = RegionService::tree();
  997. $shop = $this->shop->attributes;
  998. $freight = ['first' => 5, 'add' => 2];
  999. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  1000. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  1001. util::success($out);
  1002. }
  1003. //客户欠款的订单 ssh 2021.2.4
  1004. public function actionDebtList()
  1005. {
  1006. $get = Yii::$app->request->get();
  1007. $id = $get['id'] ?? 0;
  1008. $info = CustomClass::getCustom($id);
  1009. CustomClass::valid($info, $this->shopId);
  1010. $where = ['customId' => $id, 'debt' => 1];
  1011. $searchTime = $get['searchTime'] ?? '';
  1012. if (!empty($searchTime)) {
  1013. $startTime = $get['startTime'] ?? '';
  1014. $endTime = $get['endTime'] ?? '';
  1015. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1016. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1017. }
  1018. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'realPrice', 'actPrice', 'addTime']);
  1019. $result = ['customInfo' => $info, 'list' => $list];
  1020. util::success($result);
  1021. }
  1022. //更新打印次数
  1023. public function actionAddPrintNum()
  1024. {
  1025. $id = Yii::$app->request->get('id', 0);
  1026. $order = OrderClass::getById($id, true);
  1027. OrderClass::valid($order, $this->shopId);
  1028. $order->printNum += 1;
  1029. $order->save();
  1030. util::complete();
  1031. }
  1032. //打印订单 shish 20210714
  1033. public function actionCloudPrintOrder()
  1034. {
  1035. $id = Yii::$app->request->get('id', 0);
  1036. $order = OrderClass::getById($id, true);
  1037. OrderClass::valid($order, $this->shopId);
  1038. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  1039. util::fail('订单还没有付款');
  1040. }
  1041. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  1042. util::fail('订单已取消');
  1043. }
  1044. $ext = $this->shopExt;
  1045. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  1046. util::success(['hasNoPrint' => 1]);
  1047. }
  1048. OrderClass::onlinePrint($order);
  1049. $order->printNum += 1;
  1050. $order->save();
  1051. util::complete();
  1052. }
  1053. //订单确认完成 shish 20210809
  1054. public function actionConfirmFinish()
  1055. {
  1056. $payWay = Yii::$app->request->get('payWay', -1);
  1057. $id = Yii::$app->request->get('id', 0);
  1058. $clearType = Yii::$app->request->get('clearType', 0);
  1059. $connection = Yii::$app->db;
  1060. $transaction = $connection->beginTransaction();
  1061. try {
  1062. $order = OrderClass::getById($id, true);
  1063. OrderClass::valid($order, $this->shopId);
  1064. $purchaseId = $order->purchaseId ?? 0;
  1065. $purchase = PurchaseClass::getById($purchaseId, true);
  1066. if (empty($purchase)) {
  1067. util::fail('没有采购信息');
  1068. }
  1069. if ($clearType == 1) {
  1070. //欠款
  1071. $payWay = dict::getDict('payWay', 'debtPay');
  1072. } elseif ($clearType == 2) {
  1073. //已收款
  1074. if ($payWay == -1) {
  1075. util::fail('请选择收款方式');
  1076. }
  1077. $shopAdmin = $this->shopAdmin;
  1078. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1079. util::fail('超管才能发起已收款');
  1080. }
  1081. } else {
  1082. util::fail('请选择结算方式');
  1083. }
  1084. //不需要打印
  1085. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  1086. $order->save();
  1087. //支付
  1088. PurchaseService::payAfter($purchase, $payWay);
  1089. OrderService::payAfter($order, $payWay);
  1090. //自己送
  1091. $info = OrderClass::getById($id, true);
  1092. OrderClass::selfSend($info);
  1093. //确认送达,并且不需要微信通知
  1094. Yii::$app->params['noNeedWxNotice'] = 1;
  1095. $info = OrderClass::getById($id, true);
  1096. OrderService::reach($info);
  1097. $transaction->commit();
  1098. util::complete('操作成功');
  1099. } catch (\Exception $exception) {
  1100. $transaction->rollBack();
  1101. Yii::info("确认完成订单报错:" . $exception->getMessage());
  1102. util::fail('操作失败');
  1103. }
  1104. }
  1105. //取消订单 shish 20220110
  1106. public function actionCancel()
  1107. {
  1108. $shopAdmin = $this->shopAdmin;
  1109. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1110. util::fail('超管才能取消订单');
  1111. }
  1112. $id = Yii::$app->request->get('id');
  1113. $order = OrderClass::getById($id, true);
  1114. if ($order->orderType == 2) {
  1115. util::fail('客户下单,不能主动取消');
  1116. }
  1117. OrderClass::valid($order, $this->shopId);
  1118. OrderService::expire($order, true);
  1119. util::complete('已取消');
  1120. }
  1121. //获取收款语音播报地址 shish 20211231
  1122. public function actionGetPayVoice()
  1123. {
  1124. $id = Yii::$app->request->get('id');
  1125. $order = OrderClass::getById($id, true);
  1126. $payWay = $order->payWay ?? 0;
  1127. $money = floatval($order->actPrice);
  1128. $payment = $payWay == dict::getDict('payWay', 'alipay') ? '支付宝' : '微信';
  1129. $msg = $payment . '收款' . $money . '元';
  1130. $audio = baiduAip::transfer($msg, 4, 0);
  1131. //上传到oss
  1132. $file = time() . rand(10000, 999999) . '.mp3';
  1133. $filePath = 'tmp/arrival/money/' . $file;
  1134. oss::uploadTmpAudio($filePath, $audio);
  1135. $url = Yii::$app->params['ghsImgHost'] . $filePath;
  1136. util::success(['url' => $url]);
  1137. }
  1138. }