OrderController.php 16 KB

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