OrderController.php 51 KB

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