OrderController.php 25 KB

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