OrderController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\merchant\services\ExpressService;
  4. use biz\merchant\services\MerchantAssetService;
  5. use biz\merchant\services\MerchantExtendService;
  6. use biz\merchant\services\MerchantService;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\merchant\services\ShopService;
  9. use biz\order\classes\OrderSendClass;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizGhs\order\services\OrderService;
  12. use biz\saas\services\RegionService;
  13. use biz\user\services\UserService;
  14. use common\components\configDict;
  15. use common\components\httpUtil;
  16. use common\services\xhPayToolService;
  17. use Yii;
  18. use common\components\util;
  19. use common\components\stringUtil;
  20. class OrderController extends BaseController
  21. {
  22. public $withoutLogin = ['order-relate', 'fast-pay'];
  23. //商城下单操作 shish 2021.1.14
  24. public function actionCreateOrder()
  25. {
  26. $post = Yii::$app->request->post();
  27. $adminId = $this->adminId;
  28. $shopId = $this->shopId;
  29. if(!empty($shopId)){
  30. //供货商端 员工 开单
  31. $post['customId'] = $post['customId'] ?? 0;
  32. $post['adminId'] = $adminId;
  33. $post['userId'] = 0;
  34. }else{
  35. //供货商端 客户 开单
  36. $post['userId'] = $adminId;
  37. $post['adminId'] = 0;
  38. $post['customId'] = CustomClass::addCustom();
  39. }
  40. //零售端口:员工采购
  41. $post['payWay'] = $this->isWx == true ? 0 : 1;
  42. $post['ghsId'] = $this->ghsId;
  43. $post['shopId'] = 0;
  44. $now = time();
  45. $expireTime = $now + 1800;//订单30分钟后过期
  46. $post['deadline'] = $expireTime;
  47. $post['fromType'] = 1;//商城
  48. $post['prePrice'] = $prePrice;
  49. $post['actPrice'] = $actPrice;
  50. $order = OrderClass::addOrder($post);
  51. $orderId = $order['id'];
  52. $orderSn = $order['orderSn'];
  53. //花材商品
  54. $itemData = [
  55. [
  56. 'orderId'=>$orderId,
  57. 'itemId'=>0,
  58. 'productId'=>0,
  59. 'unitPrice'=>0.01,
  60. 'unitNum'=>0,
  61. 'num'=>0,
  62. ],
  63. ];
  64. //OrderGoodsService::add($data);//创建订单商品列表
  65. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice]);
  66. }
  67. //下单要用到的相关信息 shish 2019.12.6
  68. public function actionOrderRelate()
  69. {
  70. $regionTree = RegionService::tree();
  71. $shop = ShopService::getDefaultShop($this->ghs);
  72. $freight = MerchantExtendService::getFreight($this->ghsExtend);
  73. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  74. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->ghs];
  75. util::success($out);
  76. }
  77. //余额支付 shish 2019.12.6
  78. public function actionBalancePay()
  79. {
  80. $post = Yii::$app->request->post();
  81. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  82. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  83. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  84. //验证优惠券是否还有效
  85. if (!empty($couponId)) {
  86. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  87. }
  88. $order = OrderService::getByOrderSn($orderSn);
  89. $userId = $this->adminId;
  90. $user = UserService::getUserInfo($userId, false);
  91. //验证支付密码是否正确
  92. UserService::validPayPassword($payPassword, $user);
  93. //支付前验证订单有效性
  94. OrderService::checkBeforePay($order, $this->adminId);
  95. $typeList = configDict::getConfig('capitalType');
  96. $capitalType = $typeList['xhOrder']['id'];
  97. $totalFee = $order['prePrice'];
  98. $sourceType = 0;
  99. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  100. $actPrice = $discountData['price'];
  101. $callbackParams = [];
  102. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  103. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  104. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  105. }
  106. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  107. public function actionWxPay()
  108. {
  109. ini_set('date.timezone', 'Asia/Shanghai');
  110. $post = Yii::$app->request->post();
  111. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  112. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  113. $order = OrderService::getByOrderSn($orderSn);
  114. $orderId = $order['id'];
  115. //验证优惠券是否还有效
  116. if (!empty($couponId)) {
  117. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  118. }
  119. //支付前验证订单有效性
  120. OrderService::checkBeforePay($order, $this->adminId);
  121. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  122. $totalFee = $order['actPrice'];
  123. //小程序使用miniOpenId
  124. if (httpUtil::isMiniProgram()) {
  125. $openId = $this->admin['miniOpenId'];
  126. } else {
  127. $openId = $this->admin['openId'];
  128. }
  129. $typeList = configDict::getConfig('capitalType');
  130. $capitalType = $typeList['xhOrder']['id'];
  131. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  132. $wxPayType = 0;
  133. if (httpUtil::isMiniProgram()) {
  134. $wxPayType = 1;
  135. }
  136. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  137. //订单30分钟后过期
  138. $now = time();
  139. $expireTime = $now + 1800;
  140. $wx = Yii::getAlias("@vendor/weixin");
  141. require_once($wx . '/lib/WxPay.Api.php');
  142. require_once($wx . '/example/WxPay.JsApiPay.php');
  143. $input = new \WxPayUnifiedOrder();
  144. $input->SetBody($name);
  145. $input->SetOut_trade_no($orderSn);
  146. $input->SetTotal_fee($totalFee * 100);
  147. $input->SetTime_start(date("YmdHis", $now));
  148. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  149. $input->SetTime_expire(date("YmdHis", $expireTime));
  150. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  151. $input->SetTrade_type("JSAPI");
  152. //花卉宝代为申请的微信支付
  153. $merchantExtend = $this->ghsExtend;
  154. if ($merchantExtend['wxPayApply'] == 1) {
  155. $input->SetSub_openid($openId);
  156. } else {
  157. $input->SetOpenid($openId);
  158. }
  159. //小程序使用miniAppId
  160. if (httpUtil::isMiniProgram()) {
  161. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  162. }
  163. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  164. $tools = new \JsApiPay();
  165. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  166. $newParams = json_decode($jsApiParameters, true);
  167. //已请求微信不能修改价格
  168. $updateData = [];
  169. $updateData['modPrice'] = 0;
  170. if (empty($order['deadline'])) {
  171. $updateData['deadline'] = $expireTime;
  172. }
  173. OrderService::updateById($orderId, $updateData);
  174. util::success($newParams);
  175. }
  176. //快捷付款订单
  177. public function actionFastPay()
  178. {
  179. ini_set('date.timezone', 'Asia/Shanghai');
  180. $post = Yii::$app->request->post();
  181. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  182. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->ghs);
  183. //验证门店是否有效
  184. $shopInfo = ShopService::getById($shopId);
  185. ShopService::valid($shopInfo, $this->ghsId);
  186. $post['shopId'] = $shopId;
  187. //默认门店订单
  188. $store = isset($post['store']) ? $post['store'] : 1;
  189. $post['store'] = $store;
  190. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  191. $sourceType = $this->isWx ? 0 : 1;
  192. if (httpUtil::isMiniProgram()) {
  193. $sourceType = 2;
  194. }
  195. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  196. //兼容旧系统sourceType=3表示朋友圈来源
  197. $fromType = $sourceType == 3 ? 2 : 0;
  198. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  199. $post['userId'] = $this->adminId;
  200. $user = $this->admin;
  201. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  202. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  203. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  204. $post['bookMobile'] = $bookMobile;
  205. $prePrice = round($post['prePrice'], 2);
  206. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  207. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  208. $actPrice = $discountData['price'];
  209. $post['discountType'] = $discountData['discountType'];
  210. $post['discountAmount'] = $discountData['discountAmount'];
  211. $post['actPrice'] = $actPrice;
  212. $post['payStyle'] = 0;
  213. $post['merchantId'] = $this->ghsId;
  214. $now = time();
  215. $expireTime = $now + 300;//订单5分钟后过期
  216. $post['createTime'] = date("Y-m-d H:i:s", $now);
  217. $post['deadline'] = $expireTime;
  218. if ($actPrice <= 0) {
  219. util::fail('请填写正确的金额');
  220. }
  221. //微信支付订单提交不能修改价格
  222. $post['modPrice'] = $this->isWx ? 0 : 1;
  223. $post['sourceType'] = $sourceType;
  224. $post['goodsNum'] = 1;
  225. $post['fromType'] = $fromType;
  226. $order = OrderService::addOrder($post);
  227. $orderId = $order['id'];
  228. $orderSn = $order['orderSn'];
  229. $merchantId = $this->ghsId;
  230. if ($payWay == 0) {
  231. $orderId = $order['id'];
  232. $name = '购买商品';
  233. $totalFee = $actPrice;
  234. $user = UserService::getById($this->adminId);
  235. $openId = isset($user['openId']) ? $user['openId'] : 0;
  236. if (httpUtil::isMiniProgram()) {
  237. //小程序使用miniOpenId
  238. $openId = $user['miniOpenId'];
  239. }
  240. if (empty($openId)) {
  241. util::fail('没有找到客户的openId');
  242. }
  243. $typeList = configDict::getConfig('capitalType');
  244. $capitalType = $typeList['xhOrder']['id'];
  245. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  246. $wxPayType = 0;
  247. if (httpUtil::isMiniProgram()) {
  248. $wxPayType = 1;
  249. }
  250. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  251. $wx = Yii::getAlias("@vendor/weixin");
  252. require_once($wx . '/lib/WxPay.Api.php');
  253. require_once($wx . '/example/WxPay.JsApiPay.php');
  254. $input = new \WxPayUnifiedOrder();
  255. $input->SetBody($name);
  256. $input->SetOut_trade_no($orderSn);
  257. $input->SetTotal_fee($totalFee * 100);
  258. $input->SetTime_start(date("YmdHis", $now));
  259. $input->SetAttach($attach);
  260. //设置订单有效期5分钟
  261. $input->SetTime_expire(date("YmdHis", $expireTime));
  262. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  263. $input->SetTrade_type("JSAPI");
  264. //花卉宝代为申请的微信支付
  265. $merchantExtend = $this->ghsExtend;
  266. if ($merchantExtend['wxPayApply'] == 1) {
  267. $input->SetSub_openid($openId);
  268. } else {
  269. $input->SetOpenid($openId);
  270. }
  271. //小程序使用miniAppId
  272. if (httpUtil::isMiniProgram()) {
  273. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  274. }
  275. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  276. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  277. $tools = new \JsApiPay();
  278. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  279. $newParams = json_decode($jsApiParameters, true);
  280. $newParams['orderId'] = $orderId;
  281. util::success($newParams);
  282. } elseif ($payWay == 1) {
  283. $extend = MerchantExtendService::getByMerchantId($merchantId);
  284. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  285. util::fail('支付宝付款即将开通...');
  286. }
  287. $config = [
  288. //应用ID,您的APPID。
  289. 'app_id' => $extend['alipayPId'],
  290. //商户私钥,您的原始格式RSA私钥
  291. 'merchant_private_key' => $extend['alipayKey'],
  292. //异步通知地址
  293. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/alipay-callback",
  294. //同步跳转
  295. 'return_url' => "",
  296. //编码格式
  297. 'charset' => "UTF-8",
  298. //签名方式
  299. 'sign_type' => "RSA2",
  300. //支付宝网关
  301. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  302. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  303. 'alipay_public_key' => $extend['alipayPublicKey'],
  304. ];
  305. Yii::info(json_encode($config) . ' aplipay config');
  306. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  307. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  308. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  309. $totalFee = $order['actPrice'];
  310. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  311. $subject = '购买商品';//订单名称,必填
  312. $total_amount = $totalFee;//付款金额,必填
  313. $body = '';//商品描述,可空
  314. $timeout_express = "1m";//超时时间
  315. $typeList = configDict::getConfig('capitalType');
  316. $capitalType = $typeList['xhOrder']['id'];
  317. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  318. $passbackParams = urlencode($passbackParams);
  319. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  320. $payRequestBuilder->setBody($body);
  321. $payRequestBuilder->setSubject($subject);
  322. $payRequestBuilder->setOutTradeNo($out_trade_no);
  323. $payRequestBuilder->setTotalAmount($total_amount);
  324. $payRequestBuilder->setTimeExpress($timeout_express);
  325. //在异步通知时将该参数原样返回
  326. $payRequestBuilder->setPassbackParams($passbackParams);
  327. //同步通知
  328. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$merchantId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  329. //异步通知
  330. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/alipay-callback";
  331. $payResponse = new \AlipayTradeService($config);
  332. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  333. //直接将支付宝的html返回给前端
  334. echo $result;
  335. } elseif ($payWay == 2) {
  336. //余额支付,验证支付密码是否正确
  337. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  338. $user = UserService::getUserInfo($this->adminId, false);
  339. UserService::validPayPassword($payPassword, $user);
  340. $typeList = configDict::getConfig('capitalType');
  341. $capitalType = $typeList['xhOrder']['id'];
  342. $callbackParams = [];
  343. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  344. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  345. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  346. } else {
  347. util::fail('无效的支付方式');
  348. }
  349. }
  350. }