设为首页 加入收藏

TOP

微信 JS API 支付教程(一)
2017-11-13 14:55:41 】 浏览:754
Tags:微信 API 支付 教程

前言

最近一个项目中用到了微信开发,之前没有做过支付相关的东西,算是拿这个来练练手,刚开始接触支付时候很懵逼,加上微信支付开发文档本来就讲得不清楚,我是彻底蒙圈了,参考了很多代码之后,算是有一点思路了。

用户认证获取openId

如果你知识关注支付流程,这块可以跳过,因为我知道这些你已经做过了,在开始所有的流程之前,我觉得你应该把所有微信相关的配置放到一个properties文件中去,这样不仅显得更规范,而且会避免犯很多错误,真是一个完美的选择!

######## 配置文件
######## 公众号开发配置中的token(自定义)
wechat.token=
######## 应用id
wechat.appId=
######## 密钥(同token查看地址)
wechat.appSecret=
######## 静默授权微信回调url
wechat.callBackSlientUrl=
######## 商户Id(支付相关)
wechat.MCHID=
######## 微信下单地址
wechat.wxorder=https://api.mch.weixin.qq.com/pay/unifiedorder
######## 支付api密钥
wechat.KEY=
######## 支付结果回调地址
wechat.NOTIFYURL=

接着你可以考虑把这个properties注入到一个bean中,使用更方便,当然你还可以选择使用java来读取properties的配置,对比这两个方法,我更喜欢第一个,我就使用第一种方法来演示一下(这里使用spring boot框架,spring mvc类似)

/**
 * <p>Created on 2017/3/13.</p>
 *
 * @author StormMma
 *
 * @Description: 微信相关常量
 */
@Component
@ConfigurationProperties(locations = {"classpath:config/wechat.properties"}, prefix = "wechat")
public class WeChatConfigBean {
    /**
     * token
     */
    private String token;
    /**
     * app id
     */
    private String appId;
    /**
     * app secret
     */
    private String appSecret;
    /**
     * 静默授权回调地址
     */
    private String callBackSlientUrl;
    /**
     * 商户id
     */
    private String MCHID;
    /**
     * 异步回调地址
     */
    private String NOTIFYURL;
    /**
     * 微信统一下单地址
     */
    private String wxorder;
    /**
     * key
     */
    private String KEY;
    public String getToken() {
        return token;
    }
    public void setToken(String token) {
        this.token = token;
    }
    public String getAppId() {
        return appId;
    }
    public void setAppId(String appId) {
        this.appId = appId;
    }
    public String getAppSecret() {
        return appSecret;
    }
    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }
    public String getCallBackSlientUrl() {
        return callBackSlientUrl;
    }
    public void setCallBackSlientUrl(String callBackSlientUrl) {
        this.callBackSlientUrl = callBackSlientUrl;
    }
    public String getMCHID() {
        return MCHID;
    }
    public void setMCHID(String MCHID) {
        this.MCHID = MCHID;
    }
    public String getNOTIFYURL() {
        return NOTIFYURL;
    }
    public void setNOTIFYURL(String NOTIFYURL) {
        this.NOTIFYURL = NOTIFYURL;
    }
    public String getWxorder() {
        return wxorder;
    }
    public void setWxorder(String wxorder) {
        this.wxorder = wxorder;
    }
    public String getKEY() {
        return KEY;
    }
    public void setKEY(String KEY) {
        this.KEY = KEY;
    }

封装请求工具(这次我选择使用HttpClient, 此处的json工具我选择了ali的fastjson)

RequestUtil.java

/**
    * 发送Get请求到url,获得response的json实体
    * @param url
    * @return
    * @throws IOException
    */
   private JSONObject doGetUrl(String url) throws WechatException, ServerSystemException {
       CloseableHttpClient httpclient = HttpClients.createDefault();
       HttpGet httpGet = new HttpGet(url);
       CloseableHttpResponse response;
       String result;
       try {
           response = httpclient.execute(httpGet);
           HttpEntity entity = response.getEntity();
           result = EntityUtils.toString(entity, "UTF-8");
           httpclient.close();
       } catch (IOException e) {
           logger.error("执行GET请求发生错误!", e);
           throw new ServerSystemException("执行GET请求发生错误!{}", e);
       }
       return JSONObject.parseObject(result);
   }
   /**
    * 发送post请求
    * @param url
    * @param param
    * @return
    * @throws ServerSystemExc
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java 面试题:百度前200页都在这.. 下一篇SpringBoot 定时任务踩坑记录

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目