对接微信支付接口

这篇具有很好参考价值的文章主要介绍了对接微信支付接口。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

SpringBoot中对接微信支付接口

1.微信支付开发文档

https://pay.weixin.qq.com/wiki/doc/api/index.html

1.准备工作:

在微信上申请服务号类型的公众号,从公众号获取以下数据

  1. appid:微信公众账号或开放平台APP的唯一标识

  2. mch_id:商户号 (配置文件中的partner)

  3. partnerkey:商户密钥

    2.根据项目需求选择适合的支付方式,本例使用Native支付方式

微信支付api,微信,java,开发语言

点击查看文档->API列表

微信支付api,微信,java,开发语言

开发步骤

引入依赖

<dependency>
    <groupId>com.github.wxpay</groupId>
    <artifactId>wxpay-sdk</artifactId>
    <version>0.0.3</version>
</dependency>

修改application.proerties配置文件

#关联的公众号appid #商户号 #商户key
weixin.appid=wx34bb2aa123de3dcd
weixin.partner=1611167878
weixin.partnerkey=4ca478a4580794e2f7cf67881cb21dd4b1

参数注入:

@Component
public class PropertiesUtils implements InitializingBean {

    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.partner}")
    private String partner;
    @Value("${weixin.partnerkey}")
    private String partnerkey;

    public static String APPID;
    public static String PARTNER;
    public static String PARTNERKEY;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        APPID = appid;
        PARTNER = partner;
        PARTNERKEY = partnerkey;
    }
}

HttpClient工具类:

用于发送http请求,可直接复制

/**
 * http请求客户端
 */
