Skip to content

Commit

Permalink
🐛 #1546 修复WxRedisOps问题, #1548 修复WxOpenInMemoryConfigStorage锁问题,#1305
Browse files Browse the repository at this point in the history
…增加商户电子发票功能
  • Loading branch information
lkqm authored May 12, 2020
1 parent 609b38a commit 058ce62
Show file tree
Hide file tree
Showing 25 changed files with 1,078 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public String getValue(String key) {
@Override
public void setValue(String key, String value, int expire, TimeUnit timeUnit) {
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.psetex(key, timeUnit.toMillis(expire), value);
if (expire <= 0) {
jedis.set(key, value);
} else {
jedis.psetex(key, timeUnit.toMillis(expire), value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public String getValue(String key) {

@Override
public void setValue(String key, String value, int expire, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, expire, timeUnit);
if (expire <= 0) {
redisTemplate.opsForValue().set(key, value);
} else {
redisTemplate.opsForValue().set(key, value, expire, timeUnit);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ public String getValue(String key) {

@Override
public void setValue(String key, String value, int expire, TimeUnit timeUnit) {
redissonClient.getBucket(key).set(value, expire, timeUnit);
if (expire <= 0) {
redissonClient.getBucket(key).set(value);
} else {
redissonClient.getBucket(key).set(value, expire, timeUnit);
}
}

@Override
public Long getExpire(String key) {
return redissonClient.getBucket(key).remainTimeToLive();
long expire = redissonClient.getBucket(key).remainTimeToLive();
if (expire > 0) {
expire = expire / 1000;
}
return expire;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.chanjar.weixin.common.redis;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class CommonWxRedisOpsTest {

protected WxRedisOps wxRedisOps;
private String key = "access_token";
private String value = String.valueOf(System.currentTimeMillis());

@Test
public void testGetValue() {
wxRedisOps.setValue(key, value, 3, TimeUnit.SECONDS);
Assert.assertEquals(wxRedisOps.getValue(key), value);
}

@Test
public void testSetValue() {
String key = "access_token", value = String.valueOf(System.currentTimeMillis());
wxRedisOps.setValue(key, value, -1, TimeUnit.SECONDS);
wxRedisOps.setValue(key, value, 0, TimeUnit.SECONDS);
wxRedisOps.setValue(key, value, 1, TimeUnit.SECONDS);
}

@Test
public void testGetExpire() {
String key = "access_token", value = String.valueOf(System.currentTimeMillis());
wxRedisOps.setValue(key, value, -1, TimeUnit.SECONDS);
Assert.assertTrue(wxRedisOps.getExpire(key) < 0);
wxRedisOps.setValue(key, value, 4, TimeUnit.SECONDS);
Long expireSeconds = wxRedisOps.getExpire(key);
Assert.assertTrue(expireSeconds <= 4 && expireSeconds >= 0);
}

@Test
public void testExpire() {
String key = "access_token", value = String.valueOf(System.currentTimeMillis());
wxRedisOps.setValue(key, value, -1, TimeUnit.SECONDS);
wxRedisOps.expire(key, 4, TimeUnit.SECONDS);
Long expireSeconds = wxRedisOps.getExpire(key);
Assert.assertTrue(expireSeconds <= 4 && expireSeconds >= 0);
}

@Test
public void testGetLock() {
Assert.assertNotNull(wxRedisOps.getLock("access_token_lock"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.chanjar.weixin.common.redis;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import redis.clients.jedis.JedisPool;

public class JedisWxRedisOpsTest extends CommonWxRedisOpsTest {

JedisPool jedisPool;

@BeforeTest
public void init() {
this.jedisPool = new JedisPool("127.0.0.1", 6379);
this.wxRedisOps = new JedisWxRedisOps(jedisPool);
}

@AfterTest
public void destroy() {
this.jedisPool.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.chanjar.weixin.common.redis;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class RedisTemplateWxRedisOpsTest extends CommonWxRedisOpsTest {

StringRedisTemplate redisTemplate;

@BeforeTest
public void init() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName("127.0.0.1");
connectionFactory.setPort(6379);
connectionFactory.afterPropertiesSet();
StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);
this.redisTemplate = redisTemplate;
this.wxRedisOps = new RedisTemplateWxRedisOps(this.redisTemplate);
}

@AfterTest
public void destroy() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.chanjar.weixin.common.redis;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class RedissonWxRedisOpsTest extends CommonWxRedisOpsTest {

RedissonClient redissonClient;

@BeforeTest
public void init() {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
config.setTransportMode(TransportMode.NIO);
this.redissonClient = Redisson.create(config);
this.wxRedisOps = new RedissonWxRedisOps(this.redissonClient);
}

@AfterTest
public void destroy() {
this.redissonClient.shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package me.chanjar.weixin.mp.api;


import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.invoice.merchant.*;

/**
* 商户电子发票相关的接口
* <p>
* 重要!!!, 根据不同开票平台, 以下错误码可能开票成功(开票,冲红), 内部暂时未处理:
* 73105: 开票平台开票中,请使用相同的发票请求流水号重试开票
* 73107: 发票请求流水正在被处理,请通过查询接口获取结果
* 73100: 开票平台错误
* <p>
* 流程文档: https://developers.weixin.qq.com/doc/offiaccount/WeChat_Invoice/E_Invoice/Vendor_and_Invoicing_Platform_Mode_Instruction.html
* 接口文档: https://developers.weixin.qq.com/doc/offiaccount/WeChat_Invoice/E_Invoice/Vendor_API_List.html
*/
public interface WxMpMerchantInvoiceService {

/**
* 获取开票授权页链接
*/
InvoiceAuthPageResult getAuthPageUrl(InvoiceAuthPageRequest params) throws WxErrorException;

/**
* 获得用户授权数据
*/
InvoiceAuthDataResult getAuthData(InvoiceAuthDataRequest params) throws WxErrorException;

/**
* 拒绝开票
* <p>
* 场景: 用户授权填写数据无效
* 结果: 用户会收到一条开票失败提示
*/
void rejectInvoice(InvoiceRejectRequest params) throws WxErrorException;

/**
* 开具电子发票
*/
void makeOutInvoice(MakeOutInvoiceRequest params) throws WxErrorException;

/**
* 发票冲红
*/
void clearOutInvoice(ClearOutInvoiceRequest params) throws WxErrorException;

/**
* 查询发票信息
*
* @param fpqqlsh 发票请求流水号
* @param nsrsbh 纳税人识别号
*/
InvoiceResult queryInvoiceInfo(String fpqqlsh, String nsrsbh) throws WxErrorException;

/**
* 设置商户联系方式, 获取授权链接前需要设置商户联系信息
*/
void setMerchantContactInfo(MerchantContactInfo contact) throws WxErrorException;

/**
* 获取商户联系方式
*/
MerchantContactInfo getMerchantContactInfo() throws WxErrorException;

/**
* 配置授权页面字段
*/
void setAuthPageSetting(InvoiceAuthPageSetting authPageSetting) throws WxErrorException;

/**
* 获取授权页面配置
*/
InvoiceAuthPageSetting getAuthPageSetting() throws WxErrorException;

/**
* 设置商户开票平台信息
*/
void setMerchantInvoicePlatform(MerchantInvoicePlatformInfo merchantInvoicePlatformInfo) throws WxErrorException;

/**
* 获取商户开票平台信息
*/
MerchantInvoicePlatformInfo getMerchantInvoicePlatform(MerchantInvoicePlatformInfo merchantInvoicePlatformInfo) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.StandardSessionManager;
Expand All @@ -26,7 +29,6 @@
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -70,6 +72,10 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
private WxMpOcrService ocrService = new WxMpOcrServiceImpl(this);
private WxMpImgProcService imgProcService = new WxMpImgProcServiceImpl(this);

@Getter
@Setter
private WxMpMerchantInvoiceService merchantInvoiceService = new WxMpMerchantInvoiceServiceImpl(this, this.cardService);

private Map<String, WxMpConfigStorage> configStorageMap;

private int retrySleepMillis = 1000;
Expand Down Expand Up @@ -359,11 +365,11 @@ protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
lock.lock();
try{
if(StringUtils.equals(this.getWxMpConfigStorage().getAccessToken(), accessToken)){
try {
if (StringUtils.equals(this.getWxMpConfigStorage().getAccessToken(), accessToken)) {
this.getWxMpConfigStorage().expireAccessToken();
}
} catch (Exception ex){
} catch (Exception ex) {
this.getWxMpConfigStorage().expireAccessToken();
} finally {
lock.unlock();
Expand Down
Loading

0 comments on commit 058ce62

Please sign in to comment.