OrderController.php 21 KB

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