OrderController.php 25 KB

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