OrderController.php 52 KB

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