public class HttpClient {
    private String url;
    private Map<String, String> param;
    private int statusCode;
    private String content;
    private String xmlParam;
    private boolean isHttps;
    private boolean isCert = false;
    //证书密码 微信商户号(mch_id)
    private String certPassword;
    public boolean isHttps() {
        return isHttps;
    }
    public void setHttps(boolean isHttps) {
        this.isHttps = isHttps;
    }
    public boolean isCert() {
        return isCert;
    }
    public void setCert(boolean cert) {
        isCert = cert;
    }
    public String getXmlParam() {
        return xmlParam;
    }
    public void setXmlParam(String xmlParam) {
        this.xmlParam = xmlParam;
    }
    public HttpClient(String url, Map<String, String> param) {
        this.url = url;
        this.param = param;
    }
    public HttpClient(String url) {
        this.url = url;
    }
    public String getCertPassword() {
        return certPassword;
    }
    public void setCertPassword(String certPassword) {
        this.certPassword = certPassword;
    }
    public void setParameter(Map<String, String> map) {
        param = map;
    }
    public void addParameter(String key, String value) {
        if (param == null)
            param = new HashMap<String, String>();
        param.put(key, value);
    }
    public void post() throws ClientProtocolException, IOException {
        HttpPost http = new HttpPost(url);
        setEntity(http);
        execute(http);
    }
    public void put() throws ClientProtocolException, IOException {
        HttpPut http = new HttpPut(url);
        setEntity(http);
        execute(http);
    }
    public void get() throws ClientProtocolException, IOException {
        if (param != null) {
            StringBuilder url = new StringBuilder(this.url);
            boolean isFirst = true;
            for (String key : param.keySet()) {
                if (isFirst)
                    url.append("?");
                else
                    url.append("&");
                url.append(key).append("=").append(param.get(key));
            }
            this.url = url.toString();
        }
        HttpGet http = new HttpGet(url);
        execute(http);
    }
    /**
    * set http post,put param
    */
    private void setEntity(HttpEntityEnclosingRequestBase http) {
        if (param != null) {
            List<NameValuePair> nvps = new LinkedList<NameValuePair>();
            for (String key : param.keySet())
                nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
            http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
        }
        if (xmlParam != null) {
            http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
        }
    }
    private void execute(HttpUriRequest http) throws ClientProtocolException,
    IOException {
        CloseableHttpClient httpClient = null;
        try {
            if (isHttps) {
                if(isCert) {
                    FileInputStream inputStream = new FileInputStream(new File(ConstantPropertiesUtils.CERT));
                    KeyStore keystore = KeyStore.getInstance("PKCS12");
                    char[] partnerId2charArray = certPassword.toCharArray();
                    keystore.load(inputStream, partnerId2charArray);
                    SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
                    SSLConnectionSocketFactory sslsf =
                        new SSLConnectionSocketFactory(sslContext,
                                                       new String[] { "TLSv1" },
                                                       null,
                                                       SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
                    httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
                } else {
                    SSLContext sslContext = new SSLContextBuilder()
                        .loadTrustMaterial(null, new TrustStrategy() {
                            // 信任所有
                            public boolean isTrusted(X509Certificate[] chain,
                                                     String authType)
                                throws CertificateException {
                                return true;
                            }
                        }).build();
                    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                        sslContext);
                    httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
                        .build();
                }
            } else {
                httpClient = HttpClients.createDefault();
            }
            CloseableHttpResponse response = httpClient.execute(http);
            try {
                if (response != null) {
                    if (response.getStatusLine() != null)
                        statusCode = response.getStatusLine().getStatusCode();
                    HttpEntity entity = response.getEntity();
                    // 响应内容
                    content = EntityUtils.toString(entity, Consts.UTF_8);
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient.close();
        }
    }
    public int getStatusCode() {
        return statusCode;
    }
    public String getContent() throws ParseException, IOException {
        return content;
    }
}

到此准备工作结束,剩下的就是参考微信支付开发文档,提供微信支付接口所需参数和获取请求结果,从结果中取出我们所需的数据

准备微信支付所需参数

//对接微信支付
        //组装数据
        Map<String, String> paramMap = new HashedMap<>();
        paramMap.put("appid", PropertiesUtils.APPID); //公众号appid
        paramMap.put("mch_id",PropertiesUtils.PARTNER); //商户号
        paramMap.put("out_trade_no",orderInfo.getOutTradeNo());  //商户订单号
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符串
       String object = orderInfo.getReserveDate()+"就诊"+ orderInfo.getDepname();
        paramMap.put("body",object); //商品描述
        //paramMap.put("total_fee", order.getAmount().multiply(new BigDecimal("100")).longValue()+"");
        paramMap.put("total_fee","1"); //支付金额
        paramMap.put("spbill_create_ip","127.0.0.1"); //用户的客户端IP
        paramMap.put("notify_url", "https://2495161sb6.goho.co/api/order/weixinPay/weixinNotify");//微信支付结果通知的回调地址
        paramMap.put("trade_type", "NATIVE"); //交易类型

: total_fee 属性为支付金额,单位是分

微信支付接口地址: https://api.mch.weixin.qq.com/pay/unifiedorder

调用接口,发送Http请求

//对接微信支付
HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
     httpClient.setXmlParam(WXPayUtil.generateSignedXml(paramMap,ConstantPropertiesUtils.PARTNERKEY));
//微信接口为https请求,告诉httpClient改请求为https
httpClient.setHttps(true);
httpClient.post();

接收返回值,获取所需数据

 //获取响应数据
  String content = httpClient.getContent();
  Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
  //以下字段在return_code和result_code都为SUCCESS的时候有返回
  if(resultMap.get("return_code").equals("SUCCESS")&&resultMap.get("result_code").equals("SUCCESS")) {
     map = new HashMap<>();
     map.put("orderId", orderId); //订单id
     map.put("totalFee", orderInfo.getAmount()); 
     map.put("resultCode", resultMap.get("result_code"));
     map.put("codeUrl", resultMap.get("code_url")); //二维码链接
            }

查询订单退款等操作参考下单即可

map = new HashMap<>();
map.put(“orderId”, orderId); //订单id
map.put(“totalFee”, orderInfo.getAmount());
map.put(“resultCode”, resultMap.get(“result_code”));
map.put(“codeUrl”, resultMap.get(“code_url”)); //二维码链接
}文章来源地址https://www.toymoban.com/news/detail-548045.html


查询订单退款等操作参考下单即可

到了这里,关于对接微信支付接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序支付-java对接微信

     一共是两个方法: 一个方法后台生成预支付订单,得到预支付交易会话标识prepay_id,传给前端,让前端调起小程序支付; 一个是支付回调 目录 一、生成预支付订单  注意: 二、 支付回调         封装参数向微信发送生成预支付交易单请求,微信会返回一个prepay_id,再将

