OrderController.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\custom\classes\CustomClass;
  7. use bizHd\goods\classes\GoodsClass;
  8. use bizHd\merchant\services\ShopService;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\classes\OrderSendClass;
  11. use bizHd\order\services\OrderService;
  12. use bizHd\product\classes\ProductClass;
  13. use bizHd\saas\services\RegionService;
  14. use bizHd\shop\classes\ShopClass;
  15. use bizHd\user\services\UserService;
  16. use bizHd\work\classes\WorkClass;
  17. use bizHd\wx\classes\WxOpenClass;
  18. use common\components\dict;
  19. use common\components\dateUtil;
  20. use common\components\payUtil;
  21. use common\services\xhPayToolService;
  22. use Yii;
  23. use common\components\util;
  24. use common\components\stringUtil;
  25. use bizHd\promote\services\CouponService;
  26. use common\components\httpUtil;
  27. use biz\wx\classes\WxMessageClass;
  28. class OrderController extends BaseController
  29. {
  30. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  31. //取消订单 ssh 20221116
  32. public function actionCancel()
  33. {
  34. $get = Yii::$app->request->get();
  35. $id = $get['id'] ?? 0;
  36. $order = OrderClass::getById($id, true);
  37. OrderClass::valid($order, $this->mainId);
  38. $deadTime = $order->deadline ?? 0;
  39. $current = time();
  40. $diff = bcsub($deadTime, $current);
  41. if ($diff < 60) {
  42. util::fail('60秒会自动取消');
  43. }
  44. OrderClass::setExpire($order, true);
  45. util::complete();
  46. }
  47. //修改订单 ssh 20220926
  48. public function actionUpdate()
  49. {
  50. $post = Yii::$app->request->post();
  51. $id = $post['id'] ?? 0;
  52. $order = OrderClass::getById($id, true);
  53. OrderClass::valid($order, $this->mainId);
  54. unset($post['id']);
  55. if ($order->status == 3) {
  56. util::fail('订单已配送');
  57. }
  58. if ($order->status == 4) {
  59. util::fail('订单已完成');
  60. }
  61. if ($order->status == 5) {
  62. util::fail('订单已取消');
  63. }
  64. if ($order->fromType == 4) {
  65. util::fail('请在美团APP上修改');
  66. }
  67. $address = $post['address'] ?? '';
  68. $floor = $post['floor'] ?? '';
  69. $post['fullAddress'] = $address . $floor;
  70. OrderClass::updateById($id, $post);
  71. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  72. $remark = $post['remark'] ?? '';
  73. $reachDate = $post['reachDate'] ?? '';
  74. $reachPeriod = $post['reachPeriod'] ?? '';
  75. $cardInfo = $post['cardInfo'] ?? '';
  76. $anonymity = $post['anonymity'] ?? 0;
  77. $bookName = $post['bookName'] ?? '';
  78. if (!empty($workList)) {
  79. foreach ($workList as $work) {
  80. $work->remark = $remark;
  81. $work->reachDate = $reachDate;
  82. $work->reachPeriod = $reachPeriod;
  83. $work->cardInfo = $cardInfo;
  84. $work->anonymity = $anonymity;
  85. $work->bookName = $bookName;
  86. $work->save();
  87. }
  88. }
  89. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  90. ShopExtClass::orderUpdateRemind($shopExt, $order);
  91. util::complete();
  92. }
  93. //客户欠款的订单 ssh 2021.2.4
  94. public function actionDebtList()
  95. {
  96. $get = Yii::$app->request->get();
  97. $id = $get['id'] ?? 0;
  98. $info = CustomClass::getById($id, true);
  99. CustomClass::valid($info, $this->shopId);
  100. $where = ['customId' => $id, 'debt' => 1];
  101. $searchTime = $get['searchTime'] ?? '';
  102. if (!empty($searchTime)) {
  103. $startTime = $get['startTime'] ?? '';
  104. $endTime = $get['endTime'] ?? '';
  105. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  106. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  107. }
  108. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice']);
  109. $result = ['customInfo' => $info, 'list' => $list];
  110. util::success($result);
  111. }
  112. //发货
  113. public function actionSend()
  114. {
  115. $get = Yii::$app->request->get();
  116. $id = isset($get['id']) ? $get['id'] : 0;
  117. $order = OrderClass::getById($id, true);
  118. if (empty($order)) {
  119. util::fail('没有找到订单');
  120. }
  121. OrderClass::valid($order, $this->mainId);
  122. if ($order->status != 2) {
  123. util::fail('订单不是待发货状态');
  124. }
  125. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  126. if (!empty($workList)) {
  127. $unComplete = false;
  128. foreach ($workList as $work) {
  129. if ($work->status != 1) {
  130. $unComplete = true;
  131. }
  132. }
  133. if ($unComplete == true) {
  134. util::fail('还有制作单没有完成');
  135. }
  136. }
  137. $order->status = 4;
  138. $order->save();
  139. util::complete('操作成功');
  140. }
  141. //打印订单 ssh 20220601
  142. public function actionPrintOrder()
  143. {
  144. $get = Yii::$app->request->get();
  145. $id = isset($get['id']) ? $get['id'] : 0;
  146. $order = OrderClass::getById($id, true);
  147. if (empty($order)) {
  148. util::fail('没有找到订单');
  149. }
  150. OrderClass::valid($order, $this->mainId);
  151. OrderClass::onlinePrint($order, true);
  152. util::complete();
  153. }
  154. //微信和支付宝付款码支付复查 ssh 20220422
  155. public function actionCodePayCheck()
  156. {
  157. ini_set('date.timezone', 'Asia/Shanghai');
  158. header("Content-type: text/html; charset=utf-8");
  159. $get = Yii::$app->request->get();
  160. $id = isset($get['id']) ? $get['id'] : 0;
  161. $recheck = $get['recheck'] ?? 0;
  162. $order = OrderClass::getById($id, true);
  163. OrderClass::valid($order, $this->mainId);
  164. $orderSn = $order->orderSn ?? '';
  165. $totalFee = $order->mainPay ?? 0;
  166. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  167. util::success(['returnStatus' => 'SUCCESS']);
  168. }
  169. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  170. util::complete('订单不是待付款状态');
  171. }
  172. $connection = Yii::$app->db;
  173. $transaction = $connection->beginTransaction();
  174. try {
  175. if ($recheck == 1) {
  176. $payWay = $get['payWay'] ?? 0;
  177. } else {
  178. $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
  179. }
  180. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  181. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  182. //微信付款
  183. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  184. require_once($wx . '/lib/WxPay.Api.php');
  185. require_once($wx . '/example/WxPay.MicroPay.php');
  186. //获取微信商户号等信息
  187. $sjExtend = WxOpenClass::getMallWxInfo();
  188. $microPay = new \MicroPay();
  189. $payReturn = $microPay->query($orderSn, $sjExtend);
  190. if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
  191. if ($payReturn["trade_state"] == "SUCCESS") {
  192. //支付成功
  193. $transactionId = $payReturn['transaction_id'] ?? '';
  194. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  195. $order->save();
  196. $attach = '';
  197. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  198. $transaction->commit();
  199. //打印小票和语音播报
  200. $newOrder = OrderClass::getById($id, true);
  201. OrderClass::onlinePrint($newOrder);
  202. ShopExtClass::hdGatheringReport($newOrder);
  203. $shopId = $newOrder->shopId ?? 0;
  204. $shop = ShopClass::getById($shopId, true);
  205. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  206. util::success(['returnStatus' => 'SUCCESS']);
  207. } else if ($payReturn["trade_state"] == "USERPAYING") {
  208. //用户支付中
  209. util::complete('正在付款,请稍候..');
  210. }
  211. }
  212. if ($payReturn["err_code"] == "ORDERNOTEXIST") {
  213. //交易订单号不存在
  214. util::complete('订单没有付款');
  215. } else {
  216. util::complete('系统错误,请稍候再试');
  217. }
  218. } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
  219. //支付宝付款
  220. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  221. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  222. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  223. // $aliPId = '2017041906821571';
  224. // $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+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7FjjIGkbwa0H3A8Ydnvw5z4A9TZjjHLjC2GtVGJReqHGglMECgYEAydddA4NRPxmXvIjjmLb1Ft1qwHuyv3vvWhi50foGZXNE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RzqrAoGAZBNyYNgBguRaEd1Sxw2gPmOUfvqiUCo6F4MJ+8xN8idn4FnaofcH8ic613PQPqpB/yYaxeqgIEfnvGY9ILXCuvbePfYqFmMwzALWvZ+v7uVKa2M7HstWCrq7O+m/lQFN/ut8ZnUDcBn4WwwHa/PjCYietetO2gpDRmAdSqrWGH8CgYBkwp+r6pkJzW60kHSZIlEKAe7oJMwLNC2ZqQ4MwGwzfBaH+E34kCuR8ZqYzyAIVOa/Nwi5By1Zvi1KzBwJmUUTJ3o7WCOE96EzSrmOZlqXNCwO/36Vh6dshhhE/birIlXJSP0xdzpBQy2ksyli9eGy8cdJ2Y72Wo+TjSclRbKiPQKBgQD1C/CJRQbahz6B3Sc4NOiFUQioM30qDKbDibzUq95u5PtQOx69+/h68tA2nj7ILZOXoh6/KF6LM9WK0QctY0HR5Uy4Uz8YkY/Y0L/LExSkNdpYqbpBc8DdIsKYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  228. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  229. $config = array(
  230. //签名方式,默认为RSA2(RSA2048)
  231. 'sign_type' => "RSA2",
  232. //支付宝公钥
  233. 'alipay_public_key' => $aliPublicKey,
  234. //商户私钥
  235. 'merchant_private_key' => $aliKey,
  236. //编码格式
  237. 'charset' => "UTF-8",
  238. //支付宝网关
  239. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  240. //应用ID
  241. 'app_id' => $aliPId,
  242. //异步通知地址,只有扫码支付预下单可用
  243. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  244. //最大查询重试次数
  245. 'MaxQueryRetry' => "10",
  246. //查询间隔
  247. 'QueryDuration' => "3"
  248. );
  249. //构造查询业务请求参数对象
  250. $queryContentBuilder = new \AlipayTradeQueryContentBuilder();
  251. $queryContentBuilder->setOutTradeNo($orderSn);
  252. //初始化类对象,调用queryTradeResult方法获取查询应答
  253. $queryResponse = new \AlipayTradeService($config);
  254. $queryResult = $queryResponse->queryTradeResult($queryContentBuilder);
  255. if ($queryResult->getTradeStatus() == 'SUCCESS') {
  256. $respond = $queryResult->getResponse();
  257. $transactionId = $respond->trade_no ?? '';
  258. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  259. $order->save();
  260. $attach = '';
  261. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  262. $transaction->commit();
  263. //打印小票和语音播报
  264. $newOrder = OrderClass::getById($id, true);
  265. OrderClass::onlinePrint($newOrder);
  266. ShopExtClass::hdGatheringReport($newOrder);
  267. $shopId = $newOrder->shopId ?? 0;
  268. $shop = ShopClass::getById($shopId, true);
  269. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  270. util::success(['returnStatus' => 'SUCCESS']);
  271. } else {
  272. util::complete('没有付款');
  273. }
  274. } else {
  275. util::complete('还没有付款哦');
  276. }
  277. } catch (\Exception $e) {
  278. $transaction->rollBack();
  279. Yii::error("支付失败原因:" . $e->getMessage());
  280. util::complete('支付失败');
  281. }
  282. }
  283. //微信和支付宝付款码支付 ssh 2021.4.11
  284. public function actionCodePay()
  285. {
  286. ini_set('date.timezone', 'Asia/Shanghai');
  287. header("Content-type: text/html; charset=utf-8");
  288. $get = Yii::$app->request->get();
  289. $id = isset($get['id']) ? $get['id'] : 0;
  290. $order = OrderClass::getById($id, true);
  291. OrderClass::valid($order, $this->mainId);
  292. $authCode = (string)$get['authCode'] ?? '';
  293. if (empty($authCode)) {
  294. util::fail('没有获取到支付码');
  295. }
  296. $wxPrefix = ['10', '11', '12', '13', '14', '15'];
  297. $isWx = false;
  298. foreach ($wxPrefix as $wxId) {
  299. if (strpos($authCode, $wxId) === 0) {
  300. $isWx = true;
  301. }
  302. }
  303. $orderSn = $order->orderSn ?? '';
  304. $outTradeNo = $orderSn;
  305. $subject = '购买商品';
  306. $totalFee = $order->mainPay ?? 0;
  307. $totalAmount = $totalFee;
  308. $body = "门店";
  309. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  310. $connection = Yii::$app->db;
  311. $transaction = $connection->beginTransaction();
  312. try {
  313. if ($isWx) {
  314. $wxPayWay = dict::getDict('payWay', 'wxPay');
  315. $order->payWay = $wxPayWay;
  316. $order->save();
  317. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  318. require_once($wx . '/lib/WxPay.Api.php');
  319. require_once($wx . '/example/WxPay.MicroPay.php');
  320. $input = new \WxPayMicroPay();
  321. $input->SetAuth_code($authCode);
  322. $input->SetBody($subject);
  323. $input->SetTotal_fee($totalFee * 100);
  324. $input->SetOut_trade_no($orderSn);
  325. //获取微信商户号等信息
  326. $sjExtend = WxOpenClass::getMallWxInfo();
  327. $microPay = new \MicroPay();
  328. $res = $microPay->pay($input, $sjExtend);
  329. if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS" && isset($res["result_code"]) && $res["result_code"] == 'SUCCESS') {
  330. $transactionId = $res['transaction_id'] ?? '';
  331. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  332. $order->save();
  333. $attach = '';
  334. payUtil::thirdPay($wxPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  335. $transaction->commit();
  336. //解决重复通知
  337. $cacheKey = 'hd_shop_order_pay_' . $orderSn;
  338. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  339. if (empty($has)) {
  340. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  341. $newOrder = OrderClass::getById($id, true);
  342. //打印小票
  343. OrderClass::onlinePrint($newOrder);
  344. //语音播报
  345. ShopExtClass::hdGatheringReport($newOrder);
  346. $shopId = $newOrder->shopId ?? 0;
  347. $shop = ShopClass::getById($shopId, true);
  348. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  349. }
  350. util::success(['returnStatus' => 'SUCCESS']);
  351. } else {
  352. Yii::info(json_encode($res));
  353. $returnMsg = $res['return_msg'] ?? '';
  354. Yii::info($returnMsg);
  355. $errCode = $res['err_code'] ?? '';
  356. $msg = '';
  357. if ($errCode == 'NOTENOUGH') {
  358. $msg = '余额不足';
  359. }
  360. if ($errCode == 'USERPAYING') {
  361. $msg = '等待客户输入支付密码';
  362. }
  363. util::success(['returnStatus' => 'FAILURE'], $msg);
  364. }
  365. } else {
  366. $aliPayWay = dict::getDict('payWay', 'alipay');
  367. $order->payWay = $aliPayWay;
  368. $order->save();
  369. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  370. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  371. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  372. // $aliPId = '2017041906821571';
  373. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQ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KYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  377. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  378. $config = array(
  379. //签名方式,默认为RSA2(RSA2048)
  380. 'sign_type' => "RSA2",
  381. //支付宝公钥
  382. 'alipay_public_key' => $aliPublicKey,
  383. //商户私钥
  384. 'merchant_private_key' => $aliKey,
  385. //编码格式
  386. 'charset' => "UTF-8",
  387. //支付宝网关
  388. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  389. //应用ID
  390. 'app_id' => $aliPId,
  391. //异步通知地址,只有扫码支付预下单可用
  392. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  393. //最大查询重试次数
  394. 'MaxQueryRetry' => "10",
  395. //查询间隔
  396. 'QueryDuration' => "3"
  397. );
  398. // 支付超时,线下扫码交易定义为5分钟
  399. $timeExpress = "5m";
  400. // 创建请求builder,设置请求参数
  401. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  402. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  403. $barPayRequestBuilder->setTotalAmount($totalAmount);
  404. $barPayRequestBuilder->setAuthCode($authCode);
  405. $barPayRequestBuilder->setTimeExpress($timeExpress);
  406. $barPayRequestBuilder->setSubject($subject);
  407. $barPayRequestBuilder->setBody($body);
  408. // 调用barPay方法获取当面付应答
  409. $barPay = new \AlipayTradeService($config);
  410. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  411. if ($barPayResult->getTradeStatus() == 'SUCCESS') {
  412. $respond = $barPayResult->getResponse();
  413. $transactionId = $respond->trade_no ?? '';
  414. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  415. $order->save();
  416. $attach = '';
  417. payUtil::thirdPay($aliPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  418. $transaction->commit();
  419. //解决重复通知
  420. $cacheKey = 'hd_shop_order_pay_' . $orderSn;
  421. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  422. if (empty($has)) {
  423. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  424. $newOrder = OrderClass::getById($id, true);
  425. //打印小票
  426. OrderClass::onlinePrint($newOrder);
  427. //语音播报
  428. ShopExtClass::hdGatheringReport($newOrder);
  429. $shopId = $newOrder->shopId ?? 0;
  430. $shop = ShopClass::getById($shopId, true);
  431. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  432. }
  433. util::success(['returnStatus' => 'SUCCESS']);
  434. }
  435. util::success(['returnStatus' => 'FAILURE']);
  436. }
  437. } catch (\Exception $e) {
  438. $transaction->rollBack();
  439. Yii::error("支付失败原因:" . $e->getMessage());
  440. util::fail('支付失败');
  441. }
  442. }
  443. //开单操作 ssh 20220316
  444. public function actionCreateOrder()
  445. {
  446. $post = Yii::$app->request->post();
  447. $post['sjId'] = $this->sjId;
  448. $post['shopId'] = $this->shopId;
  449. $post['mainId'] = $this->mainId ?? 0;
  450. $now = time();
  451. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  452. $expireTime = $now + $orderValidTime;
  453. $post['deadline'] = $expireTime;
  454. $post['staffId'] = $this->shopAdminId ?? 0;
  455. $post['staffName'] = $this->shopAdmin->name ?? '';
  456. //默认情况开单员工和收款员工是同个人
  457. $staffId = $this->shopAdminId ?? 0;
  458. $staffName = $this->shopAdmin->name ?? '';
  459. if (isset($post['shopAdminId']) == false || empty($post['shopAdminId'])) {
  460. $post['shopAdminId'] = $staffId;
  461. $post['shopAdminName'] = $staffName;
  462. }
  463. if (isset($post['getStaffId']) == false || empty($post['getStaffId'])) {
  464. $post['getStaffId'] = $staffId;
  465. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  466. }
  467. $groupId = isset($post['groupId']) && !empty($post['groupId']) ? $post['groupId'] : 0;
  468. $post['fromType'] = $post['fromType'] ?? 1;
  469. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  470. if (isset($post['bookName']) == false || empty($post['bookName'])) {
  471. util::fail('请填写订花人姓名');
  472. }
  473. }
  474. $customId = isset($post['customId']) && $post['customId'] > 0 ? $post['customId'] : 0;
  475. if (empty($customId)) {
  476. $customId = $this->shop->defaultCustomId ?? 0;
  477. }
  478. $custom = CustomClass::getById($customId);
  479. if (empty($custom)) {
  480. util::fail('没有找到客户哦');
  481. }
  482. $post['customId'] = $customId;
  483. $customName = $custom['name'] ?? '';
  484. $post['customName'] = $customName;
  485. $post['customNamePy'] = stringUtil::py($customName);
  486. if (isset($post['receiveMobile']) && !empty($post['receiveMobile'])) {
  487. if (stringUtil::isMobile($post['receiveMobile']) == false) {
  488. util::fail('请填写正确的收花人手机号');
  489. }
  490. }
  491. if (isset($post['bookMobile']) && !empty($post['bookMobile'])) {
  492. if (stringUtil::isMobile($post['bookMobile']) == false) {
  493. util::fail('请填写正确的订花人手机号');
  494. }
  495. }
  496. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  497. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  498. //收银台开单默认到店自取
  499. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  500. }
  501. $productJson = $post['product'] ?? '';
  502. $productList = [];
  503. if (!empty($productJson)) {
  504. $productList = json_decode($productJson, true);
  505. }
  506. if (empty($productList) && empty($groupId)) {
  507. //商家手机上开单并生成制作单
  508. if (isset($post['unitGoodsPrice']) && !empty($post['unitGoodsPrice'])) {
  509. $unitGoodsPrice = $post['unitGoodsPrice'];
  510. if (is_numeric($unitGoodsPrice) == false) {
  511. util::fail('请输入正确的单价');
  512. }
  513. $productList = GoodsClass::orderCreateGoods($post);
  514. }
  515. }
  516. if (empty($productList) && empty($groupId)) {
  517. if (isset($post['lsGoodsPrice']) == false || $post['lsGoodsPrice'] <= 0) {
  518. $lsGoodsPrice = $post['lsGoodsPrice'];
  519. if (is_numeric($lsGoodsPrice) == false) {
  520. util::fail('请输入正确的金额');
  521. }
  522. util::fail('请填写金额或选商品');
  523. }
  524. }
  525. $connection = Yii::$app->db;
  526. $transaction = $connection->beginTransaction();
  527. try {
  528. $post['product'] = $productList;
  529. //阿东开5支老是会出现5扎情况跟踪
  530. ProductClass::adStockCheck($this->shop, $productList);
  531. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  532. $transaction->commit();
  533. //打印小票和语音播报
  534. $id = $return->id ?? 0;
  535. $order = OrderClass::getById($id, true);
  536. if ($order->fromType == 3 || $order->fromType == 4) {
  537. //除了客服号下单和美团下单,其它情况允许打小票
  538. } else {
  539. //前台打小票
  540. OrderClass::onlinePrint($order);
  541. }
  542. //前台提示收款多少钱
  543. ShopExtClass::hdGatheringReport($order);
  544. //前台提醒出示付款码
  545. if (isset($order->status) && $order->status == 1) {
  546. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  547. ShopExtClass::pleasePayReport($order);
  548. }
  549. }
  550. $shopId = $order->shopId ?? 0;
  551. $shop = ShopClass::getById($shopId, true);
  552. WxMessageClass::gatheringIncomeInform($shop, $order);
  553. //新制作单提示
  554. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  555. ShopExtClass::newWorkRemind($shopExt, $order);
  556. util::success($return);
  557. } catch (\Exception $e) {
  558. $transaction->rollBack();
  559. Yii::error("失败原因:" . $e->getMessage());
  560. util::fail('下单失败');
  561. }
  562. }
  563. //下单要用到的相关信息 ssh 2019.12.6
  564. public function actionOrderRelate()
  565. {
  566. $regionTree = RegionService::tree();
  567. $shop = $this->shop->attributes;
  568. $freight = MerchantExtendService::getFreight($this->sjExtend);
  569. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  570. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  571. util::success($out);
  572. }
  573. //余额支付 ssh 2019.12.6
  574. public function actionBalancePay()
  575. {
  576. $post = Yii::$app->request->post();
  577. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  578. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  579. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  580. $order = OrderService::getByOrderSn($orderSn);
  581. $userId = $this->adminId;
  582. $user = UserService::getUserInfo($userId);
  583. //验证支付密码是否正确
  584. UserService::validPayPassword($payPassword, $user);
  585. //支付前验证订单有效性
  586. OrderService::checkBeforePay($order);
  587. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  588. $totalFee = $order['prePrice'];
  589. $sourceType = 0;
  590. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  591. $actPrice = $discountData['price'];
  592. $callbackParams = [];
  593. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  594. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  595. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  596. }
  597. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  598. public function actionWxPay()
  599. {
  600. ini_set('date.timezone', 'Asia/Shanghai');
  601. $post = Yii::$app->request->post();
  602. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  603. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  604. $order = OrderService::getByOrderSn($orderSn);
  605. $orderId = $order['id'];
  606. //验证优惠券是否还有效
  607. if (!empty($couponId)) {
  608. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  609. }
  610. //支付前验证订单有效性
  611. OrderService::checkBeforePay($order);
  612. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  613. $totalFee = $order['realPrice'];
  614. //小程序使用miniOpenId
  615. if (httpUtil::isMiniProgram()) {
  616. $openId = $this->user['miniOpenId'];
  617. } else {
  618. $openId = $this->user['openId'];
  619. }
  620. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  621. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  622. $wxPayType = 0;
  623. if (httpUtil::isMiniProgram()) {
  624. $wxPayType = 1;
  625. }
  626. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  627. //订单30分钟后过期
  628. $now = time();
  629. $expireTime = $now + 1800;
  630. $wx = Yii::getAlias("@vendor/weixin");
  631. require_once($wx . '/lib/WxPay.Api.php');
  632. require_once($wx . '/example/WxPay.JsApiPay.php');
  633. $input = new \WxPayUnifiedOrder();
  634. $input->SetBody($name);
  635. $input->SetOut_trade_no($orderSn);
  636. $input->SetTotal_fee($totalFee * 100);
  637. $input->SetTime_start(date("YmdHis", $now));
  638. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  639. $input->SetTime_expire(date("YmdHis", $expireTime));
  640. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  641. $input->SetTrade_type("JSAPI");
  642. //花卉宝代为申请的微信支付
  643. $merchantExtend = $this->sjExtend->attributes;
  644. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  645. $input->SetSub_openid($openId);
  646. } else {
  647. $input->SetOpenid($openId);
  648. }
  649. //小程序使用miniAppId
  650. if (httpUtil::isMiniProgram()) {
  651. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  652. }
  653. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  654. $tools = new \JsApiPay();
  655. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  656. $newParams = json_decode($jsApiParameters, true);
  657. //已请求微信不能修改价格
  658. $updateData = [];
  659. $updateData['modPrice'] = 0;
  660. if (empty($order['deadline'])) {
  661. $updateData['deadline'] = $expireTime;
  662. }
  663. OrderService::updateById($orderId, $updateData);
  664. util::success($newParams);
  665. }
  666. //快捷付款订单
  667. public function actionFastPay()
  668. {
  669. ini_set('date.timezone', 'Asia/Shanghai');
  670. $post = Yii::$app->request->post();
  671. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  672. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  673. //验证门店是否有效
  674. $shopInfo = ShopService::getById($shopId);
  675. ShopService::valid($shopInfo, $this->sjId);
  676. $post['shopId'] = $shopId;
  677. //默认门店订单
  678. $store = isset($post['store']) ? $post['store'] : 1;
  679. $post['store'] = $store;
  680. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  681. $sourceType = $this->isWx ? 0 : 1;
  682. if (httpUtil::isMiniProgram()) {
  683. $sourceType = 2;
  684. }
  685. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  686. //兼容旧系统sourceType=3表示朋友圈来源
  687. $fromType = $sourceType == 3 ? 2 : 0;
  688. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  689. $post['userId'] = $this->adminId;
  690. $custom = $this->custom;
  691. $post['bookName'] = $custom['userName'] ?? '';
  692. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  693. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  694. $post['bookMobile'] = $bookMobile;
  695. $prePrice = round($post['prePrice'], 2);
  696. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  697. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  698. $actPrice = $discountData['price'];
  699. $post['discountType'] = $discountData['discountType'];
  700. $post['discountAmount'] = $discountData['discountAmount'];
  701. $post['actPrice'] = $actPrice;
  702. $post['realPrice'] = $actPrice;
  703. $post['payStyle'] = 0;
  704. $post['sjId'] = $this->sjId;
  705. $now = time();
  706. $expireTime = $now + 300;//订单5分钟后过期
  707. $post['createTime'] = date("Y-m-d H:i:s", $now);
  708. $post['deadline'] = $expireTime;
  709. if ($actPrice <= 0) {
  710. util::fail('请填写正确的金额');
  711. }
  712. //微信支付订单提交不能修改价格
  713. $post['modPrice'] = $this->isWx ? 0 : 1;
  714. $post['sourceType'] = $sourceType;
  715. $post['goodsNum'] = 1;
  716. $post['fromType'] = $fromType;
  717. $order = OrderService::addOrder($post);
  718. $orderId = $order['id'];
  719. $orderSn = $order['orderSn'];
  720. $sjId = $this->sjId;
  721. if ($payWay == 0) {
  722. $orderId = $order['id'];
  723. $name = '购买商品';
  724. $totalFee = $actPrice;
  725. $user = UserService::getById($this->adminId);
  726. $openId = isset($user['openId']) ? $user['openId'] : 0;
  727. if (httpUtil::isMiniProgram()) {
  728. //小程序使用miniOpenId
  729. $openId = $user['miniOpenId'];
  730. }
  731. if (empty($openId)) {
  732. util::fail('没有找到客户的openId');
  733. }
  734. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  735. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  736. $wxPayType = 0;
  737. if (httpUtil::isMiniProgram()) {
  738. $wxPayType = 1;
  739. }
  740. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  741. $wx = Yii::getAlias("@vendor/weixin");
  742. require_once($wx . '/lib/WxPay.Api.php');
  743. require_once($wx . '/example/WxPay.JsApiPay.php');
  744. $input = new \WxPayUnifiedOrder();
  745. $input->SetBody($name);
  746. $input->SetOut_trade_no($orderSn);
  747. $input->SetTotal_fee($totalFee * 100);
  748. $input->SetTime_start(date("YmdHis", $now));
  749. $input->SetAttach($attach);
  750. //设置订单有效期5分钟
  751. $input->SetTime_expire(date("YmdHis", $expireTime));
  752. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  753. $input->SetTrade_type("JSAPI");
  754. //花卉宝代为申请的微信支付
  755. $merchantExtend = $this->sjExtend->attributes;
  756. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  757. $input->SetSub_openid($openId);
  758. } else {
  759. $input->SetOpenid($openId);
  760. }
  761. //小程序使用miniAppId
  762. if (httpUtil::isMiniProgram()) {
  763. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  764. }
  765. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  766. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  767. $tools = new \JsApiPay();
  768. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  769. $newParams = json_decode($jsApiParameters, true);
  770. $newParams['orderId'] = $orderId;
  771. util::success($newParams);
  772. } elseif ($payWay == 1) {
  773. $extend = MerchantExtendService::getBySjId($sjId);
  774. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  775. util::fail('支付宝付款即将开通...');
  776. }
  777. $config = [
  778. //应用ID,您的APPID。
  779. 'app_id' => $extend['alipayPId'],
  780. //商户私钥,您的原始格式RSA私钥
  781. 'merchant_private_key' => $extend['alipayKey'],
  782. //异步通知地址
  783. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  784. //同步跳转
  785. 'return_url' => "",
  786. //编码格式
  787. 'charset' => "UTF-8",
  788. //签名方式
  789. 'sign_type' => "RSA2",
  790. //支付宝网关
  791. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  792. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  793. 'alipay_public_key' => $extend['alipayPublicKey'],
  794. ];
  795. Yii::info(json_encode($config) . ' aplipay config');
  796. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  797. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  798. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  799. $totalFee = $order['realPrice'];
  800. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  801. $subject = '购买商品';//订单名称,必填
  802. $total_amount = $totalFee;//付款金额,必填
  803. $body = '';//商品描述,可空
  804. $timeout_express = "1m";//超时时间
  805. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  806. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  807. $passBackParams = urlencode($passBackParams);
  808. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  809. $payRequestBuilder->setBody($body);
  810. $payRequestBuilder->setSubject($subject);
  811. $payRequestBuilder->setOutTradeNo($out_trade_no);
  812. $payRequestBuilder->setTotalAmount($total_amount);
  813. $payRequestBuilder->setTimeExpress($timeout_express);
  814. //在异步通知时将该参数原样返回
  815. $payRequestBuilder->setPassbackParams($passBackParams);
  816. //同步通知
  817. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  818. //异步通知
  819. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  820. $payResponse = new \AlipayTradeService($config);
  821. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  822. //直接将支付宝的html返回给前端
  823. echo $result;
  824. } elseif ($payWay == 2) {
  825. //余额支付,验证支付密码是否正确
  826. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  827. $user = UserService::getUserInfo($this->adminId);
  828. UserService::validPayPassword($payPassword, $user);
  829. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  830. $callbackParams = [];
  831. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  832. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  833. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  834. } else {
  835. util::fail('无效的支付方式');
  836. }
  837. }
  838. //订单评价
  839. public function actionComment()
  840. {
  841. $post = Yii::$app->request->post();
  842. $id = $post['id'];
  843. $order = OrderService::getById($id);
  844. OrderService::valid($order, $this->mainId);
  845. if ($order['grade'] > 0) {
  846. util::fail('您已经评过了');
  847. }
  848. OrderService::comment($post);
  849. util::complete('提交成功');
  850. }
  851. //订单详情 ssh 2019.12.16
  852. public function actionDetail()
  853. {
  854. $id = Yii::$app->request->get('id', 0);
  855. $detail = OrderClass::getFullInfo($id);
  856. OrderService::valid($detail, $this->mainId);
  857. util::success($detail);
  858. }
  859. public function actionList()
  860. {
  861. $get = Yii::$app->request->get();
  862. $search = isset($get['search']) ? $get['search'] : '';
  863. $shopId = $this->shopId ?? 0;
  864. if (empty($shopId)) {
  865. //没有登录不显示数据
  866. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  867. }
  868. $where = [];
  869. $where['shopId'] = $shopId;
  870. $status = $get['status'] ?? 0;
  871. if (!empty($status)) {
  872. $where['status'] = $get['status'];
  873. }
  874. $debt = $get['debt'] ?? 2;
  875. if ($debt != 2) {
  876. $where['debt'] = $debt;
  877. }
  878. if (isset($get['shopAdminId']) && !empty($get['shopAdminId'])) {
  879. $where['shopAdminId'] = $get['shopAdminId'];
  880. }
  881. if (isset($get['searchText']) && !empty($get['searchText'])) {
  882. if (is_numeric($get['searchText'])) {
  883. $where['actPrice'] = $get['searchText'];
  884. } else {
  885. $where['bookName'] = $get['searchText'];
  886. }
  887. }
  888. if (!empty($search)) {
  889. if (stringUtil::isMobile($search)) {
  890. $where['bookMobile'] = $search;
  891. } else {
  892. $where['orderSn'] = $search;
  893. }
  894. }
  895. $searchTime = $get['searchTime'] ?? '';
  896. if (!empty($searchTime)) {
  897. $startTime = $get['startTime'] ?? '';
  898. $endTime = $get['endTime'] ?? '';
  899. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  900. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  901. }
  902. $list = OrderService::getOrderList($where);
  903. $list['shop'] = $this->shop->attributes ?? [];
  904. util::success($list);
  905. }
  906. //订单归类 ssh 2019.12.17
  907. public function actionClassify()
  908. {
  909. $post = Yii::$app->request->post();
  910. $id = isset($post['id']) ? $post['id'] : 0;
  911. $order = OrderService::getById($id);
  912. OrderService::valid($order, $this->mainId);
  913. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  914. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  915. OrderService::classify($id, $categoryId, $usageId);
  916. util::complete();
  917. }
  918. //更新配送单 ssh 2019.12.17
  919. public function actionUpdateSheet()
  920. {
  921. $post = Yii::$app->request->post();
  922. $id = isset($post['id']) ? $post['id'] : 0;
  923. unset($post['id']);
  924. $order = OrderService::getById($id);
  925. OrderService::valid($order, $this->mainId);
  926. OrderService::updateSheet($order, $post);
  927. util::complete('提交成功');
  928. }
  929. //商家收款与发货 ssh 2019.12.18
  930. public function actionGathering()
  931. {
  932. $post = Yii::$app->request->post();
  933. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  934. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  935. $userId = isset($post['userId']) ? $post['userId'] : 0;
  936. $userInfo = UserService::getById($userId);
  937. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  938. util::fail('您选择的客户无效');
  939. }
  940. if ($price <= 0) {
  941. util::fail('请输入金额');
  942. }
  943. $post['prePrice'] = $price;
  944. $post['actPrice'] = $price;
  945. $post['payWay'] = $payWay;
  946. $post['sourceType'] = 1;
  947. $post['userId'] = $userId;
  948. $post['goodsNum'] = 1;
  949. $post['orderName'] = '商品';
  950. $shopId = MerchantService::getDefaultShopId($this->sj);
  951. $post['shopId'] = $shopId;
  952. $order = OrderService::addOrder($post);
  953. $id = $order['id'];
  954. $orderSn = $order['orderSn'];
  955. //流水类型列表
  956. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  957. $callbackParams = [];
  958. switch ($payWay) {
  959. case 0:
  960. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  961. break;
  962. case 1:
  963. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  964. break;
  965. case 2:
  966. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  967. break;
  968. default:
  969. util::fail('无效支付方式');
  970. }
  971. util::success($order);
  972. }
  973. //免发货管理 ssh 2020.1.6
  974. public function actionWithoutSend()
  975. {
  976. $id = Yii::$app->request->get('id');
  977. $order = OrderService::getById($id);
  978. OrderService::valid($order, $this->mainId);
  979. //配送流程变更
  980. $currentFlow = OrderSendClass::withoutSend($order);
  981. util::success(['currentFlow' => $currentFlow]);
  982. }
  983. //修改价格 ssh 2020.1.6
  984. public function actionUpdatePrice()
  985. {
  986. $id = Yii::$app->request->get('id');
  987. $price = Yii::$app->request->get('price', 1);
  988. $order = OrderService::getById($id);
  989. OrderService::valid($order, $this->mainId);
  990. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  991. util::fail('已发起支付,不能修改价格');
  992. }
  993. OrderService::updateById($id, ['actPrice' => $price]);
  994. util::complete('修改成功');
  995. }
  996. //配送单 ssh 2020.2.29
  997. public function actionGetDeliverDetail()
  998. {
  999. $id = Yii::$app->request->get('id', 0);
  1000. $order = OrderService::getById($id);
  1001. OrderService::valid($order, $this->mainId);
  1002. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1003. $reachPeriodId = $order['reachPeriod'];
  1004. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1005. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1006. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1007. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1008. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1009. $bookName = '--';
  1010. $bookMobile = '--';
  1011. }
  1012. $data = [
  1013. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1014. 'orderSn' => $order['orderSn'],
  1015. 'receiveUserName' => $order['receiveUserName'],
  1016. 'receiveMobile' => $order['receiveMobile'],
  1017. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1018. 'cardInfo' => $order['cardInfo'],
  1019. 'bookMobile' => $bookMobile,
  1020. 'bookName' => $bookName,
  1021. 'remark' => $order['remark'],
  1022. 'sendNum' => $order['sendNum'],
  1023. 'telephone' => 18030142050,
  1024. 'printTime' => '',
  1025. 'img' => '',
  1026. ];
  1027. util::success($data);
  1028. }
  1029. //运费计算 lqh 2021.4.12 暂反回固定
  1030. public function actionFreight()
  1031. {
  1032. util::success(['sedCost' => 10]);
  1033. }
  1034. }