OrderController.php 17 KB

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