对接微信支付之网页支付详解

对接微信支付之网页支付详解

  • 声明:转载请注明出处
  • 阅读对象:本文针对的是网页中的扫码支付
  • 温馨提示:微信支付坑比较多,阅读时请仔细一些,不要放过所有需要注意的内容 , 本人一路踩坑过来,希望大家引以为戒!
对接流程大致为以下几个步骤:
  • 申请认证公众号 (若已有公众号则可跳过, 注意: 1. 公众号类型需要为服务号或者订阅号 2. 公众号需要认证, 认证费用300RMB点此跳转申请公众号
  • 申请网站备案 (若网站已备案则可跳过,注意:网站的备案内容需要和所出售商品直接关系)
  • 有以上两个条件后可以开始申请开通微信支付,具体的申请流程公众号平台都有,读者可自行去查阅。 注意:1.开通微信支付需要公司账户打款验证,要及时和财务沟通好,查看公司流水。2.开通成功后,会收到邮件 ,邮件中的信息特别重要:示例如下在这里插入图片描述
  • 以上三条均申请完成即可开始配置开发环境。拿到邮件中的信息后,按照邮件中的指引配置好相关信息之后就可以开始开发了。注意:很重要的几个信息,1. API证书 2. API密钥 3. APPID
  • 可以开始开发了!!!
支付类型:

在这里插入图片描述

  • 网页支付 一般选择 JSAPI和Native 这两种,点此查看区别
  • 本文选择的是Native支付方式
    呼~,终于开始编码了

代码实现

  • 导入依赖
            <!-- 添加微信支付jar包 -->
            <dependency>
                <groupId>com.github.wxpay</groupId>
                <artifactId>wxpay-sdk</artifactId>
                <version>0.0.3</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
  • 编写配置实现类
    业务层实现类
    注意:这个实现类不要加@Service 注解,否则报错,因为springboot的@Service 是在项目启动时预创建对象,需要的是公共无参构造,以及所定义的成员变量也要注入。 总之别加就不会报错,使用没有影响,如果不相信可以加上试试,嘿嘿。

import com.github.wxpay.sdk.WXPayConfig;
import com.juehua.single.constant.PayConstant;
import com.juehua.single.model.qingshiyuanpay.ExceptionUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 微信 配置实现类
 * Created by Jason on 2018/11/20.
 */
public class WXPayConfigImpl implements WXPayConfig{
   

    private byte[] certData;
    private static WXPayConfigImpl INSTANCE;
    private static String cert_path;
    private static String notify_url;

    private WXPayConfigImpl(String certPath,String notifyUrl) throws Exception{
   
        ExceptionUtil.checkEmpty(certPath,"Sorry,Don't found your cert!"); // 校验证书路径
        ExceptionUtil.checkEmpty(notifyUrl,"Notify url can not null!");    // 校验回路径
		
		 // 读取API证书
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    /**
     * 获取实现类对象
     * @param certPath
     * @param notifyUrl
     * @return
     * @throws Exception
     */
    public static WXPayConfigImpl getInstance(String certPath,String notifyUrl) throws Exception{
   
        notify_url = notifyUrl;
        cert_path = certPath;
        if (INSTANCE == null) {
   
            synchronized (WXPayConfigImpl.class) {
   
                if (INSTANCE == null) {
   
                    INSTANCE = new WXPayConfigImpl(cert_path,notify_url);
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 获取证书字节流
     * @return
     */
    public InputStream getCertStream() {
   
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public String getAppID() {
   
        return PayConstant.APPID;
    }

    public String getMchID() {
   
        return PayConstant.MCHID;
    }

    public String getKey() {
   
        return PayConstant.APIKEY;
    }

    public String getNotifyUrl() {
   
        return notify_url;
    }

    public String getTradeType() {
   
        return PayConstant.TRADE_TYPE;
    }
    // 请求连接超时时长
    public int getHttpConnectTimeoutMs() {
   
        return PayConstant.HTTP_CONNECT_TIMEOUT;
    }
    // 请求读取超时时长
    public int getHttpReadTimeoutMs() {
   
        return PayConstant.HTTP_READ_TIMEOUT;
    }

    public String getPrimaryDomain() {
   
        return PayConstant.PRIMARY_DEMAIN;
    }

    public String getAlternateDomain() {
   
        return PayConstant.ALTERNATE_DEMAIN;
    }

    public int getReportWorkerNum() {
   
        return PayConstant.REPORT_WORKER_NUM;
    }

    public int getReportBatchSize() {
   
        return PayConstant.REPORT_BATCH_SIZE;
    }

    public String getSpbillCreateIp() {
   
        return PayConstant.BILL_CREATE_IP;
    }
}

  • 支付常量类,可以将配置信息写在这个常量里

/**
 * 支付接口相关常量
 * Created by Jason on 2018/11/15.
 */
public final class PayConstant {
   

    public static String ORDER_NAME = "XXX技术服务费";
    public static String BODY = "备注";

    /**
     *  以下常量是 微信 支付相关
     */
    // 公众号
    public static String APPID ="你的公众号appid";
    // 商户号
    public static String MCHID ="你的商户号";
    // API 安全密钥
    public static String APIKEY ="你配置的API安全密钥";
    // 支付类型 NATIVE
    public static String TRADE_TYPE="NATIVE";
    // 链接超时时长
    public static Integer HTTP_CONNECT_TIMEOUT=2000;
    // 请求读取超时时长
    public static Integer HTTP_READ_TIMEOUT = 10000;

    public static String PRIMARY_DEMAIN = "api.mch.weixin.qq.com";

    public static String ALTERNATE_DEMAIN = "api2.mch.weixin.qq.com";

    public static Integer REPORT_WORKER_NUM = 1;

    public static Integer REPORT_BATCH_SIZE= 2;

    public static String BILL_CREATE_IP="192.168.1.1";
}

  • 微信支付业务 省时间没有分接口和实现类 直接写了


import com.github.wxpay.sdk.WXPay;
import com.juehua.single.model.qingshiyuanpay.MD5Util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.Map.Entry;

/**
 * 微信支付业务
 * Created by Jason on 2018/11/20.
 */
public class WXPayService {
   
    private static Logger logger = LoggerFactory.getLogger(WXPayService.class);
    private WXPay wxpay;
    private WXPayConfigImpl config;
    private static WXPayService INSTANCE;

	
    private WXPayService(String certPath, String notifyUrl) throws Exception {
   
        config = WXPayConfigImpl.getInstance(certPath,notifyUrl);
        wxpay = new WXPay(config);
    }

    public static WXPayService getInstance(String certPath,String notifyUrl) throws Exception {
   
        if (INSTANCE == null) {
   
            synchronized (WXPayConfigImpl.class) {
   
                if (INSTANCE == null) {
   
                    INSTANCE = new WXPayService(certPath,notifyUrl);
                }
            }
        }
        
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值