OrderController.php 30 KB

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