OrderController.php 53 KB

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