OrderController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\sj\services\MerchantExtendService;
  4. use bizGhs\express\services\DadaExpressServices;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizGhs\admin\classes\AdminClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\merchant\services\ShopService;
  9. use bizGhs\order\classes\OrderClass;
  10. use bizGhs\order\services\OrderService;
  11. use bizHd\saas\services\RegionService;
  12. use bizGhs\product\classes\ProductClass;
  13. use common\components\dict;
  14. use common\components\dateUtil;
  15. use common\components\httpUtil;
  16. use common\services\xhPayToolService;
  17. use Yii;
  18. use common\components\util;
  19. use common\components\stringUtil;
  20. class OrderController extends BaseController
  21. {
  22. public $guestAccessAction = ['create-order', 'order-relate', 'fast-pay'];
  23. //订单列表 shish 2021.1.20
  24. public function actionList()
  25. {
  26. $get = Yii::$app->request->get();
  27. $status = $get['status'] ?? 0;
  28. if (!empty($status)) {
  29. $where['status'] = $status;
  30. }
  31. $where['merchantId'] = $this->sjId;
  32. if (isset($get['shopId'])) {
  33. $shopId = $get['shopId'] ?? 0;
  34. if (!empty($shopId)) {
  35. $where['shopId'] = $this->shopId;
  36. }
  37. } else {
  38. $where['shopId'] = $this->shopId;
  39. }
  40. $searchTime = $get['searchTime'] ?? '';
  41. if (!empty($searchTime)) {
  42. $startTime = $get['startTime'] ?? '';
  43. $endTime = $get['endTime'] ?? '';
  44. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  45. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  46. }
  47. $name = $get['name'] ?? '';
  48. if (!empty($name)) {
  49. if (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  50. $where['customNamePy'] = ['like', $name];
  51. } else {
  52. $where['customName'] = ['like', $name];
  53. }
  54. }
  55. $respond = OrderClass::getOrderList($where);
  56. $shop = $this->shop;
  57. $respond['allNum'] = $shop['totalOrder'] ?? 0;
  58. $respond['unPayNum'] = $shop['unPayOrder'] ?? 0;
  59. $respond['unSendNum'] = $shop['unSendOrder'] ?? 0;
  60. $respond['sendingNum'] = $shop['sendingOrder'] ?? 0;
  61. $respond['finishNum'] = $shop['finishOrder'] ?? 0;
  62. util::success($respond);
  63. }
  64. //商城下单操作 shish 2021.1.14
  65. public function actionCreateOrder()
  66. {
  67. $post = Yii::$app->request->post();
  68. $shopId = $this->shopId;
  69. $customId = $post['customId'] ?? 0;
  70. $post['customId'] = $customId;
  71. if (!empty($customId)) {
  72. $custom = CustomClass::getCustom($customId);
  73. CustomClass::valid($custom, $this->sjId);
  74. $post['customMobile'] = $custom['mobile'] ?? '';
  75. $customName = $custom['name'] ?? '';
  76. $post['customName'] = $customName;
  77. $post['customNamePy'] = $custom['py'] ?? '';
  78. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  79. }
  80. $post['shopAdminId'] = $this->shopAdminId;
  81. $post['shopId'] = $shopId;
  82. $post['payWay'] = $this->isWx == true ? 0 : 1;
  83. $post['merchantId'] = $this->sjId;
  84. $post['fromType'] = 1;
  85. $post['prePrice'] = 0.01;
  86. $post['actPrice'] = 0.01;
  87. $post['expressId'] = $post['expressId'] ?? 0;
  88. $post['clearType'] = $post['clearType'] ?? OrderClass::CLEAR_DEBT;
  89. $productJson = $post['product'] ?? '';
  90. if (empty($productJson)) {
  91. util::fail('请选择花材');
  92. }
  93. $productList = json_decode($productJson, true);
  94. if (empty($productList)) {
  95. util::fail('请选择花材');
  96. }
  97. $connection = Yii::$app->db;//事务处理
  98. $transaction = $connection->beginTransaction();
  99. try {
  100. //判断花材有效性
  101. ProductClass::valid($productList, $this->shopId);
  102. $post['product'] = $productList;
  103. $return = OrderClass::addOrder($post, $this->shop);
  104. $transaction->commit();
  105. util::success($return);
  106. } catch (\Exception $e) {
  107. $transaction->rollBack();
  108. Yii::info("下单失败原因:" . $e->getMessage());
  109. util::fail('下单失败');
  110. }
  111. }
  112. //延期支付 shish 2021.1.19
  113. public function actionDebt()
  114. {
  115. $get = Yii::$app->request->get();
  116. $id = $get['id'] ?? 0;
  117. $info = OrderClass::getOrderInfo($id);
  118. OrderClass::valid($info, $this->shopId);
  119. OrderClass::debt($info, $this->shop);
  120. util::complete();
  121. }
  122. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  123. public function actionWxPay()
  124. {
  125. ini_set('date.timezone', 'Asia/Shanghai');
  126. $post = Yii::$app->request->post();
  127. $couponId = $post['couponId'] ?? 0;
  128. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  129. $order = OrderClass::getByOrderSn($orderSn);
  130. if (empty($order)) {
  131. util::fail('订单号无效');
  132. }
  133. $id = $order['id'];
  134. //支付前验证订单有效性
  135. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  136. util::fail('没有权限操作');
  137. }
  138. $name = $order['orderName'] ?? '购买商品';
  139. $totalFee = $order['actPrice'];
  140. //强制使用小程序的miniOpenId
  141. $openId = $this->admin['miniOpenId'];
  142. $typeList = dict::getConfig('capitalType');
  143. $capitalType = $typeList['xhGhsOrder']['id'];
  144. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  145. $wxPayType = 0;
  146. if (httpUtil::isMiniProgram()) {
  147. $wxPayType = 1;
  148. }
  149. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  150. //订单30分钟后过期
  151. $now = time();
  152. $expireTime = $now + 1800;
  153. $wx = Yii::getAlias("@vendor/weixin");
  154. require_once($wx . '/lib/WxPay.Api.php');
  155. require_once($wx . '/example/WxPay.JsApiPay.php');
  156. $input = new \WxPayUnifiedOrder();
  157. $input->SetBody($name);
  158. $input->SetOut_trade_no($orderSn);
  159. $input->SetTotal_fee($totalFee * 100);
  160. $input->SetTime_start(date("YmdHis", $now));
  161. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  162. $input->SetTime_expire(date("YmdHis", $expireTime));
  163. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  164. $input->SetTrade_type("JSAPI");
  165. //花卉宝代为申请的微信支付
  166. //$merchantExtend = $this->sjExtend;
  167. $merchantExtend = WxOpenClass::getGhsWxInfo();
  168. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  169. $input->SetSub_openid($openId);
  170. } else {
  171. $input->SetOpenid($openId);
  172. }
  173. //强制使用小程序的miniAppId
  174. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  175. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  176. $tools = new \JsApiPay();
  177. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  178. $newParams = json_decode($jsApiParameters, true);
  179. //微信不能修改价格
  180. $current = date("Y-m-d H:i:s");
  181. $updateData = ['deadline' => $current, 'modPrice' => 0];
  182. OrderClass::updateById($id, $updateData);
  183. util::success($newParams);
  184. }
  185. //获取订单详情 shish 2021.1.21
  186. public function actionDetail()
  187. {
  188. $get = Yii::$app->request->get();
  189. $id = $get['id'] ?? 0;
  190. $info = OrderClass::getOrderInfo($id, true, true, true, false);
  191. OrderClass::valid($info, $this->shopId);
  192. util::success($info);
  193. }
  194. //自取和免发货流程处理 shish 2021.1.22
  195. public function actionWithoutSend()
  196. {
  197. $get = Yii::$app->request->get();
  198. $id = $get['id'] ?? 0;
  199. $info = OrderClass::getOrderInfo($id);
  200. OrderClass::valid($info, $this->shopId);
  201. OrderClass::withoutSend($info);
  202. util::complete();
  203. }
  204. //本店送 shish 2021.1.24
  205. public function actionSelfSend()
  206. {
  207. $get = Yii::$app->request->get();
  208. $id = isset($get['id']) ? $get['id'] : 0;
  209. $info = OrderClass::getOrderInfo($id);
  210. OrderClass::valid($info, $this->shopId);
  211. $connection = Yii::$app->db;
  212. $transaction = $connection->beginTransaction();
  213. try {
  214. OrderClass::selfSend($info);
  215. $transaction->commit();
  216. } catch (\Exception $exception) {
  217. $transaction->rollBack();
  218. Yii::info("本店送操作报错:" . $exception->getMessage());
  219. util::fail('操作失败');
  220. }
  221. util::complete();
  222. }
  223. //运费计算 shish 2021.1.24
  224. public function actionFreight()
  225. {
  226. //2021.3.28 接入达达
  227. $get = Yii::$app->request->get();
  228. $orderId = $get['id']??0; //订单ID
  229. $customId = $get['customId']??0;
  230. $province = $get['province']??'';
  231. $city = $get['city']??'';
  232. $address = $get['address']??'';
  233. $floor = $get['floor']??'';
  234. $fullAddress = $province.$city.$address.$floor;
  235. $sendTime = $get['sendTime']??'';
  236. $lat = $get['lat']??'';
  237. $lng = $get['long']??'';
  238. if(empty($lat) || empty($lng) || empty($fullAddress)) {
  239. util::fail('请选择配送地址');
  240. }
  241. if(!empty($sendTime)){
  242. //配送时间不为空
  243. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  244. $sendTimeInt = strtotime($sendTime);
  245. $now = time();
  246. $diff = $sendTimeInt - $now;
  247. //因服务器写入时间,改为至少提前6分钟
  248. if($diff <= 360){
  249. util::fail('配送时间至少提前6分钟');
  250. }
  251. }
  252. //更新订单表
  253. $info = OrderClass::getOrderInfo($orderId);
  254. OrderClass::valid($info, $this->shopId);
  255. $expressAddInfo = [
  256. 'province'=>$province,
  257. 'city'=>$city,
  258. 'address'=>$address,
  259. 'floor'=>$floor,
  260. 'lat'=>$lat,
  261. 'long'=>$lng,
  262. 'sendTime'=>date('Y-m-d H:i', strtotime($sendTime)),
  263. ];
  264. OrderClass::updateCustomExpressInfo($orderId,$expressAddInfo);
  265. //计算运费
  266. $res = DadaExpressServices::queryDeliverFee($info);
  267. util::success(['sedCost' => $res['fee']]);
  268. }
  269. //发快递和第三方配送 shish 2021.1.24
  270. public function actionThirdSend()
  271. {
  272. $get = Yii::$app->request->get();
  273. $id = isset($get['id']) ? $get['id'] : 0;
  274. $info = OrderClass::getOrderInfo($id);
  275. OrderClass::valid($info, $this->shopId);
  276. $connection = Yii::$app->db;
  277. $transaction = $connection->beginTransaction();
  278. try {
  279. $respond = OrderClass::thirdSend($info, $get);
  280. $transaction->commit();
  281. util::success($respond);
  282. } catch (\Exception $exception) {
  283. $transaction->rollBack();
  284. Yii::info("发快递操作报错:" . $exception->getMessage());
  285. util::fail('操作失败');
  286. }
  287. }
  288. //确认送达 shish 2021.1.24
  289. public function actionReach()
  290. {
  291. $get = Yii::$app->request->get();
  292. $id = isset($get['id']) ? $get['id'] : 0;
  293. $info = OrderClass::getOrderInfo($id);
  294. OrderClass::valid($info, $this->shopId);
  295. $connection = Yii::$app->db;
  296. $transaction = $connection->beginTransaction();
  297. try {
  298. OrderClass::reach($info);
  299. $transaction->commit();
  300. } catch (\Exception $exception) {
  301. $transaction->rollBack();
  302. Yii::info("送达操作报错:" . $exception->getMessage());
  303. util::fail('操作失败');
  304. }
  305. util::complete();
  306. }
  307. //快捷付款订单
  308. public function actionFastPay()
  309. {
  310. ini_set('date.timezone', 'Asia/Shanghai');
  311. $post = Yii::$app->request->post();
  312. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  313. $post['shopId'] = 2;
  314. //默认门店订单
  315. $store = isset($post['store']) ? $post['store'] : 1;
  316. $post['store'] = $store;
  317. $fromType = $post['fromType'] ?? 1;
  318. $post['userId'] = $this->adminId;
  319. $admin = $this->admin;
  320. $post['bookName'] = isset($admin['adminName']) ? $admin['adminName'] : '';
  321. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  322. $bookMobile = empty($bookMobile) && isset($admin['mobile']) && !empty($admin['mobile']) ? $admin['mobile'] : $bookMobile;
  323. $post['bookMobile'] = $bookMobile;
  324. $prePrice = round($post['prePrice'], 2);
  325. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  326. $sourceType = 0;
  327. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  328. $actPrice = $discountData['price'];
  329. $post['discountType'] = $discountData['discountType'];
  330. $post['discountAmount'] = $discountData['discountAmount'];
  331. $post['actPrice'] = $actPrice;
  332. $post['payStyle'] = 0;
  333. $post['merchantId'] = $this->sjId;
  334. $now = time();
  335. //订单5分钟后过期
  336. $expireTime = $now + 300;
  337. $post['createTime'] = date("Y-m-d H:i:s", $now);
  338. $post['deadline'] = $expireTime;
  339. if ($actPrice <= 0) {
  340. util::fail('请填写正确的金额');
  341. }
  342. //微信支付订单提交不能修改价格
  343. $post['modPrice'] = $this->isWx ? 0 : 1;
  344. $post['sourceType'] = $sourceType;
  345. $post['goodsNum'] = 1;
  346. $post['fromType'] = $fromType;
  347. $post['product'] = [];
  348. $order = OrderClass::addOrder($post);
  349. $id = $order['id'];
  350. $orderSn = $order['orderSn'];
  351. $merchantId = $this->sjId;
  352. if ($payWay == 0) {
  353. $id = $order['id'];
  354. $name = '购买商品';
  355. $totalFee = $actPrice;
  356. $admin = AdminClass::getAdminById($this->adminId);
  357. //小程序使用miniOpenId
  358. $openId = $admin['miniOpenId'];
  359. $typeList = dict::getConfig('capitalType');
  360. $capitalType = $typeList['xhGhsOrder']['id'];
  361. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  362. $wxPayType = 0;
  363. if (httpUtil::isMiniProgram()) {
  364. $wxPayType = 1;
  365. }
  366. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  367. $wx = Yii::getAlias("@vendor/weixin");
  368. require_once($wx . '/lib/WxPay.Api.php');
  369. require_once($wx . '/example/WxPay.JsApiPay.php');
  370. $input = new \WxPayUnifiedOrder();
  371. $input->SetBody($name);
  372. $input->SetOut_trade_no($orderSn);
  373. $input->SetTotal_fee($totalFee * 100);
  374. $input->SetTime_start(date("YmdHis", $now));
  375. $input->SetAttach($attach);
  376. //设置订单有效期5分钟
  377. $input->SetTime_expire(date("YmdHis", $expireTime));
  378. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  379. $input->SetTrade_type("JSAPI");
  380. //花卉宝代为申请的微信支付
  381. //$merchantExtend = $this->sjExtend;
  382. $merchantExtend = WxOpenClass::getGhsWxInfo();
  383. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  384. $input->SetSub_openid($openId);
  385. } else {
  386. $input->SetOpenid($openId);
  387. }
  388. //小程序使用miniAppId
  389. if (httpUtil::isMiniProgram()) {
  390. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  391. }
  392. //强制使用小程序的appId
  393. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  394. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  395. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  396. $tools = new \JsApiPay();
  397. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  398. $newParams = json_decode($jsApiParameters, true);
  399. $newParams['orderId'] = $id;
  400. util::success($newParams);
  401. } elseif ($payWay == 1) {
  402. $extend = MerchantExtendService::getByMerchantId($merchantId);
  403. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  404. util::fail('支付宝付款即将开通...');
  405. }
  406. $config = [
  407. //应用ID,您的APPID。
  408. 'app_id' => $extend['alipayPId'],
  409. //商户私钥,您的原始格式RSA私钥
  410. 'merchant_private_key' => $extend['alipayKey'],
  411. //异步通知地址
  412. 'notify_url' => Yii::$app->params['ghsHost'] . "/notice/alipay-callback",
  413. //同步跳转
  414. 'return_url' => "",
  415. //编码格式
  416. 'charset' => "UTF-8",
  417. //签名方式
  418. 'sign_type' => "RSA2",
  419. //支付宝网关
  420. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  421. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  422. 'alipay_public_key' => $extend['alipayPublicKey'],
  423. ];
  424. Yii::info(json_encode($config) . ' aplipay config');
  425. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  426. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  427. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  428. $totalFee = $order['actPrice'];
  429. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  430. $subject = '购买商品';//订单名称,必填
  431. $total_amount = $totalFee;//付款金额,必填
  432. $body = '';//商品描述,可空
  433. $timeout_express = "1m";//超时时间
  434. $typeList = dict::getConfig('capitalType');
  435. $capitalType = $typeList['xhGhsOrder']['id'];
  436. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  437. $passBackParams = urlencode($passBackParams);
  438. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  439. $payRequestBuilder->setBody($body);
  440. $payRequestBuilder->setSubject($subject);
  441. $payRequestBuilder->setOutTradeNo($out_trade_no);
  442. $payRequestBuilder->setTotalAmount($total_amount);
  443. $payRequestBuilder->setTimeExpress($timeout_express);
  444. //在异步通知时将该参数原样返回
  445. $payRequestBuilder->setPassbackParams($passBackParams);
  446. //同步通知
  447. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$merchantId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  448. //异步通知
  449. $notifyUrl = Yii::$app->params['ghsHost'] . "/notice/alipay-callback";
  450. $payResponse = new \AlipayTradeService($config);
  451. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  452. //直接将支付宝的html返回给前端
  453. echo $result;
  454. } elseif ($payWay == 2) {
  455. $typeList = dict::getConfig('capitalType');
  456. $capitalType = $typeList['xhGhsOrder']['id'];
  457. $callbackParams = [];
  458. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  459. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  460. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $id, 'balance' => $balance]);
  461. } else {
  462. util::fail('无效的支付方式');
  463. }
  464. }
  465. //下单要用到的相关信息 shish 2019.12.6
  466. public function actionOrderRelate()
  467. {
  468. $regionTree = RegionService::tree();
  469. $shop = ShopService::getDefaultShop($this->sj);
  470. $freight = ['first' => 5, 'add' => 2];
  471. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  472. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->sj];
  473. util::success($out);
  474. }
  475. //客户欠款的订单 shish 2021.2.4
  476. public function actionDebtList()
  477. {
  478. $id = Yii::$app->request->get('id', 0);
  479. $info = CustomClass::getCustom($id);
  480. CustomClass::valid($info, $this->sjId);
  481. $where = ['customId' => $id, 'debt' => 1];
  482. $respond = OrderService::getDebtOrderList($where);
  483. $respond['customInfo'] = $info;
  484. util::success($respond);
  485. }
  486. }