OrderController.php 36 KB

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