OrderController.php 17 KB

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