OrderController.php 55 KB

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