基于java实现的⽀付(H5举例)
之前也⼀直对接,⽀付宝,银联,第三⽅⽀付公司的⽀付流程,但是记录的还是⽐较少的。今天重新梳理⽀付中⼼,简单的记录下⼼得怎么开通商户号和我这就不说了。
1、只是⽀付必须要appID,所以先在商户号上关联⼀个服务号。
2、设置秘钥,这个秘钥是⾃⼰设置的,我这⾥直接就是32位随机数。
3、开通产品和设置域名
ok,到这⾥基本前期的准备⼯作差不多了。作为⽀付体系的⼀个⼤部分,肯定是需要封装到⽀付中⼼的。项⽬结构
然后再解释⼀个东西,证书,这个是在某些敏感接⼝需要进⾏证书验证的,具体申请流程⽐较简单,这⾥就不详细描述。
开发开始:
1、SDK准备
当然,说是SDK嘛,其实很多东西就是⼀个开放接⼝,还是需要⾃⼰具体去实现。所以这⾥新建2个实现类
对请求域名进⾏处理,实现容灾
public class IWXPayDomainImpl implements IWXPayDomain {
//3 minutes
private final int MIN_SWITCH_PRIMARY_MSEC = 3 * 60 * 1000;
private long switchToAlternateDomainTime = 0;
手机号转账private Map<String, DomainStatics> domainData = new HashMap();
private IWXPayDomainImpl(){
}
private static class WxPayDomainHolder{
private static IWXPayDomain holder = new IWXPayDomainImpl();
}
public static IWXPayDomain instance(){
return WxPayDomainHolder.holder;
}
@Override
public void report(String domain, long elapsedTimeMillis, Exception ex) {
DomainStatics info = (domain);
齐齐哈尔区号if(info == null){
info = new DomainStatics(domain);
domainData.put(domain, info);
}
if(ex == null){ //success
if(info.succCount >= 2){ //continue succ, clear error count
++info.succCount;
}
}else if(ex instanceof ConnectTimeoutException){史上最贱小游戏2攻略
info.succCount = info.dnsErrorCount = 0;
++tTimeoutCount;
}else if(ex instanceof UnknownHostException){
info.succCount = 0;
++info.dnsErrorCount;
}else{
info.succCount = 0;
++herErrorCount;
恍惚的拼音}
}
@Override
public DomainInfo getDomain(WXPayConfig config) {
DomainStatics primaryDomain = (WXPayConstants.DOMAIN_API); if(primaryDomain == null ||
primaryDomain.isGood()) {
return new DomainInfo(WXPayConstants.DOMAIN_API, true);
}
long now = System.currentTimeMillis();
if(switchToAlternateDomainTime == 0){ //first switch
if(switchToAlternateDomainTime == 0){ //first switch
switchToAlternateDomainTime = now;
return new DomainInfo(WXPayConstants.DOMAIN_API2, false);
}else if(now - switchToAlternateDomainTime < MIN_SWITCH_PRIMARY_MSEC){
DomainStatics alternateDomain = (WXPayConstants.DOMAIN_API2); if(alternateDomain == null ||
alternateDomain.isGood() ||
alternateDomain.badCount() < primaryDomain.badCount()){
return new DomainInfo(WXPayConstants.DOMAIN_API2, false);
}else{
return new DomainInfo(WXPayConstants.DOMAIN_API, true);
}
}else{ //force switch back
switchToAlternateDomainTime = 0;
DomainStatics alternateDomain = (WXPayConstants.DOMAIN_API2); if(alternateDomain != null) {
}
return new DomainInfo(WXPayConstants.DOMAIN_API, true);
}
用什么看日食}
static class DomainStatics {
final String domain;
int succCount = 0;
int connectTimeoutCount = 0;
int dnsErrorCount =0;
int otherErrorCount = 0;
DomainStatics(String domain) {
this.domain = domain;
}
void resetCount(){
succCount = connectTimeoutCount = dnsErrorCount = otherErrorCount = 0;
}
boolean isGood(){ return connectTimeoutCount <= 2 && dnsErrorCount <= 2; }
int badCount(){
return connectTimeoutCount + dnsErrorCount * 5 + otherErrorCount / 4;
}
}
}
配置实现:
@Slf4j
public class WxPayConfigImpl extends WXPayConfig {
private WxProperties wxProperties;
private static WxPayConfigImpl INSTANCE;
private byte[] certData;
private WxPayConfigImpl() {
}
@Override
public String getAppID() {
AppId();
}
@Override
public String getMchID() {
MchId();
}
@Override
public String getKey() {
Key();
}
@Override
public InputStream getCertStream() {
ByteArrayInputStream certBis = new Data); return certBis;
}
@Override
public IWXPayDomain getWXPayDomain() {
return IWXPayDomainImpl.instance();
}
/**
* 静态内部类保证现成安全
*/
private static class SingleWxPayConfig{
/**
* 静态对象初始化,由JVM保证线程安全
*/
private static WxPayConfigImpl instance = new WxPayConfigImpl();
}
/**
* 单例调⽤
* @return
*/
public static WxPayConfigImpl getInstance(){
return SingleWxPayConfig.instance;
}
public void setWxProperties(WxProperties wxProperties) {
this.wxProperties = wxProperties;
}
public void setCertData(byte[] certData) {
}
}
增加相关配置实现,进⾏部分功能初始化,包含证书信息。
@Configuration
@EnableConfigurationProperties(WxProperties.class)
@Slf4j
public class MyWxPayConfig {
@Autowired
private WxProperties wxProperties;
@Bean
public WXPay wxPay(){
byte[] certData = null;
try {
File file = new CertPath());
InputStream certStream = new FileInputStream(file);沙葱
certData = new byte[(int) file.length()];
certStream.close();
} catch (Exception e) {
e.printStackTrace();
<("读取证书出错!错误原因:{}",e.getMessage());
}
WxPayConfigImpl wxPayConfig = Instance(); wxPayConfig.setWxProperties(wxProperties);
wxPayConfig.setCertData(certData);
WXPay wxpay = null;
try {
wxpay = new WXPay(wxPayConfig);
} catch (Exception e) {
e.printStackTrace();
<("初始化出错");
}
return wxpay;
}
}
配置类实现:
@Data
@ConfigurationProperties(prefix = "XX.pay.wx")
public class WxProperties {
private String appId;
private String mchId;
private String key;
private String certPath;
}
增加YML⽂件配置:
XX:
pay:
wx:
appId: XX
mchId: XX
key: XX
certPath: /app_server/pay/apiclient_cert.p12
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论