    2024年02月12日
    浏览(37)
  • SpringBoot对接微信小程序支付功能开发(二,支付回调功能)

    接着上一篇: SpringBoot对接微信小程序支付功能开发(一,下单功能) 在上一篇下单功能中我们有传支付结果回调地址。 下面是回调接口实现 根据官网给的参数进行业务处理 这就完成了,微信支付回调你的地址,并且把支付的信息传进来,剩下就要根据自己业务进行操作。

    2024年02月11日
    浏览(42)
  • Java对接微信支付(史上最详细)

    本文将介绍如何使用Java对接微信支付,包括获取支付参数、支付回调处理等步骤。本文适用于已经熟悉微信支付基本原理的读者。 JDK 1.8 Maven Spring Boot 2.x 微信支付开发文档 为了进行支付,我们需要先获取微信支付的参数信息,包括appid、商户id、支付密钥等。 配置文件 我们

    2024年02月15日
    浏览(29)
  • SpringBoot对接微信小程序支付功能开发(一,下单功能)

    1,接入前准备: 接入模式选择直连模式; 申请小程序,得到APPID,并开通微信支付; 申请微信商户号,得到mchid,并绑定APPID; 配置商户API key,下载并配置商户证书,根据微信官方文档操作:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_1.shtml 上面都配置完之后会得到:小

    2024年02月10日
    浏览(47)
  • Python对接微信小程序V3接口进行支付,并使用uwsgi+nginx+django进行https部署

    网上找了很多教程,但是很乱很杂,并且教程资源很少且说的详细。这里就记录一下分享给大家 共分为以下几个步骤: 目录 一、开始前准备信息 二、使用前端code获取用户的openid 三、对接小程序v3接口下单 四、小程序支付的回调 五、安装并启动uwsgi 六、安装并启动nginx 七、

    2024年02月12日
    浏览(33)
  • weixin-java-pay对接微信V3支付记录

    https://github.com/binarywang/weixin-java-pay-demo 这个demo里, 没有v3版本的配置, 这里记录一下 v3支付, 相对之前的版本来说, 更为安全, 也相对繁琐一些, 而且请求和响应都使用了json格式的数据 1. 配置 发起支付所需的配置有三个证书文件, 在商户后台申请 apiclient_cert.p12 apiclient_key.pem ap

    2024年02月11日
    浏览(37)
  • 【微信支付】java-微信小程序支付-V3接口

    最开始需要在微信支付的官网注册一个商户; 在管理页面中申请关联小程序,通过小程序的 appid 进行关联;商户号和appid之间是多对多的关系 进入微信公众平台,功能-微信支付中确认关联 具体流程请浏览官方文档:接入前准备-小程序支付 | 微信支付商户平台文档中心 流程走

    2024年02月06日
    浏览(42)
  • java微信支付v3系列——6.微信支付查询订单API

    java微信支付v3系列——1.微信支付准备工作 java微信支付v3系列——2.微信支付基本配置 java微信支付v3系列——3.订单创建准备操作 java微信支付v3系列——4.创建订单的封装及使用 java微信支付v3系列——5.微信支付成功回调 java微信支付v3系列——6.微信支付查询订单API java微信支

    2023年04月08日
    浏览(29)
  • 对接支付通道如何收费?支付接口收费标准

    支付接口收费标准是怎样的 反映到平台方来说,就是它的盈利模式,是维持企业生存,到发展壮大的根本保障。目前第三方支付平台费用有:手续费、广告费、服务费、沉淀资金的利息收入四种。 1、手续费 手续费是第三方支付平台费用的最传统的盈利模式之一。即第三方支

    2024年02月16日
    浏览(34)
  • 关于支付通道,支付接口,支付对接的100个名词解释

    一份简明易懂的支付术语解释清单,帮助你更好地理解支付通道、支付接口和支付对接等相关概念。 100个名词的简要解释: 在线支付:通过互联网实现的支付方式,包括网银支付、第三方支付等。 支付网关:连接商户和支付机构的中间件,实现支付流程的安全处理和支付数

    2024年02月04日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包