OrderController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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\merchant\classes\ShopClass;
  8. use bizGhs\merchant\services\ShopService;
  9. use bizGhs\order\classes\OrderClass;
  10. use bizGhs\order\services\OrderItemService;
  11. use bizGhs\order\services\OrderService;
  12. use biz\saas\services\RegionService;
  13. use biz\user\services\UserService;
  14. use bizGhs\product\classes\ProductClass;
  15. use common\components\configDict;
  16. use common\components\httpUtil;
  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 $withoutLoginAction = ['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. $data = OrderClass::getOrderList($where);
  42. util::success($data);
  43. }
  44. //商城下单操作 shish 2021.1.14
  45. public function actionCreateOrder()
  46. {
  47. $post = Yii::$app->request->post();
  48. $adminId = $this->adminId;
  49. $shopId = $this->shopId;
  50. if (!empty($shopId)) {
  51. //供货商端 员工 开单
  52. $post['customId'] = $post['customId'] ?? 0;
  53. $post['adminId'] = $adminId;
  54. $post['userId'] = 0;
  55. $post['shopId'] = $shopId;
  56. } else {
  57. //供货商端 客户 开单
  58. $post['userId'] = $adminId;
  59. $post['adminId'] = 0;
  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. util::fail('下单失败');
  99. }
  100. util::success([
  101. 'orderId' => $id,
  102. 'orderSn' => $orderSn,
  103. 'totalPrice' => 0.01,
  104. 'sendType' => '快递送',
  105. 'sendTime' => '15:00',
  106. 'qrCode' => Yii::$app->params['imgHost'] . '/hhb_small.png',
  107. ]);
  108. }
  109. //延期支付 shish 2021.1.19
  110. public function actionDebt()
  111. {
  112. $get = Yii::$app->request->get();
  113. util::complete();
  114. }
  115. //已经支付
  116. public function actionHasPay()
  117. {
  118. $get = Yii::$app->request->get();
  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. // if (httpUtil::isMiniProgram()) {
  140. // $openId = $this->admin['miniOpenId'];
  141. // } else {
  142. // $openId = $this->admin['openId'];
  143. // }
  144. //强制使用小程序的miniOpenId
  145. $openId = $this->admin['miniOpenId'];
  146. $typeList = configDict::getConfig('capitalType');
  147. $capitalType = $typeList['xhGhsOrder']['id'];
  148. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  149. $wxPayType = 0;
  150. if (httpUtil::isMiniProgram()) {
  151. $wxPayType = 1;
  152. }
  153. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  154. //订单30分钟后过期
  155. $now = time();
  156. $expireTime = $now + 1800;
  157. $wx = Yii::getAlias("@vendor/weixin");
  158. require_once($wx . '/lib/WxPay.Api.php');
  159. require_once($wx . '/example/WxPay.JsApiPay.php');
  160. $input = new \WxPayUnifiedOrder();
  161. $input->SetBody($name);
  162. $input->SetOut_trade_no($orderSn);
  163. $input->SetTotal_fee($totalFee * 100);
  164. $input->SetTime_start(date("YmdHis", $now));
  165. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  166. $input->SetTime_expire(date("YmdHis", $expireTime));
  167. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  168. $input->SetTrade_type("JSAPI");
  169. //花卉宝代为申请的微信支付
  170. //$merchantExtend = $this->sjExtend;
  171. $merchantExtend = WxOpenClass::getGhsWxInfo();
  172. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  173. $input->SetSub_openid($openId);
  174. } else {
  175. $input->SetOpenid($openId);
  176. }
  177. // if (httpUtil::isMiniProgram()) {
  178. // $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  179. // }
  180. //强制使用小程序的miniAppId
  181. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  182. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  183. $tools = new \JsApiPay();
  184. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  185. $newParams = json_decode($jsApiParameters, true);
  186. //微信不能修改价格
  187. $current = date("Y-m-d H:i:s");
  188. $updateData = ['deadline' => $current, 'modPrice' => 0];
  189. OrderClass::updateById($id, $updateData);
  190. util::success($newParams);
  191. }
  192. //获取订单详情 shish 2021.1.21
  193. public function actionDetail()
  194. {
  195. $get = Yii::$app->request->get();
  196. $id = $get['id'] ?? 0;
  197. $info = OrderClass::getOrderInfo($id, true, true, true);
  198. OrderClass::valid($info, $this->shopId);
  199. util::success($info);
  200. }
  201. //重选花材 shish 2021.1.22
  202. public function actionChooseProduct()
  203. {
  204. $post = Yii::$app->request->post();
  205. $id = $post['id'] ?? 0;
  206. $product = $post['product'] ?? '[]';
  207. $product = json_decode($product, true);
  208. //检查花材是否都是同家门店的
  209. ProductClass::valid($product, $this->shopId);
  210. $info = OrderClass::getOrderInfo($id);
  211. OrderClass::valid($info, $this->shopId);
  212. //添加修改花材
  213. OrderItemService::replace($info, $product);
  214. util::complete();
  215. }
  216. //自取和免发货流程处理 shish 2021.1.22
  217. public function actionWithoutSend()
  218. {
  219. $get = Yii::$app->request->get();
  220. $id = $get['id'] ?? 0;
  221. $info = OrderClass::getOrderInfo($id);
  222. OrderClass::valid($info, $this->shopId);
  223. OrderClass::withoutSend($info);
  224. util::complete();
  225. }
  226. //本店送 shish 2021.1.24
  227. public function actionSelfSend()
  228. {
  229. $get = Yii::$app->request->get();
  230. $id = isset($get['id']) ? $get['id'] : 0;
  231. $info = OrderClass::getOrderInfo($id);
  232. OrderClass::valid($info, $this->shopId);
  233. OrderClass::selfSend($info);
  234. util::complete();
  235. }
  236. //运费计算 shish 2021.1.24
  237. public function actionFreight()
  238. {
  239. util::success(['sndCost' => 20]);
  240. }
  241. //发快递和第三方配送 shish 2021.1.24
  242. public function actionThirdSend()
  243. {
  244. $respond = [
  245. 'sendSide' => '达达',
  246. 'distance' => 5.5,
  247. 'sendCost' => 20,
  248. 'status' => 1,
  249. 'tip' => 5,
  250. ];
  251. util::success($respond);
  252. }
  253. //确认送达 shish 2021.1.24
  254. public function actionReach()
  255. {
  256. $get = Yii::$app->request->get();
  257. $id = isset($get['id']) ? $get['id'] : 0;
  258. $info = OrderClass::getOrderInfo($id);
  259. OrderClass::valid($info, $this->shopId);
  260. util::complete();
  261. }
  262. //快捷付款订单
  263. public function actionFastPay()
  264. {
  265. ini_set('date.timezone', 'Asia/Shanghai');
  266. $post = Yii::$app->request->post();
  267. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  268. $post['shopId'] = 2;
  269. //默认门店订单
  270. $store = isset($post['store']) ? $post['store'] : 1;
  271. $post['store'] = $store;
  272. $fromType = $post['fromType'] ?? 1;
  273. $post['userId'] = $this->adminId;
  274. $admin = $this->admin;
  275. $post['bookName'] = isset($admin['adminName']) ? $admin['adminName'] : '';
  276. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  277. $bookMobile = empty($bookMobile) && isset($admin['mobile']) && !empty($admin['mobile']) ? $admin['mobile'] : $bookMobile;
  278. $post['bookMobile'] = $bookMobile;
  279. $prePrice = round($post['prePrice'], 2);
  280. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  281. $sourceType = 0;
  282. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  283. $actPrice = $discountData['price'];
  284. $post['discountType'] = $discountData['discountType'];
  285. $post['discountAmount'] = $discountData['discountAmount'];
  286. $post['actPrice'] = $actPrice;
  287. $post['payStyle'] = 0;
  288. $post['merchantId'] = $this->sjId;
  289. $now = time();
  290. //订单5分钟后过期
  291. $expireTime = $now + 300;
  292. $post['createTime'] = date("Y-m-d H:i:s", $now);
  293. $post['deadline'] = $expireTime;
  294. if ($actPrice <= 0) {
  295. util::fail('请填写正确的金额');
  296. }
  297. //微信支付订单提交不能修改价格
  298. $post['modPrice'] = $this->isWx ? 0 : 1;
  299. $post['sourceType'] = $sourceType;
  300. $post['goodsNum'] = 1;
  301. $post['fromType'] = $fromType;
  302. $post['product'] = [];
  303. $order = OrderClass::addOrder($post);
  304. $id = $order['id'];
  305. $orderSn = $order['orderSn'];
  306. $merchantId = $this->sjId;
  307. if ($payWay == 0) {
  308. $id = $order['id'];
  309. $name = '购买商品';
  310. $totalFee = $actPrice;
  311. $admin = AdminClass::getAdminById($this->adminId);
  312. $openId = isset($admin['openId']) ? $admin['openId'] : 0;
  313. if (httpUtil::isMiniProgram()) {
  314. //小程序使用miniOpenId
  315. $openId = $admin['miniOpenId'];
  316. } else {
  317. //util::fail('仅支持小程序支付');
  318. $openId = $admin['miniOpenId'];
  319. }
  320. $typeList = configDict::getConfig('capitalType');
  321. $capitalType = $typeList['xhGhsOrder']['id'];
  322. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  323. $wxPayType = 0;
  324. if (httpUtil::isMiniProgram()) {
  325. $wxPayType = 1;
  326. }
  327. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  328. $wx = Yii::getAlias("@vendor/weixin");
  329. require_once($wx . '/lib/WxPay.Api.php');
  330. require_once($wx . '/example/WxPay.JsApiPay.php');
  331. $input = new \WxPayUnifiedOrder();
  332. $input->SetBody($name);
  333. $input->SetOut_trade_no($orderSn);
  334. $input->SetTotal_fee($totalFee * 100);
  335. $input->SetTime_start(date("YmdHis", $now));
  336. $input->SetAttach($attach);
  337. //设置订单有效期5分钟
  338. $input->SetTime_expire(date("YmdHis", $expireTime));
  339. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  340. $input->SetTrade_type("JSAPI");
  341. //花卉宝代为申请的微信支付
  342. //$merchantExtend = $this->sjExtend;
  343. $merchantExtend = WxOpenClass::getGhsWxInfo();
  344. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  345. $input->SetSub_openid($openId);
  346. } else {
  347. $input->SetOpenid($openId);
  348. }
  349. //小程序使用miniAppId
  350. if (httpUtil::isMiniProgram()) {
  351. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  352. }
  353. //强制使用小程序的appId
  354. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  355. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  356. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  357. $tools = new \JsApiPay();
  358. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  359. $newParams = json_decode($jsApiParameters, true);
  360. $newParams['orderId'] = $id;
  361. util::success($newParams);
  362. } elseif ($payWay == 1) {
  363. $extend = MerchantExtendService::getByMerchantId($merchantId);
  364. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  365. util::fail('支付宝付款即将开通...');
  366. }
  367. $config = [
  368. //应用ID,您的APPID。
  369. 'app_id' => $extend['alipayPId'],
  370. //商户私钥,您的原始格式RSA私钥
  371. 'merchant_private_key' => $extend['alipayKey'],
  372. //异步通知地址
  373. 'notify_url' => Yii::$app->params['ghsHost'] . "/notice/alipay-callback",
  374. //同步跳转
  375. 'return_url' => "",
  376. //编码格式
  377. 'charset' => "UTF-8",
  378. //签名方式
  379. 'sign_type' => "RSA2",
  380. //支付宝网关
  381. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  382. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  383. 'alipay_public_key' => $extend['alipayPublicKey'],
  384. ];
  385. Yii::info(json_encode($config) . ' aplipay config');
  386. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  387. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  388. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  389. $totalFee = $order['actPrice'];
  390. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  391. $subject = '购买商品';//订单名称,必填
  392. $total_amount = $totalFee;//付款金额,必填
  393. $body = '';//商品描述,可空
  394. $timeout_express = "1m";//超时时间
  395. $typeList = configDict::getConfig('capitalType');
  396. $capitalType = $typeList['xhGhsOrder']['id'];
  397. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  398. $passbackParams = urlencode($passbackParams);
  399. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  400. $payRequestBuilder->setBody($body);
  401. $payRequestBuilder->setSubject($subject);
  402. $payRequestBuilder->setOutTradeNo($out_trade_no);
  403. $payRequestBuilder->setTotalAmount($total_amount);
  404. $payRequestBuilder->setTimeExpress($timeout_express);
  405. //在异步通知时将该参数原样返回
  406. $payRequestBuilder->setPassbackParams($passbackParams);
  407. //同步通知
  408. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$merchantId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  409. //异步通知
  410. $notifyUrl = Yii::$app->params['ghsHost'] . "/notice/alipay-callback";
  411. $payResponse = new \AlipayTradeService($config);
  412. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  413. //直接将支付宝的html返回给前端
  414. echo $result;
  415. } elseif ($payWay == 2) {
  416. //余额支付,验证支付密码是否正确
  417. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  418. $typeList = configDict::getConfig('capitalType');
  419. $capitalType = $typeList['xhGhsOrder']['id'];
  420. $callbackParams = [];
  421. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  422. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  423. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $id, 'balance' => $balance]);
  424. } else {
  425. util::fail('无效的支付方式');
  426. }
  427. }
  428. //下单要用到的相关信息 shish 2019.12.6
  429. public function actionOrderRelate()
  430. {
  431. $regionTree = RegionService::tree();
  432. $shop = ShopService::getDefaultShop($this->sj);
  433. $freight = ['first' => 5, 'add' => 2];
  434. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  435. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->sj];
  436. util::success($out);
  437. }
  438. }