| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace mall\controllers;
- use bizMall\user\services\UserService;
- use common\components\httpUtil;
- use common\components\stringUtil;
- use common\components\util;
- use common\services\xhRechargeService;
- use Yii;
- class RechargeController extends BaseController
- {
-
- //发起充值 shish 2019.12.19
- public function actionLaunch()
- {
- $rechargeAmount = Yii::$app->request->get('amount', 1);
- $account = $this->sjId;
- $sj = $this->sj;
- $extend = $this->sjExtend;
- $user = $this->user;
- ini_set('date.timezone', 'Asia/Shanghai');
- if ($rechargeAmount <= 0) {
- util::fail('请填写充值金额');
- }
- $rechargeAmount = round($rechargeAmount, 2);//四舍五入,保留二位小数
- $userId = $user['id'];
- $rechargeData['userId'] = $userId;
- $rechargeData['actPrice'] = $rechargeAmount;
- $rechargeData['payStyle'] = 0;
- $rechargeData['merchantId'] = $sj['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'];
- $orderSn = $payment['orderSn'];
-
- UserService::updateVisitTime($sjId, $userId);
- $totalFee = $rechargeAmount;
- $name = '充值' . $totalFee . '元';
- //小程序使用miniOpenId
- if (httpUtil::isMiniProgram()) {
- $openId = $user['miniOpenId'];
- } else {
- $openId = $user['openId'];
- }
- if (empty($openId)) {
- util::fail('没有找到用户信息 wx');
- }
- $typeList = dict::getConfig('capitalType');
- $capitalType = $typeList['xhRecharge']['id'];
- $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
- $wx = Yii::getAlias("@vendor/weixin");
- require_once($wx . '/lib/WxPay.Api.php');
- require_once($wx . '/example/WxPay.JsApiPay.php');
- $input = new \WxPayUnifiedOrder();
- $input->SetBody($name);
- $input->SetOut_trade_no($orderSn);
- $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['wxPayApply'] == 1) {
- //$input->SetOpenid($openId);
- $input->SetSub_openid($openId);
- //自设置 微信支付
- } else {
- $input->SetOpenid($openId);
- }
- //小程序使用miniAppId
- if(httpUtil::isMiniProgram()){
- $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
- }
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
- $newParams = json_decode($jsApiParameters, true);
- $newParams['orderSn'] = $orderSn;
- util::success($newParams);
- }
-
- }
|