OrderController.php 21 KB

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