之前做过娃娃机的项目,因为直接对接微信和支付宝利率稍微高了一些,所以公司决定直接走银行:微信和支付宝调用银行的接口来完成支付
支付宝支付流程:
注册个人或者企业账号然后接入开发平台--->h5接入-->查看开发文档
当时我们做的是扫描设备二维码跳转授权登陆页面然后获取用户信息后进行充值和其他操作的:
需要导入的maven配置文件:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
这个是要看文档的哈,我们是通过扫支付宝账号跳转页面的所以是属于h5的,
判断是微信支付还是支付宝支付:然后将设备id带过去
@RequestMapping("/Landing")
public String Landing(HttpServletRequest req, HttpServletResponse response, HttpSession session) {
try {
String userAgent = req.getHeader("user-agent");
// 支付宝
if (userAgent != null && userAgent.contains("AlipayClient")) {
// String equid = req.getParameter("equipmentid");
session.setAttribute("sel_equipmentid", equid);
return this.aliPayEmpower();
// 微信
} else if (userAgent != null && userAgent.contains("MicroMessenger")) {
String APPID = Constant.APPID;
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPID + "&redirect_uri=" + "http://www.dm168.cn/start_equip" + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
String equid = req.getParameter("equipmentid");
session.setAttribute("sel_equipmentid", equid);
return "redirect:" + url;
// 其他
} else {
return "";
}
} catch (Exception e) {
System.out.println(e);
return "";
}
}
//支付宝授权登陆:
@RequestMapping("/start_equip_alipay")
public String start_equip_alipay(Model model, HttpServletRequest req, HttpServletResponse res, HttpSession session) {
String APPID = 敏感信息
String PRIVATE_Key = 敏感信息
String ALIPAY_PUBLIC_KEY =敏感信息
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APPID, PRIVATE_Key, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2");
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
// 获取token 和userid
String code = req.getParameter("auth_code");
request.setCode(code);
request.setGrantType("authorization_code");
try {
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
String accessToken = response.getAccessToken();
String userId = response.getUserId();
// 根据token获取用户信息
AlipayClient alipayUserMsg = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APPID, PRIVATE_Key, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2");
AlipayUserInfoShareRequest requestUserMsg = new AlipayUserInfoShareRequest();
try {
AlipayUserInfoShareResponse userinfoShareResponse = alipayUserMsg.execute(requestUserMsg, accessToken);
if (userinfoShareResponse.isSuccess()) {
System.out.print(userinfoShareResponse.getBody());
String openid = userinfoShareResponse.getUserId();
String name = userinfoShareResponse.getNickName();
System.out.println("调用成功");
Player player = new Player();
player.setPlayerOppenid(openid);
player.setPlayerPhoto(userinfoShareResponse.getAvatar());
player.setPlayerName(name);
/**
* 用户授权登陆,如果有数据,更新,没有数据就创建
*/
PlayerService.saveOrUpdate(player);
} else {
System.out.println("调用失败");
}
} catch (AlipayApiException e) {
//处理异常
e.printStackTrace();
}
} catch (AlipayApiException e) {
//处理异常
e.printStackTrace();
}
return "/starting_equipment/start_equip";
}
//获取到了用户所有信息之后,用户充值操作,调用统一下单接口,微信是调用银行的下单。
//微信的:
/**
* 玩游戏 登录 微信
*
* @param model
* @return
*/
@RequestMapping("/start_equip")
public String start_equip(Model model, HttpServletRequest req, HttpServletResponse res, HttpSession session) {
try {
String code = req.getParameter("code");
//获取code后,请求以下链接获取access_token:
String getaccess_tokenurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Constant.APPID + "&secret=" + Constant.AppSecret + "&code=" + code + "&grant_type=authorization_code";
Map url = WXPayUtil.GetURL(getaccess_tokenurl);
String access_token = url.get("access_token").toString();
String openid = url.get("openid").toString();
//下拉用户信息
String userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
Map user = WXPayUtil.GetURL(userurl);
String nickname = user.get("nickname").toString();
String headimgurl = user.get("headimgurl").toString();
String sex = user.get("sex").toString();
String province = user.get("province").toString();//省份
String city = user.get("city").toString();//城市
String country = user.get("country").toString();//国家
///业务操作
} catch (Exception e) {
logger.info(e);
}
return "/starting_equipment/start_equip";
}
//支付宝是根据银行返回的订单号来吊起支付接口的,微信支付时时根据银行返回的
微信根据返回的信息吊起微信jsapi