OrderController.php 54 KB

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