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