OrderController.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. <?php
  2. namespace hd\controllers;
  3. use Yii;
  4. use biz\shop\classes\ShopExtClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use biz\sj\services\MerchantService;
  7. use bizHd\custom\classes\CustomClass;
  8. use bizHd\custom\classes\HdClass;
  9. use bizHd\goods\classes\GoodsClass;
  10. use bizHd\merchant\services\ShopService;
  11. use bizHd\order\classes\OrderClass;
  12. use bizHd\order\classes\OrderForwardClass;
  13. use bizHd\order\classes\OrderItemClass;
  14. use bizHd\order\classes\OrderSendClass;
  15. use hd\models\order\CreateOrderForm;
  16. use bizHd\express\classes\HdDeliveryOrderClass;
  17. use bizHd\order\services\OrderService;
  18. use bizHd\product\classes\ProductClass;
  19. use bizHd\saas\services\RegionService;
  20. use bizHd\shop\classes\ShopClass;
  21. use bizHd\user\services\UserService;
  22. use bizHd\work\classes\WorkClass;
  23. use common\components\dict;
  24. use common\components\dateUtil;
  25. use common\components\noticeUtil;
  26. use common\components\orderSn;
  27. use common\components\payUtil;
  28. use common\services\xhPayToolService;
  29. use common\components\util;
  30. use common\components\stringUtil;
  31. use bizHd\promote\services\CouponService;
  32. use common\components\httpUtil;
  33. use biz\wx\classes\WxMessageClass;
  34. use common\components\lakala\Lakala;
  35. class OrderController extends BaseController
  36. {
  37. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list', 'info'];
  38. public function actionDebt()
  39. {
  40. $get = Yii::$app->request->get();
  41. $id = $get['id'] ?? 0;
  42. $info = OrderClass::getById($id, true);
  43. if (empty($info)) {
  44. util::fail('没有找到订单');
  45. }
  46. if ($info->mainId != $this->mainId) {
  47. util::fail('不是你的订单');
  48. }
  49. //4秒内不允许重复提交
  50. util::checkRepeatCommit($id, 4);
  51. if ($info->status != 1) {
  52. util::fail('订单已付款或取消');
  53. }
  54. $payWay = dict::getDict('payWay', 'debtPay');
  55. $connection = Yii::$app->db;
  56. $transaction = $connection->beginTransaction();
  57. try {
  58. $actPrice = $info->actPrice ?? 0;
  59. $info->debtPrice = $actPrice;
  60. $info->remainDebtPrice = $actPrice;
  61. $info->debt = 1;
  62. $info->save();
  63. OrderClass::payAfter($info, $payWay);
  64. //到店自取订单,并且是待取货状态,则自动完成取货,多处要同步修改,关键词 reach_shop_fetch_goods
  65. if ($info->getGoods == 1 && $info->sendType == 1 && $info->status == 2) {
  66. $info->status = 4;
  67. $info->save();
  68. }
  69. $transaction->commit();
  70. util::complete('赊账成功');
  71. } catch (\Exception $exception) {
  72. $transaction->rollBack();
  73. Yii::info("赊账失败原因:" . $exception->getMessage());
  74. util::fail('操作失败');
  75. }
  76. }
  77. //取消订单 ssh 20221231
  78. public function actionCancel()
  79. {
  80. //避免重复提交
  81. $adminId = $this->adminId;
  82. util::checkRepeatCommit($adminId, 8);
  83. $get = Yii::$app->request->get();
  84. $id = $get['id'] ?? 0;
  85. $order = OrderClass::getById($id, true);
  86. if ($order->mainId != $this->mainId) {
  87. util::fail('不是你的订单');
  88. }
  89. $staff = $this->shopAdmin;
  90. $connection = Yii::$app->db;
  91. $transaction = $connection->beginTransaction();
  92. try {
  93. OrderClass::setExpire($order, true, $staff);
  94. $transaction->commit();
  95. util::complete();
  96. } catch (\Exception $exception) {
  97. $transaction->rollBack();
  98. Yii::info("取消原因:" . $exception->getMessage());
  99. util::fail('取消失败');
  100. }
  101. }
  102. //修改订单 ssh 20220926
  103. public function actionUpdate()
  104. {
  105. $post = Yii::$app->request->post();
  106. $id = $post['id'] ?? 0;
  107. $order = OrderClass::getById($id, true);
  108. OrderClass::valid($order, $this->mainId);
  109. unset($post['id']);
  110. if ($order->status == 3) {
  111. util::fail('订单已配送');
  112. }
  113. if ($order->status == 4) {
  114. util::fail('订单已完成');
  115. }
  116. if ($order->status == 5) {
  117. util::fail('订单已取消');
  118. }
  119. if ($order->fromType == 4) {
  120. util::fail('请在美团APP上修改');
  121. }
  122. $address = $post['address'] ?? '';
  123. $floor = $post['floor'] ?? '';
  124. $post['fullAddress'] = $address . $floor;
  125. OrderClass::updateById($id, $post);
  126. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  127. $remark = $post['remark'] ?? '';
  128. $reachDate = $post['reachDate'] ?? '';
  129. $reachPeriod = $post['reachPeriod'] ?? '';
  130. $cardInfo = $post['cardInfo'] ?? '';
  131. $anonymity = $post['anonymity'] ?? 0;
  132. $bookName = $post['bookName'] ?? '';
  133. if (!empty($workList)) {
  134. foreach ($workList as $work) {
  135. $work->remark = $remark;
  136. $work->reachDate = $reachDate;
  137. $work->reachPeriod = $reachPeriod;
  138. $work->cardInfo = $cardInfo;
  139. $work->anonymity = $anonymity;
  140. $work->bookName = $bookName;
  141. $work->save();
  142. }
  143. }
  144. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  145. ShopExtClass::orderUpdateRemind($shopExt, $order);
  146. util::complete();
  147. }
  148. //客户欠款的订单 ssh 2021.2.4
  149. public function actionDebtList()
  150. {
  151. $get = Yii::$app->request->get();
  152. $id = $get['id'] ?? 0;
  153. $info = CustomClass::getById($id, true);
  154. CustomClass::valid($info, $this->shopId);
  155. $where = ['customId' => $id, 'debt' => 1];
  156. $searchTime = $get['searchTime'] ?? '';
  157. if (!empty($searchTime)) {
  158. $startTime = $get['startTime'] ?? '';
  159. $endTime = $get['endTime'] ?? '';
  160. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  161. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  162. }
  163. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice', 'debtPrice']);
  164. if (!empty($list)) {
  165. foreach ($list as $key => $value) {
  166. $debtPrice = $value['debtPrice'] ?? 0;
  167. $remainDebtPrice = $value['remainDebtPrice'] ?? 0;
  168. $hasPayDebt = 0;
  169. if ($debtPrice > $remainDebtPrice) {
  170. $hasPayDebt = bcsub($debtPrice, $remainDebtPrice, 2);
  171. }
  172. $list[$key]['hasPayDebt'] = floatval($hasPayDebt);
  173. }
  174. }
  175. $result = ['customInfo' => $info, 'list' => $list];
  176. util::success($result);
  177. }
  178. //确认送达 ssh 20250902
  179. public function actionReach()
  180. {
  181. //解决重复提交
  182. util::checkRepeatCommit($this->adminId, 5);
  183. $get = Yii::$app->request->get();
  184. $id = $get['id'] ?? 0;
  185. $order = OrderClass::getById($id, true);
  186. if (empty($order)) {
  187. util::fail('没有找到订单');
  188. }
  189. OrderClass::valid($order, $this->mainId);
  190. if ($order->status == 5) {
  191. util::fail('订单已取消');
  192. }
  193. if ($order->status == 1) {
  194. util::fail('订单待付款');
  195. }
  196. if ($order->status == 2) {
  197. util::fail('订单未发货');
  198. }
  199. if ($order->status == 4) {
  200. util::fail('订单已经送到了');
  201. }
  202. if (!in_array($order->sendStatus, [-1, -4])) {
  203. util::fail('由跑腿送货,无需确认');
  204. }
  205. $order->status = 4;
  206. $order->save();
  207. util::complete('操作成功');
  208. }
  209. //确认取货
  210. public function actionFetch()
  211. {
  212. $get = Yii::$app->request->get();
  213. $id = $get['id'] ?? 0;
  214. //解决重复提交
  215. util::checkRepeatCommit($id . '_' . $this->adminId, 5);
  216. $order = OrderClass::getById($id, true);
  217. if (empty($order)) {
  218. util::fail('没有找到订单');
  219. }
  220. OrderClass::valid($order, $this->mainId);
  221. if ($order->status == 5) {
  222. util::fail('订单已取消');
  223. }
  224. if ($order->status == 1) {
  225. util::fail('订单待付款');
  226. }
  227. if ($order->status == 4) {
  228. util::fail('订单已取过了');
  229. }
  230. if (in_array($order->sendStatus, [-1, 0, 1, 2, 4, 5])) {
  231. util::fail('由跑腿送货,无需确认');
  232. }
  233. $order->status = 4;
  234. $order->save();
  235. util::complete('操作成功');
  236. }
  237. //确认发货
  238. public function actionSend()
  239. {
  240. //解决重复提交
  241. util::checkRepeatCommit($this->adminId, 5);
  242. $get = Yii::$app->request->get();
  243. $id = $get['id'] ?? 0;
  244. $order = OrderClass::getById($id, true);
  245. if (empty($order)) {
  246. util::fail('没有找到订单');
  247. }
  248. OrderClass::valid($order, $this->mainId);
  249. if ($order->status != 2) {
  250. util::fail('订单不是待发货状态');
  251. }
  252. // $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  253. // if (!empty($workList)) {
  254. // $unComplete = false;
  255. // foreach ($workList as $work) {
  256. // if ($work->status != 1) {
  257. // $unComplete = true;
  258. // }
  259. // }
  260. // if ($unComplete == true) {
  261. // util::fail('还有制作单没有完成');
  262. // }
  263. // }
  264. $order->status = 3;
  265. $order->save();
  266. util::complete('已发货');
  267. }
  268. //打印订单 ssh 20220601
  269. public function actionPrintOrder()
  270. {
  271. $get = Yii::$app->request->get();
  272. $id = isset($get['id']) ? $get['id'] : 0;
  273. $order = OrderClass::getById($id, true);
  274. if (empty($order)) {
  275. util::fail('没有找到订单');
  276. }
  277. if ($order->forward == 1) {
  278. util::fail('售后付款单不能打印');
  279. }
  280. OrderClass::valid($order, $this->mainId);
  281. OrderClass::onlinePrint($order, true);
  282. util::complete();
  283. }
  284. //微信和支付宝付款码支付复查 ssh 20220422
  285. public function actionCodePayCheck()
  286. {
  287. ini_set('date.timezone', 'Asia/Shanghai');
  288. header("Content-type: text/html; charset=utf-8");
  289. $get = Yii::$app->request->get();
  290. $id = isset($get['id']) ? $get['id'] : 0;
  291. util::checkRepeatCommit($id, 3);
  292. $order = OrderClass::getById($id, true);
  293. OrderClass::valid($order, $this->mainId);
  294. $orderSn = $order->orderSn ?? '';
  295. $totalFee = $order->mainPay ?? 0;
  296. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  297. util::success(['returnStatus' => 'SUCCESS']);
  298. }
  299. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  300. util::complete('订单已取消');
  301. }
  302. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  303. util::complete('订单不是待付款状态');
  304. }
  305. $connection = Yii::$app->db;
  306. $transaction = $connection->beginTransaction();
  307. try {
  308. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  309. $shop = $this->shop;
  310. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  311. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  312. $params = [
  313. 'appid' => 'OP00002119',
  314. 'serial_no' => '018b08cfddbd',
  315. 'merchant_no' => $shop->lklSjNo,
  316. 'term_no' => $shop->lklScanTermNo,
  317. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  318. 'lklCertificatePath' => $lklCertificatePath,
  319. ];
  320. $laResource = new Lakala($params);
  321. $scanParams = ['orderSn' => $orderSn,];
  322. $response = $laResource->query($scanParams);
  323. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  324. if (isset($response['resp_data']['trade_state']) && $response['resp_data']['trade_state'] == 'SUCCESS') {
  325. $payWayType = dict::getDict('payWay', 'wxPay');
  326. $account_type = $response['resp_data']['account_type'] ?? '';
  327. if ($account_type == 'WECHAT') {
  328. $payWayType = dict::getDict('payWay', 'wxPay');
  329. }
  330. if ($account_type == 'ALIPAY') {
  331. $payWayType = dict::getDict('payWay', 'alipay');
  332. }
  333. $transactionId = $response['resp_data']['trade_no'] ?? '';
  334. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  335. $order->save();
  336. $attach = '';
  337. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  338. $transaction->commit();
  339. //解决重复通知
  340. $cacheKey = 'hd_shop_order_pay_check_' . $orderSn;
  341. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  342. if (empty($has)) {
  343. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  344. //打印小票和语音播报
  345. $newOrder = OrderClass::getById($id, true);
  346. OrderClass::onlinePrint($newOrder);
  347. ShopExtClass::hdGatheringReport($newOrder);
  348. //到店自取订单,并且是待取货状态,则自动完成取货,多处要同步修改,关键词 reach_shop_fetch_goods
  349. if ($newOrder->getGoods == 1 && $newOrder->sendType == 1 && $newOrder->status == 2) {
  350. $newOrder->status = 4;
  351. $newOrder->save();
  352. }
  353. $shopId = $newOrder->shopId ?? 0;
  354. $shop = ShopClass::getById($shopId, true);
  355. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  356. }
  357. util::success(['returnStatus' => 'SUCCESS']);
  358. } else {
  359. util::complete('未知状态..');
  360. }
  361. }
  362. util::complete('未知状态..');
  363. } catch (\Exception $e) {
  364. $transaction->rollBack();
  365. util::complete('支付失败');
  366. }
  367. }
  368. //微信和支付宝付款码支付 ssh 2021.4.11
  369. public function actionCodePay()
  370. {
  371. ini_set('date.timezone', 'Asia/Shanghai');
  372. header("Content-type: text/html; charset=utf-8");
  373. $get = Yii::$app->request->get();
  374. $id = isset($get['id']) ? $get['id'] : 0;
  375. util::checkRepeatCommit($id, 3);
  376. $version = $get['version'] ?? 0;
  377. $order = OrderClass::getById($id, true);
  378. OrderClass::valid($order, $this->mainId);
  379. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  380. $msg = '订单已取消';
  381. util::success(['returnStatus' => 'FAILURE'], $msg);
  382. }
  383. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  384. $msg = '订单不是待付款状态';
  385. util::success(['returnStatus' => 'FAILURE'], $msg);
  386. }
  387. $shop = $this->shop;
  388. $sj = $shop->merchantName;
  389. $shopName = $shop->shopName;
  390. $name = $shopName == '首店' ? $sj : $sj . '-' . $shopName;
  391. $authCode = (string)$get['authCode'] ?? '';
  392. if ($version > 0) {
  393. if (empty($authCode)) {
  394. $msg = '没有扫到付款码';
  395. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  396. noticeUtil::push($remind . ' 编号005', '15280215347');
  397. util::success(['returnStatus' => 'FAILURE'], $msg);
  398. }
  399. //微信文档说明 https://pay.weixin.qq.com/doc/v2/merchant/4011936234
  400. $wxPattern = '/^(10|11|12|13|14|15)\d{16}$/';
  401. //支付宝文档说明 https://opendocs.alipay.com/open/194/106039/
  402. $aliPattern = '/^(25|26|27|28|29|30)\d{14,22}$/';
  403. if (!preg_match($wxPattern, $authCode) && !preg_match($aliPattern, $authCode)) {
  404. $msg = '付款码错误,请使用微信或支付宝付款码';
  405. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  406. //noticeUtil::push($remind . ' 编号009', '15280215347');
  407. util::success(['returnStatus' => 'FAILURE'], $msg);
  408. }
  409. } else {
  410. if (empty($authCode)) {
  411. util::fail('没有获取到支付码');
  412. }
  413. }
  414. $orderSn = $order->orderSn ?? '';
  415. $lsCodePayErrorKey = 'ls_code_pay_error_' . $id;
  416. $hasFailPay = Yii::$app->redis->executeCommand('GET', [$lsCodePayErrorKey]);
  417. //前面有付失败过,要重新生成单号
  418. if (!empty($hasFailPay)) {
  419. $oldOrderSn = $order->orderSn;
  420. $orderSn = orderSn::getOrderSn();
  421. $order->orderSn = $orderSn;
  422. $order->save();
  423. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  424. }
  425. $subject = '购买花材 ' . $orderSn;
  426. $totalFee = $order->mainPay ?? 0;
  427. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  428. $connection = Yii::$app->db;
  429. $transaction = $connection->beginTransaction();
  430. try {
  431. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  432. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  433. $params = [
  434. 'appid' => 'OP00002119',
  435. 'serial_no' => '018b08cfddbd',
  436. 'merchant_no' => $shop->lklSjNo,
  437. 'term_no' => $shop->lklScanTermNo,
  438. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  439. 'lklCertificatePath' => $lklCertificatePath,
  440. ];
  441. $laResource = new Lakala($params);
  442. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  443. $scanParams = [
  444. 'orderSn' => $orderSn,
  445. 'amount' => $totalFee,
  446. 'capitalType' => $capitalType,
  447. 'notifyUrl' => $notifyUrl,
  448. 'subject' => $subject,
  449. 'authCode' => $authCode,
  450. ];
  451. $response = $laResource->scanPay($scanParams);
  452. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  453. $account_type = $response['resp_data']['account_type'] ?? '';
  454. $payWayType = dict::getDict('payWay', 'wxPay');
  455. if ($account_type == 'WECHAT') {
  456. $payWayType = dict::getDict('payWay', 'wxPay');
  457. }
  458. if ($account_type == 'ALIPAY') {
  459. $payWayType = dict::getDict('payWay', 'alipay');
  460. }
  461. $order->payWay = $payWayType;
  462. $transactionId = $response['resp_data']['trade_no'] ?? '';
  463. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  464. $order->save();
  465. $attach = '';
  466. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  467. $transaction->commit();
  468. //解决重复通知
  469. $cacheKey = 'hd_shop_order_pay_' . $orderSn;
  470. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  471. if (empty($has)) {
  472. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  473. $newOrder = OrderClass::getById($id, true);
  474. //到店自取订单,并且是待取货状态,则自动完成取货,多处要同步修改,关键词 reach_shop_fetch_goods
  475. if ($newOrder->getGoods == 1 && $newOrder->sendType == 1 && $newOrder->status == 2) {
  476. $newOrder->status = 4;
  477. $newOrder->save();
  478. }
  479. //打印小票
  480. OrderClass::onlinePrint($newOrder);
  481. //语音播报
  482. ShopExtClass::hdGatheringReport($newOrder);
  483. $shopId = $newOrder->shopId ?? 0;
  484. $shop = ShopClass::getById($shopId, true);
  485. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  486. }
  487. util::success(['returnStatus' => 'SUCCESS']);
  488. } else {
  489. //标记有没付成功的次数,原因有可能是余额不足等
  490. Yii::$app->redis->executeCommand('SETEX', [$lsCodePayErrorKey, 1200, 'hasFail']);
  491. $msg = $response['msg'] ?? '';
  492. $retCode = $response['code'] ?? '';
  493. $fullMsg = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  494. if ($retCode == 'BBS10000' || $retCode == 'BBS11105') {
  495. $shopId = $order->shopId;
  496. ShopExtClass::remindInputPassword($shopId);
  497. }
  498. //noticeUtil::push($fullMsg . ' ' . $retCode . ' 编号008', '15280215347');
  499. util::success(['returnStatus' => 'FAILURE', 'returnCode' => $retCode], $msg);
  500. }
  501. } catch (\Exception $e) {
  502. $transaction->rollBack();
  503. util::fail('支付失败');
  504. }
  505. }
  506. //开单操作 ssh 20220316
  507. public function actionCreateOrder()
  508. {
  509. $post = Yii::$app->request->post();
  510. $post['sjId'] = $this->sjId;
  511. $post['shopId'] = $this->shopId;
  512. $post['mainId'] = $this->mainId ?? 0;
  513. $shop = $this->shop;
  514. if ($shop->memberLevelSet == 0) {
  515. //util::fail('请先设置会员等级');
  516. }
  517. $now = time();
  518. $orderValidTime = 600;
  519. $expireTime = $now + $orderValidTime;
  520. $post['deadline'] = $expireTime;
  521. $post['staffId'] = $this->shopAdminId ?? 0;
  522. $post['staffName'] = $this->shopAdmin->name ?? '';
  523. $post['flowerNum'] = isset($post['flowerNum']) && $post['flowerNum'] > 0 ? $post['flowerNum'] : 0;
  524. $version = $post['version'] ?? 0; //验证已移至 CreateOrderForm
  525. if ($version < 3) {
  526. util::fail('请升级版本,或用小程序');
  527. }
  528. //默认情况开单员工和收款员工是同个人
  529. $staffId = $this->shopAdminId ?? 0;
  530. $staffName = $this->shopAdmin->name ?? '';
  531. if (empty($post['shopAdminId'])) {
  532. $post['shopAdminId'] = $staffId;
  533. $post['shopAdminName'] = $staffName;
  534. }
  535. if (empty($post['getStaffId'])) {
  536. $post['getStaffId'] = $staffId;
  537. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  538. }
  539. $groupId = !empty($post['groupId']) ? $post['groupId'] : 0;
  540. $post['fromType'] = $post['fromType'] ?? 1;
  541. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  542. if (empty($post['bookName'])) {
  543. util::fail('请填写订花人姓名');
  544. }
  545. }
  546. $customId = $post['customId'] ?? 0;
  547. $custom = CustomClass::getById($customId, true);
  548. if (empty($custom)) {
  549. util::fail('没有找到客户哦');
  550. }
  551. $hdId = $custom->hdId ?? 0;
  552. $hd = HdClass::getById($hdId, true);
  553. if (empty($hd)) {
  554. util::fail('客户对应的花店信息缺失,编号869');
  555. }
  556. $hdName = $hd->name ?? '';
  557. $post['hdId'] = $hdId;
  558. $post['hdName'] = $hdName;
  559. $customName = $custom->name ?? '';
  560. $post['customName'] = $customName;
  561. $post['customNamePy'] = stringUtil::py($customName);
  562. if (!empty($post['receiveMobile'])) {
  563. if (!stringUtil::isMobile($post['receiveMobile'])) {
  564. util::fail('请填写正确的收花人手机号');
  565. }
  566. }
  567. if (!empty($post['bookMobile'])) {
  568. if (!stringUtil::isMobile($post['bookMobile'])) {
  569. util::fail('请填写正确的订花人手机号');
  570. }
  571. }
  572. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  573. $post['sendType'] = $post['sendType'] ?? 1;
  574. $forward = $post['forward'] ?? 0;
  575. if ($forward == 1) {
  576. if ($hasPay != 1) {
  577. util::fail('售后付款单只能走线下转账');
  578. }
  579. $historyDate = $post['historyDate'] ?? '';
  580. if (!empty($historyDate)) {
  581. if ($historyDate != date("Y-m-d")) {
  582. util::fail('售后付款单不能修改账单日期,只能今天');
  583. }
  584. }
  585. } else {
  586. //如果不是售后付款单,并且选的不是余额支付,余额又够付当前订单,则自动切到余额支付
  587. $balance = $custom->balance ?? 0;
  588. $modifyPrice = $post['modifyPrice'] ?? 0;
  589. if ($hasPay != 4 && $balance > $modifyPrice) {
  590. $post['hasPay'] = 4;
  591. $hasPay = 4;
  592. }
  593. }
  594. $productJson = $post['product'] ?? '';
  595. $productList = [];
  596. if (!empty($productJson)) {
  597. $productList = json_decode($productJson, true);
  598. }
  599. if (empty($productList) && empty($groupId)) {
  600. //商家手机上开单并生成制作单
  601. if (!empty($post['unitGoodsPrice'])) {
  602. util::fail('此功能停用,有需要请联系管理员');
  603. $unitGoodsPrice = $post['unitGoodsPrice'];
  604. if (!is_numeric($unitGoodsPrice)) {
  605. util::fail('请输入正确的单价');
  606. }
  607. $productList = GoodsClass::orderCreateGoods($post);
  608. }
  609. }
  610. //直接收款
  611. $zjGathering = $post['zjGathering'] ?? 0;
  612. if ($zjGathering == 1) {
  613. $lsGoodsPrice = $post['lsGoodsPrice'] ?? 0;
  614. if (!is_numeric($lsGoodsPrice) || $lsGoodsPrice <= 0) {
  615. util::fail('请填写价格');
  616. }
  617. //直接收款不需要打印小票
  618. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  619. $shopId = $this->shopId;
  620. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  621. $zjGatheringItem = !empty($ext) ? $ext->zjGatheringItem : 0;
  622. if (empty($zjGatheringItem)) {
  623. util::fail('请设置直接收款的商品');
  624. }
  625. $zjGatheringItemInfo = ProductClass::getById($zjGatheringItem, true);
  626. if (empty($zjGatheringItemInfo)) {
  627. util::fail('请设置直接收款的商品哈');
  628. }
  629. $productList = [['productId' => $zjGatheringItem, 'unitType' => 0, 'num' => 1, 'property' => '1', 'unitPrice' => $lsGoodsPrice]];
  630. }
  631. $connection = Yii::$app->db;
  632. $transaction = $connection->beginTransaction();
  633. try {
  634. $post['product'] = $productList;
  635. $reduceStock = $post['reduceStock'] ?? 0;
  636. if ($reduceStock == 1) {
  637. //如果扣除库,则走另外一条路
  638. $return = OrderClass::reduceMyStock($post, $this->shop, $custom);
  639. $transaction->commit();
  640. util::success($return);
  641. }
  642. //解决开单提交订单时,别人修改价格,造成提交价格不是真实卖价问题!!
  643. $dealPrice = $post['dealPrice'] ?? 0;
  644. //解决重复提交
  645. util::checkRepeatCommit($this->adminId . '_' . $dealPrice, 3);
  646. if ($dealPrice == 0) {
  647. $productIds = [];
  648. $hsIds = [];
  649. foreach ($productList as $pv) {
  650. $property = $pv['property'] ?? 0;
  651. $puId = $pv['productId'] ?? 0;
  652. if ($property == 0) {
  653. $hsIds[] = $puId;
  654. } else {
  655. $productIds[] = $puId;
  656. }
  657. }
  658. $itemInfo = [];
  659. if (!empty($productIds)) {
  660. $itemInfo = ProductClass::getByIds($productIds, null, 'id');
  661. }
  662. $hsInfo = [];
  663. if (!empty($hsIds)) {
  664. $hsInfo = GoodsClass::getByIds($hsIds, null, 'id');
  665. }
  666. $diff = [];
  667. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  668. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  669. $level = 0;
  670. foreach ($productList as $currentProduct) {
  671. $property = $currentProduct['property'] ?? 0;
  672. $currentPrice = $currentProduct['unitPrice'] ?? 0;
  673. $currentId = $currentProduct['productId'] ?? 0;
  674. if ($property == 1) {
  675. $systemInfo = $itemInfo[$currentId] ?? [];
  676. if (!empty($systemInfo)) {
  677. $name = $systemInfo['name'] ?? '';
  678. $systemPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  679. if (floatval($currentPrice) != floatval($systemPrice)) {
  680. $diff[] = [
  681. 'name' => $name,
  682. 'price' => $systemPrice,
  683. 'kdPrice' => $currentPrice,
  684. ];
  685. }
  686. }
  687. } else {
  688. $systemInfo = $hsInfo[$currentId] ?? [];
  689. if (!empty($systemInfo)) {
  690. $params = [];
  691. $name = $systemInfo['name'] ?? '';
  692. $hsData = GoodsClass::getFinalPrice($systemInfo, $shop, $custom, $params);
  693. $systemPrice = $hsData['price'] ?? 0;
  694. if ($systemInfo['priceType'] == 1) {
  695. if (floatval($currentPrice) != floatval($systemPrice)) {
  696. $diff[] = [
  697. 'name' => $name,
  698. 'price' => $systemPrice,
  699. 'kdPrice' => $currentPrice,
  700. ];
  701. }
  702. }
  703. }
  704. }
  705. }
  706. if (!empty($diff)) {
  707. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  708. }
  709. }
  710. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  711. $return = OrderService::createHdOrder($post, $custom, $hasPay);
  712. $staff = $this->shopAdmin;
  713. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  714. OrderClass::shPayChange($return, $shop, $staff);
  715. //打印小票和语音播报等使用
  716. $id = $return->id ?? 0;
  717. $order = OrderClass::getById($id, true);
  718. //如果需要生成制作单
  719. $needWork = $post['needWork'] ?? 0;
  720. if ($needWork == 1) {
  721. $main = $this->main;
  722. WorkClass::needWork($order, $main);
  723. $has = WorkClass::getByCondition(['orderId' => $id], true);
  724. if (!empty($has)) {
  725. $return->hasWork = 1;
  726. $return->save();
  727. }
  728. }
  729. //到店自取订单,并且是待取货状态,则自动完成取货,多处要同步修改,关键词 reach_shop_fetch_goods
  730. if ($order->getGoods == 1 && $order->sendType == 1 && $order->status == 2) {
  731. $order->status = 4;
  732. $order->save();
  733. }
  734. $transaction->commit();
  735. if ($order->fromType == 3 || $order->fromType == 4) {
  736. //除了客服号下单和美团下单,其它情况允许打小票
  737. } else {
  738. //前台打小票
  739. OrderClass::onlinePrint($order);
  740. }
  741. //前台提示收款多少钱
  742. ShopExtClass::hdGatheringReport($order);
  743. //前台提醒出示付款码
  744. if (isset($order->status) && $order->status == 1) {
  745. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  746. ShopExtClass::pleasePayReport($order);
  747. }
  748. }
  749. $shopId = $order->shopId ?? 0;
  750. $shop = ShopClass::getById($shopId, true);
  751. WxMessageClass::gatheringIncomeInform($shop, $order);
  752. if ($needWork == 1) {
  753. //新制作单提示
  754. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  755. ShopExtClass::newWorkRemind($shopExt, $order);
  756. }
  757. //跑腿订单生产者创建订单
  758. $sendType = $post['sendType'] ?? 0;
  759. if($order->payStatus == 1 && $sendType == dict::getDict('sendType', 'thirdSend') && $post['callErrand'] == 1){ // $post 中有 pickupTime,则说明是要立即发跑腿 或者 callErrand == 1
  760. $platform = $post['deliveryPlatform'];
  761. $deliveryPrice = $post['deliveryPrice'];
  762. $deliveryDistance = $post['deliveryDistance'];
  763. $deliveryBracketContent = $post['deliveryBracketContent'];
  764. $pickupTime = $post['pickupTime'];
  765. $allParams = $post['deliveryAllParams'];
  766. $data = [
  767. 'orderId' => $order->id,
  768. 'mainId' => $this->mainId,
  769. 'shopId' => $this->shopId,
  770. 'platform' => $platform,
  771. 'ip' => Yii::$app->request->userIP,
  772. 'deliveryPrice' => $deliveryPrice,
  773. 'deliveryDistance' => $deliveryDistance,
  774. 'deliveryBracketContent' => $deliveryBracketContent,
  775. 'pickupTime' => $pickupTime,
  776. 'allParams' => $allParams,
  777. //-----------------------
  778. 'weight' => $post['weight'] ?? 1,
  779. 'remark' => $post['deliveryRemark'] ?? '',
  780. 'price' => $order->actPrice,
  781. ];
  782. HdDeliveryOrderClass::askCreateOrder($data);
  783. }
  784. util::success($return);
  785. } catch (\Exception $e) {
  786. $transaction->rollBack();
  787. Yii::error("下单失败原因:" . $e->getMessage());
  788. util::fail('下单失败');
  789. }
  790. }
  791. //下单要用到的相关信息 ssh 2019.12.6
  792. public function actionOrderRelate()
  793. {
  794. $regionTree = RegionService::tree();
  795. $shop = $this->shop->attributes;
  796. $freight = MerchantExtendService::getFreight($this->sjExtend);
  797. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  798. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  799. util::success($out);
  800. }
  801. //余额支付 ssh 2019.12.6
  802. public function actionBalancePay()
  803. {
  804. $post = Yii::$app->request->post();
  805. $orderSn = $post['orderSn'] ?? 0;
  806. $payPassword = $post['payPassword'] ?? 0;
  807. $couponId = $post['couponId'] ?? 0;
  808. $order = OrderService::getByOrderSn($orderSn);
  809. $userId = $this->adminId;
  810. $user = UserService::getUserInfo($userId);
  811. //验证支付密码是否正确
  812. UserService::validPayPassword($payPassword, $user);
  813. //支付前验证订单有效性
  814. OrderService::checkBeforePay($order);
  815. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  816. $totalFee = $order['prePrice'];
  817. $sourceType = 0;
  818. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  819. $actPrice = $discountData['price'];
  820. $callbackParams = [];
  821. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  822. $balance = $respond['balance'] ?? 0;
  823. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  824. }
  825. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  826. public function actionWxPay()
  827. {
  828. ini_set('date.timezone', 'Asia/Shanghai');
  829. $post = Yii::$app->request->post();
  830. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  831. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  832. $order = OrderService::getByOrderSn($orderSn);
  833. $orderId = $order['id'];
  834. //验证优惠券是否还有效
  835. if (!empty($couponId)) {
  836. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  837. }
  838. //支付前验证订单有效性
  839. OrderService::checkBeforePay($order);
  840. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  841. $totalFee = $order['realPrice'];
  842. //小程序使用miniOpenId
  843. if (httpUtil::isMiniProgram()) {
  844. $openId = $this->user['miniOpenId'];
  845. } else {
  846. $openId = $this->user['openId'];
  847. }
  848. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  849. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  850. $wxPayType = 0;
  851. if (httpUtil::isMiniProgram()) {
  852. $wxPayType = 1;
  853. }
  854. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  855. //订单30分钟后过期
  856. $now = time();
  857. $expireTime = $now + 1800;
  858. $wx = Yii::getAlias("@vendor/weixin");
  859. require_once($wx . '/lib/WxPay.Api.php');
  860. require_once($wx . '/example/WxPay.JsApiPay.php');
  861. $input = new \WxPayUnifiedOrder();
  862. $input->SetBody($name);
  863. $input->SetOut_trade_no($orderSn);
  864. $input->SetTotal_fee($totalFee * 100);
  865. $input->SetTime_start(date("YmdHis", $now));
  866. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  867. $input->SetTime_expire(date("YmdHis", $expireTime));
  868. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  869. $input->SetTrade_type("JSAPI");
  870. //花卉宝代为申请的微信支付
  871. $merchantExtend = $this->sjExtend->attributes;
  872. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  873. $input->SetSub_openid($openId);
  874. } else {
  875. $input->SetOpenid($openId);
  876. }
  877. //小程序使用miniAppId
  878. if (httpUtil::isMiniProgram()) {
  879. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  880. }
  881. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  882. $tools = new \JsApiPay();
  883. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  884. $newParams = json_decode($jsApiParameters, true);
  885. //已请求微信不能修改价格
  886. $updateData = [];
  887. $updateData['modPrice'] = 0;
  888. if (empty($order['deadline'])) {
  889. $updateData['deadline'] = $expireTime;
  890. }
  891. OrderService::updateById($orderId, $updateData);
  892. util::success($newParams);
  893. }
  894. //快捷付款订单
  895. public function actionFastPay()
  896. {
  897. ini_set('date.timezone', 'Asia/Shanghai');
  898. $post = Yii::$app->request->post();
  899. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  900. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  901. //验证门店是否有效
  902. $shopInfo = ShopService::getById($shopId);
  903. ShopService::valid($shopInfo, $this->sjId);
  904. $post['shopId'] = $shopId;
  905. //默认门店订单
  906. $store = isset($post['store']) ? $post['store'] : 1;
  907. $post['store'] = $store;
  908. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  909. $sourceType = $this->isWx ? 0 : 1;
  910. if (httpUtil::isMiniProgram()) {
  911. $sourceType = 2;
  912. }
  913. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  914. //兼容旧系统sourceType=3表示朋友圈来源
  915. $fromType = $sourceType == 3 ? 2 : 0;
  916. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  917. $post['userId'] = $this->adminId;
  918. $custom = $this->custom;
  919. $post['bookName'] = $custom['userName'] ?? '';
  920. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  921. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  922. $post['bookMobile'] = $bookMobile;
  923. $prePrice = round($post['prePrice'], 2);
  924. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  925. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  926. $actPrice = $discountData['price'];
  927. $post['discountType'] = $discountData['discountType'];
  928. $post['discountAmount'] = $discountData['discountAmount'];
  929. $post['actPrice'] = $actPrice;
  930. $post['realPrice'] = $actPrice;
  931. $post['payStyle'] = 0;
  932. $post['sjId'] = $this->sjId;
  933. $now = time();
  934. $expireTime = $now + 300;//订单5分钟后过期
  935. $post['createTime'] = date("Y-m-d H:i:s", $now);
  936. $post['deadline'] = $expireTime;
  937. if ($actPrice <= 0) {
  938. util::fail('请填写正确的金额');
  939. }
  940. //微信支付订单提交不能修改价格
  941. $post['modPrice'] = $this->isWx ? 0 : 1;
  942. $post['sourceType'] = $sourceType;
  943. $post['goodsNum'] = 1;
  944. $post['fromType'] = $fromType;
  945. $order = OrderService::addOrder($post);
  946. $orderId = $order['id'];
  947. $orderSn = $order['orderSn'];
  948. $sjId = $this->sjId;
  949. if ($payWay == 0) {
  950. $orderId = $order['id'];
  951. $name = '购买商品';
  952. $totalFee = $actPrice;
  953. $user = UserService::getById($this->adminId);
  954. $openId = isset($user['openId']) ? $user['openId'] : 0;
  955. if (httpUtil::isMiniProgram()) {
  956. //小程序使用miniOpenId
  957. $openId = $user['miniOpenId'];
  958. }
  959. if (empty($openId)) {
  960. util::fail('没有找到客户的openId');
  961. }
  962. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  963. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  964. $wxPayType = 0;
  965. if (httpUtil::isMiniProgram()) {
  966. $wxPayType = 1;
  967. }
  968. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  969. $wx = Yii::getAlias("@vendor/weixin");
  970. require_once($wx . '/lib/WxPay.Api.php');
  971. require_once($wx . '/example/WxPay.JsApiPay.php');
  972. $input = new \WxPayUnifiedOrder();
  973. $input->SetBody($name);
  974. $input->SetOut_trade_no($orderSn);
  975. $input->SetTotal_fee($totalFee * 100);
  976. $input->SetTime_start(date("YmdHis", $now));
  977. $input->SetAttach($attach);
  978. //设置订单有效期5分钟
  979. $input->SetTime_expire(date("YmdHis", $expireTime));
  980. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  981. $input->SetTrade_type("JSAPI");
  982. //花卉宝代为申请的微信支付
  983. $merchantExtend = $this->sjExtend->attributes;
  984. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  985. $input->SetSub_openid($openId);
  986. } else {
  987. $input->SetOpenid($openId);
  988. }
  989. //小程序使用miniAppId
  990. if (httpUtil::isMiniProgram()) {
  991. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  992. }
  993. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  994. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  995. $tools = new \JsApiPay();
  996. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  997. $newParams = json_decode($jsApiParameters, true);
  998. $newParams['orderId'] = $orderId;
  999. util::success($newParams);
  1000. } elseif ($payWay == 1) {
  1001. $extend = MerchantExtendService::getBySjId($sjId);
  1002. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  1003. util::fail('支付宝付款即将开通...');
  1004. }
  1005. $config = [
  1006. //应用ID,您的APPID。
  1007. 'app_id' => $extend['alipayPId'],
  1008. //商户私钥,您的原始格式RSA私钥
  1009. 'merchant_private_key' => $extend['alipayKey'],
  1010. //异步通知地址
  1011. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  1012. //同步跳转
  1013. 'return_url' => "",
  1014. //编码格式
  1015. 'charset' => "UTF-8",
  1016. //签名方式
  1017. 'sign_type' => "RSA2",
  1018. //支付宝网关
  1019. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  1020. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  1021. 'alipay_public_key' => $extend['alipayPublicKey'],
  1022. ];
  1023. Yii::info(json_encode($config) . ' aplipay config');
  1024. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  1025. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  1026. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  1027. $totalFee = $order['realPrice'];
  1028. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  1029. $subject = '购买商品';//订单名称,必填
  1030. $total_amount = $totalFee;//付款金额,必填
  1031. $body = '';//商品描述,可空
  1032. $timeout_express = "1m";//超时时间
  1033. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1034. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  1035. $passBackParams = urlencode($passBackParams);
  1036. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  1037. $payRequestBuilder->setBody($body);
  1038. $payRequestBuilder->setSubject($subject);
  1039. $payRequestBuilder->setOutTradeNo($out_trade_no);
  1040. $payRequestBuilder->setTotalAmount($total_amount);
  1041. $payRequestBuilder->setTimeExpress($timeout_express);
  1042. //在异步通知时将该参数原样返回
  1043. $payRequestBuilder->setPassbackParams($passBackParams);
  1044. //同步通知
  1045. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  1046. //异步通知
  1047. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  1048. $payResponse = new \AlipayTradeService($config);
  1049. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  1050. //直接将支付宝的html返回给前端
  1051. echo $result;
  1052. } elseif ($payWay == 2) {
  1053. //余额支付
  1054. util::fail('暂不支持');
  1055. } else {
  1056. util::fail('无效的支付方式');
  1057. }
  1058. }
  1059. //订单评价
  1060. public function actionComment()
  1061. {
  1062. $post = Yii::$app->request->post();
  1063. $id = $post['id'];
  1064. $order = OrderService::getById($id);
  1065. OrderService::valid($order, $this->mainId);
  1066. if ($order['grade'] > 0) {
  1067. util::fail('您已经评过了');
  1068. }
  1069. OrderService::comment($post);
  1070. util::complete('提交成功');
  1071. }
  1072. //订单详情,不需要登陆可以查看,要有盐,给跑腿上查看订单信息用的
  1073. public function actionInfo()
  1074. {
  1075. $get = Yii::$app->request->get();
  1076. $id = $get['id'] ?? 0;
  1077. $salt = $get['salt'] ?? '';
  1078. $detail = OrderClass::getFullInfo($id);
  1079. if (empty($detail)) {
  1080. util::fail('没有找到订单');
  1081. }
  1082. if (empty($detail['salt'])) {
  1083. util::fail('没有找到订单信息');
  1084. }
  1085. if (empty($salt)) {
  1086. util::fail('请求参数错误');
  1087. }
  1088. if ($detail['salt'] != $salt) {
  1089. util::fail('不是你的订单');
  1090. }
  1091. util::success($detail);
  1092. }
  1093. //订单详情
  1094. public function actionDetail()
  1095. {
  1096. $id = Yii::$app->request->get('id', 0);
  1097. $detail = OrderClass::getFullInfo($id);
  1098. OrderService::valid($detail, $this->mainId);
  1099. $forwardMap = [];
  1100. if ($detail['forward'] == 1) {
  1101. //当前单如果已经是售后付款单,则取出其对应的原单
  1102. $map = OrderForwardClass::getByCondition(['shOrderId' => $id], true);
  1103. if (!empty($map)) {
  1104. $originOrderId = $map->orderId ?? 0;
  1105. $forwardMap['orderId'] = $originOrderId;
  1106. }
  1107. }
  1108. $detail['forwardMap'] = $forwardMap;
  1109. $shOrderMap = [];
  1110. if ($detail['hasForward'] == 1) {
  1111. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1112. }
  1113. $detail['shOrderMap'] = $shOrderMap;
  1114. $main = $this->main;
  1115. //是否走售后付款单模式判断 ssh
  1116. $ret = OrderClass::ifShPay($detail, $main, 0);
  1117. $shAddPay = $ret['shAddPay'];
  1118. $shAddPayReason = $ret['shAddPayReason'];
  1119. $detail['shAddPay'] = $shAddPay;
  1120. $detail['shAddPayReason'] = $shAddPayReason;
  1121. util::success($detail);
  1122. }
  1123. public function actionList()
  1124. {
  1125. $get = Yii::$app->request->get();
  1126. $searchText = $get['searchText'] ?? '';
  1127. $searchStyle = $get['searchStyle'] ?? 0;
  1128. $shopId = $this->shopId ?? 0;
  1129. if (empty($shopId)) {
  1130. //没有登录不显示数据
  1131. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  1132. }
  1133. $where = [];
  1134. $where['shopId'] = $shopId;
  1135. if (!empty($get['customId'])) {
  1136. $where['customId'] = $get['customId'];
  1137. }
  1138. if (!empty($get['ids'])) {
  1139. $stringIds = $get['ids'];
  1140. $newIds = explode(',', $stringIds);
  1141. $where['id'] = ['in', $newIds];
  1142. }
  1143. $status = $get['status'] ?? 0;
  1144. if (!empty($status)) {
  1145. $where['status'] = $get['status'];
  1146. }
  1147. $debt = $get['debt'] ?? -1;
  1148. if ($debt > -1) {
  1149. $where['debt'] = $debt;
  1150. }
  1151. if (!empty($get['shopAdminId'])) {
  1152. $where['shopAdminId'] = $get['shopAdminId'];
  1153. }
  1154. if ($searchStyle == 1 && !empty($searchText)) {
  1155. $where['sendNum'] = $searchText;
  1156. }
  1157. if (isset($get['repeat']) && $get['repeat'] > -1) {
  1158. $where['repeat'] = $get['repeat'];
  1159. }
  1160. if (!empty($get['searchTime'])) {
  1161. $startTime = $get['startTime'] ?? '';
  1162. $endTime = $get['endTime'] ?? '';
  1163. $period = dateUtil::formatTime($get['searchTime'], $startTime, $endTime);
  1164. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1165. }
  1166. $list = OrderService::getOrderList($where);
  1167. $list['shop'] = $this->shop->attributes ?? [];
  1168. util::success($list);
  1169. }
  1170. //订单归类 ssh 2019.12.17
  1171. public function actionClassify()
  1172. {
  1173. $post = Yii::$app->request->post();
  1174. $id = $post['id'] ?? 0;
  1175. $order = OrderService::getById($id);
  1176. OrderService::valid($order, $this->mainId);
  1177. $categoryId = $post['categoryId'];
  1178. $usageId = $post['usageId'];
  1179. OrderService::classify($id, $categoryId, $usageId);
  1180. util::complete();
  1181. }
  1182. //更新配送单 ssh 2019.12.17
  1183. public function actionUpdateSheet()
  1184. {
  1185. $post = Yii::$app->request->post();
  1186. $id = $post['id'] ?? 0;
  1187. unset($post['id']);
  1188. $order = OrderService::getById($id);
  1189. OrderService::valid($order, $this->mainId);
  1190. OrderService::updateSheet($order, $post);
  1191. util::complete('提交成功');
  1192. }
  1193. //商家收款与发货 ssh 2019.12.18
  1194. public function actionGathering()
  1195. {
  1196. $post = Yii::$app->request->post();
  1197. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  1198. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  1199. $userId = $post['userId'] ?? 0;
  1200. $userInfo = UserService::getById($userId);
  1201. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  1202. util::fail('您选择的客户无效');
  1203. }
  1204. if ($price <= 0) {
  1205. util::fail('请输入金额');
  1206. }
  1207. $post['prePrice'] = $price;
  1208. $post['actPrice'] = $price;
  1209. $post['payWay'] = $payWay;
  1210. $post['sourceType'] = 1;
  1211. $post['userId'] = $userId;
  1212. $post['goodsNum'] = 1;
  1213. $post['orderName'] = '商品';
  1214. $shopId = MerchantService::getDefaultShopId($this->sj);
  1215. $post['shopId'] = $shopId;
  1216. $order = OrderService::addOrder($post);
  1217. $id = $order['id'];
  1218. $orderSn = $order['orderSn'];
  1219. //流水类型列表
  1220. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1221. $callbackParams = [];
  1222. switch ($payWay) {
  1223. case 0:
  1224. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  1225. break;
  1226. case 1:
  1227. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  1228. break;
  1229. case 2:
  1230. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  1231. break;
  1232. default:
  1233. util::fail('无效支付方式');
  1234. }
  1235. util::success($order);
  1236. }
  1237. //免发货管理 ssh 2020.1.6
  1238. public function actionWithoutSend()
  1239. {
  1240. $id = Yii::$app->request->get('id');
  1241. $order = OrderService::getById($id);
  1242. OrderService::valid($order, $this->mainId);
  1243. //配送流程变更
  1244. $currentFlow = OrderSendClass::withoutSend($order);
  1245. util::success(['currentFlow' => $currentFlow]);
  1246. }
  1247. //修改价格 ssh 2020.1.6
  1248. public function actionUpdatePrice()
  1249. {
  1250. $id = Yii::$app->request->get('id');
  1251. $price = Yii::$app->request->get('price', 1);
  1252. $order = OrderService::getById($id);
  1253. OrderService::valid($order, $this->mainId);
  1254. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1255. util::fail('已发起支付,不能修改价格');
  1256. }
  1257. OrderService::updateById($id, ['actPrice' => $price]);
  1258. util::complete('修改成功');
  1259. }
  1260. //配送单 ssh 2020.2.29
  1261. public function actionGetDeliverDetail()
  1262. {
  1263. $id = Yii::$app->request->get('id', 0);
  1264. $order = OrderService::getById($id);
  1265. OrderService::valid($order, $this->mainId);
  1266. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1267. $reachPeriodId = $order['reachPeriod'];
  1268. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1269. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1270. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1271. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1272. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1273. $bookName = '--';
  1274. $bookMobile = '--';
  1275. }
  1276. $data = [
  1277. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1278. 'orderSn' => $order['orderSn'],
  1279. 'receiveUserName' => $order['receiveUserName'],
  1280. 'receiveMobile' => $order['receiveMobile'],
  1281. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1282. 'cardInfo' => $order['cardInfo'],
  1283. 'bookMobile' => $bookMobile,
  1284. 'bookName' => $bookName,
  1285. 'remark' => $order['remark'],
  1286. 'sendNum' => $order['sendNum'],
  1287. 'telephone' => 18030142050,
  1288. 'printTime' => '',
  1289. 'img' => '',
  1290. ];
  1291. util::success($data);
  1292. }
  1293. //运费计算 lqh 2021.4.12 暂反回固定
  1294. public function actionFreight()
  1295. {
  1296. util::success(['sedCost' => 10]);
  1297. }
  1298. }