OrderController.php 55 KB

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