OrderController.php 49 KB

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