、⽀付宝⼆码合⼀扫码⽀付实现思路(java)
⼀、⽀付⼆维码(预订单)
根据需要购买的信息创建预订单,将订单信息保存到Redis中,并设置有效期,注意⽣产⼆维码的链接后的参数可以关联到Redis中的key;QRCode  为servlet扫码请求的URL;
⼆、创建⼆维码扫码请求地址servlet:QRCodeServlet;⽀付重定向请求servlet:WechatPayServlet;⽀付宝重定向请求servlet:AliPayServlet;QRCodeServlet  ⽤于⽤户使⽤或者⽀付宝扫码⼆维码进⾏客户端识别及重定向到对应的业务处理;
package com.platform.cloudlottery.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.t.support.SpringBeanAutowiringSupport;
import com.platform.cloudlotterymon.CommonConfig;
import com.platform.fig.MyAliPayConfig;
import com.platform.fig.WXPublicConfig;
import com.platform.cloudlotterymon.wechat.util.HttpUtil;
import com.del.SysPayChannel;
import com.platform.cloudlottery.service.Impl.SysPayChannelServiceImpl;
import com.platform.cloudlottery.web.StatusContant.PayTypeConstant;
/**
* @ClassName: QRCodeServlet
* @Description: TODO(根据请求的后缀获取该数据编码对应的数据,并重定向到页⾯)
* @author chenkun
* @date 2018年12⽉29⽇
*
*/
public class QRCodeServlet extends HttpServlet {
private static final long serialVersionUID = -8457626626670970403L;
protected Logger logger = Logger(getClass());
private static final String UrlStr = "QRCode/";
private static final String wechatPay = "wechatPay/";
private static final String aliPay = "aliPay/";
@Autowired
private SysPayChannelServiceImpl payChannelService;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, ServletContext());
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info(">>>>请求开始>>>>");
String userAgent = Header("user-agent");
String RequestURL = RequestURL().toString();
logger.info("URL : " + RequestURL);
String ReqInfo = RequestURL.substring(RequestURL.indexOf(UrlStr)+UrlStr.length());
logger.info("URL : " + ReqInfo);
CommonConfig commonConfig = new CommonConfig();
if (userAgent != null && ains("AlipayClient")) {
logger.info("来⾃⽀付宝");
String redirecturi = HttpUtil.Domain() + aliPay + ReqInfo);
logger.info("REDIRECT_URI="+redirecturi);
SysPayChannel channel = payChannelService.selectByChannelType(PayTypeConstant.Alipay);
MyAliPayConfig aliPayConfig = new MyAliPayConfig();
aliPayConfig.Appid());
// 授权页⾯地址
String requestUrl = Authgateway();
requestUrl = place("APPID", AppId()).replace("SCOPE", Scope()).replace("REDIRECT_URI", redirecturi);
// 重定向到授权页⾯
response.sendRedirect(requestUrl);
} else if (userAgent != null && ains("MicroMessenger")) {
logger.info("来⾃");
String redirecturi = HttpUtil.Domain() + wechatPay + ReqInfo);
logger.info("REDIRECT_URI="+redirecturi);
SysPayChannel channel = payChannelService.selectByChannelType(PayTypeConstant.Wechat);
WXPublicConfig publicConfig = new WXPublicConfig();
publicConfig.Appid());
springframework作用
publicConfig.Originid());
publicConfig.Appsecret());
publicConfig.Encodingaeskey());
// 授权页⾯地址
String requestUrl = Authorizeinterface();
requestUrl = place("APPID", AppId()).replace("REDIRECT_URI", redirect
uri).replace("SCOPE", Scope()).replace("STATE", State());  // 重定向到授权页⾯
response.sendRedirect(requestUrl);
} else {
logger.info("未知来源");
}
logger.info(">>>>请求结束>>>>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
WechatPayServlet 在获取到Redis中预订单数据后,创建真实订单并调⽤“统⼀下单接⼝”;
package com.platform.cloudlottery.servlet;
import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPayUtil;
import com.platform.cloudlotterymon.CommonConfig;
import com.platform.cloudlotterymon.jedis.JedisUtil;
import com.platform.cloudlotterymon.lang.StringUtils;
import com.platform.cloudlotterymon.utils.BusinessCodeUtils;
import com.platform.cloudlotterymon.wechat.bean.WeiXinOAuth2Token;
import com.platform.cloudlotterymon.wechat.bean.WeiXinUserInfo;
import com.platform.fig.WXPayConfig;
import com.platform.fig.WXPublicConfig;
import com.platform.cloudlotterymon.wechat.util.WeiXinOAuth2Util;
import com.platform.cloudlotterymon.wechat.util.WeiXinPayUtils;
import com.del.SysPayChannel;
import com.platform.cloudlottery.service.Impl.LotteryOrderServiceImpl;
import com.platform.cloudlottery.service.Impl.SysPayChannelServiceImpl;
import com.platform.cloudlottery.service.Impl.UserMemberServiceImpl;
import com.platform.cloudlottery.service.OrderServcie;
import com.platform.cloudlottery.service.UserInfoService;
import com.platform.cloudlottery.web.ResultContant;
import com.platform.cloudlottery.web.StatusContant.PayTypeConstant;
import com.platform.cloudlottery.web.SysKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.t.support.SpringBeanAutowiringSupport;
import redis.clients.jedis.Jedis;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @ClassName: WechatPayServlet
* @Description: TODO(这⾥⽤⼀句话描述这个类的作⽤)
* @author chenkun
* @date 2019年1⽉5⽇
*
*/
public class WechatPayServlet extends HttpServlet {
private static final long serialVersionUID = -8457626626670970403L;
protected Logger logger = Logger(getClass());
private static Jedis redis = Jedis();
@Value("${config.domain}")
private String domain;
@Value("${config.isProduction}")
private boolean isProduction;
/
/ 请求路径包含的字符串
private static final String UrlStr = "wechatPay/";
@Autowired
private SysPayChannelServiceImpl payChannelService;
@Autowired
private UserMemberServiceImpl memberService;
@Autowired
private LotteryOrderServiceImpl lotteryOrderService;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, ServletContext());
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.debug(">>>>请求开始>>>>");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
try {
// ⽤户同意授权后,能获取到code
String code = Parameter("code");
// ⽤户同意授权
if (!"authdeny".equals(code)) {
CommonConfig commonConfig = new CommonConfig();
String RequestURL = RequestURL().toString();
logger.debug("URL : " + RequestURL);
String QRCodeUrl = RequestURL.substring(RequestURL.indexOf(UrlStr) + UrlStr.length());
String QRCodeReqInfo = QRCodeUrl.split("&")[0];
String operatorId = QRCodeUrl.split("&")[1];
logger.debug("QRCodeReqInfo : " + QRCodeReqInfo +";operatorId : " + operatorId);
String advancekey = Lotteryorder() + QRCodeReqInfo;
SysPayChannel channel = payChannelService.selectByChannelType(PayTypeConstant.Wechat);
WXPublicConfig publicConfig = new WXPublicConfig();
publicConfig.Appid());
publicConfig.Originid());
publicConfig.Appsecret());
publicConfig.Encodingaeskey());
WXPayConfig payConfig = new WXPayConfig();
payConfig.Mchid());
payConfig.Appid());
payConfig.Wxkey());
payConfig.PayCertUrl());
payConfig.Spbillcreateip());
// 获取⽹页授权access_token
WeiXinOAuth2Token weixinOauth2Token = OAuth2AccessToken(publicConfig,code);
// ⽹页授权接⼝访问凭证
String accessToken = AccessToken();
logger.debug("accessToken=" + accessToken);
// ⽤户标识
String openId = OpenId();
logger.debug("openId="+openId);
// 获取⽤户信息
WeiXinUserInfo userInfo = OAuth2UserInfo(publicConfig, accessToken, openId);
logger.NickName()+"=====⽀付====="+OpenId());
//添加或更新⽤户信息
String userid = UserInfoService.CreateUserMember(userInfo,memberService);
if (!ists(advancekey)) {// 判断key是否存在
logger.debug("⼆维码失效");
request.setAttribute("code", userdqrcode);
request.setAttribute("message", "⼆维码失效");
logger.debug(">>>>请求结束>>>>");
return;
}
if ((advancekey)).equals("")) {
logger.debug("⼆维码失效");
request.setAttribute("code", userdqrcode);
request.setAttribute("message", "⼆维码失效");
logger.debug(">>>>请求结束>>>>");
return;
}
JSONObject jsonObject = JSONObject.(advancekey));
if (null == jsonObject) {
logger.debug("⼆维码失效");
request.setAttribute("code", userdqrcode);
request.setAttribute("message", "⼆维码失效");
logger.debug(">>>>请求结束>>>>");
return;
}
if ((advancekey + "_lock") != null && !(advancekey + "_lock").equals("")){
logger.debug("⼆维码失效");
request.setAttribute("code", userdqrcode);
request.setAttribute("message", "⼆维码失效");
logger.debug(">>>>请求结束>>>>");
return;
}
redis.setex(advancekey + "_lock", 1, "lock");
String orderid = String(SysKey.deviceSn));
int money = );
int lotterytype = IntValue(SysKey.lotteryType);
if (!orderid.equals("") && money!=0) {
//创建订单
boolean bool = ateorder(QRCodeReqInfo, PayTypeConstant.Wechat, AppID(), userid, openId, orderid, jsonObject, lotteryOrderService);  if (bool) {
//删除预订单信息
redis.del(advancekey);//⼀个预订单只能创建⼀个订单
String paymoney = String.valueOf(money);
if (!isProduction) {//测试环境
paymoney = BigDecimal.valueOf(Long.valueOf(paymoney)).divide(new BigDecimal(100)).toString();//真实⾦额除100
}
logger.debug("是否⽣产环境:"+isProduction+";订单⾦额为:"+String.valueOf(money)+";实际⽀付⾦额为:"+paymoney);
//创建订单
Map<String, String> map = ateOrderJsapi(domain, payConfig, orderid, paymoney, lotterytype==0?"即开票":"电脑票", openId);
logger.debug("创建⽀付预订单返回数据:"+JSONString(map));
if (map != null) {
if (("return_code").equals("SUCCESS")) {
if (("result_code").equals("SUCCESS")) {
logger.debug("创建⽀付预订单成功");
Map<String, String> data = new LinkedHashMap<String, String>();
data.put("appId", AppID());
data.put("timeStamp", String.valueOf(new Date().getTime()/1000));
data.put("nonceStr", ateNonceStr());
data.put("package", "prepay_id="+("prepay_id"));
data.put("signType", "MD5");
data.put("paySign", ateSignature(data, Key()));
logger.debug("返回到客户端的数据:"+JSONString(data));
request.setAttribute("appId", ("appId"));
request.setAttribute("timeStamp", ("timeStamp"));
request.setAttribute("nonceStr", ("nonceStr"));
request.setAttribute("package", ("package"));
request.setAttribute("signType", ("signType"));
request.setAttribute("paySign", ("paySign"));
request.setAttribute("code", ResultContant.sucess);
request.setAttribute("message", "成功");
}else{
logger.debug("创建订单失败:创建⽀付预订单失败");
request.setAttribute("code", ateordererror);
request.setAttribute("message", "创建订单失败");
}
} else {
logger.debug("创建订单失败:创建⽀付预订单失败");
request.setAttribute("code", ateordererror);
request.setAttribute("message", "创建订单失败");
}
} else {
logger.debug("创建订单失败:创建⽀付预订单失败");
request.setAttribute("code", ateordererror);
request.setAttribute("message", "创建订单失败");
}
} else {
logger.debug("创建订单失败:创建⽀付预订单失败");
request.setAttribute("code", ateordererror);
request.setAttribute("message", "创建订单失败");
}
} else {
logger.debug("创建订单失败;订单⾦额或者订单号数据有误");
request.setAttribute("code", ateordererror);
request.setAttribute("message", "创建订单失败");
}
}
} catch (Exception e) {
e.printStackTrace();
logger.debug("系统异常");
request.setAttribute("code", userdqrcode);
request.setAttribute("message", "⼆维码失效");
}
logger.debug(">>>>请求结束>>>>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
}
}
AliPayServlet  在获取到Redis中预订单数据后,创建真实订单并调⽤⽀付宝“⼿机⽹站⽀付接⼝”;package com.platform.cloudlottery.servlet;
import com.alibaba.fastjson.JSONObject;
import com.platform.cloudlotterymon.CommonConfig;
import com.platform.cloudlotterymon.alipay.bean.AliPayOAuth2Token;
import com.platform.cloudlotterymon.alipay.bean.AliPayUserInfo;
import com.platform.fig.MyAliPayConfig;
import com.platform.cloudlotterymon.alipay.uitl.AliPayOAuth2Util;
import com.platform.cloudlotterymon.alipay.uitl.AlipayPayUtils;
import com.platform.cloudlotterymon.jedis.JedisUtil;
import com.platform.cloudlotterymon.lang.StringUtils;
import com.platform.cloudlotterymon.properties.PropertiesUtils;
import com.platform.cloudlotterymon.utils.BusinessCodeUtils;
import com.del.SysPayChannel;
import com.platform.cloudlottery.service.Impl.LotteryOrderServiceImpl;
import com.platform.cloudlottery.service.Impl.SysPayChannelServiceImpl;
import com.platform.cloudlottery.service.Impl.UserMemberServiceImpl;
import com.platform.cloudlottery.service.OrderServcie;
import com.platform.cloudlottery.service.UserInfoService;
import com.platform.cloudlottery.web.ResultContant;
import com.platform.cloudlottery.web.StatusContant.PayTypeConstant;
import com.platform.cloudlottery.web.SysKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.t.support.SpringBeanAutowiringSupport;
import redis.clients.jedis.Jedis;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Properties;
/**
* @ClassName: AliPayServlet
* @Description: TODO(这⾥⽤⼀句话描述这个类的作⽤)
* @author chenkun
* @date 2019年1⽉5⽇
*
*/
public class AliPayServlet extends HttpServlet {
private static final long serialVersionUID = -8457626626670970403L;
protected Logger logger = Logger(getClass());
private static Jedis redis = Jedis();
@Value("${config.domain}")
private String domain;
@Value("${config.isProduction}")
private boolean isProduction;
// 请求路径包含的字符串
private static final String UrlStr = "aliPay/";
@Autowired
private SysPayChannelServiceImpl payChannelService;
@Autowired
private UserMemberServiceImpl memberService;
@Autowired
private LotteryOrderServiceImpl lotteryOrderService;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, ServletContext());
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.debug(">>>>请求开始>>>>");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
try {
//⽤户同意授权后,能获取到code
String code = Parameter("auth_code");
/
/⽤户同意授权
if (!code.equals("")) {
CommonConfig commonConfig = new CommonConfig();
//具体业务
String RequestURL = RequestURL().toString();
logger.debug("URL : " + RequestURL);
String QRCodeUrl = RequestURL.substring(RequestURL.indexOf(UrlStr) + UrlStr.length());
String QRCodeReqInfo = QRCodeUrl.split("&")[0];
String operatorId = QRCodeUrl.split("&")[1];
logger.debug("QRCodeReqInfo : " + QRCodeReqInfo +";operatorId : " + operatorId);
String advancekey = Lotteryorder() + QRCodeReqInfo;
SysPayChannel channel = payChannelService.selectByChannelType(PayTypeConstant.Alipay);
MyAliPayConfig aliPayConfig = new MyAliPayConfig();
aliPayConfig.Appid());
String certsrc = PayCertUrl();
Properties propertiesFile = PropertiesFile(certsrc);
if (propertiesFile != null) {
aliPayConfig.Property("ALI_PAYEE_ACCOUNT"));
aliPayConfig.Property("ALI_APP_ID"));
aliPayConfig.Property("ALI_ALIPAY_PUBLIC_KEY"));
aliPayConfig.Property("ALI_APP_PAY_PUBLIC_KEY"));
aliPayConfig.Property("ALI_APP_PRIVATE_KEY"));
}
//获取⽹页授权access_token
AliPayOAuth2Token aliPayOAuth2Token = OAuth2AccessToken(aliPayConfig,code);
//⽹页授权接⼝访问凭证
String accessToken = AccessToken();
logger.debug("accessToken=" + accessToken);
//⽤户标识
String aliuserid = Userid();
logger.debug("aliuserid="+aliuserid);
//获取⽤户信息
AliPayUserInfo userInfo = OAuth2UserInfo(aliPayConfig,accessToken,aliuserid);
logger.NickName()+"=====⽀付宝⽀付====="+UserId());
//添加或更新⽤户信息
String userid = UserInfoService.CreateUserMember(userInfo,memberService);
if (!ists(advancekey)) {// 判断key是否存在
logger.debug("⼆维码失效");
request.setAttribute("code", userdqrcode);
request.setAttribute("message", "⼆维码失效");
logger.debug(">>>>请求结束>>>>");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论