• 微信支付与支付宝支付


    之前做过娃娃机的项目,因为直接对接微信和支付宝利率稍微高了一些,所以公司决定直接走银行:微信和支付宝调用银行的接口来完成支付

    支付宝支付流程:

    注册个人或者企业账号然后接入开发平台--->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

    一条路不能回头,就是一生要走许多路,有成长之路。很多事情不能自己掌控,即使再孤单再寂寞,仍要继续走下去,不许停也不能回头。------成长之旅。
  • 相关阅读:
    PAT (Advanced Level) Practice 1071 Speech Patterns (25分)
    PAT (Advanced Level) Practice 1070 Mooncake (25分)
    PAT (Advanced Level) Practice 1069 The Black Hole of Numbers (20分)
    PAT (Advanced Level) Practice 1074 Reversing Linked List (25分)
    PAT (Advanced Level) Practice 1073 Scientific Notation (20分)
    第一次冲刺个人总结01
    构建之法阅读笔记01
    人月神话阅读笔记01
    四则运算2
    学习进度条(软件工程概论1-8周)
  • 原文地址:https://www.cnblogs.com/java7115/p/9679224.html
Copyright © 2020-2023  润新知