OrderController.php 25 KB

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