OrderController.php 30 KB

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