mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-30 10:07:06 +08:00 
			
		
		
		
	🎨 优化代码
This commit is contained in:
		| @ -3,21 +3,25 @@ package me.chanjar.weixin.common.util.json; | ||||
| import com.google.gson.JsonObject; | ||||
| import com.google.gson.JsonParser; | ||||
| import com.google.gson.stream.JsonReader; | ||||
| import lombok.NoArgsConstructor; | ||||
|  | ||||
| import java.io.Reader; | ||||
|  | ||||
| /** | ||||
|  * @author niefy | ||||
|  */ | ||||
| public class GsonParser { | ||||
|   private static final JsonParser JSON_PARSER = new JsonParser(); | ||||
|  | ||||
|   public static JsonObject parse(String json){ | ||||
|   public static JsonObject parse(String json) { | ||||
|     return JSON_PARSER.parse(json).getAsJsonObject(); | ||||
|   } | ||||
|  | ||||
|   public static JsonObject parse(Reader json){ | ||||
|   public static JsonObject parse(Reader json) { | ||||
|     return JSON_PARSER.parse(json).getAsJsonObject(); | ||||
|   } | ||||
|  | ||||
|   public static JsonObject parse(JsonReader json){ | ||||
|   public static JsonObject parse(JsonReader json) { | ||||
|     return JSON_PARSER.parse(json).getAsJsonObject(); | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -13,7 +13,6 @@ import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||
|  * @author chanjarster | ||||
|  */ | ||||
| public class WxGsonBuilder { | ||||
|  | ||||
|   private static final GsonBuilder INSTANCE = new GsonBuilder(); | ||||
|  | ||||
|   static { | ||||
|  | ||||
| @ -71,6 +71,7 @@ | ||||
|       <artifactId>lombok</artifactId> | ||||
|     </dependency> | ||||
|     <dependency> | ||||
|       <!-- 待优化去掉 --> | ||||
|       <groupId>com.fasterxml.jackson.core</groupId> | ||||
|       <artifactId>jackson-databind</artifactId> | ||||
|       <version>2.9.10.4</version> | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| package com.github.binarywang.wxpay.bean.payscore; | ||||
|  | ||||
| import com.google.gson.annotations.SerializedName; | ||||
| import lombok.AllArgsConstructor; | ||||
| import lombok.Data; | ||||
| import lombok.NoArgsConstructor; | ||||
|  | ||||
| @ -14,6 +15,7 @@ import java.io.Serializable; | ||||
|  */ | ||||
| @Data | ||||
| @NoArgsConstructor | ||||
| @AllArgsConstructor | ||||
| public class TimeRange implements Serializable { | ||||
|   private static final long serialVersionUID = 8169562173656314930L; | ||||
|   /** | ||||
|  | ||||
| @ -6,6 +6,7 @@ import lombok.Builder; | ||||
| import lombok.Data; | ||||
| import lombok.NoArgsConstructor; | ||||
| import lombok.experimental.Accessors; | ||||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||
|  | ||||
| import java.io.Serializable; | ||||
| import java.util.List; | ||||
| @ -22,6 +23,10 @@ import java.util.List; | ||||
| public class WxPayScoreRequest implements Serializable { | ||||
|   private static final long serialVersionUID = 364764508076146082L; | ||||
|  | ||||
|   public String toJson() { | ||||
|     return WxGsonBuilder.create().toJson(this); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * out_order_no : 1234323JKHDFE1243252 | ||||
|    * appid : wxd678efh567hg6787 | ||||
|  | ||||
| @ -3,6 +3,7 @@ package com.github.binarywang.wxpay.bean.payscore; | ||||
| import com.google.gson.annotations.SerializedName; | ||||
| import lombok.Data; | ||||
| import lombok.NoArgsConstructor; | ||||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||
|  | ||||
| import java.io.Serializable; | ||||
| import java.util.List; | ||||
| @ -17,6 +18,10 @@ import java.util.Map; | ||||
| public class WxPayScoreResult implements Serializable { | ||||
|   private static final long serialVersionUID = 8809250065540275770L; | ||||
|  | ||||
|   public static WxPayScoreResult fromJson(String json) { | ||||
|     return WxGsonBuilder.create().fromJson(json, WxPayScoreResult.class); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * appid : wxd678efh567hg6787 | ||||
|    * mchid : 1230000109 | ||||
|  | ||||
| @ -8,9 +8,8 @@ import com.github.binarywang.wxpay.exception.WxPayException; | ||||
| import com.github.binarywang.wxpay.service.PayScoreService; | ||||
| import com.github.binarywang.wxpay.service.WxPayService; | ||||
| import com.github.binarywang.wxpay.v3.util.AesUtils; | ||||
| import com.google.gson.Gson; | ||||
| import com.google.gson.GsonBuilder; | ||||
| import lombok.RequiredArgsConstructor; | ||||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.apache.http.client.utils.URIBuilder; | ||||
|  | ||||
| @ -26,7 +25,6 @@ import java.util.Map; | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| public class PayScoreServiceImpl implements PayScoreService { | ||||
|   private static final Gson GSON = new GsonBuilder().create(); | ||||
|   private final WxPayService payService; | ||||
|  | ||||
|   @Override | ||||
| @ -37,8 +35,8 @@ public class PayScoreServiceImpl implements PayScoreService { | ||||
|     request.setAppid(config.getAppId()); | ||||
|     request.setServiceId(config.getServiceId()); | ||||
|     request.setNotifyUrl(config.getPayScoreNotifyUrl()); | ||||
|     String result = payService.postV3(url, GSON.toJson(request)); | ||||
|     WxPayScoreResult wxPayScoreCreateResult = GSON.fromJson(result, WxPayScoreResult.class); | ||||
|     String result = this.payService.postV3(url, request.toJson()); | ||||
|     WxPayScoreResult wxPayScoreCreateResult = WxPayScoreResult.fromJson(result); | ||||
|  | ||||
|     //补充算一下签名给小程序跳转用 | ||||
|     String currentTimeMillis = System.currentTimeMillis() + ""; | ||||
| @ -84,7 +82,7 @@ public class PayScoreServiceImpl implements PayScoreService { | ||||
|     uriBuilder.setParameter("appid", config.getAppId()); | ||||
|     try { | ||||
|       String result = payService.getV3(uriBuilder.build()); | ||||
|       return GSON.fromJson(result, WxPayScoreResult.class); | ||||
|       return WxPayScoreResult.fromJson(result); | ||||
|     } catch (URISyntaxException e) { | ||||
|       throw new WxPayException("未知异常!", e); | ||||
|     } | ||||
| @ -95,12 +93,12 @@ public class PayScoreServiceImpl implements PayScoreService { | ||||
|   public WxPayScoreResult cancelServiceOrder(String outOrderNo, String reason) throws WxPayException { | ||||
|     WxPayConfig config = this.payService.getConfig(); | ||||
|     String url = String.format("%s/v3/payscore/serviceorder/%s/cancel", this.payService.getPayBaseUrl(), outOrderNo); | ||||
|     HashMap<String, Object> map = new HashMap<>(4); | ||||
|     Map<String, Object> map = new HashMap<>(4); | ||||
|     map.put("appid", config.getAppId()); | ||||
|     map.put("service_id", config.getServiceId()); | ||||
|     map.put("reason", reason); | ||||
|     String result = payService.postV3(url, GSON.toJson(map)); | ||||
|     return GSON.fromJson(result, WxPayScoreResult.class); | ||||
|     String result = payService.postV3(url, WxGsonBuilder.create().toJson(map)); | ||||
|     return WxPayScoreResult.fromJson(result); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
| @ -111,8 +109,8 @@ public class PayScoreServiceImpl implements PayScoreService { | ||||
|     request.setAppid(config.getAppId()); | ||||
|     request.setServiceId(config.getServiceId()); | ||||
|     request.setOutOrderNo(null); | ||||
|     String result = payService.postV3(url, GSON.toJson(request)); | ||||
|     return GSON.fromJson(result, WxPayScoreResult.class); | ||||
|     String result = payService.postV3(url, request.toJson()); | ||||
|     return WxPayScoreResult.fromJson(result); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
| @ -123,19 +121,19 @@ public class PayScoreServiceImpl implements PayScoreService { | ||||
|     request.setAppid(config.getAppId()); | ||||
|     request.setServiceId(config.getServiceId()); | ||||
|     request.setOutOrderNo(null); | ||||
|     String result = payService.postV3(url, GSON.toJson(request)); | ||||
|     return GSON.fromJson(result, WxPayScoreResult.class); | ||||
|     String result = payService.postV3(url, request.toJson()); | ||||
|     return WxPayScoreResult.fromJson(result); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WxPayScoreResult payServiceOrder(String outOrderNo) throws WxPayException { | ||||
|     WxPayConfig config = this.payService.getConfig(); | ||||
|     String url = String.format("%s/v3/payscore/serviceorder/%s/pay", this.payService.getPayBaseUrl(), outOrderNo); | ||||
|     HashMap<String, Object> map = new HashMap<>(2); | ||||
|     Map<String, Object> map = new HashMap<>(2); | ||||
|     map.put("appid", config.getAppId()); | ||||
|     map.put("service_id", config.getServiceId()); | ||||
|     String result = payService.postV3(url, GSON.toJson(map)); | ||||
|     return GSON.fromJson(result, WxPayScoreResult.class); | ||||
|     String result = payService.postV3(url, WxGsonBuilder.create().toJson(map)); | ||||
|     return WxPayScoreResult.fromJson(result); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
| @ -146,13 +144,13 @@ public class PayScoreServiceImpl implements PayScoreService { | ||||
|     request.setAppid(config.getAppId()); | ||||
|     request.setServiceId(config.getServiceId()); | ||||
|     request.setOutOrderNo(null); | ||||
|     String result = payService.postV3(url, GSON.toJson(request)); | ||||
|     return GSON.fromJson(result, WxPayScoreResult.class); | ||||
|     String result = payService.postV3(url, request.toJson()); | ||||
|     return WxPayScoreResult.fromJson(result); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public PayScoreNotifyData parseNotifyData(String data) { | ||||
|     return GSON.fromJson(data, PayScoreNotifyData.class); | ||||
|     return WxGsonBuilder.create().fromJson(data, PayScoreNotifyData.class); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
| @ -163,8 +161,7 @@ public class PayScoreServiceImpl implements PayScoreService { | ||||
|     String nonce = resource.getNonce(); | ||||
|     String apiV3Key = this.payService.getConfig().getApiV3Key(); | ||||
|     try { | ||||
|       String s = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key); | ||||
|       return GSON.fromJson(s, WxPayScoreResult.class); | ||||
|       return WxPayScoreResult.fromJson(AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key)); | ||||
|     } catch (GeneralSecurityException | IOException e) { | ||||
|       throw new WxPayException("解析报文异常!", e); | ||||
|     } | ||||
|  | ||||
| @ -25,6 +25,7 @@ import java.security.cert.CertificateExpiredException; | ||||
| import java.security.cert.CertificateNotYetValidException; | ||||
| import java.security.cert.X509Certificate; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.concurrent.locks.ReentrantLock; | ||||
|  | ||||
| @ -148,30 +149,32 @@ public class AutoUpdateCertificatesVerifier implements Verifier { | ||||
|    * 反序列化证书并解密 | ||||
|    */ | ||||
|   private List<X509Certificate> deserializeToCerts(byte[] apiV3Key, String body) throws GeneralSecurityException, IOException { | ||||
|     AesUtils decryptor = new AesUtils(apiV3Key); | ||||
|     AesUtils aesUtils = new AesUtils(apiV3Key); | ||||
|     ObjectMapper mapper = new ObjectMapper(); | ||||
|     JsonNode dataNode = mapper.readTree(body).get("data"); | ||||
|     List<X509Certificate> newCertList = new ArrayList<>(); | ||||
|     if (dataNode != null) { | ||||
|       for (int i = 0, count = dataNode.size(); i < count; i++) { | ||||
|         JsonNode encryptCertificateNode = dataNode.get(i).get("encrypt_certificate"); | ||||
|         //解密 | ||||
|         String cert = decryptor.decryptToString( | ||||
|           encryptCertificateNode.get("associated_data").toString().replaceAll("\"", "") | ||||
|             .getBytes(StandardCharsets.UTF_8), | ||||
|           encryptCertificateNode.get("nonce").toString().replaceAll("\"", "") | ||||
|             .getBytes(StandardCharsets.UTF_8), | ||||
|           encryptCertificateNode.get("ciphertext").toString().replaceAll("\"", "")); | ||||
|     if (dataNode == null) { | ||||
|       return Collections.emptyList(); | ||||
|     } | ||||
|  | ||||
|         X509Certificate x509Cert = PemUtils | ||||
|           .loadCertificate(new ByteArrayInputStream(cert.getBytes(StandardCharsets.UTF_8))); | ||||
|         try { | ||||
|           x509Cert.checkValidity(); | ||||
|         } catch (CertificateExpiredException | CertificateNotYetValidException e) { | ||||
|           continue; | ||||
|         } | ||||
|         newCertList.add(x509Cert); | ||||
|     List<X509Certificate> newCertList = new ArrayList<>(); | ||||
|     for (int i = 0, count = dataNode.size(); i < count; i++) { | ||||
|       JsonNode encryptCertificateNode = dataNode.get(i).get("encrypt_certificate"); | ||||
|       //解密 | ||||
|       String cert = aesUtils.decryptToString( | ||||
|         encryptCertificateNode.get("associated_data").toString().replaceAll("\"", "") | ||||
|           .getBytes(StandardCharsets.UTF_8), | ||||
|         encryptCertificateNode.get("nonce").toString().replaceAll("\"", "") | ||||
|           .getBytes(StandardCharsets.UTF_8), | ||||
|         encryptCertificateNode.get("ciphertext").toString().replaceAll("\"", "")); | ||||
|  | ||||
|       X509Certificate x509Cert = PemUtils | ||||
|         .loadCertificate(new ByteArrayInputStream(cert.getBytes(StandardCharsets.UTF_8))); | ||||
|       try { | ||||
|         x509Cert.checkValidity(); | ||||
|       } catch (CertificateExpiredException | CertificateNotYetValidException e) { | ||||
|         continue; | ||||
|       } | ||||
|       newCertList.add(x509Cert); | ||||
|     } | ||||
|  | ||||
|     return newCertList; | ||||
|  | ||||
| @ -0,0 +1,54 @@ | ||||
| package com.github.binarywang.wxpay.bean.payscore; | ||||
|  | ||||
| import org.testng.annotations.Test; | ||||
|  | ||||
| /** | ||||
|  * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  * @date 2020-07-11 | ||||
|  */ | ||||
| public class WxPayScoreRequestTest { | ||||
|   @Test | ||||
|   public void testToJson() { | ||||
|     WxPayScoreRequest request = WxPayScoreRequest.builder() | ||||
|       .outOrderNo("QLS202005201058000201") | ||||
|       .appid("123") | ||||
|       .serviceId("345") | ||||
|       .serviceIntroduction("租借服务") | ||||
|       .timeRange(new TimeRange("OnAccept", "20200520225840")) | ||||
|       .build(); | ||||
|     System.out.println(request.toJson()); | ||||
|     /*    { | ||||
|       "out_order_no":"QLS202005201058000201", | ||||
|       "appid":"123", | ||||
|       "service_id":"345", | ||||
|       "service_introduction":"租借服务", | ||||
|       "time_range":{ | ||||
|       "start_time":"OnAccept", | ||||
|         "end_time":"20200520225840" | ||||
|     }, | ||||
|       "location":{ | ||||
|       "start_location":"山", | ||||
|         "end_location":"山" | ||||
|     }, | ||||
|       "risk_fund":{ | ||||
|       "name":"DEPOSIT", | ||||
|         "amount":200, | ||||
|         "description":"丢失偿还费用2元/台" | ||||
|     }, | ||||
|       "attach":"", | ||||
|       "notify_url":"/pay/notify/payScore", | ||||
|       "openid":"", | ||||
|       "need_user_confirm":true, | ||||
|       "profit_sharing":false, | ||||
|       "post_payments":[ | ||||
|       { | ||||
|         "name":"租借服务", | ||||
|         "amount":100, | ||||
|         "description":"服务费:1元/台", | ||||
|         "count":1 | ||||
|       } | ||||
|     ], | ||||
|       "total_amount":0 | ||||
|     }*/ | ||||
|   } | ||||
| } | ||||
| @ -64,7 +64,7 @@ public class PayScoreServiceImplTest { | ||||
|   } | ||||
|  | ||||
|   @Test | ||||
|   public void testQueryServiceOrder() throws URISyntaxException, WxPayException { | ||||
|   public void testQueryServiceOrder() throws WxPayException { | ||||
|     //两个参数选填一个 | ||||
|     this.payService.getPayScoreService().queryServiceOrder("11", ""); | ||||
|   } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang