| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- namespace mini\controllers;
- use biz\user\services\UserService;
- use common\components\configDict;
- use common\components\stringUtil;
- use common\components\util;
- use common\services\xhMerchantExtendService;
- use common\services\xhMerchantService;
- use common\services\xhOrderService;
- use common\services\xhPayToolService;
- use common\services\xhRechargeService;
- use common\services\xhUserAssetService;
- use common\services\xhUserService;
- use Yii;
- use yii\web\Controller;
- use yii\helpers\Json;
- use linslin\yii2\curl;
- use common\components\jsSDK;
- class PayController extends BaseController
- {
-
- public $enableCsrfValidation = false;
-
- public $secretKey = 'wx3hj5a26af5c17fb86921f3';
-
- public function actionBalancePay()
- {
- $account = $this->merchantId;
- $merchant = $this->merchant;
- $payAmount = Yii::$app->request->get('payAmount', 2000);
- $shopId = $merchant['defaultShopId'];
- $user = $this->userInfo;
- ini_set('date.timezone', 'Asia/Shanghai');
- if ($payAmount < 0) {
- util::failInfo('付款金额太少');
- }
- $userId = $user['id'];
- $userAsset = xhUserAssetService::getByUserId($userId);
- $level = isset($userAsset['memberLevel']) ? $userAsset['memberLevel'] : 0;
-
- $merchantExtend = xhMerchantExtendService::getByMerchantId($account);
- $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
- $discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount'];
- $prePrice = $payAmount;
- $actPrice = $payAmount;
- //折扣后保留一位小数
- if (!empty($discount) && $prePrice >= 1) {
- $currentPrice = ($prePrice * $discount) / 100;
- $actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2);
- }
-
- $userId = $user['id'];
- $payData['userId'] = $userId;
- $payData['prePrice'] = $prePrice;
- $payData['actPrice'] = $actPrice;
- $payData['payStyle'] = 0;
- $payData['shopId'] = $shopId;
- $payData['merchantId'] = $merchant['id'];
- $now = time();
- $expireTime = $now + 300;//订单5分钟后过期
- $payData['createTime'] = date("Y-m-d H:i:s", $now);
- $payData['addTime'] = time();
- $payData['deadline'] = $expireTime;
- $payData['id'] = stringUtil::generateOrderNo($account, $userId);
- $payData['sourceType'] = 1;//订单来源:0商城订单 1付款订单
- $payment = xhOrderService::add($payData);
- $orderId = $payment['id'];
-
- $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
- $order = xhOrderService::getById($orderId);
-
- $typeList = configDict::getConfig('capitalType');
- $capitalType = $typeList['xhOrder']['id'];
- $totalFee = $order['actPrice'];
-
- $return = xhPayToolService::balancePay($orderId, $totalFee, $capitalType, $couponId);
- if ($return['code'] == 'A0001') {
- util::sendSuccess();
- }
- util::failInfo($return['msg']);
- }
-
- //小程序快速付款页 shish 2019.7.26
- public function actionGetPayParams()
- {
- $merchant = $this->merchant;
- $shopId = $merchant['defaultShopId'];
- $account = $this->merchantId;
- $extend = xhMerchantExtendService::getByMerchantId($account);
- if (empty($extend)) {
- util::failInfo('商家信息有误');
- }
- $user = $this->userInfo;
- if (empty($user)) {
- util::failInfo('没有用户信息');
- }
- ini_set('date.timezone', 'Asia/Shanghai');
- $payAmount = Yii::$app->request->get('payAmount', 2000);
- if ($payAmount < 0) {
- util::failInfo('付款金额太少');
- }
-
- $userId = $user['id'];
- $userAsset = xhUserAssetService::getByUserId($userId);
- $level = isset($userAsset['memberLevel']) ? $userAsset['memberLevel'] : 0;
-
- $memberIntegral = xhMerchantExtendService::getGradeList($extend, 'desc');
- $discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount'];
- $prePrice = $payAmount;
- $actPrice = $payAmount;
- //折扣后保留一位小数
- if (!empty($discount) && $prePrice >= 1) {
- $currentPrice = ($prePrice * $discount) / 100;
- $actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2);
- }
-
- $userId = $user['id'];
- $payData['userId'] = $userId;
- $payData['prePrice'] = $prePrice;
- $payData['actPrice'] = $actPrice;
- $payData['payStyle'] = 0;
- $payData['shopId'] = $shopId;
- $payData['merchantId'] = $merchant['id'];
- $now = time();
- $expireTime = $now + 300;//订单5分钟后过期
- $payData['createTime'] = date("Y-m-d H:i:s", $now);
- $payData['addTime'] = time();
- $payData['deadline'] = $expireTime;
- $payData['id'] = stringUtil::generateOrderNo($account, $userId);
- $payData['sourceType'] = 1;//订单来源:0商城订单 1付款订单
- $payment = xhOrderService::add($payData);
- $orderId = $payment['id'];
-
- Yii::$app->params['userId'] = $userId;
- Yii::$app->params['merchantId'] = $merchant['id'];
- UserService::updateVisitTime();
-
- $name = '买单';
- $totalFee = $actPrice;
- $openId = $user['miniOpenId'];
- if (empty($openId)) {
- util::failInfo('miniOpenId empty');
- }
-
- $typeList = configDict::getConfig('capitalType');
- $capitalType = $typeList['xhOrder']['id'];
- $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
- $weixin = Yii::getAlias("@vendor/weixin");
- require_once($weixin . '/lib/WxPay.Api.php');
- require_once($weixin . '/example/WxPay.JsApiPay.php');
- $input = new \WxPayUnifiedOrder();
- $input->SetBody($name);
- $input->SetOut_trade_no($orderId);
- $input->SetTotal_fee($totalFee * 100);
- $input->SetTime_start(date("YmdHis", $now));
- $input->SetAttach($attach);
- $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
- $input->SetNotify_url(Yii::$app->params['miniUrl'] . '/notice/mini-pay-callback/');
- $input->SetTrade_type("JSAPI");
-
- //服务商 代设置微信支付,要添加的参数设置
- if ($extend['generalMerchant'] == 1) {
- //$input->SetOpenid($openId);
- $input->SetSub_openid($openId);
- //自设置 微信支付
- } else {
- $input->SetOpenid($openId);
- }
- //这里用的wxAppId是小程序的appId
- $extend['wxAppId'] = $extend['miniAppId'];
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
- $newParams = json_decode($jsApiParameters, true);
- $newParams['orderId'] = $orderId;
- util::successInfo('操作成功', 'A0001', $newParams);
- }
-
- //小程序商品发起支付需要的参数
- public function actionGetMiniPayParams()
- {
- $id = Yii::$app->request->get('id');
- $merchant = $this->merchant;
-
- $extend = xhMerchantExtendService::getByMerchantId($this->merchantId);
-
- $payment = xhOrderService::getById($id);
- $orderId = $payment['id'];
-
- Yii::$app->params['userId'] = $this->userId;
- Yii::$app->params['merchantId'] = $merchant['id'];
- UserService::updateVisitTime();
-
- $name = '买单';
- $totalFee = $payment['actPrice'];
- $openId = $this->userInfo['miniOpenId'];
- if (empty($openId)) {
- util::failInfo('miniOpenId empty');
- }
- $now = time();
- $expireTime = $now + 300;//订单5分钟后过期
-
- $typeList = configDict::getConfig('capitalType');
- $capitalType = $typeList['xhOrder']['id'];
- $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
- $weixin = Yii::getAlias("@vendor/weixin");
- require_once($weixin . '/lib/WxPay.Api.php');
- require_once($weixin . '/example/WxPay.JsApiPay.php');
- $input = new \WxPayUnifiedOrder();
- $input->SetBody($name);
- $input->SetOut_trade_no($orderId);
- $input->SetTotal_fee($totalFee * 100);
- $input->SetTime_start(date("YmdHis", $now));
- $input->SetAttach($attach);
- $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
- $input->SetNotify_url(Yii::$app->params['miniUrl'] . '/notice/mini-pay-callback/');
- $input->SetTrade_type("JSAPI");
-
- //服务商 代设置微信支付,要添加的参数设置
- if ($extend['generalMerchant'] == 1) {
- //$input->SetOpenid($openId);
- $input->SetSub_openid($openId);
- //自设置 微信支付
- } else {
- $input->SetOpenid($openId);
- }
- //这里用的wxAppId是小程序的appId
- $extend['wxAppId'] = $extend['miniAppId'];
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
- $newParams = json_decode($jsApiParameters, true);
- $newParams['orderId'] = $orderId;
- util::successInfo('操作成功', 'A0001', $newParams);
- }
-
- public function actionGetRechargeParams()
- {
- $account = $this->merchantId;
- $merchant = $this->merchant;
- $extend = xhMerchantExtendService::getByMerchantId($account);
- if (empty($extend)) {
- util::failInfo('没有商家信息...');
- }
- $user = $this->userInfo;
- if (empty($user)) {
- util::failInfo('没有用户信息');
- }
- ini_set('date.timezone', 'Asia/Shanghai');
- $rechargeAmount = Yii::$app->request->get('rechargeAmount', 0);
- if ($rechargeAmount <= 0) {
- util::failInfo('请填写充值金额');
- }
- $rechargeAmount = round($rechargeAmount, 2);//四舍五入,保留二位小数
- $userId = $user['id'];
- $rechargeData['userId'] = $userId;
- $rechargeData['actPrice'] = $rechargeAmount;
- $rechargeData['payStyle'] = 0;
- $rechargeData['merchantId'] = $merchant['id'];
- $now = time();
- $expireTime = $now + 300;//订单5分钟后过期
- $rechargeData['createTime'] = date("Y-m-d H:i:s", $now);
- $rechargeData['addTime'] = time();
- $rechargeData['deadline'] = $expireTime;
- $rechargeData['id'] = stringUtil::generateOrderNo($account, $userId);
- $rechargeData['sourceType'] = 1;//订单来源:0商城订单 1付款订单
- $payment = xhRechargeService::add($rechargeData);
- $orderId = $payment['id'];
-
- Yii::$app->params['userId'] = $userId;
- Yii::$app->params['merchantId'] = $merchant['id'];
- UserService::updateVisitTime();
-
- $name = '充值';
- $totalFee = $rechargeAmount;
- $openId = $user['miniOpenId'];
- if (empty($openId)) {
- util::failInfo('miniOpenId empty');
- }
-
- $typeList = configDict::getConfig('capitalType');
- $capitalType = $typeList['xhRecharge']['id'];
- $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
- $weixin = Yii::getAlias("@vendor/weixin");
- require_once($weixin . '/lib/WxPay.Api.php');
- require_once($weixin . '/example/WxPay.JsApiPay.php');
- $input = new \WxPayUnifiedOrder();
- $input->SetBody($name);
- $input->SetOut_trade_no($orderId);
- $input->SetTotal_fee($totalFee * 100);
- $input->SetTime_start(date("YmdHis", $now));
- $input->SetAttach($attach);
- $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
- $input->SetNotify_url(Yii::$app->params['miniUrl'] . '/notice/weixin-recharge-callback/');
- $input->SetTrade_type("JSAPI");
-
- //服务商 代设置微信支付,要添加的参数设置
- if ($extend['generalMerchant'] == 1) {
- //$input->SetOpenid($openId);
- $input->SetSub_openid($openId);
- //自设置 微信支付
- } else {
- $input->SetOpenid($openId);
- }
- //这里用的wxAppId是小程序的appId
- $extend['wxAppId'] = $extend['miniAppId'];
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
- $newParams = json_decode($jsApiParameters, true);
- $newParams['orderId'] = $orderId;
- util::sendJson($newParams);
- }
-
- }
|