OrderController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. namespace ghs\controllers;
  3. use bizTy\sj\services\MerchantExtendService;
  4. use biz\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 biz\saas\services\RegionService;
  14. use biz\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. OrderClass::selfSend($info);
  228. util::complete();
  229. }
  230. //运费计算 shish 2021.1.24
  231. public function actionFreight()
  232. {
  233. util::success(['sndCost' => 20]);
  234. }
  235. //发快递和第三方配送 shish 2021.1.24
  236. public function actionThirdSend()
  237. {
  238. $respond = [
  239. 'sendSide' => '达达',
  240. 'distance' => 5.5,
  241. 'sendCost' => 20,
  242. 'status' => 1,
  243. 'tip' => 5,
  244. ];
  245. util::success($respond);
  246. }
  247. //确认送达 shish 2021.1.24
  248. public function actionReach()
  249. {
  250. $get = Yii::$app->request->get();
  251. $id = isset($get['id']) ? $get['id'] : 0;
  252. $info = OrderClass::getOrderInfo($id);
  253. OrderClass::valid($info, $this->shopId);
  254. util::complete();
  255. }
  256. //快捷付款订单
  257. public function actionFastPay()
  258. {
  259. ini_set('date.timezone', 'Asia/Shanghai');
  260. $post = Yii::$app->request->post();
  261. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  262. $post['shopId'] = 2;
  263. //默认门店订单
  264. $store = isset($post['store']) ? $post['store'] : 1;
  265. $post['store'] = $store;
  266. $fromType = $post['fromType'] ?? 1;
  267. $post['userId'] = $this->adminId;
  268. $admin = $this->admin;
  269. $post['bookName'] = isset($admin['adminName']) ? $admin['adminName'] : '';
  270. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  271. $bookMobile = empty($bookMobile) && isset($admin['mobile']) && !empty($admin['mobile']) ? $admin['mobile'] : $bookMobile;
  272. $post['bookMobile'] = $bookMobile;
  273. $prePrice = round($post['prePrice'], 2);
  274. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  275. $sourceType = 0;
  276. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  277. $actPrice = $discountData['price'];
  278. $post['discountType'] = $discountData['discountType'];
  279. $post['discountAmount'] = $discountData['discountAmount'];
  280. $post['actPrice'] = $actPrice;
  281. $post['payStyle'] = 0;
  282. $post['merchantId'] = $this->sjId;
  283. $now = time();
  284. //订单5分钟后过期
  285. $expireTime = $now + 300;
  286. $post['createTime'] = date("Y-m-d H:i:s", $now);
  287. $post['deadline'] = $expireTime;
  288. if ($actPrice <= 0) {
  289. util::fail('请填写正确的金额');
  290. }
  291. //微信支付订单提交不能修改价格
  292. $post['modPrice'] = $this->isWx ? 0 : 1;
  293. $post['sourceType'] = $sourceType;
  294. $post['goodsNum'] = 1;
  295. $post['fromType'] = $fromType;
  296. $post['product'] = [];
  297. $order = OrderClass::addOrder($post);
  298. $id = $order['id'];
  299. $orderSn = $order['orderSn'];
  300. $merchantId = $this->sjId;
  301. if ($payWay == 0) {
  302. $id = $order['id'];
  303. $name = '购买商品';
  304. $totalFee = $actPrice;
  305. $admin = AdminClass::getAdminById($this->adminId);
  306. $openId = isset($admin['openId']) ? $admin['openId'] : 0;
  307. if (httpUtil::isMiniProgram()) {
  308. //小程序使用miniOpenId
  309. $openId = $admin['miniOpenId'];
  310. } else {
  311. //util::fail('仅支持小程序支付');
  312. $openId = $admin['miniOpenId'];
  313. }
  314. $typeList = configDict::getConfig('capitalType');
  315. $capitalType = $typeList['xhGhsOrder']['id'];
  316. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  317. $wxPayType = 0;
  318. if (httpUtil::isMiniProgram()) {
  319. $wxPayType = 1;
  320. }
  321. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  322. $wx = Yii::getAlias("@vendor/weixin");
  323. require_once($wx . '/lib/WxPay.Api.php');
  324. require_once($wx . '/example/WxPay.JsApiPay.php');
  325. $input = new \WxPayUnifiedOrder();
  326. $input->SetBody($name);
  327. $input->SetOut_trade_no($orderSn);
  328. $input->SetTotal_fee($totalFee * 100);
  329. $input->SetTime_start(date("YmdHis", $now));
  330. $input->SetAttach($attach);
  331. //设置订单有效期5分钟
  332. $input->SetTime_expire(date("YmdHis", $expireTime));
  333. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  334. $input->SetTrade_type("JSAPI");
  335. //花卉宝代为申请的微信支付
  336. //$merchantExtend = $this->sjExtend;
  337. $merchantExtend = WxOpenClass::getGhsWxInfo();
  338. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  339. $input->SetSub_openid($openId);
  340. } else {
  341. $input->SetOpenid($openId);
  342. }
  343. //小程序使用miniAppId
  344. if (httpUtil::isMiniProgram()) {
  345. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  346. }
  347. //强制使用小程序的appId
  348. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  349. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  350. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  351. $tools = new \JsApiPay();
  352. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  353. $newParams = json_decode($jsApiParameters, true);
  354. $newParams['orderId'] = $id;
  355. util::success($newParams);
  356. } elseif ($payWay == 1) {
  357. $extend = MerchantExtendService::getByMerchantId($merchantId);
  358. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  359. util::fail('支付宝付款即将开通...');
  360. }
  361. $config = [
  362. //应用ID,您的APPID。
  363. 'app_id' => $extend['alipayPId'],
  364. //商户私钥,您的原始格式RSA私钥
  365. 'merchant_private_key' => $extend['alipayKey'],
  366. //异步通知地址
  367. 'notify_url' => Yii::$app->params['ghsHost'] . "/notice/alipay-callback",
  368. //同步跳转
  369. 'return_url' => "",
  370. //编码格式
  371. 'charset' => "UTF-8",
  372. //签名方式
  373. 'sign_type' => "RSA2",
  374. //支付宝网关
  375. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  376. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  377. 'alipay_public_key' => $extend['alipayPublicKey'],
  378. ];
  379. Yii::info(json_encode($config) . ' aplipay config');
  380. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  381. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  382. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  383. $totalFee = $order['actPrice'];
  384. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  385. $subject = '购买商品';//订单名称,必填
  386. $total_amount = $totalFee;//付款金额,必填
  387. $body = '';//商品描述,可空
  388. $timeout_express = "1m";//超时时间
  389. $typeList = configDict::getConfig('capitalType');
  390. $capitalType = $typeList['xhGhsOrder']['id'];
  391. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  392. $passbackParams = urlencode($passbackParams);
  393. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  394. $payRequestBuilder->setBody($body);
  395. $payRequestBuilder->setSubject($subject);
  396. $payRequestBuilder->setOutTradeNo($out_trade_no);
  397. $payRequestBuilder->setTotalAmount($total_amount);
  398. $payRequestBuilder->setTimeExpress($timeout_express);
  399. //在异步通知时将该参数原样返回
  400. $payRequestBuilder->setPassbackParams($passbackParams);
  401. //同步通知
  402. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$merchantId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  403. //异步通知
  404. $notifyUrl = Yii::$app->params['ghsHost'] . "/notice/alipay-callback";
  405. $payResponse = new \AlipayTradeService($config);
  406. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  407. //直接将支付宝的html返回给前端
  408. echo $result;
  409. } elseif ($payWay == 2) {
  410. //余额支付,验证支付密码是否正确
  411. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  412. $typeList = configDict::getConfig('capitalType');
  413. $capitalType = $typeList['xhGhsOrder']['id'];
  414. $callbackParams = [];
  415. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  416. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  417. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $id, 'balance' => $balance]);
  418. } else {
  419. util::fail('无效的支付方式');
  420. }
  421. }
  422. //下单要用到的相关信息 shish 2019.12.6
  423. public function actionOrderRelate()
  424. {
  425. $regionTree = RegionService::tree();
  426. $shop = ShopService::getDefaultShop($this->sj);
  427. $freight = ['first' => 5, 'add' => 2];
  428. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  429. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->sj];
  430. util::success($out);
  431. }
  432. //客户欠款的订单 shish 2021.2.4
  433. public function actionDebtList()
  434. {
  435. $id = Yii::$app->request->get('id', 0);
  436. $info = CustomClass::getCustom($id);
  437. CustomClass::valid($info, $this->sjId);
  438. $where = ['customId' => $id, 'debt' => 1];
  439. $list = OrderService::getDebtOrderList($where);
  440. util::success($list);
  441. }
  442. //收款 shish 2021.2.4
  443. public function actionClearDebt()
  444. {
  445. $ids = Yii::$app->request->post('id');
  446. util::complete();
  447. }
  448. }