OrderController.php 48 KB

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