OrderController.php 16 KB

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