OrderController.php 17 KB

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