OrderController.php 32 KB

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