OrderController.php 58 KB

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