OrderController.php 15 KB

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