OrderController.php 21 KB

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