OrderController.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7FjjIGkbwa0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ANBjXqVc/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+f9zaMRazfnw9L8x31pByyMktLs12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uePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+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. WxMessageClass::gatheringIncomeInform($shop, $order);
  425. //新制作单提示
  426. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  427. ShopExtClass::newWorkRemind($shopExt, $order);
  428. util::success($return);
  429. } catch (\Exception $e) {
  430. $transaction->rollBack();
  431. Yii::error("失败原因:" . $e->getMessage());
  432. util::fail('下单失败');
  433. }
  434. }
  435. //下单要用到的相关信息 ssh 2019.12.6
  436. public function actionOrderRelate()
  437. {
  438. $regionTree = RegionService::tree();
  439. $shop = $this->shop->attributes;
  440. $freight = MerchantExtendService::getFreight($this->sjExtend);
  441. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  442. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  443. util::success($out);
  444. }
  445. //余额支付 ssh 2019.12.6
  446. public function actionBalancePay()
  447. {
  448. $post = Yii::$app->request->post();
  449. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  450. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  451. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  452. $order = OrderService::getByOrderSn($orderSn);
  453. $userId = $this->adminId;
  454. $user = UserService::getUserInfo($userId);
  455. //验证支付密码是否正确
  456. UserService::validPayPassword($payPassword, $user);
  457. //支付前验证订单有效性
  458. OrderService::checkBeforePay($order);
  459. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  460. $totalFee = $order['prePrice'];
  461. $sourceType = 0;
  462. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  463. $actPrice = $discountData['price'];
  464. $callbackParams = [];
  465. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  466. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  467. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  468. }
  469. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  470. public function actionWxPay()
  471. {
  472. ini_set('date.timezone', 'Asia/Shanghai');
  473. $post = Yii::$app->request->post();
  474. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  475. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  476. $order = OrderService::getByOrderSn($orderSn);
  477. $orderId = $order['id'];
  478. //验证优惠券是否还有效
  479. if (!empty($couponId)) {
  480. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  481. }
  482. //支付前验证订单有效性
  483. OrderService::checkBeforePay($order);
  484. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  485. $totalFee = $order['realPrice'];
  486. //小程序使用miniOpenId
  487. if (httpUtil::isMiniProgram()) {
  488. $openId = $this->user['miniOpenId'];
  489. } else {
  490. $openId = $this->user['openId'];
  491. }
  492. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  493. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  494. $wxPayType = 0;
  495. if (httpUtil::isMiniProgram()) {
  496. $wxPayType = 1;
  497. }
  498. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  499. //订单30分钟后过期
  500. $now = time();
  501. $expireTime = $now + 1800;
  502. $wx = Yii::getAlias("@vendor/weixin");
  503. require_once($wx . '/lib/WxPay.Api.php');
  504. require_once($wx . '/example/WxPay.JsApiPay.php');
  505. $input = new \WxPayUnifiedOrder();
  506. $input->SetBody($name);
  507. $input->SetOut_trade_no($orderSn);
  508. $input->SetTotal_fee($totalFee * 100);
  509. $input->SetTime_start(date("YmdHis", $now));
  510. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  511. $input->SetTime_expire(date("YmdHis", $expireTime));
  512. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  513. $input->SetTrade_type("JSAPI");
  514. //花卉宝代为申请的微信支付
  515. $merchantExtend = $this->sjExtend->attributes;
  516. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  517. $input->SetSub_openid($openId);
  518. } else {
  519. $input->SetOpenid($openId);
  520. }
  521. //小程序使用miniAppId
  522. if (httpUtil::isMiniProgram()) {
  523. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  524. }
  525. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  526. $tools = new \JsApiPay();
  527. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  528. $newParams = json_decode($jsApiParameters, true);
  529. //已请求微信不能修改价格
  530. $updateData = [];
  531. $updateData['modPrice'] = 0;
  532. if (empty($order['deadline'])) {
  533. $updateData['deadline'] = $expireTime;
  534. }
  535. OrderService::updateById($orderId, $updateData);
  536. util::success($newParams);
  537. }
  538. //快捷付款订单
  539. public function actionFastPay()
  540. {
  541. ini_set('date.timezone', 'Asia/Shanghai');
  542. $post = Yii::$app->request->post();
  543. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  544. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  545. //验证门店是否有效
  546. $shopInfo = ShopService::getById($shopId);
  547. ShopService::valid($shopInfo, $this->sjId);
  548. $post['shopId'] = $shopId;
  549. //默认门店订单
  550. $store = isset($post['store']) ? $post['store'] : 1;
  551. $post['store'] = $store;
  552. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  553. $sourceType = $this->isWx ? 0 : 1;
  554. if (httpUtil::isMiniProgram()) {
  555. $sourceType = 2;
  556. }
  557. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  558. //兼容旧系统sourceType=3表示朋友圈来源
  559. $fromType = $sourceType == 3 ? 2 : 0;
  560. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  561. $post['userId'] = $this->adminId;
  562. $custom = $this->custom;
  563. $post['bookName'] = $custom['userName'] ?? '';
  564. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  565. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  566. $post['bookMobile'] = $bookMobile;
  567. $prePrice = round($post['prePrice'], 2);
  568. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  569. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  570. $actPrice = $discountData['price'];
  571. $post['discountType'] = $discountData['discountType'];
  572. $post['discountAmount'] = $discountData['discountAmount'];
  573. $post['actPrice'] = $actPrice;
  574. $post['realPrice'] = $actPrice;
  575. $post['payStyle'] = 0;
  576. $post['sjId'] = $this->sjId;
  577. $now = time();
  578. $expireTime = $now + 300;//订单5分钟后过期
  579. $post['createTime'] = date("Y-m-d H:i:s", $now);
  580. $post['deadline'] = $expireTime;
  581. if ($actPrice <= 0) {
  582. util::fail('请填写正确的金额');
  583. }
  584. //微信支付订单提交不能修改价格
  585. $post['modPrice'] = $this->isWx ? 0 : 1;
  586. $post['sourceType'] = $sourceType;
  587. $post['goodsNum'] = 1;
  588. $post['fromType'] = $fromType;
  589. $order = OrderService::addOrder($post);
  590. $orderId = $order['id'];
  591. $orderSn = $order['orderSn'];
  592. $sjId = $this->sjId;
  593. if ($payWay == 0) {
  594. $orderId = $order['id'];
  595. $name = '购买商品';
  596. $totalFee = $actPrice;
  597. $user = UserService::getById($this->adminId);
  598. $openId = isset($user['openId']) ? $user['openId'] : 0;
  599. if (httpUtil::isMiniProgram()) {
  600. //小程序使用miniOpenId
  601. $openId = $user['miniOpenId'];
  602. }
  603. if (empty($openId)) {
  604. util::fail('没有找到客户的openId');
  605. }
  606. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  607. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  608. $wxPayType = 0;
  609. if (httpUtil::isMiniProgram()) {
  610. $wxPayType = 1;
  611. }
  612. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  613. $wx = Yii::getAlias("@vendor/weixin");
  614. require_once($wx . '/lib/WxPay.Api.php');
  615. require_once($wx . '/example/WxPay.JsApiPay.php');
  616. $input = new \WxPayUnifiedOrder();
  617. $input->SetBody($name);
  618. $input->SetOut_trade_no($orderSn);
  619. $input->SetTotal_fee($totalFee * 100);
  620. $input->SetTime_start(date("YmdHis", $now));
  621. $input->SetAttach($attach);
  622. //设置订单有效期5分钟
  623. $input->SetTime_expire(date("YmdHis", $expireTime));
  624. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  625. $input->SetTrade_type("JSAPI");
  626. //花卉宝代为申请的微信支付
  627. $merchantExtend = $this->sjExtend->attributes;
  628. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  629. $input->SetSub_openid($openId);
  630. } else {
  631. $input->SetOpenid($openId);
  632. }
  633. //小程序使用miniAppId
  634. if (httpUtil::isMiniProgram()) {
  635. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  636. }
  637. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  638. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  639. $tools = new \JsApiPay();
  640. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  641. $newParams = json_decode($jsApiParameters, true);
  642. $newParams['orderId'] = $orderId;
  643. util::success($newParams);
  644. } elseif ($payWay == 1) {
  645. $extend = MerchantExtendService::getBySjId($sjId);
  646. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  647. util::fail('支付宝付款即将开通...');
  648. }
  649. $config = [
  650. //应用ID,您的APPID。
  651. 'app_id' => $extend['alipayPId'],
  652. //商户私钥,您的原始格式RSA私钥
  653. 'merchant_private_key' => $extend['alipayKey'],
  654. //异步通知地址
  655. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  656. //同步跳转
  657. 'return_url' => "",
  658. //编码格式
  659. 'charset' => "UTF-8",
  660. //签名方式
  661. 'sign_type' => "RSA2",
  662. //支付宝网关
  663. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  664. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  665. 'alipay_public_key' => $extend['alipayPublicKey'],
  666. ];
  667. Yii::info(json_encode($config) . ' aplipay config');
  668. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  669. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  670. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  671. $totalFee = $order['realPrice'];
  672. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  673. $subject = '购买商品';//订单名称,必填
  674. $total_amount = $totalFee;//付款金额,必填
  675. $body = '';//商品描述,可空
  676. $timeout_express = "1m";//超时时间
  677. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  678. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  679. $passBackParams = urlencode($passBackParams);
  680. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  681. $payRequestBuilder->setBody($body);
  682. $payRequestBuilder->setSubject($subject);
  683. $payRequestBuilder->setOutTradeNo($out_trade_no);
  684. $payRequestBuilder->setTotalAmount($total_amount);
  685. $payRequestBuilder->setTimeExpress($timeout_express);
  686. //在异步通知时将该参数原样返回
  687. $payRequestBuilder->setPassbackParams($passBackParams);
  688. //同步通知
  689. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  690. //异步通知
  691. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  692. $payResponse = new \AlipayTradeService($config);
  693. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  694. //直接将支付宝的html返回给前端
  695. echo $result;
  696. } elseif ($payWay == 2) {
  697. //余额支付,验证支付密码是否正确
  698. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  699. $user = UserService::getUserInfo($this->adminId);
  700. UserService::validPayPassword($payPassword, $user);
  701. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  702. $callbackParams = [];
  703. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  704. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  705. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  706. } else {
  707. util::fail('无效的支付方式');
  708. }
  709. }
  710. //订单评价
  711. public function actionComment()
  712. {
  713. $post = Yii::$app->request->post();
  714. $id = $post['id'];
  715. $order = OrderService::getById($id);
  716. OrderService::valid($order, $this->mainId);
  717. if ($order['grade'] > 0) {
  718. util::fail('您已经评过了');
  719. }
  720. OrderService::comment($post);
  721. util::complete('提交成功');
  722. }
  723. //订单详情 ssh 2019.12.16
  724. public function actionDetail()
  725. {
  726. $id = Yii::$app->request->get('id', 0);
  727. $detail = OrderClass::getFullInfo($id);
  728. OrderService::valid($detail, $this->mainId);
  729. util::success($detail);
  730. }
  731. public function actionList()
  732. {
  733. $get = Yii::$app->request->get();
  734. $search = isset($get['search']) ? $get['search'] : '';
  735. $shopId = $this->shopId ?? 0;
  736. if (empty($shopId)) {
  737. //没有登录不显示数据
  738. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  739. }
  740. $where = [];
  741. $where['shopId'] = $shopId;
  742. $status = $get['status'] ?? 0;
  743. if (!empty($status)) {
  744. $where['status'] = $get['status'];
  745. }
  746. if (isset($get['shopAdminId']) && !empty($get['shopAdminId'])) {
  747. $where['shopAdminId'] = $get['shopAdminId'];
  748. }
  749. if (isset($get['searchText']) && !empty($get['searchText'])) {
  750. if (is_numeric($get['searchText'])) {
  751. $where['sendNum'] = $get['searchText'];
  752. } else {
  753. $where['bookName'] = $get['searchText'];
  754. }
  755. }
  756. if (!empty($search)) {
  757. if (stringUtil::isMobile($search)) {
  758. $where['bookMobile'] = $search;
  759. } else {
  760. $where['orderSn'] = $search;
  761. }
  762. }
  763. $searchTime = $get['searchTime'] ?? '';
  764. if (!empty($searchTime)) {
  765. $startTime = $get['startTime'] ?? '';
  766. $endTime = $get['endTime'] ?? '';
  767. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  768. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  769. }
  770. $list = OrderService::getOrderList($where);
  771. $list['shop'] = $this->shop->attributes ?? [];
  772. util::success($list);
  773. }
  774. //订单归类 ssh 2019.12.17
  775. public function actionClassify()
  776. {
  777. $post = Yii::$app->request->post();
  778. $id = isset($post['id']) ? $post['id'] : 0;
  779. $order = OrderService::getById($id);
  780. OrderService::valid($order, $this->mainId);
  781. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  782. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  783. OrderService::classify($id, $categoryId, $usageId);
  784. util::complete();
  785. }
  786. //更新配送单 ssh 2019.12.17
  787. public function actionUpdateSheet()
  788. {
  789. $post = Yii::$app->request->post();
  790. $id = isset($post['id']) ? $post['id'] : 0;
  791. unset($post['id']);
  792. $order = OrderService::getById($id);
  793. OrderService::valid($order, $this->mainId);
  794. OrderService::updateSheet($order, $post);
  795. util::complete('提交成功');
  796. }
  797. //商家收款与发货 ssh 2019.12.18
  798. public function actionGathering()
  799. {
  800. $post = Yii::$app->request->post();
  801. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  802. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  803. $userId = isset($post['userId']) ? $post['userId'] : 0;
  804. $userInfo = UserService::getById($userId);
  805. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  806. util::fail('您选择的客户无效');
  807. }
  808. if ($price <= 0) {
  809. util::fail('请输入金额');
  810. }
  811. $post['prePrice'] = $price;
  812. $post['actPrice'] = $price;
  813. $post['payWay'] = $payWay;
  814. $post['sourceType'] = 1;
  815. $post['userId'] = $userId;
  816. $post['goodsNum'] = 1;
  817. $post['orderName'] = '商品';
  818. $shopId = MerchantService::getDefaultShopId($this->sj);
  819. $post['shopId'] = $shopId;
  820. $order = OrderService::addOrder($post);
  821. $id = $order['id'];
  822. $orderSn = $order['orderSn'];
  823. //流水类型列表
  824. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  825. $callbackParams = [];
  826. switch ($payWay) {
  827. case 0:
  828. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  829. break;
  830. case 1:
  831. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  832. break;
  833. case 2:
  834. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  835. break;
  836. default:
  837. util::fail('无效支付方式');
  838. }
  839. util::success($order);
  840. }
  841. //免发货管理 ssh 2020.1.6
  842. public function actionWithoutSend()
  843. {
  844. $id = Yii::$app->request->get('id');
  845. $order = OrderService::getById($id);
  846. OrderService::valid($order, $this->mainId);
  847. //配送流程变更
  848. $currentFlow = OrderSendClass::withoutSend($order);
  849. util::success(['currentFlow' => $currentFlow]);
  850. }
  851. //修改价格 ssh 2020.1.6
  852. public function actionUpdatePrice()
  853. {
  854. $id = Yii::$app->request->get('id');
  855. $price = Yii::$app->request->get('price', 1);
  856. $order = OrderService::getById($id);
  857. OrderService::valid($order, $this->mainId);
  858. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  859. util::fail('已发起支付,不能修改价格');
  860. }
  861. OrderService::updateById($id, ['actPrice' => $price]);
  862. util::complete('修改成功');
  863. }
  864. //配送单 ssh 2020.2.29
  865. public function actionGetDeliverDetail()
  866. {
  867. $id = Yii::$app->request->get('id', 0);
  868. $order = OrderService::getById($id);
  869. OrderService::valid($order, $this->mainId);
  870. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  871. $reachPeriodId = $order['reachPeriod'];
  872. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  873. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  874. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  875. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  876. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  877. $bookName = '--';
  878. $bookMobile = '--';
  879. }
  880. $data = [
  881. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  882. 'orderSn' => $order['orderSn'],
  883. 'receiveUserName' => $order['receiveUserName'],
  884. 'receiveMobile' => $order['receiveMobile'],
  885. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  886. 'cardInfo' => $order['cardInfo'],
  887. 'bookMobile' => $bookMobile,
  888. 'bookName' => $bookName,
  889. 'remark' => $order['remark'],
  890. 'sendNum' => $order['sendNum'],
  891. 'telephone' => 18030142050,
  892. 'printTime' => '',
  893. 'img' => '',
  894. ];
  895. util::success($data);
  896. }
  897. //运费计算 lqh 2021.4.12 暂反回固定
  898. public function actionFreight()
  899. {
  900. util::success(['sedCost' => 10]);
  901. }
  902. }