OrderController.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. //支付宝扫码支付 shish 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. //微信和支付宝扫码支付 shish 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+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnv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ey = '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. //微信和支付宝付款码支付复查 shish 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::complete('没有找到订单');
  221. }
  222. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  223. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  224. util::complete('订单已付款');
  225. }
  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::getWxInfo();
  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/iD1p9iVfgMx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QdaNGh2F13iLozIDLQOtKw3WfcjOq0xpZ5pwXq0RI8iSw+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. //微信和支付宝付款码支付 shish 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::getWxInfo();
  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/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KX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W1OEj4sfEdViozPh3Ar/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. $post['ghsId'] = $custom['ghsId'] ?? 0;
  560. $post['customMobile'] = $custom['mobile'] ?? '';
  561. $customName = $custom['name'] ?? '';
  562. $post['customName'] = $customName;
  563. $post['customNamePy'] = $custom['py'] ?? '';
  564. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  565. $post['shopAdminId'] = $this->shopAdminId;
  566. $post['province'] = $custom['province'] ?? '';
  567. $post['city'] = $custom['city'] ?? '';
  568. $post['dist'] = $custom['dist'] ?? '';
  569. $post['address'] = $custom['address'] ?? '';
  570. $post['floor'] = $custom['floor'] ?? '';
  571. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  572. $post['showAddress'] = $custom['showAddress'] ?? '';
  573. $post['long'] = $custom['long'] ?? '';
  574. $post['lat'] = $custom['lat'] ?? '';
  575. $shopAdmin = $this->shopAdmin->attributes;
  576. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  577. $post['sjId'] = $this->sjId;
  578. $post['shopId'] = $shopId;
  579. $post['mainId'] = $this->mainId;
  580. $post['fromType'] = 1;
  581. $post['expressId'] = $post['expressId'] ?? 0;
  582. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  583. $post['book'] = $post['book'] ?? 0;
  584. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  585. $post['debt'] = OrderClass::DEBT_NO;
  586. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  587. $post['debt'] = OrderClass::DEBT_YES;
  588. }
  589. $shopAdmin = $this->shopAdmin;
  590. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  591. if ($hasPay == dict::getDict('hasPay', 'payed')) {
  592. util::fail('没有权限');
  593. }
  594. }
  595. $productJson = $post['product'] ?? '';
  596. if (empty($productJson)) {
  597. util::fail('请选择花材');
  598. }
  599. $productList = json_decode($productJson, true);
  600. if (empty($productList)) {
  601. util::fail('请选择花材');
  602. }
  603. $connection = Yii::$app->db;//事务处理
  604. $transaction = $connection->beginTransaction();
  605. try {
  606. //判断花材有效性
  607. ProductClass::valid($productList, $this->mainId);
  608. $post['product'] = $productList;
  609. //商家开单到时间会自动标记为欠款,所以360天内不过期,以保证足够时间标记欠款,具体参数查看dict local_gys_kd_auto_set_debt_time
  610. $post['deadline'] = date("Y-m-d H:i:s", strtotime('+360 day'));
  611. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  612. $transaction->commit();
  613. util::success($return);
  614. } catch (\Exception $e) {
  615. $transaction->rollBack();
  616. Yii::error("下单失败原因:" . $e->getMessage());
  617. util::fail('下单失败');
  618. }
  619. }
  620. //修改订单 ssh 20210810
  621. public function actionUpdateOrder()
  622. {
  623. $post = Yii::$app->request->post();
  624. //PC端开单
  625. if (isset($post['clearType'])) {
  626. $clearType = $post['clearType'];
  627. if ($clearType == OrderClass::CLEAR_DEBT) {
  628. $post['debt'] = OrderClass::DEBT_YES;
  629. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  630. } else {
  631. $post['debt'] = OrderClass::DEBT_NO;
  632. }
  633. }
  634. //员工不能开已收款订单
  635. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  636. if ($debt == OrderClass::DEBT_NO) {
  637. $shopAdmin = $this->shopAdmin;
  638. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  639. util::fail('不能开已收款订单');
  640. }
  641. }
  642. //PC端开单
  643. if (isset($post['getType'])) {
  644. util::fail('暂不支持PC端修改订单');
  645. $getType = $post['getType'];
  646. //自取
  647. if ($getType == 1) {
  648. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  649. } else {
  650. //选择配送时,在发货时让商家自己再确认一下用什么方式
  651. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  652. }
  653. }
  654. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  655. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  656. $post['debt'] = OrderClass::DEBT_NO;
  657. }
  658. $id = $post['id'] ?? 0;
  659. $order = OrderClass::getLockById($id);
  660. if (empty($order)) {
  661. util::fail('没有找到订单');
  662. }
  663. OrderClass::valid($order, $this->shopId);
  664. unset($post['id']);
  665. $shopAdmin = $this->shopAdmin;
  666. $post['shopAdminId'] = $this->shopAdminId;
  667. $post['shopAdminName'] = $shopAdmin->name ?? '';
  668. $post['fromType'] = 1;
  669. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  670. $productJson = $post['product'] ?? '';
  671. if (empty($productJson)) {
  672. util::fail('请选择花材');
  673. }
  674. $productList = json_decode($productJson, true);
  675. if (empty($productList)) {
  676. util::fail('请选择花材');
  677. }
  678. $connection = Yii::$app->db;//事务处理
  679. $transaction = $connection->beginTransaction();
  680. try {
  681. //判断花材有效性
  682. ProductClass::valid($productList, $this->mainId);
  683. $post['product'] = $productList;
  684. //商家开单订单有效期,到期记欠款
  685. $validTime = dict::getDict('order_pay_has_time');
  686. $current = time() + $validTime;
  687. $post['deadline'] = date("Y-m-d H:i:s", $current);
  688. $upData = [
  689. 'payWay' => $post['payWay'],
  690. 'sendType' => $post['sendType'],
  691. 'debt' => $post['debt'],
  692. 'needPrint' => $post['needPrint'],
  693. 'modifyPrice' => $post['modifyPrice'],
  694. 'shopAdminId' => $post['shopAdminId'],
  695. 'shopAdminName' => $post['shopAdminName'],
  696. 'fromType' => 1,
  697. 'sendCost' => $post['sendCost'],
  698. 'product' => $post['product'],
  699. 'deadline' => $post['deadline'],
  700. 'debt' => $post['debt'],
  701. ];
  702. $return = OrderService::updateOrder($order, $upData);
  703. $transaction->commit();
  704. util::success($return);
  705. } catch (\Exception $e) {
  706. $transaction->rollBack();
  707. Yii::error("下单失败原因:" . $e->getMessage());
  708. util::fail('下单失败');
  709. }
  710. }
  711. //延期支付 ssh 2021.1.19
  712. public function actionDebt()
  713. {
  714. $id = Yii::$app->request->get('id');
  715. $order = OrderClass::getById($id, true);
  716. OrderClass::valid($order, $this->shopId);
  717. $payWay = dict::getDict('payWay', 'debtPay');
  718. $cgId = $order->purchaseId ?? 0;
  719. $cg = PurchaseClass::getById($cgId, true);
  720. if (empty($cg)) {
  721. util::fail('没有找到采购单');
  722. }
  723. PurchaseService::payAfter($cg, $payWay);
  724. OrderService::payAfter($order, $payWay);
  725. util::complete();
  726. }
  727. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  728. public function actionWxPay()
  729. {
  730. ini_set('date.timezone', 'Asia/Shanghai');
  731. $post = Yii::$app->request->post();
  732. $couponId = $post['couponId'] ?? 0;
  733. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  734. $order = OrderClass::getByOrderSn($orderSn);
  735. if (empty($order)) {
  736. util::fail('订单号无效');
  737. }
  738. $id = $order['id'];
  739. //支付前验证订单有效性
  740. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  741. util::fail('没有权限操作');
  742. }
  743. $name = $order['orderName'] ?? '购买商品';
  744. $totalFee = $order['actPrice'];
  745. //强制使用小程序的miniOpenId
  746. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  747. if (empty($openId)) {
  748. util::fail('没有找到openId');
  749. }
  750. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  751. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  752. $wxPayType = 0;
  753. if (httpUtil::isMiniProgram()) {
  754. $wxPayType = 1;
  755. }
  756. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  757. //订单30分钟后过期
  758. $now = time();
  759. $expireTime = $now + 1800;
  760. $wx = Yii::getAlias("@vendor/weixin");
  761. require_once($wx . '/lib/WxPay.Api.php');
  762. require_once($wx . '/example/WxPay.JsApiPay.php');
  763. $input = new \WxPayUnifiedOrder();
  764. $input->SetBody($name);
  765. $input->SetOut_trade_no($orderSn);
  766. $input->SetTotal_fee($totalFee * 100);
  767. $input->SetTime_start(date("YmdHis", $now));
  768. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  769. $input->SetTime_expire(date("YmdHis", $expireTime));
  770. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  771. $input->SetTrade_type("JSAPI");
  772. //花卉宝代为申请的微信支付
  773. //$sjExtend = $this->sjExtend->attributes;
  774. $sjExtend = WxOpenClass::getGhsWxInfo();
  775. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  776. $input->SetSub_openid($openId);
  777. } else {
  778. $input->SetOpenid($openId);
  779. }
  780. //强制使用小程序的miniAppId
  781. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  782. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  783. $tools = new \JsApiPay();
  784. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  785. $newParams = json_decode($jsApiParameters, true);
  786. //微信不能修改价格
  787. $current = date("Y-m-d H:i:s");
  788. $updateData = ['deadline' => $current, 'modPrice' => 0];
  789. OrderClass::updateById($id, $updateData);
  790. util::success($newParams);
  791. }
  792. //获取订单详情 ssh 2021.1.21
  793. public function actionDetail()
  794. {
  795. $get = Yii::$app->request->get();
  796. $id = $get['id'] ?? 0;
  797. $info = OrderService::getOrderInfo($id, true, true, true, false);
  798. OrderClass::valid($info, $this->shopId);
  799. //是否有云打印判断
  800. $hasCloudPrint = 0;
  801. $shopExt = $this->shopExt;
  802. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  803. $hasCloudPrint = 1;
  804. }
  805. $info['hasCloudPrint'] = $hasCloudPrint;
  806. //店长信息
  807. $customShopId = $info['customShopId'] ?? 0;
  808. $superInfo = ShopAdminClass::getManager($customShopId);
  809. $info['customSuper'] = $superInfo;
  810. $purchaseId = $info['purchaseId'] ?? 0;
  811. $cg = PurchaseClass::getById($purchaseId);
  812. $info['cgInfo'] = $cg;
  813. util::success($info);
  814. }
  815. //自取免发货流程处理 ssh 2021.1.22
  816. public function actionWithoutSend()
  817. {
  818. $get = Yii::$app->request->get();
  819. $id = $get['id'] ?? 0;
  820. $info = OrderService::getById($id, true);
  821. OrderClass::valid($info, $this->shopId);
  822. $purchaseId = $info->purchaseId;
  823. $purchase = PurchaseClass::getById($purchaseId, true);
  824. OrderService::withoutSend($info, $purchase);
  825. util::complete();
  826. }
  827. //本店送 ssh 2021.1.24
  828. public function actionSelfSend()
  829. {
  830. $get = Yii::$app->request->get();
  831. $id = isset($get['id']) ? $get['id'] : 0;
  832. $info = OrderClass::getById($id, true);
  833. OrderClass::valid($info->attributes, $this->shopId);
  834. $connection = Yii::$app->db;
  835. $transaction = $connection->beginTransaction();
  836. try {
  837. OrderClass::selfSend($info);
  838. $transaction->commit();
  839. } catch (\Exception $exception) {
  840. $transaction->rollBack();
  841. Yii::info("本店送操作报错:" . $exception->getMessage());
  842. util::fail('操作失败');
  843. }
  844. util::complete();
  845. }
  846. //运费计算 ssh 2021.1.24
  847. public function actionFreight()
  848. {
  849. //2021.3.28 接入达达
  850. $get = Yii::$app->request->get();
  851. $post = Yii::$app->request->post();
  852. if (empty($get)) {
  853. $get = $post;
  854. }
  855. $orderId = $get['id'] ?? 0; //订单ID
  856. $customId = $get['customId'] ?? 0;
  857. if (empty($orderId)) {
  858. //自定义运费
  859. //freight::getCost();
  860. if (empty($customId)) {
  861. util::fail("请选择客户");
  862. }
  863. $customInfo = CustomClass::getById($customId);
  864. if (empty($customInfo)) {
  865. util::fail('没有找到客户');
  866. }
  867. $shop = $this->shop;
  868. //花材信息
  869. $productJson = $get['product'] ?? '';
  870. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  871. if (empty($productJson)) {
  872. util::fail('请选择花材');
  873. }
  874. $productList = json_decode($productJson, true);
  875. if (empty($productList)) {
  876. util::fail('请选择花材');
  877. }
  878. $productList = ProductClass::mergeItemInfo($productList);
  879. $weight = 0;
  880. $price = 0;
  881. $productData = ProductClass::getProductMapData($productList);
  882. $bigNum = 0;
  883. foreach ($productList as $key => $val) {
  884. $productId = $val['productId'];
  885. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  886. $bigNum += $val['bigNum'];
  887. $weight = bcadd($w, $weight, 2);
  888. $ratio = $productData[$productId]['ratio'] ?? 0;
  889. //大小数量合并多少扎
  890. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  891. //售卖价格
  892. $itemPrice = $productData[$productId]['price'] ?? 0;
  893. //合计价格
  894. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  895. }
  896. //没有经纬度的客户运费直接返回空 shish 20210612
  897. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  898. util::success(['sedCost' => '']);
  899. }
  900. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  901. util::success(['sedCost' => $cost]);
  902. }
  903. $province = $get['province'] ?? '';
  904. $city = $get['city'] ?? '';
  905. $address = $get['address'] ?? '';
  906. $floor = $get['floor'] ?? '';
  907. $customName = $get['customName'] ?? '';
  908. $customMobile = $get['customMobile'] ?? '';
  909. $fullAddress = $province . $city . $address . $floor;
  910. $sendTime = $get['sendTime'] ?? '';
  911. $lat = $get['lat'] ?? '';
  912. $lng = $get['long'] ?? '';
  913. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  914. util::fail('请填写客户地址');
  915. }
  916. ini_set('date.timezone', 'Asia/Shanghai');
  917. if (!empty($sendTime)) {
  918. //配送时间不为空
  919. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  920. $sendTimeInt = strtotime($sendTime);
  921. $now = time();
  922. $diff = $sendTimeInt - $now;
  923. //因服务器写入时间,改为至少提前6分钟
  924. if ($diff <= 360) {
  925. util::fail('配送时间至少提前6分钟');
  926. }
  927. } else {
  928. //默认立即发送
  929. $sendTime = 0;
  930. }
  931. //更新订单表
  932. $info = OrderService::getOrderInfo($orderId);
  933. OrderClass::valid($info, $this->shopId);
  934. if ($sendTime) {
  935. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  936. } else {
  937. $sendTime = '0000-00-00 00:00:00';
  938. }
  939. $expressAddInfo = [
  940. 'customName' => $customName,
  941. 'customMobile' => $customMobile,
  942. 'province' => $province,
  943. 'city' => $city,
  944. 'address' => $address,
  945. 'floor' => $floor,
  946. 'lat' => $lat,
  947. 'long' => $lng,
  948. 'sendTime' => $sendTime,
  949. ];
  950. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  951. //计算运费
  952. $info['province'] = $province;
  953. $info['city'] = $city;
  954. $info['address'] = $address;
  955. $info['floor'] = $floor;
  956. $info['fullAddress'] = $fullAddress;
  957. $info['lat'] = $lat;
  958. $info['long'] = $lng;
  959. $info['sendTime'] = $expressAddInfo['sendTime'];
  960. $res = DadaExpressServices::queryDeliverFee($info);
  961. util::success(['sedCost' => $res['fee']]);
  962. }
  963. //发快递和第三方配送 ssh 2021.1.24
  964. public function actionThirdSend()
  965. {
  966. $get = Yii::$app->request->get();
  967. $id = isset($get['id']) ? $get['id'] : 0;
  968. $info = OrderService::getById($id, true);
  969. OrderClass::valid($info->attributes, $this->shopId);
  970. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  971. util::fail('请填写客户地址');
  972. }
  973. $connection = Yii::$app->db;
  974. $transaction = $connection->beginTransaction();
  975. try {
  976. $respond = OrderClass::thirdSend($info, $get);
  977. $transaction->commit();
  978. util::success($respond);
  979. } catch (\Exception $exception) {
  980. $transaction->rollBack();
  981. Yii::info("发快递操作报错:" . $exception->getMessage());
  982. util::fail('操作失败' . $exception->getMessage());
  983. }
  984. }
  985. //确认送达 ssh 2021.1.24
  986. public function actionReach()
  987. {
  988. $get = Yii::$app->request->get();
  989. $id = isset($get['id']) ? $get['id'] : 0;
  990. $info = OrderClass::getById($id, true);
  991. OrderClass::valid($info->attributes, $this->shopId);
  992. $connection = Yii::$app->db;
  993. $transaction = $connection->beginTransaction();
  994. try {
  995. OrderService::reach($info);
  996. $transaction->commit();
  997. } catch (\Exception $exception) {
  998. $transaction->rollBack();
  999. Yii::info("送达操作报错:" . $exception->getMessage());
  1000. util::fail('操作失败');
  1001. }
  1002. util::complete();
  1003. }
  1004. //下单要用到的相关信息 ssh 2019.12.6
  1005. public function actionOrderRelate()
  1006. {
  1007. $regionTree = RegionService::tree();
  1008. $shop = $this->shop->attributes;
  1009. $freight = ['first' => 5, 'add' => 2];
  1010. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  1011. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  1012. util::success($out);
  1013. }
  1014. //客户欠款的订单 ssh 2021.2.4
  1015. public function actionDebtList()
  1016. {
  1017. $get = Yii::$app->request->get();
  1018. $id = $get['id'] ?? 0;
  1019. $info = CustomClass::getCustom($id);
  1020. CustomClass::valid($info, $this->shopId);
  1021. $where = ['customId' => $id, 'debt' => 1];
  1022. $searchTime = $get['searchTime'] ?? '';
  1023. if (!empty($searchTime)) {
  1024. $startTime = $get['startTime'] ?? '';
  1025. $endTime = $get['endTime'] ?? '';
  1026. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1027. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1028. }
  1029. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'realPrice', 'actPrice', 'addTime']);
  1030. $result = ['customInfo' => $info, 'list' => $list];
  1031. util::success($result);
  1032. }
  1033. //更新打印次数
  1034. public function actionAddPrintNum()
  1035. {
  1036. $id = Yii::$app->request->get('id', 0);
  1037. $order = OrderClass::getById($id, true);
  1038. OrderClass::valid($order, $this->shopId);
  1039. $order->printNum += 1;
  1040. $order->save();
  1041. util::complete();
  1042. }
  1043. //打印订单 shish 20210714
  1044. public function actionCloudPrintOrder()
  1045. {
  1046. $id = Yii::$app->request->get('id', 0);
  1047. $order = OrderClass::getById($id, true);
  1048. OrderClass::valid($order, $this->shopId);
  1049. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  1050. util::fail('订单还没有付款');
  1051. }
  1052. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  1053. util::fail('订单已取消');
  1054. }
  1055. $ext = $this->shopExt;
  1056. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  1057. util::success(['hasNoPrint' => 1]);
  1058. }
  1059. OrderClass::onlinePrint($order);
  1060. $order->printNum += 1;
  1061. $order->save();
  1062. util::complete();
  1063. }
  1064. //订单确认完成 shish 20210809
  1065. public function actionConfirmFinish()
  1066. {
  1067. $payWay = Yii::$app->request->get('payWay', -1);
  1068. $id = Yii::$app->request->get('id', 0);
  1069. $clearType = Yii::$app->request->get('clearType', 0);
  1070. $connection = Yii::$app->db;
  1071. $transaction = $connection->beginTransaction();
  1072. try {
  1073. $order = OrderClass::getById($id, true);
  1074. OrderClass::valid($order, $this->shopId);
  1075. $purchaseId = $order->purchaseId ?? 0;
  1076. $purchase = PurchaseClass::getById($purchaseId, true);
  1077. if (empty($purchase)) {
  1078. util::fail('没有采购信息');
  1079. }
  1080. if ($clearType == 1) {
  1081. //欠款
  1082. $payWay = dict::getDict('payWay', 'debtPay');
  1083. } elseif ($clearType == 2) {
  1084. //已收款
  1085. if ($payWay == -1) {
  1086. util::fail('请选择收款方式');
  1087. }
  1088. $shopAdmin = $this->shopAdmin;
  1089. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1090. util::fail('超管才能发起已收款');
  1091. }
  1092. } else {
  1093. util::fail('请选择结算方式');
  1094. }
  1095. //不需要打印
  1096. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  1097. $order->save();
  1098. //支付
  1099. PurchaseService::payAfter($purchase, $payWay);
  1100. OrderService::payAfter($order, $payWay);
  1101. //自己送
  1102. $info = OrderClass::getById($id, true);
  1103. OrderClass::selfSend($info);
  1104. //确认送达,并且不需要微信通知
  1105. Yii::$app->params['noNeedWxNotice'] = 1;
  1106. $info = OrderClass::getById($id, true);
  1107. OrderService::reach($info);
  1108. $transaction->commit();
  1109. util::complete('操作成功');
  1110. } catch (\Exception $exception) {
  1111. $transaction->rollBack();
  1112. Yii::info("确认完成订单报错:" . $exception->getMessage());
  1113. util::fail('操作失败');
  1114. }
  1115. }
  1116. //取消订单 shish 20220110
  1117. public function actionCancel()
  1118. {
  1119. $shopAdmin = $this->shopAdmin;
  1120. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1121. util::fail('超管才能取消订单');
  1122. }
  1123. $id = Yii::$app->request->get('id');
  1124. $order = OrderClass::getById($id, true);
  1125. if ($order->orderType == 2) {
  1126. util::fail('客户下单,不能主动取消');
  1127. }
  1128. OrderClass::valid($order, $this->shopId);
  1129. OrderService::expire($order, true);
  1130. util::complete('已取消');
  1131. }
  1132. //获取收款语音播报地址 shish 20211231
  1133. public function actionGetPayVoice()
  1134. {
  1135. $id = Yii::$app->request->get('id');
  1136. $order = OrderClass::getById($id, true);
  1137. $payWay = $order->payWay ?? 0;
  1138. $money = floatval($order->actPrice);
  1139. $payment = $payWay == dict::getDict('payWay', 'alipay') ? '支付宝' : '微信';
  1140. $msg = $payment . '收款' . $money . '元';
  1141. $audio = baiduAip::transfer($msg, 4, 0);
  1142. //上传到oss
  1143. $file = time() . rand(10000, 999999) . '.mp3';
  1144. $filePath = 'tmp/arrival/money/' . $file;
  1145. oss::uploadTmpAudio($filePath, $audio);
  1146. $url = Yii::$app->params['ghsImgHost'] . $filePath;
  1147. util::success(['url' => $url]);
  1148. }
  1149. }