微信小程序对接主要涉及的接口(仅供参考)

1.授权登录需要获取到用户的唯一标识openid以及session_key会话密钥/**
* @Description: 获取openid用户唯一标识以及session_key会话密钥
*/
@PostMapping("/getOpenId")
public Map getOpenId(String code){Map  map = new HashMap<>();//登录凭证不能为空if (null == code || code.length() == 0){map.put("errMessage","code 不能为空!");return map;}//1.向微信服务器 试用登录凭证 code 获取session_key 和openid//请求参数Map params = new HashMap<>();params.put("appid",StringInfo.APPID);params.put("secret",StringInfo.APPSECRET);params.put("js_code",code);params.put("grant_type","authorization_code");String result = HttpClientUtil.doGet("https://api.weixin.qq.com/sns/jscode2session?",params);JSONObject json = JSON.parseObject(result);System.out.println(json);if (json != null) {//当请求成功if (json.getString("errcode")==null) {map.put("openid",json.getString("openid"));//用户的唯一标识map.put("sessionKey",json.getString("session_key"));//会话密钥if (json.getString("unionid") != null) {map.put("unionid", json.getString("unionid"));//用户在开放平台的唯一标识符}}else{//当请求不成功的时候,将错误信息map.put("errMessage",json.getString("errmsg"));}}System.out.println(JSON.toJSON(map));return map;
}

2.获取AccessToken

/**
* @Description:  获取AccessToken
*/
@PostMapping("/getAccessToken")
public Map getAccessToken(){Map map = new HashMap<>();Map params = new HashMap<>();//请求参数params.put("grant_type","client_credential");params.put("appid", StringInfo.APPID);params.put("secret",StringInfo.APPSECRET);//向微信服务端发起请求String result = HttpClientUtil.doGet("https://api.weixin.qq.com/cgi-bin/token?",params);//将结果转为json对象JSONObject json = JSON.parseObject(result);if (json != null) {if (json.getString("errcode") == null){map.put("accessToken",json.getString("access_token"));map.put("expiresIn",json.getString("expires_in"));}else {map.put("errMessage",json.getString("errmsg"));}}return map;
}

 

3.微信付款

/*** 支付接口* @param openid 用户的唯一标识* @param body1 商品描述信息* @param total_fee1 金额* @return*/
@RequestMapping("/WeiXinPay")
public @ResponseBody Object WeiXinPay(String openid,String body1,int total_fee1) {try {String appid = userService.getUser().getAppid(); // 微信小程序--》“开发者ID”String mch_id = userService.getUser().getMchId(); // 商户号,将该值赋值给partnerString key = userService.getUser().getMchKey(); // 微信支付商户平台登录)--》“API安全”--》“API密钥”--“设置密钥”(设置之后的那个值就是partnerkey,32位)LOGGER.debug(appid);LOGGER.debug(mch_id);LOGGER.debug(key);String body = body1; // 描述int total_fee = total_fee1; // 支付金额Date data = new Date();String notify_url = "https://www.baidu.com/weixinpay/notify"; // 自己发布在公网回调链接,不然无法访问String out_trade_no = IdUtils.genOrderName();//IdUtils-->见下面LOGGER.debug(out_trade_no);Map map = WeiXinAtcion.me.weixinPlay(mch_id, appid,key, openid, total_fee, out_trade_no, notify_url, body);return map;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return TTResult.fail();
}

 

6.微信退款

/*** * @param request* @param database* @param out_trade_no* @param all_total_fee* @param refund_fee* @return*/
@RequestMapping("/WeiXinRefund")
public @ResponseBody Object WeiXinRefund(HttpServletRequest request,@RequestParam("out_trade_no") String out_trade_no,@RequestParam("all_total_fee") int all_total_fee,@RequestParam("refund_fee") int refund_fee) {try {LOGGER.debug("订单号是--->"+out_trade_no);LOGGER.debug("总金额--->"+all_total_fee);LOGGER.debug("剩余金额--->"+refund_fee);// out_trade_no 退款订单号// all_total_fee 订单金额// refund_fee  输0  all_total_fee-refund_fee=退款的金额String appid = userService.getUser().getAppid(); // 微信小程序--》“开发者ID”String mch_id = userService.getUser().getMchId(); // 商户号,将该值赋值给partnerString key = userService.getUser().getMchKey(); // 微信支付商户平台登录)--》“API安全”--》“API密钥”--“设置密钥”(设置之后的那个值就是partnerkey,32位)Map refundmap = WeiXinAtcion.me.wechatRefund(request, mch_id, appid, key, out_trade_no, all_total_fee,refund_fee, userService.getUser().getCert());if (refundmap.get("return_code").equals("SUCCESS")) {if (refundmap.get("result_code").equals("FAIL")) {LOGGER.debug("退款失败:原因" + refundmap.get("err_code_des"));}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部