OrderController.php 19 KB

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