OrderController.php 53 KB

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