소스 검색

客户充值

shish 6 년 전
부모
커밋
b3acbf147b
1개의 변경된 파일97개의 추가작업 그리고 0개의 파일을 삭제
  1. 97 0
      app/client/controllers/RechargeController.php

+ 97 - 0
app/client/controllers/RechargeController.php

@@ -0,0 +1,97 @@
+<?php
+
+namespace client\controllers;
+
+use biz\user\services\UserService;
+use common\components\httpUtil;
+use common\components\stringUtil;
+use common\components\util;
+use common\services\xhRechargeService;
+use Yii;
+use yii\web\Controller;
+
+class RechargeController extends BaseController
+{
+	
+	//发起充值 shish 2019.12.19
+	public function actionLaunch()
+	{
+		$rechargeAmount = Yii::$app->request->get('amount', 1);
+		$account = $this->merchantId;
+		$merchant = $this->merchant;
+		$extend = $this->merchantExtend;
+		$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'] = $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'];
+		$orderSn = $payment['orderSn'];
+		
+		UserService::updateVisitTime($merchantId, $userId);
+
+		$totalFee = $rechargeAmount;
+		$name = '充值' . $totalFee . '元';
+
+		//小程序使用miniOpenId
+		if (httpUtil::isMiniProgram()) {
+			$openId = $user['miniOpenId'];
+		} else {
+			$openId = $user['openId'];
+		}
+		if (empty($openId)) {
+			util::fail('没有找到用户信息 wx');
+		}
+
+		$typeList = configDict::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['generalMerchant'] == 1) {
+			//$input->SetOpenid($openId);
+			$input->SetSub_openid($openId);
+			//自设置 微信支付
+		} else {
+			$input->SetOpenid($openId);
+		}
+		//小程序使用miniAppId
+		if(httpUtil::isMiniProgram()){
+			$merchantExtend['wxAppId'] = $merchantExtend['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);
+	}
+	
+}