OrderController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <?php
  2. namespace hd\controllers;
  3. use biz\sj\services\MerchantAssetService;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizGhs\product\classes\ProductClass;
  7. use bizHd\custom\classes\CustomClass;
  8. use bizHd\merchant\services\ShopService;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\classes\OrderSendClass;
  11. use bizHd\order\services\OrderService;
  12. use bizHd\saas\services\RegionService;
  13. use bizHd\user\services\UserService;
  14. use bizGhs\order\classes\OrderItemClass;
  15. use common\components\dict;
  16. use common\components\dateUtil;
  17. use common\services\xhPayToolService;
  18. use Yii;
  19. use common\components\util;
  20. use common\components\stringUtil;
  21. use bizHd\goods\services\GoodsService;
  22. use bizHd\merchant\services\ExpressService;
  23. use bizHd\order\services\OrderGoodsService;
  24. use bizHd\promote\services\CouponService;
  25. use common\components\httpUtil;
  26. class OrderController extends BaseController
  27. {
  28. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  29. //开单操作 ssh 2019.12.3
  30. public function actionCreateOrder()
  31. {
  32. $post = Yii::$app->request->post();
  33. $needSend = isset($post['needSend']) && is_numeric($post['needSend']) ? $post['needSend'] : 1;
  34. $orderType = $post['orderType'] ?? 1;
  35. $post['sjId'] = $this->sjId;
  36. $post['shopId'] = $this->shopId;
  37. $post['payWay'] = $this->isWx == true ? 0 : 1;
  38. $now = time();
  39. $expireTime = $now + 1800;//订单30分钟后过期
  40. $post['deadline'] = $expireTime;
  41. $post['fromType'] = 1;//商城
  42. $customId = $post['customId'] ?? 0;
  43. if (!empty($customId)) {
  44. $customInfo = CustomClass::getCustomInfo($customId);
  45. CustomClass::valid($customInfo, $this->shopId);
  46. $post['customId'] = $customId;
  47. }
  48. //门店订单
  49. $post['store'] = 1;
  50. //员工信息
  51. $shopAdmin = $this->shopAdmin->attributes;
  52. $shopAdminName = $shopAdmin['name'] ?? '';
  53. $post['shopAdminName'] = $shopAdminName;
  54. $post['shopAdminId'] = $this->shopAdminId;
  55. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  56. if ($orderType == 1) {
  57. //$orderType = 1 成品订单
  58. $goodsInfo = $post['goodsInfo'] ?? '';
  59. $goodsInfoArr = json_decode($goodsInfo, true);
  60. if (empty($goodsInfoArr)) {
  61. util::fail('请选择商品');
  62. }
  63. //总的商品金额
  64. $totalGoodsPrice = 0;
  65. $orderGoods = [];
  66. foreach ($goodsInfoArr as $key => $currentGoods) {
  67. $currentGoodsId = $currentGoods['goodsId'];
  68. $currentGoodsInfo = GoodsService::getGoodsInfo($currentGoodsId);
  69. if (empty($currentGoodsInfo)) {
  70. util::fail('没有找到商品');
  71. }
  72. $goodsStyleId = $currentGoods['goodsStyleId'];
  73. $goodsNum = $currentGoods['goodsNum'];
  74. //计算总金额
  75. $unitPrice = $currentGoodsInfo['price'];
  76. $goodsStyleList = isset($currentGoodsInfo['goodsStyleList']) ? $currentGoodsInfo['goodsStyleList'] : [];
  77. if (!empty($goodsStyleId)) {
  78. $idKeyStyleList = array_column($goodsStyleList, null, 'id');
  79. if (empty($goodsStyleList)) {
  80. util::fail('商品款式没有找到');
  81. }
  82. $styleIdList = array_keys(array_column($goodsStyleList, null, "id"));
  83. if (in_array($goodsStyleId, $styleIdList) == false) {
  84. util::fail('商品款式没有找到!');
  85. }
  86. if (isset($idKeyStyleList[$goodsStyleId]['price']) == false) {
  87. util::fail('没有找到多款式的价格');
  88. }
  89. $unitPrice = $idKeyStyleList[$goodsStyleId]['price'];
  90. }
  91. $goodsPrice = $unitPrice * $goodsNum;
  92. $totalGoodsPrice = stringUtil::calcAdd($totalGoodsPrice, $goodsPrice);
  93. $orderGoods[] = [
  94. 'goodsId' => $currentGoodsId,
  95. 'userId' => $this->adminId,
  96. 'sjId' => $this->sjId,
  97. 'title' => $currentGoodsInfo['goodsName'],
  98. 'cover' => isset($goodsInfo['shortImgList']) && !empty($currentGoodsInfo['shortImgList']) ? current($currentGoodsInfo['shortImgList']) : '',
  99. 'unitPrice' => $unitPrice,
  100. 'num' => $goodsNum,
  101. 'goodsStyleName' => isset($currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
  102. 'goodsStyleId' => $goodsStyleId,
  103. 'createTime' => date("Y-m-d H:i:s"),
  104. ];
  105. }
  106. $user = $this->custom;
  107. $post['bookName'] = isset($user['name']) ? $user['name'] : '';
  108. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  109. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  110. $post['bookMobile'] = $bookMobile;
  111. //计算运费
  112. $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
  113. $sendCost = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
  114. //按距离计算运费并且客户要求送的
  115. //运费计算方式
  116. //$freightType = 0;
  117. //if ($freightType == 0 && $needSend == 1) {
  118. //$lat = $post['latitude']??'';//收货人纬度
  119. //$lng = $post['longitude']??'';//收货人经度
  120. //$respond = ExpressService::getUserDistance($lng, $lat, $defaultShopId, $this->sjExtend);
  121. //$sendCost = isset($respond['fee']) ? $respond['fee'] : 0;
  122. //}
  123. $post['sendCost'] = $sendCost;
  124. $currentPrice = stringUtil::calcAdd($sendCost, $totalGoodsPrice);
  125. $post['prePrice'] = $currentPrice;
  126. if (isset($post['modifyPrice'])) {
  127. //商家打折,修改金额
  128. $modifyPrice = $post['modifyPrice'] ?? 0;
  129. if ($modifyPrice > $currentPrice) {
  130. util::fail('实收金额大于订单总金额');
  131. }
  132. if ($modifyPrice < $currentPrice) {
  133. $post['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  134. $post['discountType'] = dict::getDict('discountType', 'discount');
  135. $currentPrice = $modifyPrice;
  136. }
  137. }
  138. $post['actPrice'] = $currentPrice;
  139. $post['realPrice'] = $currentPrice;
  140. $order = OrderClass::addOrder($post);
  141. $orderId = $order['id'];
  142. $orderSn = $order['orderSn'];
  143. foreach ($orderGoods as $key => $val) {
  144. $val['orderId'] = $orderId;
  145. $val['orderSn'] = $orderSn;
  146. OrderGoodsService::add($val);//创建订单商品列表
  147. }
  148. } else {
  149. //$orderType = 2 花材订单 组合开单
  150. $productData = $post['product'] ?? '';
  151. $product = json_decode($productData, true);
  152. if (empty($product)) {
  153. util::fail('请选择花材');
  154. }
  155. $currentPrice = $post['packingFee'] ?? 0; //包装费
  156. $orderProduct = [];
  157. $level = 0;
  158. $productInfo = ProductClass::formatProductInfo($product, $level);
  159. $currentPrice = bcadd($currentPrice, $productInfo['price'], 2);
  160. $sendCost = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
  161. $post['sendCost'] = $sendCost;
  162. $currentPrice = bcadd($currentPrice, $sendCost, 2);
  163. $post['prePrice'] = $currentPrice;
  164. if (isset($post['modifyPrice'])) {
  165. //商家打折,修改金额
  166. $modifyPrice = $post['modifyPrice'] ?? 0;
  167. if ($modifyPrice > $currentPrice) {
  168. util::fail('实收金额大于订单总金额');
  169. }
  170. if ($modifyPrice < $currentPrice) {
  171. $post['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  172. $post['discountType'] = dict::getDict('discountType', 'discount');
  173. $currentPrice = $modifyPrice;
  174. }
  175. }
  176. $post['actPrice'] = $currentPrice;
  177. $post['realPrice'] = $currentPrice;
  178. foreach ($product as $key => $val) {
  179. $productId = $val['productId'];
  180. $bigNum = $val['bigNum'];
  181. $smallNum = $val['smallNum'];
  182. $orderProduct[] = [
  183. 'productId' => $productId,
  184. 'bigNum' => $bigNum,
  185. 'smallNum' => $smallNum,
  186. ];
  187. //减库存
  188. ProductClass::decreaseStock($productId, $bigNum, $smallNum, true);
  189. }
  190. $order = OrderClass::addOrder($post);
  191. $orderId = $order['id'];
  192. $orderSn = $order['orderSn'];
  193. foreach ($orderProduct as $key => $val) {
  194. $val['orderSn'] = $orderSn;
  195. OrderItemClass::add($val);
  196. }
  197. }
  198. $payWay = $post['payWay'] ?? dict::getDict('payWay', 'wxPay');
  199. OrderClass::staffKdPay($payWay, $orderSn);
  200. util::success(['orderSn' => $orderSn, 'totalPrice' => $currentPrice, 'couponId' => $couponId, 'orderId' => $orderId]);
  201. }
  202. //下单要用到的相关信息 ssh 2019.12.6
  203. public function actionOrderRelate()
  204. {
  205. $regionTree = RegionService::tree();
  206. $shop = $this->shop->attributes;
  207. $freight = MerchantExtendService::getFreight($this->sjExtend);
  208. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  209. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  210. util::success($out);
  211. }
  212. //余额支付 ssh 2019.12.6
  213. public function actionBalancePay()
  214. {
  215. $post = Yii::$app->request->post();
  216. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  217. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  218. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  219. //验证优惠券是否还有效
  220. if (!empty($couponId)) {
  221. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  222. }
  223. $order = OrderService::getByOrderSn($orderSn);
  224. $userId = $this->adminId;
  225. $user = UserService::getUserInfo($userId);
  226. //验证支付密码是否正确
  227. UserService::validPayPassword($payPassword, $user);
  228. //支付前验证订单有效性
  229. OrderService::checkBeforePay($order, $this->adminId);
  230. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  231. $totalFee = $order['prePrice'];
  232. $sourceType = 0;
  233. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  234. $actPrice = $discountData['price'];
  235. $callbackParams = [];
  236. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  237. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  238. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  239. }
  240. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  241. public function actionWxPay()
  242. {
  243. ini_set('date.timezone', 'Asia/Shanghai');
  244. $post = Yii::$app->request->post();
  245. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  246. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  247. $order = OrderService::getByOrderSn($orderSn);
  248. $orderId = $order['id'];
  249. //验证优惠券是否还有效
  250. if (!empty($couponId)) {
  251. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  252. }
  253. //支付前验证订单有效性
  254. OrderService::checkBeforePay($order, $this->adminId);
  255. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  256. $totalFee = $order['realPrice'];
  257. //小程序使用miniOpenId
  258. if (httpUtil::isMiniProgram()) {
  259. $openId = $this->user['miniOpenId'];
  260. } else {
  261. $openId = $this->user['openId'];
  262. }
  263. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  264. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  265. $wxPayType = 0;
  266. if (httpUtil::isMiniProgram()) {
  267. $wxPayType = 1;
  268. }
  269. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  270. //订单30分钟后过期
  271. $now = time();
  272. $expireTime = $now + 1800;
  273. $wx = Yii::getAlias("@vendor/weixin");
  274. require_once($wx . '/lib/WxPay.Api.php');
  275. require_once($wx . '/example/WxPay.JsApiPay.php');
  276. $input = new \WxPayUnifiedOrder();
  277. $input->SetBody($name);
  278. $input->SetOut_trade_no($orderSn);
  279. $input->SetTotal_fee($totalFee * 100);
  280. $input->SetTime_start(date("YmdHis", $now));
  281. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  282. $input->SetTime_expire(date("YmdHis", $expireTime));
  283. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  284. $input->SetTrade_type("JSAPI");
  285. //花卉宝代为申请的微信支付
  286. $merchantExtend = $this->sjExtend->attributes;
  287. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  288. $input->SetSub_openid($openId);
  289. } else {
  290. $input->SetOpenid($openId);
  291. }
  292. //小程序使用miniAppId
  293. if (httpUtil::isMiniProgram()) {
  294. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  295. }
  296. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  297. $tools = new \JsApiPay();
  298. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  299. $newParams = json_decode($jsApiParameters, true);
  300. //已请求微信不能修改价格
  301. $updateData = [];
  302. $updateData['modPrice'] = 0;
  303. if (empty($order['deadline'])) {
  304. $updateData['deadline'] = $expireTime;
  305. }
  306. OrderService::updateById($orderId, $updateData);
  307. util::success($newParams);
  308. }
  309. //快捷付款订单
  310. public function actionFastPay()
  311. {
  312. ini_set('date.timezone', 'Asia/Shanghai');
  313. $post = Yii::$app->request->post();
  314. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  315. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  316. //验证门店是否有效
  317. $shopInfo = ShopService::getById($shopId);
  318. ShopService::valid($shopInfo, $this->sjId);
  319. $post['shopId'] = $shopId;
  320. //默认门店订单
  321. $store = isset($post['store']) ? $post['store'] : 1;
  322. $post['store'] = $store;
  323. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  324. $sourceType = $this->isWx ? 0 : 1;
  325. if (httpUtil::isMiniProgram()) {
  326. $sourceType = 2;
  327. }
  328. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  329. //兼容旧系统sourceType=3表示朋友圈来源
  330. $fromType = $sourceType == 3 ? 2 : 0;
  331. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  332. $post['userId'] = $this->adminId;
  333. $custom = $this->custom;
  334. $post['bookName'] = $custom['userName'] ?? '';
  335. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  336. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  337. $post['bookMobile'] = $bookMobile;
  338. $prePrice = round($post['prePrice'], 2);
  339. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  340. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  341. $actPrice = $discountData['price'];
  342. $post['discountType'] = $discountData['discountType'];
  343. $post['discountAmount'] = $discountData['discountAmount'];
  344. $post['actPrice'] = $actPrice;
  345. $post['realPrice'] = $actPrice;
  346. $post['payStyle'] = 0;
  347. $post['sjId'] = $this->sjId;
  348. $now = time();
  349. $expireTime = $now + 300;//订单5分钟后过期
  350. $post['createTime'] = date("Y-m-d H:i:s", $now);
  351. $post['deadline'] = $expireTime;
  352. if ($actPrice <= 0) {
  353. util::fail('请填写正确的金额');
  354. }
  355. //微信支付订单提交不能修改价格
  356. $post['modPrice'] = $this->isWx ? 0 : 1;
  357. $post['sourceType'] = $sourceType;
  358. $post['goodsNum'] = 1;
  359. $post['fromType'] = $fromType;
  360. $order = OrderService::addOrder($post);
  361. $orderId = $order['id'];
  362. $orderSn = $order['orderSn'];
  363. $sjId = $this->sjId;
  364. if ($payWay == 0) {
  365. $orderId = $order['id'];
  366. $name = '购买商品';
  367. $totalFee = $actPrice;
  368. $user = UserService::getById($this->adminId);
  369. $openId = isset($user['openId']) ? $user['openId'] : 0;
  370. if (httpUtil::isMiniProgram()) {
  371. //小程序使用miniOpenId
  372. $openId = $user['miniOpenId'];
  373. }
  374. if (empty($openId)) {
  375. util::fail('没有找到客户的openId');
  376. }
  377. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  378. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  379. $wxPayType = 0;
  380. if (httpUtil::isMiniProgram()) {
  381. $wxPayType = 1;
  382. }
  383. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  384. $wx = Yii::getAlias("@vendor/weixin");
  385. require_once($wx . '/lib/WxPay.Api.php');
  386. require_once($wx . '/example/WxPay.JsApiPay.php');
  387. $input = new \WxPayUnifiedOrder();
  388. $input->SetBody($name);
  389. $input->SetOut_trade_no($orderSn);
  390. $input->SetTotal_fee($totalFee * 100);
  391. $input->SetTime_start(date("YmdHis", $now));
  392. $input->SetAttach($attach);
  393. //设置订单有效期5分钟
  394. $input->SetTime_expire(date("YmdHis", $expireTime));
  395. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  396. $input->SetTrade_type("JSAPI");
  397. //花卉宝代为申请的微信支付
  398. $merchantExtend = $this->sjExtend->attributes;
  399. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  400. $input->SetSub_openid($openId);
  401. } else {
  402. $input->SetOpenid($openId);
  403. }
  404. //小程序使用miniAppId
  405. if (httpUtil::isMiniProgram()) {
  406. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  407. }
  408. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  409. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  410. $tools = new \JsApiPay();
  411. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  412. $newParams = json_decode($jsApiParameters, true);
  413. $newParams['orderId'] = $orderId;
  414. util::success($newParams);
  415. } elseif ($payWay == 1) {
  416. $extend = MerchantExtendService::getBySjId($sjId);
  417. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  418. util::fail('支付宝付款即将开通...');
  419. }
  420. $config = [
  421. //应用ID,您的APPID。
  422. 'app_id' => $extend['alipayPId'],
  423. //商户私钥,您的原始格式RSA私钥
  424. 'merchant_private_key' => $extend['alipayKey'],
  425. //异步通知地址
  426. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  427. //同步跳转
  428. 'return_url' => "",
  429. //编码格式
  430. 'charset' => "UTF-8",
  431. //签名方式
  432. 'sign_type' => "RSA2",
  433. //支付宝网关
  434. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  435. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  436. 'alipay_public_key' => $extend['alipayPublicKey'],
  437. ];
  438. Yii::info(json_encode($config) . ' aplipay config');
  439. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  440. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  441. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  442. $totalFee = $order['realPrice'];
  443. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  444. $subject = '购买商品';//订单名称,必填
  445. $total_amount = $totalFee;//付款金额,必填
  446. $body = '';//商品描述,可空
  447. $timeout_express = "1m";//超时时间
  448. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  449. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  450. $passBackParams = urlencode($passBackParams);
  451. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  452. $payRequestBuilder->setBody($body);
  453. $payRequestBuilder->setSubject($subject);
  454. $payRequestBuilder->setOutTradeNo($out_trade_no);
  455. $payRequestBuilder->setTotalAmount($total_amount);
  456. $payRequestBuilder->setTimeExpress($timeout_express);
  457. //在异步通知时将该参数原样返回
  458. $payRequestBuilder->setPassbackParams($passBackParams);
  459. //同步通知
  460. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  461. //异步通知
  462. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  463. $payResponse = new \AlipayTradeService($config);
  464. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  465. //直接将支付宝的html返回给前端
  466. echo $result;
  467. } elseif ($payWay == 2) {
  468. //余额支付,验证支付密码是否正确
  469. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  470. $user = UserService::getUserInfo($this->adminId);
  471. UserService::validPayPassword($payPassword, $user);
  472. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  473. $callbackParams = [];
  474. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  475. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  476. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  477. } else {
  478. util::fail('无效的支付方式');
  479. }
  480. }
  481. //订单评价
  482. public function actionComment()
  483. {
  484. $post = Yii::$app->request->post();
  485. $id = $post['id'];
  486. $order = OrderService::getById($id);
  487. OrderService::valid($order, $this->shopId);
  488. if ($order['grade'] > 0) {
  489. util::fail('您已经评过了');
  490. }
  491. OrderService::comment($post);
  492. util::complete('提交成功');
  493. }
  494. //订单详情 ssh 2019.12.16
  495. public function actionDetail()
  496. {
  497. $id = Yii::$app->request->get('id', 0);
  498. $orderSn = Yii::$app->request->get('orderSn', '');
  499. $detail = [];
  500. if (!empty($id)) {
  501. $detail = OrderService::getOrderById($id);
  502. }
  503. if (!empty($orderSn)) {
  504. $detail = OrderService::getOrderBySn($orderSn);
  505. }
  506. OrderService::valid($detail, $this->shopId);
  507. //因前端未修改,这里先做一下兼容,好像打印时要调用到
  508. $detail['receiveProvince'] = $detail['province'] ?? '';
  509. $detail['receiveCity'] = $detail['city'] ?? '';
  510. $detail['receiveAddress'] = $detail['address'] ?? '';
  511. $detail['receiveFloor'] = $detail['floor'] ?? '';
  512. util::success($detail);
  513. }
  514. public function actionList()
  515. {
  516. $get = Yii::$app->request->get();
  517. $search = isset($get['search']) ? $get['search'] : '';
  518. $where = [];
  519. $where['shopId'] = $this->shopId;
  520. $status = $get['status'] ?? 0;
  521. if (!empty($status)) {
  522. $where['status'] = $get['status'];
  523. }
  524. if (!empty($search)) {
  525. if (stringUtil::isMobile($search)) {
  526. $where['bookMobile'] = $search;
  527. } else {
  528. $where['orderSn'] = $search;
  529. }
  530. }
  531. $searchTime = $get['searchTime'] ?? '';
  532. if (!empty($searchTime)) {
  533. $startTime = $get['startTime'] ?? '';
  534. $endTime = $get['endTime'] ?? '';
  535. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  536. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  537. }
  538. $list = OrderService::getOrderList($where);
  539. $list['shop'] = $this->shop->attributes;
  540. util::success($list);
  541. }
  542. //订单归类 ssh 2019.12.17
  543. public function actionClassify()
  544. {
  545. $post = Yii::$app->request->post();
  546. $id = isset($post['id']) ? $post['id'] : 0;
  547. $order = OrderService::getById($id);
  548. OrderService::valid($order, $this->shopId);
  549. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  550. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  551. OrderService::classify($id, $categoryId, $usageId);
  552. util::complete();
  553. }
  554. //更新配送单 ssh 2019.12.17
  555. public function actionUpdateSheet()
  556. {
  557. $post = Yii::$app->request->post();
  558. $id = isset($post['id']) ? $post['id'] : 0;
  559. unset($post['id']);
  560. $order = OrderService::getById($id);
  561. OrderService::valid($order, $this->shopId);
  562. OrderService::updateSheet($order, $post);
  563. util::complete('提交成功');
  564. }
  565. //商家收款与发货 ssh 2019.12.18
  566. public function actionGathering()
  567. {
  568. $post = Yii::$app->request->post();
  569. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  570. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  571. $userId = isset($post['userId']) ? $post['userId'] : 0;
  572. $userInfo = UserService::getById($userId);
  573. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  574. util::fail('您选择的客户无效');
  575. }
  576. if ($price <= 0) {
  577. util::fail('请输入金额');
  578. }
  579. $post['prePrice'] = $price;
  580. $post['actPrice'] = $price;
  581. $post['payWay'] = $payWay;
  582. $post['sourceType'] = 1;
  583. $post['userId'] = $userId;
  584. $post['goodsNum'] = 1;
  585. $post['orderName'] = '商品';
  586. $shopId = MerchantService::getDefaultShopId($this->sj);
  587. $post['shopId'] = $shopId;
  588. $order = OrderService::addOrder($post);
  589. $id = $order['id'];
  590. $orderSn = $order['orderSn'];
  591. //流水类型列表
  592. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  593. $callbackParams = [];
  594. switch ($payWay) {
  595. case 0:
  596. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  597. break;
  598. case 1:
  599. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  600. break;
  601. case 2:
  602. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  603. break;
  604. default:
  605. util::fail('无效支付方式');
  606. }
  607. util::success($order);
  608. }
  609. //免发货管理 ssh 2020.1.6
  610. public function actionWithoutSend()
  611. {
  612. $id = Yii::$app->request->get('id');
  613. $order = OrderService::getById($id);
  614. OrderService::valid($order, $this->shopId);
  615. //配送流程变更
  616. $currentFlow = OrderSendClass::withoutSend($order);
  617. util::success(['currentFlow' => $currentFlow]);
  618. }
  619. //修改价格 ssh 2020.1.6
  620. public function actionUpdatePrice()
  621. {
  622. $id = Yii::$app->request->get('id');
  623. $price = Yii::$app->request->get('price', 1);
  624. $order = OrderService::getById($id);
  625. OrderService::valid($order, $this->shopId);
  626. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  627. util::fail('已发起支付,不能修改价格');
  628. }
  629. OrderService::updateById($id, ['actPrice' => $price]);
  630. util::complete('修改成功');
  631. }
  632. //配送单 ssh 2020.2.29
  633. public function actionGetDeliverDetail()
  634. {
  635. $id = Yii::$app->request->get('id', 0);
  636. $order = OrderService::getById($id);
  637. OrderService::valid($order, $this->shopId);
  638. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  639. $reachPeriodId = $order['reachPeriod'];
  640. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  641. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  642. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  643. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  644. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  645. $bookName = '--';
  646. $bookMobile = '--';
  647. }
  648. $data = [
  649. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  650. 'orderSn' => $order['orderSn'],
  651. 'receiveUserName' => $order['receiveUserName'],
  652. 'receiveMobile' => $order['receiveMobile'],
  653. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  654. 'cardInfo' => $order['cardInfo'],
  655. 'bookMobile' => $bookMobile,
  656. 'bookName' => $bookName,
  657. 'remark' => $order['remark'],
  658. 'sendNum' => $order['sendNum'],
  659. 'telephone' => 18030142050,
  660. 'printTime' => '',
  661. 'img' => '',
  662. ];
  663. util::success($data);
  664. }
  665. //运费计算 linqh 2021.4.12 暂反回固定
  666. public function actionFreight()
  667. {
  668. util::success(['sedCost' => 10]);
  669. }
  670. }