OrderController.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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::fail('不是待付款订单');
  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::fail('没有找到采购单');
  54. }
  55. $deadline = $cg->deadline;
  56. $current = date("Y-m-d H:i:s");
  57. if ((strtotime($deadline) + 20) < strtotime($current)) {
  58. util::fail('订单已经失效');
  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::fail('二维码生成失败');
  104. }
  105. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  106. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  107. util::success(['url' => $payUrl]);
  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/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8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t6WRXDnKSeyxFVwSZ7g==';
  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::fail('二维码生成失败');
  153. }
  154. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  155. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  156. util::success(['url' => $payUrl]);
  157. }
  158. util::fail('二维码生成失败..');
  159. }
  160. util::fail('不支持的收款方式');
  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 = '2017041906821571';
  236. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm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hsywqQECgYBa8+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. }
  368. util::success(['returnStatus' => 'FAILURE']);
  369. } else {
  370. $aliPayWay = dict::getDict('payWay', 'alipay');
  371. $order->payWay = $aliPayWay;
  372. $order->save();
  373. $cg->payWay = $aliPayWay;
  374. $cg->save();
  375. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  376. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  377. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  378. $aliPId = '2017041906821571';
  379. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDqI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bwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  381. $config = array(
  382. //签名方式,默认为RSA2(RSA2048)
  383. 'sign_type' => "RSA2",
  384. //支付宝公钥
  385. 'alipay_public_key' => $aliPublicKey,
  386. //商户私钥
  387. 'merchant_private_key' => $aliKey,
  388. //编码格式
  389. 'charset' => "UTF-8",
  390. //支付宝网关
  391. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  392. //应用ID
  393. 'app_id' => $aliPId,
  394. //异步通知地址,只有扫码支付预下单可用
  395. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  396. //最大查询重试次数
  397. 'MaxQueryRetry' => "10",
  398. //查询间隔
  399. 'QueryDuration' => "3"
  400. );
  401. // 支付超时,线下扫码交易定义为5分钟
  402. $timeExpress = "5m";
  403. // 创建请求builder,设置请求参数
  404. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  405. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  406. $barPayRequestBuilder->setTotalAmount($totalAmount);
  407. $barPayRequestBuilder->setAuthCode($authCode);
  408. $barPayRequestBuilder->setTimeExpress($timeExpress);
  409. $barPayRequestBuilder->setSubject($subject);
  410. $barPayRequestBuilder->setBody($body);
  411. // 调用barPay方法获取当面付应答
  412. $barPay = new \AlipayTradeService($config);
  413. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  414. if ($barPayResult->getTradeStatus() == 'SUCCESS') {
  415. $respond = $barPayResult->getResponse();
  416. $openId = '';
  417. $aliUserId = $respond->buyer_user_id ?? '';
  418. $transactionId = $respond->trade_no ?? '';
  419. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  420. $cg->codePay = 1;
  421. $cg->payOpenId = $openId;
  422. $cg->aliUserId = $aliUserId;
  423. //自取订单
  424. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  425. $cg->save();
  426. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  427. $order->codePay = 1;
  428. //自取订单
  429. $order->sendType = OrderClass::SEND_TYPE_NO;
  430. $order->save();
  431. $attach = '';
  432. payUtil::thirdPay($aliPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  433. util::success(['returnStatus' => 'SUCCESS']);
  434. }
  435. util::success(['returnStatus' => 'FAILURE']);
  436. }
  437. }
  438. //订单列表 ssh 2021.1.20
  439. public function actionList()
  440. {
  441. $get = Yii::$app->request->get();
  442. $status = $get['status'] ?? 0;
  443. if (!empty($status)) {
  444. $where['status'] = $status;
  445. }
  446. $where['sjId'] = $this->sjId;
  447. if (isset($get['shopId'])) {
  448. $shopId = $get['shopId'] ?? 0;
  449. if (!empty($shopId)) {
  450. $where['shopId'] = $this->shopId;
  451. }
  452. } else {
  453. $where['shopId'] = $this->shopId;
  454. }
  455. $searchTime = $get['searchTime'] ?? '';
  456. if (!empty($searchTime)) {
  457. $startTime = $get['startTime'] ?? '';
  458. $endTime = $get['endTime'] ?? '';
  459. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  460. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  461. }
  462. $name = $get['name'] ?? '';
  463. if (!empty($name)) {
  464. $searchType = $get['searchType'] ?? 0;
  465. if (empty($searchType)) {
  466. //订单编号
  467. if (strpos($name, 'XSD') === 0) {
  468. $where['orderSn'] = $name;
  469. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  470. $where['customNamePy'] = ['like', $name];
  471. } else {
  472. $where['customName'] = ['like', $name];
  473. }
  474. } elseif ($searchType == 1) {
  475. $where['sendNum'] = $name;
  476. } else {
  477. }
  478. }
  479. $respond = OrderClass::getOrderList($where);
  480. $respond['shop'] = $this->shop;
  481. $respond['shopExt'] = $this->shopExt;
  482. util::success($respond);
  483. }
  484. //商城下单操作 ssh 2021.1.14
  485. public function actionCreateOrder()
  486. {
  487. $post = Yii::$app->request->post();
  488. if (isset($post['newVersion']) == false) {
  489. util::fail('请升级小程序');
  490. }
  491. unset($post['newVersion']);
  492. $shopId = $this->shopId;
  493. $customId = $post['customId'] ?? 0;
  494. $post['customId'] = $customId;
  495. //开单必须选客户
  496. if (empty($customId)) {
  497. util::fail('请选择客户');
  498. }
  499. $custom = CustomClass::getCustom($customId);
  500. if (empty($custom)) {
  501. util::fail('请选客户哦');
  502. }
  503. CustomClass::valid($custom, $this->shopId);
  504. $post['ghsId'] = $custom['ghsId'] ?? 0;
  505. $post['customMobile'] = $custom['mobile'] ?? '';
  506. $customName = $custom['name'] ?? '';
  507. $post['customName'] = $customName;
  508. $post['customNamePy'] = $custom['py'] ?? '';
  509. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  510. $post['shopAdminId'] = $this->shopAdminId;
  511. $post['province'] = $custom['province'] ?? '';
  512. $post['city'] = $custom['city'] ?? '';
  513. $post['dist'] = $custom['dist'] ?? '';
  514. $post['address'] = $custom['address'] ?? '';
  515. $post['floor'] = $custom['floor'] ?? '';
  516. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  517. $post['showAddress'] = $custom['showAddress'] ?? '';
  518. $post['long'] = $custom['long'] ?? '';
  519. $post['lat'] = $custom['lat'] ?? '';
  520. $shopAdmin = $this->shopAdmin->attributes;
  521. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  522. $post['sjId'] = $this->sjId;
  523. $post['shopId'] = $shopId;
  524. $post['fromType'] = 1;
  525. $post['expressId'] = $post['expressId'] ?? 0;
  526. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  527. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  528. $post['debt'] = OrderClass::DEBT_NO;
  529. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  530. $post['debt'] = OrderClass::DEBT_YES;
  531. }
  532. $shopAdmin = $this->shopAdmin;
  533. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  534. if ($hasPay == dict::getDict('hasPay', 'payed')) {
  535. util::fail('没有权限');
  536. }
  537. }
  538. $productJson = $post['product'] ?? '';
  539. if (empty($productJson)) {
  540. util::fail('请选择花材');
  541. }
  542. $productList = json_decode($productJson, true);
  543. if (empty($productList)) {
  544. util::fail('请选择花材');
  545. }
  546. $connection = Yii::$app->db;//事务处理
  547. $transaction = $connection->beginTransaction();
  548. try {
  549. //判断花材有效性
  550. ProductClass::valid($productList, $this->shopId);
  551. $post['product'] = $productList;
  552. //商家开单订单有效期更长(30)天
  553. $validTime = dict::getDict('kd_order_valid');
  554. $current = time() + $validTime;
  555. $post['deadline'] = date("Y-m-d H:i:s", $current);
  556. $return = OrderService::createOrder($post, $custom, $hasPay);
  557. $transaction->commit();
  558. util::success($return);
  559. } catch (\Exception $e) {
  560. $transaction->rollBack();
  561. Yii::error("下单失败原因:" . $e->getMessage());
  562. util::fail('下单失败');
  563. }
  564. }
  565. //修改订单 ssh 20210810
  566. public function actionUpdateOrder()
  567. {
  568. $post = Yii::$app->request->post();
  569. if (isset($post['newVersion']) == false) {
  570. util::fail('请升级小程序');
  571. }
  572. unset($post['newVersion']);
  573. //PC端开单
  574. if (isset($post['clearType'])) {
  575. $clearType = $post['clearType'];
  576. if ($clearType == OrderClass::CLEAR_DEBT) {
  577. $post['debt'] = OrderClass::DEBT_YES;
  578. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  579. } else {
  580. $post['debt'] = OrderClass::DEBT_NO;
  581. }
  582. }
  583. //员工不能开已收款订单
  584. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  585. if ($debt == OrderClass::DEBT_NO) {
  586. $shopAdmin = $this->shopAdmin;
  587. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  588. util::fail('不能开已收款订单');
  589. }
  590. }
  591. //PC端开单
  592. if (isset($post['getType'])) {
  593. util::fail('暂不支持PC端修改订单');
  594. $getType = $post['getType'];
  595. //自取
  596. if ($getType == 1) {
  597. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  598. } else {
  599. //选择配送时,在发货时让商家自己再确认一下用什么方式
  600. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  601. }
  602. }
  603. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  604. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  605. $post['debt'] = OrderClass::DEBT_NO;
  606. }
  607. $id = $post['id'] ?? 0;
  608. $order = OrderClass::getLockById($id);
  609. if (empty($order)) {
  610. util::fail('没有找到订单');
  611. }
  612. OrderClass::valid($order, $this->shopId);
  613. unset($post['id']);
  614. $shopAdmin = $this->shopAdmin;
  615. $post['shopAdminId'] = $this->shopAdminId;
  616. $post['shopAdminName'] = $shopAdmin->name ?? '';
  617. $post['fromType'] = 1;
  618. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  619. $productJson = $post['product'] ?? '';
  620. if (empty($productJson)) {
  621. util::fail('请选择花材');
  622. }
  623. $productList = json_decode($productJson, true);
  624. if (empty($productList)) {
  625. util::fail('请选择花材');
  626. }
  627. $connection = Yii::$app->db;//事务处理
  628. $transaction = $connection->beginTransaction();
  629. try {
  630. //判断花材有效性
  631. ProductClass::valid($productList, $this->shopId);
  632. $post['product'] = $productList;
  633. //商家开单订单有效期更长(30)天
  634. $validTime = dict::getDict('kd_order_valid');
  635. $current = time() + $validTime;
  636. $post['deadline'] = date("Y-m-d H:i:s", $current);
  637. $upData = [
  638. 'payWay' => $post['payWay'],
  639. 'sendType' => $post['sendType'],
  640. 'debt' => $post['debt'],
  641. 'needPrint' => $post['needPrint'],
  642. 'modifyPrice' => $post['modifyPrice'],
  643. 'shopAdminId' => $post['shopAdminId'],
  644. 'shopAdminName' => $post['shopAdminName'],
  645. 'fromType' => 1,
  646. 'sendCost' => $post['sendCost'],
  647. 'product' => $post['product'],
  648. 'deadline' => $post['deadline'],
  649. 'debt' => $post['debt'],
  650. ];
  651. $return = OrderService::updateOrder($order, $upData);
  652. $transaction->commit();
  653. util::success($return);
  654. } catch (\Exception $e) {
  655. $transaction->rollBack();
  656. Yii::error("下单失败原因:" . $e->getMessage());
  657. util::fail('下单失败');
  658. }
  659. }
  660. //延期支付 ssh 2021.1.19
  661. public function actionDebt()
  662. {
  663. $get = Yii::$app->request->get();
  664. $id = $get['id'] ?? 0;
  665. $info = OrderService::getOrderInfo($id);
  666. OrderClass::valid($info, $this->shopId);
  667. OrderClass::debt($info, $this->shop);
  668. util::complete();
  669. }
  670. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  671. public function actionWxPay()
  672. {
  673. ini_set('date.timezone', 'Asia/Shanghai');
  674. $post = Yii::$app->request->post();
  675. $couponId = $post['couponId'] ?? 0;
  676. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  677. $order = OrderClass::getByOrderSn($orderSn);
  678. if (empty($order)) {
  679. util::fail('订单号无效');
  680. }
  681. $id = $order['id'];
  682. //支付前验证订单有效性
  683. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  684. util::fail('没有权限操作');
  685. }
  686. $name = $order['orderName'] ?? '购买商品';
  687. $totalFee = $order['actPrice'];
  688. //强制使用小程序的miniOpenId
  689. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  690. if (empty($openId)) {
  691. util::fail('没有找到openId');
  692. }
  693. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  694. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  695. $wxPayType = 0;
  696. if (httpUtil::isMiniProgram()) {
  697. $wxPayType = 1;
  698. }
  699. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  700. //订单30分钟后过期
  701. $now = time();
  702. $expireTime = $now + 1800;
  703. $wx = Yii::getAlias("@vendor/weixin");
  704. require_once($wx . '/lib/WxPay.Api.php');
  705. require_once($wx . '/example/WxPay.JsApiPay.php');
  706. $input = new \WxPayUnifiedOrder();
  707. $input->SetBody($name);
  708. $input->SetOut_trade_no($orderSn);
  709. $input->SetTotal_fee($totalFee * 100);
  710. $input->SetTime_start(date("YmdHis", $now));
  711. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  712. $input->SetTime_expire(date("YmdHis", $expireTime));
  713. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  714. $input->SetTrade_type("JSAPI");
  715. //花卉宝代为申请的微信支付
  716. //$sjExtend = $this->sjExtend->attributes;
  717. $sjExtend = WxOpenClass::getGhsWxInfo();
  718. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  719. $input->SetSub_openid($openId);
  720. } else {
  721. $input->SetOpenid($openId);
  722. }
  723. //强制使用小程序的miniAppId
  724. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  725. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  726. $tools = new \JsApiPay();
  727. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  728. $newParams = json_decode($jsApiParameters, true);
  729. //微信不能修改价格
  730. $current = date("Y-m-d H:i:s");
  731. $updateData = ['deadline' => $current, 'modPrice' => 0];
  732. OrderClass::updateById($id, $updateData);
  733. util::success($newParams);
  734. }
  735. //获取订单详情 ssh 2021.1.21
  736. public function actionDetail()
  737. {
  738. $get = Yii::$app->request->get();
  739. $id = $get['id'] ?? 0;
  740. $info = OrderService::getOrderInfo($id, true, true, true, false);
  741. OrderClass::valid($info, $this->shopId);
  742. //是否有云打印判断
  743. $hasCloudPrint = 0;
  744. $shopExt = $this->shopExt;
  745. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  746. $hasCloudPrint = 1;
  747. }
  748. $info['hasCloudPrint'] = $hasCloudPrint;
  749. //店长信息
  750. $customShopId = $info['customShopId'] ?? 0;
  751. $superInfo = ShopAdminClass::getSuper($customShopId);
  752. $info['customSuper'] = $superInfo;
  753. $purchaseId = $info['purchaseId'] ?? 0;
  754. $cg = PurchaseClass::getById($purchaseId);
  755. $info['cgInfo'] = $cg;
  756. util::success($info);
  757. }
  758. //自取免发货流程处理 ssh 2021.1.22
  759. public function actionWithoutSend()
  760. {
  761. $get = Yii::$app->request->get();
  762. $id = $get['id'] ?? 0;
  763. $info = OrderService::getById($id, true);
  764. OrderClass::valid($info, $this->shopId);
  765. $purchaseId = $info->purchaseId;
  766. $purchase = PurchaseClass::getById($purchaseId, true);
  767. OrderService::withoutSend($info, $purchase);
  768. util::complete();
  769. }
  770. //本店送 ssh 2021.1.24
  771. public function actionSelfSend()
  772. {
  773. $get = Yii::$app->request->get();
  774. $id = isset($get['id']) ? $get['id'] : 0;
  775. $info = OrderClass::getById($id, true);
  776. OrderClass::valid($info->attributes, $this->shopId);
  777. $connection = Yii::$app->db;
  778. $transaction = $connection->beginTransaction();
  779. try {
  780. OrderClass::selfSend($info);
  781. $transaction->commit();
  782. } catch (\Exception $exception) {
  783. $transaction->rollBack();
  784. Yii::info("本店送操作报错:" . $exception->getMessage());
  785. util::fail('操作失败');
  786. }
  787. util::complete();
  788. }
  789. //运费计算 ssh 2021.1.24
  790. public function actionFreight()
  791. {
  792. //2021.3.28 接入达达
  793. $get = Yii::$app->request->get();
  794. $post = Yii::$app->request->post();
  795. if (empty($get)) {
  796. $get = $post;
  797. }
  798. $orderId = $get['id'] ?? 0; //订单ID
  799. $customId = $get['customId'] ?? 0;
  800. if (empty($orderId)) {
  801. //自定义运费
  802. //freight::getCost();
  803. if (empty($customId)) {
  804. util::fail("请选择客户");
  805. }
  806. $customInfo = CustomClass::getById($customId);
  807. if (empty($customInfo)) {
  808. util::fail('没有找到客户');
  809. }
  810. $shop = $this->shop;
  811. //花材信息
  812. $productJson = $get['product'] ?? '';
  813. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  814. if (empty($productJson)) {
  815. util::fail('请选择花材');
  816. }
  817. $productList = json_decode($productJson, true);
  818. if (empty($productList)) {
  819. util::fail('请选择花材');
  820. }
  821. $productList = ProductClass::mergeItemInfo($productList);
  822. $weight = 0;
  823. $price = 0;
  824. $productData = ProductClass::getProductMapData($productList);
  825. $bigNum = 0;
  826. foreach ($productList as $key => $val) {
  827. $productId = $val['productId'];
  828. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  829. $bigNum += $val['bigNum'];
  830. $weight = bcadd($w, $weight, 2);
  831. $ratio = $productData[$productId]['ratio'] ?? 0;
  832. //大小数量合并多少扎
  833. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  834. //售卖价格
  835. $itemPrice = $productData[$productId]['price'] ?? 0;
  836. //合计价格
  837. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  838. }
  839. //没有经纬度的客户运费直接返回空 shish 20210612
  840. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  841. util::success(['sedCost' => '']);
  842. }
  843. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  844. util::success(['sedCost' => $cost]);
  845. }
  846. $province = $get['province'] ?? '';
  847. $city = $get['city'] ?? '';
  848. $address = $get['address'] ?? '';
  849. $floor = $get['floor'] ?? '';
  850. $customName = $get['customName'] ?? '';
  851. $customMobile = $get['customMobile'] ?? '';
  852. $fullAddress = $province . $city . $address . $floor;
  853. $sendTime = $get['sendTime'] ?? '';
  854. $lat = $get['lat'] ?? '';
  855. $lng = $get['long'] ?? '';
  856. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  857. util::fail('请填写客户地址');
  858. }
  859. ini_set('date.timezone', 'Asia/Shanghai');
  860. if (!empty($sendTime)) {
  861. //配送时间不为空
  862. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  863. $sendTimeInt = strtotime($sendTime);
  864. $now = time();
  865. $diff = $sendTimeInt - $now;
  866. //因服务器写入时间,改为至少提前6分钟
  867. if ($diff <= 360) {
  868. util::fail('配送时间至少提前6分钟');
  869. }
  870. } else {
  871. //默认立即发送
  872. $sendTime = 0;
  873. }
  874. //更新订单表
  875. $info = OrderService::getOrderInfo($orderId);
  876. OrderClass::valid($info, $this->shopId);
  877. if ($sendTime) {
  878. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  879. } else {
  880. $sendTime = '0000-00-00 00:00:00';
  881. }
  882. $expressAddInfo = [
  883. 'customName' => $customName,
  884. 'customMobile' => $customMobile,
  885. 'province' => $province,
  886. 'city' => $city,
  887. 'address' => $address,
  888. 'floor' => $floor,
  889. 'lat' => $lat,
  890. 'long' => $lng,
  891. 'sendTime' => $sendTime,
  892. ];
  893. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  894. //计算运费
  895. $info['province'] = $province;
  896. $info['city'] = $city;
  897. $info['address'] = $address;
  898. $info['floor'] = $floor;
  899. $info['fullAddress'] = $fullAddress;
  900. $info['lat'] = $lat;
  901. $info['long'] = $lng;
  902. $info['sendTime'] = $expressAddInfo['sendTime'];
  903. $res = DadaExpressServices::queryDeliverFee($info);
  904. util::success(['sedCost' => $res['fee']]);
  905. }
  906. //发快递和第三方配送 ssh 2021.1.24
  907. public function actionThirdSend()
  908. {
  909. $get = Yii::$app->request->get();
  910. $id = isset($get['id']) ? $get['id'] : 0;
  911. $info = OrderService::getById($id, true);
  912. OrderClass::valid($info->attributes, $this->shopId);
  913. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  914. util::fail('请填写客户地址');
  915. }
  916. $connection = Yii::$app->db;
  917. $transaction = $connection->beginTransaction();
  918. try {
  919. $respond = OrderClass::thirdSend($info, $get);
  920. $transaction->commit();
  921. util::success($respond);
  922. } catch (\Exception $exception) {
  923. $transaction->rollBack();
  924. Yii::info("发快递操作报错:" . $exception->getMessage());
  925. util::fail('操作失败' . $exception->getMessage());
  926. }
  927. }
  928. //确认送达 ssh 2021.1.24
  929. public function actionReach()
  930. {
  931. $get = Yii::$app->request->get();
  932. $id = isset($get['id']) ? $get['id'] : 0;
  933. $info = OrderClass::getById($id, true);
  934. OrderClass::valid($info->attributes, $this->shopId);
  935. $connection = Yii::$app->db;
  936. $transaction = $connection->beginTransaction();
  937. try {
  938. OrderService::reach($info);
  939. $transaction->commit();
  940. } catch (\Exception $exception) {
  941. $transaction->rollBack();
  942. Yii::info("送达操作报错:" . $exception->getMessage());
  943. util::fail('操作失败');
  944. }
  945. util::complete();
  946. }
  947. //下单要用到的相关信息 ssh 2019.12.6
  948. public function actionOrderRelate()
  949. {
  950. $regionTree = RegionService::tree();
  951. $shop = $this->shop->attributes;
  952. $freight = ['first' => 5, 'add' => 2];
  953. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  954. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  955. util::success($out);
  956. }
  957. //客户欠款的订单 ssh 2021.2.4
  958. public function actionDebtList()
  959. {
  960. $id = Yii::$app->request->get('id', 0);
  961. $info = CustomClass::getCustom($id);
  962. CustomClass::valid($info, $this->shopId);
  963. $where = ['customId' => $id, 'debt' => 1];
  964. $respond = OrderService::getDebtOrderList($where);
  965. $respond['customInfo'] = $info;
  966. util::success($respond);
  967. }
  968. //更新打印次数
  969. public function actionAddPrintNum()
  970. {
  971. $id = Yii::$app->request->get('id', 0);
  972. $order = OrderClass::getById($id, true);
  973. OrderClass::valid($order, $this->shopId);
  974. $order->printNum += 1;
  975. $order->save();
  976. util::complete();
  977. }
  978. //打印订单 shish 20210714
  979. public function actionCloudPrintOrder()
  980. {
  981. $id = Yii::$app->request->get('id', 0);
  982. $order = OrderClass::getById($id, true);
  983. OrderClass::valid($order, $this->shopId);
  984. $ext = $this->shopExt;
  985. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  986. util::success(['hasNoPrint' => 1]);
  987. }
  988. OrderClass::onlinePrint($order);
  989. $order->printNum += 1;
  990. $order->save();
  991. util::complete();
  992. }
  993. //订单确认完成 shish 20210809
  994. public function actionConfirmFinish()
  995. {
  996. $payWay = Yii::$app->request->get('payWay', -1);
  997. $id = Yii::$app->request->get('id', 0);
  998. $clearType = Yii::$app->request->get('clearType', 0);
  999. $connection = Yii::$app->db;
  1000. $transaction = $connection->beginTransaction();
  1001. try {
  1002. $order = OrderClass::getById($id, true);
  1003. OrderClass::valid($order, $this->shopId);
  1004. $purchaseId = $order->purchaseId ?? 0;
  1005. $purchase = PurchaseClass::getById($purchaseId, true);
  1006. if (empty($purchase)) {
  1007. util::fail('没有采购信息');
  1008. }
  1009. if ($clearType == 1) {
  1010. //欠款
  1011. $payWay = dict::getDict('payWay', 'debtPay');
  1012. } elseif ($clearType == 2) {
  1013. //已收款
  1014. if ($payWay == -1) {
  1015. util::fail('请选择收款方式');
  1016. }
  1017. $shopAdmin = $this->shopAdmin;
  1018. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1019. util::fail('超管才能发起已收款');
  1020. }
  1021. } else {
  1022. util::fail('请选择结算方式');
  1023. }
  1024. //不需要打印
  1025. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  1026. $order->save();
  1027. //支付
  1028. PurchaseService::payAfter($purchase, $payWay);
  1029. OrderService::payAfter($order, $payWay);
  1030. //自己送
  1031. $info = OrderClass::getById($id, true);
  1032. OrderClass::selfSend($info);
  1033. //确认送达,并且不需要微信通知
  1034. Yii::$app->params['noNeedWxNotice'] = 1;
  1035. $info = OrderClass::getById($id, true);
  1036. OrderService::reach($info);
  1037. $transaction->commit();
  1038. util::complete('操作成功');
  1039. } catch (\Exception $exception) {
  1040. $transaction->rollBack();
  1041. Yii::info("确认完成订单报错:" . $exception->getMessage());
  1042. util::fail('操作失败');
  1043. }
  1044. }
  1045. //取消订单 shish 20210810
  1046. public function actionCancel()
  1047. {
  1048. $shopAdmin = $this->shopAdmin;
  1049. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1050. util::fail('超管才能取消订单');
  1051. }
  1052. $id = Yii::$app->request->get('id', 0);
  1053. $order = OrderClass::getLockById($id);
  1054. OrderClass::valid($order, $this->shopId);
  1055. OrderService::expire($order, true);
  1056. util::complete();
  1057. }
  1058. //获取收款语音播报地址 shish 20211231
  1059. public function actionGetPayVoice()
  1060. {
  1061. $id = Yii::$app->request->get('id');
  1062. $order = OrderClass::getById($id, true);
  1063. $payWay = $order->payWay ?? 0;
  1064. $money = floatval($order->actPrice);
  1065. $payment = $payWay == dict::getDict('payWay', 'alipay') ? '支付宝' : '微信';
  1066. $msg = $payment . '收款' . $money . '元';
  1067. $audio = baiduAip::transfer($msg, 4, 0);
  1068. //上传到oss
  1069. $file = time() . rand(10000, 999999) . '.mp3';
  1070. $filePath = 'tmp/arrival/money/' . $file;
  1071. oss::uploadTmpAudio($filePath, $audio);
  1072. $url = Yii::$app->params['ghsImgHost'] . $filePath;
  1073. util::success(['url' => $url]);
  1074. }
  1075. }