OrderController.php 33 KB

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