OrderController.php 33 KB

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