OrderController.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\express\services\DadaExpressServices;
  4. use bizHd\purchase\classes\PurchaseClass;
  5. use bizHd\purchase\services\PurchaseService;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\services\OrderService;
  10. use bizHd\saas\services\RegionService;
  11. use bizGhs\product\classes\ProductClass;
  12. use common\components\baiduAip;
  13. use common\components\dict;
  14. use common\components\dateUtil;
  15. use common\components\freight;
  16. use common\components\httpUtil;
  17. use common\components\oss;
  18. use common\components\qrCodeUtil;
  19. use Yii;
  20. use common\components\util;
  21. use biz\shop\classes\ShopAdminClass;
  22. class OrderController extends BaseController
  23. {
  24. public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
  25. //支付宝扫码支付 shish 20210103
  26. public function actionScanPayCheck()
  27. {
  28. $get = Yii::$app->request->get();
  29. $id = isset($get['id']) ? $get['id'] : 0;
  30. $order = OrderClass::getById($id, true);
  31. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY && $order->status != OrderClass::ORDER_STATUS_CANCEL) {
  32. util::success(['returnStatus' => 'SUCCESS']);
  33. }
  34. util::success(['returnStatus' => 'FAILURE']);
  35. }
  36. //微信和支付宝扫码支付 shish 20210103
  37. public function actionScanPay()
  38. {
  39. ini_set('date.timezone', 'Asia/Shanghai');
  40. header("Content-type: text/html; charset=utf-8");
  41. $get = Yii::$app->request->get();
  42. $id = isset($get['id']) ? $get['id'] : 0;
  43. $order = OrderClass::getById($id, true);
  44. if (isset($order->status) == false || $order->status != 1) {
  45. util::fail('不是待付款订单');
  46. }
  47. OrderClass::valid($order, $this->shopId);
  48. $cgId = $order->purchaseId ?? 0;
  49. $cg = PurchaseClass::getById($cgId, true);
  50. $orderSn = $cg->orderSn ?? '';
  51. if (empty($orderSn)) {
  52. util::fail('没有找到采购单');
  53. }
  54. $deadline = $cg->deadline;
  55. $current = date("Y-m-d H:i:s");
  56. if ((strtotime($deadline) + 20) < strtotime($current)) {
  57. util::fail('订单已经失效');
  58. }
  59. $totalFee = $cg->actPrice ?? 0;
  60. $outTradeNo = $orderSn;
  61. $subject = '购买商品';
  62. $totalAmount = $totalFee;
  63. $body = "到店订单";
  64. //标记为扫码支付 和 自取订单
  65. $order->codePay = 2;
  66. $order->sendType = OrderClass::SEND_TYPE_NO;
  67. $order->save();
  68. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  69. $cg->codePay = 2;
  70. $cg->save();
  71. $wxPayWay = dict::getDict('payWay', 'wxPay');
  72. $aliPayWay = dict::getDict('payWay', 'alipay');
  73. $payWay = Yii::$app->request->get('payWay', $wxPayWay);
  74. if ($payWay == $wxPayWay) {
  75. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  76. require_once($wx . '/lib/WxPay.Api.php');
  77. require_once($wx . '/example/WxPay.NativePay.php');
  78. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  79. $wxPayType = 0;
  80. if (httpUtil::isMiniProgram()) {
  81. $wxPayType = 1;
  82. }
  83. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  84. $attach = "couponId=0&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  85. $input = new \WxPayUnifiedOrder();
  86. $input->SetBody($subject);
  87. $input->SetOut_trade_no($orderSn);
  88. $input->SetTotal_fee($totalFee * 100);
  89. $input->SetAttach($attach);
  90. $input->SetTime_start(date("YmdHis"));
  91. $input->SetTime_expire(date("YmdHis", time() + 600));
  92. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  93. $input->SetTrade_type("NATIVE");
  94. //native支付必传
  95. $input->SetProduct_id("hdCG_" . $cgId);
  96. //获取微信商户号等信息
  97. $sjExtend = WxOpenClass::getWxInfo();
  98. $notify = new \NativePay();
  99. $result = $notify->GetPayUrl($input, $sjExtend);
  100. $url = $result["code_url"] ?? '';
  101. if (empty($url)) {
  102. util::fail('二维码生成失败');
  103. }
  104. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  105. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  106. util::success(['url' => $payUrl]);
  107. }
  108. if ($payWay == $aliPayWay) {
  109. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  110. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  111. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  112. $aliPId = '2017041906821571';
  113. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDqI9jWHE1ZhkGmB1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s+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  115. $config = array(
  116. //签名方式,默认为RSA2(RSA2048)
  117. 'sign_type' => "RSA2",
  118. //支付宝公钥
  119. 'alipay_public_key' => $aliPublicKey,
  120. //商户私钥
  121. 'merchant_private_key' => $aliKey,
  122. //编码格式
  123. 'charset' => "UTF-8",
  124. //支付宝网关
  125. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  126. //应用ID
  127. 'app_id' => $aliPId,
  128. //异步通知地址,只有扫码支付预下单可用
  129. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  130. //最大查询重试次数
  131. 'MaxQueryRetry' => "10",
  132. //查询间隔
  133. 'QueryDuration' => "3"
  134. );
  135. // 支付超时,线下扫码交易定义为5分钟
  136. $timeExpress = "5m";
  137. // 创建请求builder,设置请求参数
  138. $qrPayRequestBuilder = new \AlipayTradePayContentBuilder();
  139. $qrPayRequestBuilder->setOutTradeNo($outTradeNo);
  140. $qrPayRequestBuilder->setTotalAmount($totalAmount);
  141. $qrPayRequestBuilder->setTimeExpress($timeExpress);
  142. $qrPayRequestBuilder->setSubject($subject);
  143. $qrPayRequestBuilder->setBody($body);
  144. // 调用barPay方法获取当面付应答
  145. $qrPay = new \AlipayTradeService($config);
  146. $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);
  147. if ($qrPayResult->getTradeStatus() == 'SUCCESS') {
  148. $respond = $qrPayResult->getResponse();
  149. $url = $respond->qr_code ?? '';
  150. if (empty($url)) {
  151. util::fail('二维码生成失败');
  152. }
  153. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  154. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  155. util::success(['url' => $payUrl]);
  156. }
  157. util::fail('二维码生成失败..');
  158. }
  159. util::fail('不支持的收款方式');
  160. }
  161. //微信和支付宝付款码支付复查 shish 20211231
  162. public function actionCodePayCheck()
  163. {
  164. ini_set('date.timezone', 'Asia/Shanghai');
  165. header("Content-type: text/html; charset=utf-8");
  166. $get = Yii::$app->request->get();
  167. $id = isset($get['id']) ? $get['id'] : 0;
  168. $order = OrderClass::getById($id, true);
  169. if (empty($order)) {
  170. util::complete('没有找到订单');
  171. }
  172. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  173. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  174. util::complete('订单已付款');
  175. }
  176. util::complete('订单不是待付款状态');
  177. }
  178. OrderClass::valid($order, $this->shopId);
  179. $cgId = $order->purchaseId ?? 0;
  180. $cg = PurchaseClass::getById($cgId, true);
  181. $orderSn = $cg->orderSn ?? '';
  182. if (empty($orderSn)) {
  183. util::complete('没有找到采购单');
  184. }
  185. $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
  186. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  187. //微信付款
  188. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  189. require_once($wx . '/lib/WxPay.Api.php');
  190. require_once($wx . '/example/WxPay.MicroPay.php');
  191. //获取微信商户号等信息
  192. $sjExtend = WxOpenClass::getWxInfo();
  193. $microPay = new \MicroPay();
  194. $payReturn = $microPay->query($orderSn, $sjExtend);
  195. if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
  196. if ($payReturn["trade_state"] == "SUCCESS") {
  197. //支付成功
  198. $transactionId = $payReturn['transaction_id'] ?? '';
  199. $openId = $payReturn['openid'] ?? '';
  200. $aliUserId = '';
  201. PurchaseService::codePay($payWay, $orderSn, $transactionId, $openId, $aliUserId);
  202. util::success(['returnStatus' => 'SUCCESS']);
  203. } else if ($payReturn["trade_state"] == "USERPAYING") {
  204. //用户支付中
  205. util::complete('正在付款,请稍候..');
  206. }
  207. }
  208. if ($payReturn["err_code"] == "ORDERNOTEXIST") {
  209. //交易订单号不存在
  210. util::complete('订单没有付款');
  211. } else {
  212. util::complete('系统错误,请稍候再试');
  213. }
  214. } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
  215. //支付宝付款
  216. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  217. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  218. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  219. $aliPId = '2017041906821571';
  220. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFz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c34gvbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  222. $config = array(
  223. //签名方式,默认为RSA2(RSA2048)
  224. 'sign_type' => "RSA2",
  225. //支付宝公钥
  226. 'alipay_public_key' => $aliPublicKey,
  227. //商户私钥
  228. 'merchant_private_key' => $aliKey,
  229. //编码格式
  230. 'charset' => "UTF-8",
  231. //支付宝网关
  232. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  233. //应用ID
  234. 'app_id' => $aliPId,
  235. //异步通知地址,只有扫码支付预下单可用
  236. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  237. //最大查询重试次数
  238. 'MaxQueryRetry' => "10",
  239. //查询间隔
  240. 'QueryDuration' => "3"
  241. );
  242. //构造查询业务请求参数对象
  243. $queryContentBuilder = new \AlipayTradeQueryContentBuilder();
  244. $queryContentBuilder->setOutTradeNo($orderSn);
  245. //初始化类对象,调用queryTradeResult方法获取查询应答
  246. $queryResponse = new \AlipayTradeService($config);
  247. $queryResult = $queryResponse->queryTradeResult($queryContentBuilder);
  248. if ($queryResult->getTradeStatus() == 'SUCCESS') {
  249. $respond = $queryResult->getResponse();
  250. $openId = '';
  251. $aliUserId = $respond->buyer_user_id ?? '';
  252. $transactionId = $respond->trade_no ?? '';
  253. PurchaseService::codePay($payWay, $orderSn, $transactionId, $openId, $aliUserId);
  254. util::success(['returnStatus' => 'SUCCESS']);
  255. }
  256. } else {
  257. util::fail('还没有付款哦');
  258. }
  259. }
  260. //微信和支付宝付款码支付 shish 2021.4.11
  261. public function actionCodePay()
  262. {
  263. ini_set('date.timezone', 'Asia/Shanghai');
  264. header("Content-type: text/html; charset=utf-8");
  265. $get = Yii::$app->request->get();
  266. $id = isset($get['id']) ? $get['id'] : 0;
  267. $order = OrderClass::getById($id, true);
  268. if (isset($order->status) == false || $order->status != 1) {
  269. util::fail('不是待付款订单');
  270. }
  271. OrderClass::valid($order, $this->shopId);
  272. $cgId = $order->purchaseId ?? 0;
  273. $cg = PurchaseClass::getById($cgId, true);
  274. $orderSn = $cg->orderSn ?? '';
  275. if (empty($orderSn)) {
  276. util::fail('没有找到采购单');
  277. }
  278. //付款码
  279. $authCode = (string)$get['authCode'] ?? '';
  280. if (empty($authCode)) {
  281. util::fail('支付失败');
  282. }
  283. $wxPrefix = ['10', '11', '12', '13', '14', '15'];
  284. $isWx = false;
  285. foreach ($wxPrefix as $wxId) {
  286. if (strpos($authCode, $wxId) === 0) {
  287. $isWx = true;
  288. }
  289. }
  290. $deadline = $cg->deadline;
  291. $current = date("Y-m-d H:i:s");
  292. if ((strtotime($deadline) + 20) < strtotime($current)) {
  293. util::fail('订单已经失效');
  294. }
  295. $totalFee = $cg->actPrice ?? 0;
  296. $outTradeNo = $orderSn;
  297. $subject = '购买商品';
  298. $totalAmount = $totalFee;
  299. $body = "门店";
  300. if ($isWx) {
  301. $order->payWay = dict::getDict('payWay', 'wxPay');
  302. $order->save();
  303. $cg->payWay = dict::getDict('payWay', 'wxPay');
  304. $cg->save();
  305. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  306. require_once($wx . '/lib/WxPay.Api.php');
  307. require_once($wx . '/example/WxPay.MicroPay.php');
  308. $input = new \WxPayMicroPay();
  309. $input->SetAuth_code($authCode);
  310. $input->SetBody($subject);
  311. $input->SetTotal_fee($totalFee * 100);
  312. $input->SetOut_trade_no($orderSn);
  313. //获取微信商户号等信息
  314. $sjExtend = WxOpenClass::getWxInfo();
  315. $microPay = new \MicroPay();
  316. $res = $microPay->pay($input, $sjExtend);
  317. if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS" && isset($res["result_code"]) && $res["result_code"] == 'SUCCESS') {
  318. $transactionId = $res['transaction_id'] ?? '';
  319. $openId = $res['openid'] ?? '';
  320. $aliUserId = '';
  321. $payWay = dict::getDict('payWay', 'wxPay');
  322. PurchaseService::codePay($payWay, $orderSn, $transactionId, $openId, $aliUserId);
  323. util::success(['returnStatus' => 'SUCCESS']);
  324. }
  325. util::success(['returnStatus' => 'FAILURE']);
  326. } else {
  327. $aliPayWay = dict::getDict('payWay', 'alipay');
  328. $order->payWay = $aliPayWay;
  329. $order->save();
  330. $cg->payWay = $aliPayWay;
  331. $cg->save();
  332. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  333. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  334. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  335. $aliPId = '2017041906821571';
  336. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12E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xFVwSZ7g==';
  337. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsz3m9juAR1xew4DY6c34gvbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  338. $config = array(
  339. //签名方式,默认为RSA2(RSA2048)
  340. 'sign_type' => "RSA2",
  341. //支付宝公钥
  342. 'alipay_public_key' => $aliPublicKey,
  343. //商户私钥
  344. 'merchant_private_key' => $aliKey,
  345. //编码格式
  346. 'charset' => "UTF-8",
  347. //支付宝网关
  348. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  349. //应用ID
  350. 'app_id' => $aliPId,
  351. //异步通知地址,只有扫码支付预下单可用
  352. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  353. //最大查询重试次数
  354. 'MaxQueryRetry' => "10",
  355. //查询间隔
  356. 'QueryDuration' => "3"
  357. );
  358. // 支付超时,线下扫码交易定义为5分钟
  359. $timeExpress = "5m";
  360. // 创建请求builder,设置请求参数
  361. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  362. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  363. $barPayRequestBuilder->setTotalAmount($totalAmount);
  364. $barPayRequestBuilder->setAuthCode($authCode);
  365. $barPayRequestBuilder->setTimeExpress($timeExpress);
  366. $barPayRequestBuilder->setSubject($subject);
  367. $barPayRequestBuilder->setBody($body);
  368. // 调用barPay方法获取当面付应答
  369. $barPay = new \AlipayTradeService($config);
  370. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  371. if ($barPayResult->getTradeStatus() == 'SUCCESS') {
  372. $respond = $barPayResult->getResponse();
  373. $openId = '';
  374. $aliUserId = $respond->buyer_user_id ?? '';
  375. $transactionId = $respond->trade_no ?? '';
  376. PurchaseService::codePay($aliPayWay, $orderSn, $transactionId, $openId, $aliUserId);
  377. util::success(['returnStatus' => 'SUCCESS']);
  378. }
  379. util::success(['returnStatus' => 'FAILURE']);
  380. }
  381. }
  382. //订单列表 ssh 2021.1.20
  383. public function actionList()
  384. {
  385. $get = Yii::$app->request->get();
  386. $status = $get['status'] ?? 0;
  387. if (!empty($status)) {
  388. $where['status'] = $status;
  389. }
  390. $where['sjId'] = $this->sjId;
  391. if (isset($get['shopId'])) {
  392. $shopId = $get['shopId'] ?? 0;
  393. if (!empty($shopId)) {
  394. $where['shopId'] = $this->shopId;
  395. }
  396. } else {
  397. $where['shopId'] = $this->shopId;
  398. }
  399. $searchTime = $get['searchTime'] ?? '';
  400. if (!empty($searchTime)) {
  401. $startTime = $get['startTime'] ?? '';
  402. $endTime = $get['endTime'] ?? '';
  403. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  404. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  405. }
  406. $name = $get['name'] ?? '';
  407. if (!empty($name)) {
  408. $searchType = $get['searchType'] ?? 0;
  409. if (empty($searchType)) {
  410. //订单编号
  411. if (strpos($name, 'XSD') === 0) {
  412. $where['orderSn'] = $name;
  413. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  414. $where['customNamePy'] = ['like', $name];
  415. } else {
  416. $where['customName'] = ['like', $name];
  417. }
  418. } elseif ($searchType == 1) {
  419. $where['sendNum'] = $name;
  420. } else {
  421. }
  422. }
  423. $respond = OrderClass::getOrderList($where);
  424. $respond['shop'] = $this->shop;
  425. $respond['shopExt'] = $this->shopExt;
  426. util::success($respond);
  427. }
  428. //商城下单操作 ssh 2021.1.14
  429. public function actionCreateOrder()
  430. {
  431. $post = Yii::$app->request->post();
  432. if (isset($post['newVersion']) == false) {
  433. util::fail('请升级小程序');
  434. }
  435. unset($post['newVersion']);
  436. $shopId = $this->shopId;
  437. $customId = $post['customId'] ?? 0;
  438. $post['customId'] = $customId;
  439. //开单必须选客户
  440. if (empty($customId)) {
  441. util::fail('请选择客户');
  442. }
  443. $custom = CustomClass::getCustom($customId);
  444. if (empty($custom)) {
  445. util::fail('请选客户哦');
  446. }
  447. CustomClass::valid($custom, $this->shopId);
  448. $post['ghsId'] = $custom['ghsId'] ?? 0;
  449. $post['customMobile'] = $custom['mobile'] ?? '';
  450. $customName = $custom['name'] ?? '';
  451. $post['customName'] = $customName;
  452. $post['customNamePy'] = $custom['py'] ?? '';
  453. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  454. $post['shopAdminId'] = $this->shopAdminId;
  455. $post['province'] = $custom['province'] ?? '';
  456. $post['city'] = $custom['city'] ?? '';
  457. $post['dist'] = $custom['dist'] ?? '';
  458. $post['address'] = $custom['address'] ?? '';
  459. $post['floor'] = $custom['floor'] ?? '';
  460. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  461. $post['showAddress'] = $custom['showAddress'] ?? '';
  462. $post['long'] = $custom['long'] ?? '';
  463. $post['lat'] = $custom['lat'] ?? '';
  464. $shopAdmin = $this->shopAdmin->attributes;
  465. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  466. $post['sjId'] = $this->sjId;
  467. $post['shopId'] = $shopId;
  468. $post['fromType'] = 1;
  469. $post['expressId'] = $post['expressId'] ?? 0;
  470. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  471. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  472. $post['debt'] = OrderClass::DEBT_NO;
  473. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  474. $post['debt'] = OrderClass::DEBT_YES;
  475. }
  476. $shopAdmin = $this->shopAdmin;
  477. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  478. if ($hasPay == dict::getDict('hasPay', 'payed')) {
  479. util::fail('没有权限');
  480. }
  481. }
  482. $productJson = $post['product'] ?? '';
  483. if (empty($productJson)) {
  484. util::fail('请选择花材');
  485. }
  486. $productList = json_decode($productJson, true);
  487. if (empty($productList)) {
  488. util::fail('请选择花材');
  489. }
  490. $connection = Yii::$app->db;//事务处理
  491. $transaction = $connection->beginTransaction();
  492. try {
  493. //判断花材有效性
  494. ProductClass::valid($productList, $this->shopId);
  495. $post['product'] = $productList;
  496. //商家开单订单有效期更长(30)天
  497. $validTime = dict::getDict('kd_order_valid');
  498. $current = time() + $validTime;
  499. $post['deadline'] = date("Y-m-d H:i:s", $current);
  500. $return = OrderService::createOrder($post, $custom, $hasPay);
  501. $transaction->commit();
  502. util::success($return);
  503. } catch (\Exception $e) {
  504. $transaction->rollBack();
  505. Yii::error("下单失败原因:" . $e->getMessage());
  506. util::fail('下单失败');
  507. }
  508. }
  509. //修改订单 ssh 20210810
  510. public function actionUpdateOrder()
  511. {
  512. $post = Yii::$app->request->post();
  513. if (isset($post['newVersion']) == false) {
  514. util::fail('请升级小程序');
  515. }
  516. unset($post['newVersion']);
  517. //PC端开单
  518. if (isset($post['clearType'])) {
  519. $clearType = $post['clearType'];
  520. if ($clearType == OrderClass::CLEAR_DEBT) {
  521. $post['debt'] = OrderClass::DEBT_YES;
  522. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  523. } else {
  524. $post['debt'] = OrderClass::DEBT_NO;
  525. }
  526. }
  527. //员工不能开已收款订单
  528. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  529. if ($debt == OrderClass::DEBT_NO) {
  530. $shopAdmin = $this->shopAdmin;
  531. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  532. util::fail('不能开已收款订单');
  533. }
  534. }
  535. //PC端开单
  536. if (isset($post['getType'])) {
  537. util::fail('暂不支持PC端修改订单');
  538. $getType = $post['getType'];
  539. //自取
  540. if ($getType == 1) {
  541. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  542. } else {
  543. //选择配送时,在发货时让商家自己再确认一下用什么方式
  544. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  545. }
  546. }
  547. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  548. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  549. $post['debt'] = OrderClass::DEBT_NO;
  550. }
  551. $id = $post['id'] ?? 0;
  552. $order = OrderClass::getLockById($id);
  553. if (empty($order)) {
  554. util::fail('没有找到订单');
  555. }
  556. OrderClass::valid($order, $this->shopId);
  557. unset($post['id']);
  558. $shopAdmin = $this->shopAdmin;
  559. $post['shopAdminId'] = $this->shopAdminId;
  560. $post['shopAdminName'] = $shopAdmin->name ?? '';
  561. $post['fromType'] = 1;
  562. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  563. $productJson = $post['product'] ?? '';
  564. if (empty($productJson)) {
  565. util::fail('请选择花材');
  566. }
  567. $productList = json_decode($productJson, true);
  568. if (empty($productList)) {
  569. util::fail('请选择花材');
  570. }
  571. $connection = Yii::$app->db;//事务处理
  572. $transaction = $connection->beginTransaction();
  573. try {
  574. //判断花材有效性
  575. ProductClass::valid($productList, $this->shopId);
  576. $post['product'] = $productList;
  577. //商家开单订单有效期更长(30)天
  578. $validTime = dict::getDict('kd_order_valid');
  579. $current = time() + $validTime;
  580. $post['deadline'] = date("Y-m-d H:i:s", $current);
  581. $upData = [
  582. 'payWay' => $post['payWay'],
  583. 'sendType' => $post['sendType'],
  584. 'debt' => $post['debt'],
  585. 'needPrint' => $post['needPrint'],
  586. 'modifyPrice' => $post['modifyPrice'],
  587. 'shopAdminId' => $post['shopAdminId'],
  588. 'shopAdminName' => $post['shopAdminName'],
  589. 'fromType' => 1,
  590. 'sendCost' => $post['sendCost'],
  591. 'product' => $post['product'],
  592. 'deadline' => $post['deadline'],
  593. 'debt' => $post['debt'],
  594. ];
  595. $return = OrderService::updateOrder($order, $upData);
  596. $transaction->commit();
  597. util::success($return);
  598. } catch (\Exception $e) {
  599. $transaction->rollBack();
  600. Yii::error("下单失败原因:" . $e->getMessage());
  601. util::fail('下单失败');
  602. }
  603. }
  604. //延期支付 ssh 2021.1.19
  605. public function actionDebt()
  606. {
  607. $get = Yii::$app->request->get();
  608. $id = $get['id'] ?? 0;
  609. $info = OrderService::getOrderInfo($id);
  610. OrderClass::valid($info, $this->shopId);
  611. OrderClass::debt($info, $this->shop);
  612. util::complete();
  613. }
  614. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  615. public function actionWxPay()
  616. {
  617. ini_set('date.timezone', 'Asia/Shanghai');
  618. $post = Yii::$app->request->post();
  619. $couponId = $post['couponId'] ?? 0;
  620. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  621. $order = OrderClass::getByOrderSn($orderSn);
  622. if (empty($order)) {
  623. util::fail('订单号无效');
  624. }
  625. $id = $order['id'];
  626. //支付前验证订单有效性
  627. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  628. util::fail('没有权限操作');
  629. }
  630. $name = $order['orderName'] ?? '购买商品';
  631. $totalFee = $order['actPrice'];
  632. //强制使用小程序的miniOpenId
  633. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  634. if (empty($openId)) {
  635. util::fail('没有找到openId');
  636. }
  637. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  638. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  639. $wxPayType = 0;
  640. if (httpUtil::isMiniProgram()) {
  641. $wxPayType = 1;
  642. }
  643. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  644. //订单30分钟后过期
  645. $now = time();
  646. $expireTime = $now + 1800;
  647. $wx = Yii::getAlias("@vendor/weixin");
  648. require_once($wx . '/lib/WxPay.Api.php');
  649. require_once($wx . '/example/WxPay.JsApiPay.php');
  650. $input = new \WxPayUnifiedOrder();
  651. $input->SetBody($name);
  652. $input->SetOut_trade_no($orderSn);
  653. $input->SetTotal_fee($totalFee * 100);
  654. $input->SetTime_start(date("YmdHis", $now));
  655. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  656. $input->SetTime_expire(date("YmdHis", $expireTime));
  657. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  658. $input->SetTrade_type("JSAPI");
  659. //花卉宝代为申请的微信支付
  660. //$sjExtend = $this->sjExtend->attributes;
  661. $sjExtend = WxOpenClass::getGhsWxInfo();
  662. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  663. $input->SetSub_openid($openId);
  664. } else {
  665. $input->SetOpenid($openId);
  666. }
  667. //强制使用小程序的miniAppId
  668. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  669. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  670. $tools = new \JsApiPay();
  671. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  672. $newParams = json_decode($jsApiParameters, true);
  673. //微信不能修改价格
  674. $current = date("Y-m-d H:i:s");
  675. $updateData = ['deadline' => $current, 'modPrice' => 0];
  676. OrderClass::updateById($id, $updateData);
  677. util::success($newParams);
  678. }
  679. //获取订单详情 ssh 2021.1.21
  680. public function actionDetail()
  681. {
  682. $get = Yii::$app->request->get();
  683. $id = $get['id'] ?? 0;
  684. $info = OrderService::getOrderInfo($id, true, true, true, false);
  685. OrderClass::valid($info, $this->shopId);
  686. //是否有云打印判断
  687. $hasCloudPrint = 0;
  688. $shopExt = $this->shopExt;
  689. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  690. $hasCloudPrint = 1;
  691. }
  692. $info['hasCloudPrint'] = $hasCloudPrint;
  693. //店长信息
  694. $customShopId = $info['customShopId'] ?? 0;
  695. $superInfo = ShopAdminClass::getSuper($customShopId);
  696. $info['customSuper'] = $superInfo;
  697. $purchaseId = $info['purchaseId'] ?? 0;
  698. $cg = PurchaseClass::getById($purchaseId);
  699. $info['cgInfo'] = $cg;
  700. util::success($info);
  701. }
  702. //自取免发货流程处理 ssh 2021.1.22
  703. public function actionWithoutSend()
  704. {
  705. $get = Yii::$app->request->get();
  706. $id = $get['id'] ?? 0;
  707. $info = OrderService::getById($id, true);
  708. OrderClass::valid($info, $this->shopId);
  709. $purchaseId = $info->purchaseId;
  710. $purchase = PurchaseClass::getById($purchaseId, true);
  711. OrderService::withoutSend($info, $purchase);
  712. util::complete();
  713. }
  714. //本店送 ssh 2021.1.24
  715. public function actionSelfSend()
  716. {
  717. $get = Yii::$app->request->get();
  718. $id = isset($get['id']) ? $get['id'] : 0;
  719. $info = OrderClass::getById($id, true);
  720. OrderClass::valid($info->attributes, $this->shopId);
  721. $connection = Yii::$app->db;
  722. $transaction = $connection->beginTransaction();
  723. try {
  724. OrderClass::selfSend($info);
  725. $transaction->commit();
  726. } catch (\Exception $exception) {
  727. $transaction->rollBack();
  728. Yii::info("本店送操作报错:" . $exception->getMessage());
  729. util::fail('操作失败');
  730. }
  731. util::complete();
  732. }
  733. //运费计算 ssh 2021.1.24
  734. public function actionFreight()
  735. {
  736. //2021.3.28 接入达达
  737. $get = Yii::$app->request->get();
  738. $post = Yii::$app->request->post();
  739. if (empty($get)) {
  740. $get = $post;
  741. }
  742. $orderId = $get['id'] ?? 0; //订单ID
  743. $customId = $get['customId'] ?? 0;
  744. if (empty($orderId)) {
  745. //自定义运费
  746. //freight::getCost();
  747. if (empty($customId)) {
  748. util::fail("请选择客户");
  749. }
  750. $customInfo = CustomClass::getById($customId);
  751. if (empty($customInfo)) {
  752. util::fail('没有找到客户');
  753. }
  754. $shop = $this->shop;
  755. //花材信息
  756. $productJson = $get['product'] ?? '';
  757. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  758. if (empty($productJson)) {
  759. util::fail('请选择花材');
  760. }
  761. $productList = json_decode($productJson, true);
  762. if (empty($productList)) {
  763. util::fail('请选择花材');
  764. }
  765. $productList = ProductClass::mergeItemInfo($productList);
  766. $weight = 0;
  767. $price = 0;
  768. $productData = ProductClass::getProductMapData($productList);
  769. $bigNum = 0;
  770. foreach ($productList as $key => $val) {
  771. $productId = $val['productId'];
  772. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  773. $bigNum += $val['bigNum'];
  774. $weight = bcadd($w, $weight, 2);
  775. $ratio = $productData[$productId]['ratio'] ?? 0;
  776. //大小数量合并多少扎
  777. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  778. //售卖价格
  779. $itemPrice = $productData[$productId]['price'] ?? 0;
  780. //合计价格
  781. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  782. }
  783. //没有经纬度的客户运费直接返回空 shish 20210612
  784. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  785. util::success(['sedCost' => '']);
  786. }
  787. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  788. util::success(['sedCost' => $cost]);
  789. }
  790. $province = $get['province'] ?? '';
  791. $city = $get['city'] ?? '';
  792. $address = $get['address'] ?? '';
  793. $floor = $get['floor'] ?? '';
  794. $customName = $get['customName'] ?? '';
  795. $customMobile = $get['customMobile'] ?? '';
  796. $fullAddress = $province . $city . $address . $floor;
  797. $sendTime = $get['sendTime'] ?? '';
  798. $lat = $get['lat'] ?? '';
  799. $lng = $get['long'] ?? '';
  800. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  801. util::fail('请填写客户地址');
  802. }
  803. ini_set('date.timezone', 'Asia/Shanghai');
  804. if (!empty($sendTime)) {
  805. //配送时间不为空
  806. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  807. $sendTimeInt = strtotime($sendTime);
  808. $now = time();
  809. $diff = $sendTimeInt - $now;
  810. //因服务器写入时间,改为至少提前6分钟
  811. if ($diff <= 360) {
  812. util::fail('配送时间至少提前6分钟');
  813. }
  814. } else {
  815. //默认立即发送
  816. $sendTime = 0;
  817. }
  818. //更新订单表
  819. $info = OrderService::getOrderInfo($orderId);
  820. OrderClass::valid($info, $this->shopId);
  821. if ($sendTime) {
  822. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  823. } else {
  824. $sendTime = '0000-00-00 00:00:00';
  825. }
  826. $expressAddInfo = [
  827. 'customName' => $customName,
  828. 'customMobile' => $customMobile,
  829. 'province' => $province,
  830. 'city' => $city,
  831. 'address' => $address,
  832. 'floor' => $floor,
  833. 'lat' => $lat,
  834. 'long' => $lng,
  835. 'sendTime' => $sendTime,
  836. ];
  837. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  838. //计算运费
  839. $info['province'] = $province;
  840. $info['city'] = $city;
  841. $info['address'] = $address;
  842. $info['floor'] = $floor;
  843. $info['fullAddress'] = $fullAddress;
  844. $info['lat'] = $lat;
  845. $info['long'] = $lng;
  846. $info['sendTime'] = $expressAddInfo['sendTime'];
  847. $res = DadaExpressServices::queryDeliverFee($info);
  848. util::success(['sedCost' => $res['fee']]);
  849. }
  850. //发快递和第三方配送 ssh 2021.1.24
  851. public function actionThirdSend()
  852. {
  853. $get = Yii::$app->request->get();
  854. $id = isset($get['id']) ? $get['id'] : 0;
  855. $info = OrderService::getById($id, true);
  856. OrderClass::valid($info->attributes, $this->shopId);
  857. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  858. util::fail('请填写客户地址');
  859. }
  860. $connection = Yii::$app->db;
  861. $transaction = $connection->beginTransaction();
  862. try {
  863. $respond = OrderClass::thirdSend($info, $get);
  864. $transaction->commit();
  865. util::success($respond);
  866. } catch (\Exception $exception) {
  867. $transaction->rollBack();
  868. Yii::info("发快递操作报错:" . $exception->getMessage());
  869. util::fail('操作失败' . $exception->getMessage());
  870. }
  871. }
  872. //确认送达 ssh 2021.1.24
  873. public function actionReach()
  874. {
  875. $get = Yii::$app->request->get();
  876. $id = isset($get['id']) ? $get['id'] : 0;
  877. $info = OrderClass::getById($id, true);
  878. OrderClass::valid($info->attributes, $this->shopId);
  879. $connection = Yii::$app->db;
  880. $transaction = $connection->beginTransaction();
  881. try {
  882. OrderService::reach($info);
  883. $transaction->commit();
  884. } catch (\Exception $exception) {
  885. $transaction->rollBack();
  886. Yii::info("送达操作报错:" . $exception->getMessage());
  887. util::fail('操作失败');
  888. }
  889. util::complete();
  890. }
  891. //下单要用到的相关信息 ssh 2019.12.6
  892. public function actionOrderRelate()
  893. {
  894. $regionTree = RegionService::tree();
  895. $shop = $this->shop->attributes;
  896. $freight = ['first' => 5, 'add' => 2];
  897. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  898. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  899. util::success($out);
  900. }
  901. //客户欠款的订单 ssh 2021.2.4
  902. public function actionDebtList()
  903. {
  904. $id = Yii::$app->request->get('id', 0);
  905. $info = CustomClass::getCustom($id);
  906. CustomClass::valid($info, $this->shopId);
  907. $where = ['customId' => $id, 'debt' => 1];
  908. $respond = OrderService::getDebtOrderList($where);
  909. $respond['customInfo'] = $info;
  910. util::success($respond);
  911. }
  912. //更新打印次数
  913. public function actionAddPrintNum()
  914. {
  915. $id = Yii::$app->request->get('id', 0);
  916. $order = OrderClass::getById($id, true);
  917. OrderClass::valid($order, $this->shopId);
  918. $order->printNum += 1;
  919. $order->save();
  920. util::complete();
  921. }
  922. //打印订单 shish 20210714
  923. public function actionCloudPrintOrder()
  924. {
  925. $id = Yii::$app->request->get('id', 0);
  926. $order = OrderClass::getById($id, true);
  927. OrderClass::valid($order, $this->shopId);
  928. $ext = $this->shopExt;
  929. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  930. util::success(['hasNoPrint' => 1]);
  931. }
  932. OrderClass::onlinePrint($order);
  933. $order->printNum += 1;
  934. $order->save();
  935. util::complete();
  936. }
  937. //订单确认完成 shish 20210809
  938. public function actionConfirmFinish()
  939. {
  940. $payWay = Yii::$app->request->get('payWay', -1);
  941. $id = Yii::$app->request->get('id', 0);
  942. $clearType = Yii::$app->request->get('clearType', 0);
  943. $connection = Yii::$app->db;
  944. $transaction = $connection->beginTransaction();
  945. try {
  946. $order = OrderClass::getById($id, true);
  947. OrderClass::valid($order, $this->shopId);
  948. $purchaseId = $order->purchaseId ?? 0;
  949. $purchase = PurchaseClass::getById($purchaseId, true);
  950. if (empty($purchase)) {
  951. util::fail('没有采购信息');
  952. }
  953. if ($clearType == 1) {
  954. //欠款
  955. $payWay = dict::getDict('payWay', 'debtPay');
  956. } elseif ($clearType == 2) {
  957. //已收款
  958. if ($payWay == -1) {
  959. util::fail('请选择收款方式');
  960. }
  961. $shopAdmin = $this->shopAdmin;
  962. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  963. util::fail('超管才能发起已收款');
  964. }
  965. } else {
  966. util::fail('请选择结算方式');
  967. }
  968. //不需要打印
  969. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  970. $order->save();
  971. //支付
  972. PurchaseService::payAfter($purchase, $payWay);
  973. OrderService::payAfter($order, $payWay);
  974. //自己送
  975. $info = OrderClass::getById($id, true);
  976. OrderClass::selfSend($info);
  977. //确认送达,并且不需要微信通知
  978. Yii::$app->params['noNeedWxNotice'] = 1;
  979. $info = OrderClass::getById($id, true);
  980. OrderService::reach($info);
  981. $transaction->commit();
  982. util::complete('操作成功');
  983. } catch (\Exception $exception) {
  984. $transaction->rollBack();
  985. Yii::info("确认完成订单报错:" . $exception->getMessage());
  986. util::fail('操作失败');
  987. }
  988. }
  989. //取消订单 shish 20210810
  990. public function actionCancel()
  991. {
  992. $shopAdmin = $this->shopAdmin;
  993. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  994. util::fail('超管才能取消订单');
  995. }
  996. $id = Yii::$app->request->get('id', 0);
  997. $order = OrderClass::getLockById($id);
  998. OrderClass::valid($order, $this->shopId);
  999. OrderService::expire($order, true);
  1000. util::complete();
  1001. }
  1002. //获取收款语音播报地址 shish 20211231
  1003. public function actionGetPayVoice()
  1004. {
  1005. $id = Yii::$app->request->get('id');
  1006. $order = OrderClass::getById($id, true);
  1007. $payWay = $order->payWay ?? 0;
  1008. $money = floatval($order->actPrice);
  1009. $payment = $payWay == dict::getDict('payWay', 'alipay') ? '支付宝' : '微信';
  1010. $msg = $payment . '收款' . $money . '元';
  1011. $audio = baiduAip::transfer($msg, 4, 0);
  1012. //上传到oss
  1013. $file = time() . rand(10000, 999999) . '.mp3';
  1014. $filePath = 'tmp/arrival/money/' . $file;
  1015. oss::uploadTmpAudio($filePath, $audio);
  1016. $url = Yii::$app->params['ghsImgHost'] . $filePath;
  1017. util::success(['url' => $url]);
  1018. }
  1019. }