微信公众号h5使用jsapi支付

准备工作


准备好公众号及支付商户号相关的:appid,商户号id,appsecret,apikey(支付密钥v2),下载好api支付证书路径,公众号中配置好业务域名(前端与域名)。

域名穿透内网


我使用的是frpc, 文档与下载地址
frpc.ini

[common]
server_addr = #fprs程序所在服务器地址
server_port = 7000

[web]
type = http
local_port = 5100
custom_domains =#域名一

[web2]
type = http
local_port = 5101
custom_domains = #域名二

[web3]
type = http
local_port = 5102
custom_domains =#域名三

[web4]
type = http
local_port = 80
custom_domains =#域名四

将frps.exe 和frps.ini 复制到域名解析到的服务器,运行

本地运行 frpc -c frpc.ini,域名就能解析到本地了

下载支付demo


地址: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
我下载的是java,
将demo的文件放到项目中
在这里插入图片描述

修改WXPay


修改WXPay构造函数,使加密方式一直是md5,前端使用chooseWXPay调起微信支付时,修改wxpay后就不再出现:支付签名验证错误了

   public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception {
        this.config = config;
        this.notifyUrl = notifyUrl;
        this.autoReport = autoReport;
        this.useSandbox = useSandbox;
//        if (useSandbox) {
//            this.signType = WXPayConstants.SignType.MD5; // 沙箱环境
//        }
//        else {
//            this.signType = WXPayConstants.SignType.HMACSHA256;
//        }
        this.signType = WXPayConstants.SignType.MD5;
        this.wxPayRequest = new WXPayRequest(config);
    }

新建PayConfig

public  class PayConfig extends WXPayConfig {
    private byte[] certData;

    private String appId;
    private String appsecret;
    private String mchId;
    private String certpath;
    private String key;

    public PayConfig(String appId,String appsecret,String mchId,String certpath,String apiKey) throws Exception {
        this.appId = appId;
        this.appsecret = appsecret;
        this.mchId = mchId;
        this.certpath = certpath;
        this.key = apiKey;
        String filePath = certpath +"apiclient_cert.p12";
        File file = new File(filePath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getAppID() {
        return appId;
    }

    public String getMchID() {
        return mchId;
    }

    public String getKey() {
        return key;
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    @Override
    protected IWXPayDomain getWXPayDomain() {
        return new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API,true);
            }
        };
    }
}

统一下单 java端

 @Override
    public PayResultVo pay(String openId,String body,String orderId,String totalFee,String ip) throws Exception {
        PayConfig config = new PayConfig(appId,appsecret,mchId,certpath,apikey);
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("body", body);
        data.put("out_trade_no", orderId);
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", totalFee);
        data.put("spbill_create_ip", ip);
        data.put("notify_url", myUrl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值