OrderController.php 20 KB

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