OrderController.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7FjjIGkbwa0H3A8Ydnvw5z4A9TZjjHLjC2GtVGJReqHGglMECgYEAydddA4NRPxmXvIjjmLb1Ft1qwHuyv3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CCqXBtQsF4KITRzqrAoGAZBNyYNgBguRaEd1Sxw2gPmOUfvqiUCo6F4MJ+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/smaszwqqfWGAyTMvt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pYqbpBc8DdIsKYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/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. //3秒内不允许重复开单
  532. $staffId = $this->shopAdminId ?? 0;
  533. $cacheKey = 'hd_create_order_' . $staffId . '_' . $customId;
  534. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  535. if (!empty($has)) {
  536. util::fail('请稍等');
  537. }
  538. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 3, 'has']);
  539. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  540. $transaction->commit();
  541. //打印小票和语音播报
  542. $id = $return->id ?? 0;
  543. $order = OrderClass::getById($id, true);
  544. if ($order->fromType == 3 || $order->fromType == 4) {
  545. //除了客服号下单和美团下单,其它情况允许打小票
  546. } else {
  547. //前台打小票
  548. OrderClass::onlinePrint($order);
  549. }
  550. //前台提示收款多少钱
  551. ShopExtClass::hdGatheringReport($order);
  552. //前台提醒出示付款码
  553. if (isset($order->status) && $order->status == 1) {
  554. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  555. ShopExtClass::pleasePayReport($order);
  556. }
  557. }
  558. $shopId = $order->shopId ?? 0;
  559. $shop = ShopClass::getById($shopId, true);
  560. WxMessageClass::gatheringIncomeInform($shop, $order);
  561. //新制作单提示
  562. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  563. ShopExtClass::newWorkRemind($shopExt, $order);
  564. util::success($return);
  565. } catch (\Exception $e) {
  566. $transaction->rollBack();
  567. Yii::error("失败原因:" . $e->getMessage());
  568. util::fail('下单失败');
  569. }
  570. }
  571. //下单要用到的相关信息 ssh 2019.12.6
  572. public function actionOrderRelate()
  573. {
  574. $regionTree = RegionService::tree();
  575. $shop = $this->shop->attributes;
  576. $freight = MerchantExtendService::getFreight($this->sjExtend);
  577. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  578. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  579. util::success($out);
  580. }
  581. //余额支付 ssh 2019.12.6
  582. public function actionBalancePay()
  583. {
  584. $post = Yii::$app->request->post();
  585. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  586. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  587. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  588. $order = OrderService::getByOrderSn($orderSn);
  589. $userId = $this->adminId;
  590. $user = UserService::getUserInfo($userId);
  591. //验证支付密码是否正确
  592. UserService::validPayPassword($payPassword, $user);
  593. //支付前验证订单有效性
  594. OrderService::checkBeforePay($order);
  595. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  596. $totalFee = $order['prePrice'];
  597. $sourceType = 0;
  598. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  599. $actPrice = $discountData['price'];
  600. $callbackParams = [];
  601. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  602. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  603. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  604. }
  605. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  606. public function actionWxPay()
  607. {
  608. ini_set('date.timezone', 'Asia/Shanghai');
  609. $post = Yii::$app->request->post();
  610. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  611. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  612. $order = OrderService::getByOrderSn($orderSn);
  613. $orderId = $order['id'];
  614. //验证优惠券是否还有效
  615. if (!empty($couponId)) {
  616. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  617. }
  618. //支付前验证订单有效性
  619. OrderService::checkBeforePay($order);
  620. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  621. $totalFee = $order['realPrice'];
  622. //小程序使用miniOpenId
  623. if (httpUtil::isMiniProgram()) {
  624. $openId = $this->user['miniOpenId'];
  625. } else {
  626. $openId = $this->user['openId'];
  627. }
  628. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  629. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  630. $wxPayType = 0;
  631. if (httpUtil::isMiniProgram()) {
  632. $wxPayType = 1;
  633. }
  634. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  635. //订单30分钟后过期
  636. $now = time();
  637. $expireTime = $now + 1800;
  638. $wx = Yii::getAlias("@vendor/weixin");
  639. require_once($wx . '/lib/WxPay.Api.php');
  640. require_once($wx . '/example/WxPay.JsApiPay.php');
  641. $input = new \WxPayUnifiedOrder();
  642. $input->SetBody($name);
  643. $input->SetOut_trade_no($orderSn);
  644. $input->SetTotal_fee($totalFee * 100);
  645. $input->SetTime_start(date("YmdHis", $now));
  646. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  647. $input->SetTime_expire(date("YmdHis", $expireTime));
  648. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  649. $input->SetTrade_type("JSAPI");
  650. //花卉宝代为申请的微信支付
  651. $merchantExtend = $this->sjExtend->attributes;
  652. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  653. $input->SetSub_openid($openId);
  654. } else {
  655. $input->SetOpenid($openId);
  656. }
  657. //小程序使用miniAppId
  658. if (httpUtil::isMiniProgram()) {
  659. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  660. }
  661. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  662. $tools = new \JsApiPay();
  663. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  664. $newParams = json_decode($jsApiParameters, true);
  665. //已请求微信不能修改价格
  666. $updateData = [];
  667. $updateData['modPrice'] = 0;
  668. if (empty($order['deadline'])) {
  669. $updateData['deadline'] = $expireTime;
  670. }
  671. OrderService::updateById($orderId, $updateData);
  672. util::success($newParams);
  673. }
  674. //快捷付款订单
  675. public function actionFastPay()
  676. {
  677. ini_set('date.timezone', 'Asia/Shanghai');
  678. $post = Yii::$app->request->post();
  679. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  680. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  681. //验证门店是否有效
  682. $shopInfo = ShopService::getById($shopId);
  683. ShopService::valid($shopInfo, $this->sjId);
  684. $post['shopId'] = $shopId;
  685. //默认门店订单
  686. $store = isset($post['store']) ? $post['store'] : 1;
  687. $post['store'] = $store;
  688. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  689. $sourceType = $this->isWx ? 0 : 1;
  690. if (httpUtil::isMiniProgram()) {
  691. $sourceType = 2;
  692. }
  693. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  694. //兼容旧系统sourceType=3表示朋友圈来源
  695. $fromType = $sourceType == 3 ? 2 : 0;
  696. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  697. $post['userId'] = $this->adminId;
  698. $custom = $this->custom;
  699. $post['bookName'] = $custom['userName'] ?? '';
  700. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  701. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  702. $post['bookMobile'] = $bookMobile;
  703. $prePrice = round($post['prePrice'], 2);
  704. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  705. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  706. $actPrice = $discountData['price'];
  707. $post['discountType'] = $discountData['discountType'];
  708. $post['discountAmount'] = $discountData['discountAmount'];
  709. $post['actPrice'] = $actPrice;
  710. $post['realPrice'] = $actPrice;
  711. $post['payStyle'] = 0;
  712. $post['sjId'] = $this->sjId;
  713. $now = time();
  714. $expireTime = $now + 300;//订单5分钟后过期
  715. $post['createTime'] = date("Y-m-d H:i:s", $now);
  716. $post['deadline'] = $expireTime;
  717. if ($actPrice <= 0) {
  718. util::fail('请填写正确的金额');
  719. }
  720. //微信支付订单提交不能修改价格
  721. $post['modPrice'] = $this->isWx ? 0 : 1;
  722. $post['sourceType'] = $sourceType;
  723. $post['goodsNum'] = 1;
  724. $post['fromType'] = $fromType;
  725. $order = OrderService::addOrder($post);
  726. $orderId = $order['id'];
  727. $orderSn = $order['orderSn'];
  728. $sjId = $this->sjId;
  729. if ($payWay == 0) {
  730. $orderId = $order['id'];
  731. $name = '购买商品';
  732. $totalFee = $actPrice;
  733. $user = UserService::getById($this->adminId);
  734. $openId = isset($user['openId']) ? $user['openId'] : 0;
  735. if (httpUtil::isMiniProgram()) {
  736. //小程序使用miniOpenId
  737. $openId = $user['miniOpenId'];
  738. }
  739. if (empty($openId)) {
  740. util::fail('没有找到客户的openId');
  741. }
  742. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  743. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  744. $wxPayType = 0;
  745. if (httpUtil::isMiniProgram()) {
  746. $wxPayType = 1;
  747. }
  748. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  749. $wx = Yii::getAlias("@vendor/weixin");
  750. require_once($wx . '/lib/WxPay.Api.php');
  751. require_once($wx . '/example/WxPay.JsApiPay.php');
  752. $input = new \WxPayUnifiedOrder();
  753. $input->SetBody($name);
  754. $input->SetOut_trade_no($orderSn);
  755. $input->SetTotal_fee($totalFee * 100);
  756. $input->SetTime_start(date("YmdHis", $now));
  757. $input->SetAttach($attach);
  758. //设置订单有效期5分钟
  759. $input->SetTime_expire(date("YmdHis", $expireTime));
  760. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  761. $input->SetTrade_type("JSAPI");
  762. //花卉宝代为申请的微信支付
  763. $merchantExtend = $this->sjExtend->attributes;
  764. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  765. $input->SetSub_openid($openId);
  766. } else {
  767. $input->SetOpenid($openId);
  768. }
  769. //小程序使用miniAppId
  770. if (httpUtil::isMiniProgram()) {
  771. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  772. }
  773. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  774. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  775. $tools = new \JsApiPay();
  776. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  777. $newParams = json_decode($jsApiParameters, true);
  778. $newParams['orderId'] = $orderId;
  779. util::success($newParams);
  780. } elseif ($payWay == 1) {
  781. $extend = MerchantExtendService::getBySjId($sjId);
  782. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  783. util::fail('支付宝付款即将开通...');
  784. }
  785. $config = [
  786. //应用ID,您的APPID。
  787. 'app_id' => $extend['alipayPId'],
  788. //商户私钥,您的原始格式RSA私钥
  789. 'merchant_private_key' => $extend['alipayKey'],
  790. //异步通知地址
  791. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  792. //同步跳转
  793. 'return_url' => "",
  794. //编码格式
  795. 'charset' => "UTF-8",
  796. //签名方式
  797. 'sign_type' => "RSA2",
  798. //支付宝网关
  799. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  800. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  801. 'alipay_public_key' => $extend['alipayPublicKey'],
  802. ];
  803. Yii::info(json_encode($config) . ' aplipay config');
  804. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  805. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  806. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  807. $totalFee = $order['realPrice'];
  808. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  809. $subject = '购买商品';//订单名称,必填
  810. $total_amount = $totalFee;//付款金额,必填
  811. $body = '';//商品描述,可空
  812. $timeout_express = "1m";//超时时间
  813. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  814. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  815. $passBackParams = urlencode($passBackParams);
  816. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  817. $payRequestBuilder->setBody($body);
  818. $payRequestBuilder->setSubject($subject);
  819. $payRequestBuilder->setOutTradeNo($out_trade_no);
  820. $payRequestBuilder->setTotalAmount($total_amount);
  821. $payRequestBuilder->setTimeExpress($timeout_express);
  822. //在异步通知时将该参数原样返回
  823. $payRequestBuilder->setPassbackParams($passBackParams);
  824. //同步通知
  825. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  826. //异步通知
  827. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  828. $payResponse = new \AlipayTradeService($config);
  829. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  830. //直接将支付宝的html返回给前端
  831. echo $result;
  832. } elseif ($payWay == 2) {
  833. //余额支付,验证支付密码是否正确
  834. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  835. $user = UserService::getUserInfo($this->adminId);
  836. UserService::validPayPassword($payPassword, $user);
  837. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  838. $callbackParams = [];
  839. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  840. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  841. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  842. } else {
  843. util::fail('无效的支付方式');
  844. }
  845. }
  846. //订单评价
  847. public function actionComment()
  848. {
  849. $post = Yii::$app->request->post();
  850. $id = $post['id'];
  851. $order = OrderService::getById($id);
  852. OrderService::valid($order, $this->mainId);
  853. if ($order['grade'] > 0) {
  854. util::fail('您已经评过了');
  855. }
  856. OrderService::comment($post);
  857. util::complete('提交成功');
  858. }
  859. //订单详情 ssh 2019.12.16
  860. public function actionDetail()
  861. {
  862. $id = Yii::$app->request->get('id', 0);
  863. $detail = OrderClass::getFullInfo($id);
  864. OrderService::valid($detail, $this->mainId);
  865. util::success($detail);
  866. }
  867. public function actionList()
  868. {
  869. $get = Yii::$app->request->get();
  870. $search = isset($get['search']) ? $get['search'] : '';
  871. $shopId = $this->shopId ?? 0;
  872. if (empty($shopId)) {
  873. //没有登录不显示数据
  874. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  875. }
  876. $where = [];
  877. $where['shopId'] = $shopId;
  878. $status = $get['status'] ?? 0;
  879. if (!empty($status)) {
  880. $where['status'] = $get['status'];
  881. }
  882. $debt = $get['debt'] ?? 2;
  883. if ($debt != 2) {
  884. $where['debt'] = $debt;
  885. }
  886. if (isset($get['shopAdminId']) && !empty($get['shopAdminId'])) {
  887. $where['shopAdminId'] = $get['shopAdminId'];
  888. }
  889. if (isset($get['searchText']) && !empty($get['searchText'])) {
  890. if (is_numeric($get['searchText'])) {
  891. $where['actPrice'] = $get['searchText'];
  892. } else {
  893. $where['bookName'] = $get['searchText'];
  894. }
  895. }
  896. if (!empty($search)) {
  897. if (stringUtil::isMobile($search)) {
  898. $where['bookMobile'] = $search;
  899. } else {
  900. $where['orderSn'] = $search;
  901. }
  902. }
  903. $searchTime = $get['searchTime'] ?? '';
  904. if (!empty($searchTime)) {
  905. $startTime = $get['startTime'] ?? '';
  906. $endTime = $get['endTime'] ?? '';
  907. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  908. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  909. }
  910. $list = OrderService::getOrderList($where);
  911. $list['shop'] = $this->shop->attributes ?? [];
  912. util::success($list);
  913. }
  914. //订单归类 ssh 2019.12.17
  915. public function actionClassify()
  916. {
  917. $post = Yii::$app->request->post();
  918. $id = isset($post['id']) ? $post['id'] : 0;
  919. $order = OrderService::getById($id);
  920. OrderService::valid($order, $this->mainId);
  921. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  922. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  923. OrderService::classify($id, $categoryId, $usageId);
  924. util::complete();
  925. }
  926. //更新配送单 ssh 2019.12.17
  927. public function actionUpdateSheet()
  928. {
  929. $post = Yii::$app->request->post();
  930. $id = isset($post['id']) ? $post['id'] : 0;
  931. unset($post['id']);
  932. $order = OrderService::getById($id);
  933. OrderService::valid($order, $this->mainId);
  934. OrderService::updateSheet($order, $post);
  935. util::complete('提交成功');
  936. }
  937. //商家收款与发货 ssh 2019.12.18
  938. public function actionGathering()
  939. {
  940. $post = Yii::$app->request->post();
  941. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  942. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  943. $userId = isset($post['userId']) ? $post['userId'] : 0;
  944. $userInfo = UserService::getById($userId);
  945. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  946. util::fail('您选择的客户无效');
  947. }
  948. if ($price <= 0) {
  949. util::fail('请输入金额');
  950. }
  951. $post['prePrice'] = $price;
  952. $post['actPrice'] = $price;
  953. $post['payWay'] = $payWay;
  954. $post['sourceType'] = 1;
  955. $post['userId'] = $userId;
  956. $post['goodsNum'] = 1;
  957. $post['orderName'] = '商品';
  958. $shopId = MerchantService::getDefaultShopId($this->sj);
  959. $post['shopId'] = $shopId;
  960. $order = OrderService::addOrder($post);
  961. $id = $order['id'];
  962. $orderSn = $order['orderSn'];
  963. //流水类型列表
  964. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  965. $callbackParams = [];
  966. switch ($payWay) {
  967. case 0:
  968. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  969. break;
  970. case 1:
  971. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  972. break;
  973. case 2:
  974. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  975. break;
  976. default:
  977. util::fail('无效支付方式');
  978. }
  979. util::success($order);
  980. }
  981. //免发货管理 ssh 2020.1.6
  982. public function actionWithoutSend()
  983. {
  984. $id = Yii::$app->request->get('id');
  985. $order = OrderService::getById($id);
  986. OrderService::valid($order, $this->mainId);
  987. //配送流程变更
  988. $currentFlow = OrderSendClass::withoutSend($order);
  989. util::success(['currentFlow' => $currentFlow]);
  990. }
  991. //修改价格 ssh 2020.1.6
  992. public function actionUpdatePrice()
  993. {
  994. $id = Yii::$app->request->get('id');
  995. $price = Yii::$app->request->get('price', 1);
  996. $order = OrderService::getById($id);
  997. OrderService::valid($order, $this->mainId);
  998. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  999. util::fail('已发起支付,不能修改价格');
  1000. }
  1001. OrderService::updateById($id, ['actPrice' => $price]);
  1002. util::complete('修改成功');
  1003. }
  1004. //配送单 ssh 2020.2.29
  1005. public function actionGetDeliverDetail()
  1006. {
  1007. $id = Yii::$app->request->get('id', 0);
  1008. $order = OrderService::getById($id);
  1009. OrderService::valid($order, $this->mainId);
  1010. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1011. $reachPeriodId = $order['reachPeriod'];
  1012. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1013. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1014. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1015. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1016. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1017. $bookName = '--';
  1018. $bookMobile = '--';
  1019. }
  1020. $data = [
  1021. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1022. 'orderSn' => $order['orderSn'],
  1023. 'receiveUserName' => $order['receiveUserName'],
  1024. 'receiveMobile' => $order['receiveMobile'],
  1025. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1026. 'cardInfo' => $order['cardInfo'],
  1027. 'bookMobile' => $bookMobile,
  1028. 'bookName' => $bookName,
  1029. 'remark' => $order['remark'],
  1030. 'sendNum' => $order['sendNum'],
  1031. 'telephone' => 18030142050,
  1032. 'printTime' => '',
  1033. 'img' => '',
  1034. ];
  1035. util::success($data);
  1036. }
  1037. //运费计算 lqh 2021.4.12 暂反回固定
  1038. public function actionFreight()
  1039. {
  1040. util::success(['sedCost' => 10]);
  1041. }
  1042. }