OrderController.php 53 KB

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