OrderController.php 54 KB

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