From 39cea92171147da6bf578553c4388c2b87e2a654 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Tue, 19 May 2020 21:28:26 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=88=86=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=A7=84=E8=8C=83=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/config/WxPayAutoConfiguration.java | 2 +- weixin-java-pay/pom.xml | 13 +- .../wxpay/bean/payscore/Detail.java | 36 +++ .../wxpay/bean/payscore/Location.java | 27 +++ .../wxpay/bean/payscore/NotifyData.java | 45 ---- .../bean/payscore/PayScoreNotifyData.java | 56 +++++ .../wxpay/bean/payscore/PostDiscount.java | 31 +++ .../wxpay/bean/payscore/PostPayment.java | 33 +++ .../wxpay/bean/payscore/RiskFund.java | 30 +++ .../wxpay/bean/payscore/TimeRange.java | 27 +++ .../bean/payscore/WxPayScoreRequest.java | 165 +++++--------- .../wxpay/bean/payscore/WxPayScoreResult.java | 208 +++++++----------- .../binarywang/wxpay/config/WxPayConfig.java | 97 ++++---- .../wxpay/service/PayScoreService.java | 132 +++++------ .../service/impl/PayScoreServiceImpl.java | 172 ++++++--------- .../impl/WxPayServiceApacheHttpImpl.java | 13 +- .../service/impl/PayScoreServiceImplTest.java | 46 ++++ 17 files changed, 587 insertions(+), 546 deletions(-) create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Detail.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Location.java delete mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/NotifyData.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PayScoreNotifyData.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostDiscount.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostPayment.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/RiskFund.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/TimeRange.java create mode 100644 weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImplTest.java diff --git a/spring-boot-starters/wx-java-pay-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java b/spring-boot-starters/wx-java-pay-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java index ed5da834a..2dd44004a 100644 --- a/spring-boot-starters/wx-java-pay-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java +++ b/spring-boot-starters/wx-java-pay-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java @@ -55,7 +55,7 @@ public class WxPayAutoConfiguration { payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.properties.getPrivateKeyPath())); payConfig.setPrivateCertPath(StringUtils.trimToNull(this.properties.getPrivateCertPath())); payConfig.setCertSerialNo(StringUtils.trimToNull(this.properties.getCertSerialNo())); - payConfig.setApiv3Key(StringUtils.trimToNull(this.properties.getApiv3Key())); + payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getApiv3Key())); wxPayService.setConfig(payConfig); return wxPayService; diff --git a/weixin-java-pay/pom.xml b/weixin-java-pay/pom.xml index 68e7a0576..a74299968 100644 --- a/weixin-java-pay/pom.xml +++ b/weixin-java-pay/pom.xml @@ -82,19 +82,10 @@ org.projectlombok lombok - - com.fasterxml.jackson.core - jackson-databind - 2.9.7 + com.google.code.gson + gson - - com.alibaba - fastjson - 1.2.58 - - - diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Detail.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Detail.java new file mode 100644 index 000000000..b52c2abc1 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Detail.java @@ -0,0 +1,36 @@ +package com.github.binarywang.wxpay.bean.payscore; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 明细. + * + * @author doger.wang + * @date 2020-05-19 + */ +@Data +@NoArgsConstructor +public class Detail implements Serializable { + private static final long serialVersionUID = -3901373259400050385L; + /** + * seq : 1 + * amount : 900 + * paid_type : NEWTON + * paid_time : 20091225091210 + * transaction_id : 15646546545165651651 + */ + @SerializedName("seq") + private int seq; + @SerializedName("amount") + private int amount; + @SerializedName("paid_type") + private String paidType; + @SerializedName("paid_time") + private String paidTime; + @SerializedName("transaction_id") + private String transactionId; +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Location.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Location.java new file mode 100644 index 000000000..b3c82f7d9 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/Location.java @@ -0,0 +1,27 @@ +package com.github.binarywang.wxpay.bean.payscore; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 服务位置信息. + * + * @author doger.wang + * @date 2020-05-19 + */ +@Data +@NoArgsConstructor +public class Location implements Serializable { + private static final long serialVersionUID = -4510224826631515344L; + /** + * start_location : 嗨客时尚主题展餐厅 + * end_location : 嗨客时尚主题展餐厅 + */ + @SerializedName("start_location") + private String startLocation; + @SerializedName("end_location") + private String endLocation; +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/NotifyData.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/NotifyData.java deleted file mode 100644 index b46de1f11..000000000 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/NotifyData.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.github.binarywang.wxpay.bean.payscore; - -import lombok.Data; -import lombok.NoArgsConstructor; - -/** - * 微信支付分确认订单跟支付回调对象 - * @author doger.wang - * @date 2020/5/14 12:18 - */ -@NoArgsConstructor -@Data -public class NotifyData { - - - /** - * id : EV-2018022511223320873 - * create_time : 20180225112233 - * resource_type : encrypt-resource - * event_type : PAYSCORE.USER_CONFIRM - * resource : {"algorithm":"AEAD_AES_256_GCM","ciphertext":"...","nonce":"...","associated_data":""} - */ - - private String id; - private String create_time; - private String resource_type; - private String event_type; - private Resource resource; - - @NoArgsConstructor - @Data - public static class Resource { - /** - * algorithm : AEAD_AES_256_GCM - * ciphertext : ... - * nonce : ... - * associated_data : - */ - - private String algorithm; - private String ciphertext; - private String nonce; - private String associated_data; - } -} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PayScoreNotifyData.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PayScoreNotifyData.java new file mode 100644 index 000000000..81d5568bc --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PayScoreNotifyData.java @@ -0,0 +1,56 @@ +package com.github.binarywang.wxpay.bean.payscore; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 微信支付分确认订单跟支付回调对象 + * + * @author doger.wang + * @date 2020/5/14 12:18 + */ +@NoArgsConstructor +@Data +public class PayScoreNotifyData implements Serializable { + private static final long serialVersionUID = -8538014389773390989L; + + /** + * id : EV-2018022511223320873 + * create_time : 20180225112233 + * resource_type : encrypt-resource + * event_type : PAYSCORE.USER_CONFIRM + * resource : {"algorithm":"AEAD_AES_256_GCM","ciphertext":"...","nonce":"...","associated_data":""} + */ + @SerializedName("id") + private String id; + @SerializedName("create_time") + private String createTime; + @SerializedName("resource_type") + private String resourceType; + @SerializedName("event_type") + private String eventType; + @SerializedName("resource") + private Resource resource; + + @Data + public static class Resource implements Serializable { + private static final long serialVersionUID = 8530711804335261449L; + /** + * algorithm : AEAD_AES_256_GCM + * ciphertext : ... + * nonce : ... + * associated_data : + */ + @SerializedName("algorithm") + private String algorithm; + @SerializedName("ciphertext") + private String cipherText; + @SerializedName("nonce") + private String nonce; + @SerializedName("associated_data") + private String associatedData; + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostDiscount.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostDiscount.java new file mode 100644 index 000000000..ebd2cf2b3 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostDiscount.java @@ -0,0 +1,31 @@ +package com.github.binarywang.wxpay.bean.payscore; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 后付费商户优惠. + * + * @author doger.wang + * @date 2020-05-19 + */ +@Data +@NoArgsConstructor +public class PostDiscount implements Serializable { + private static final long serialVersionUID = 2764537888242763379L; + /** + * name : 满20减1元 + * description : 不与其他优惠叠加 + */ + @SerializedName("name") + private String name; + @SerializedName("description") + private String description; + @SerializedName("count") + private int count; + @SerializedName("amount") + private int amount; +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostPayment.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostPayment.java new file mode 100644 index 000000000..fef0b5ab8 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/PostPayment.java @@ -0,0 +1,33 @@ +package com.github.binarywang.wxpay.bean.payscore; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 后付费项目. + * + * @author doger.wang + * @date 2020-05-19 + */ +@Data +@NoArgsConstructor +public class PostPayment implements Serializable { + private static final long serialVersionUID = 2007722927556382895L; + /** + * name : 就餐费用服务费 + * amount : 4000 + * description : 就餐人均100元服务费:100/小时 + * count : 1 + */ + @SerializedName("name") + private String name; + @SerializedName("amount") + private int amount; + @SerializedName("description") + private String description; + @SerializedName("count") + private int count; +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/RiskFund.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/RiskFund.java new file mode 100644 index 000000000..c6bd84018 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/RiskFund.java @@ -0,0 +1,30 @@ +package com.github.binarywang.wxpay.bean.payscore; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 订单风险金信息. + * + * @author doger.wang + * @date 2020-05-19 + */ +@Data +@NoArgsConstructor +public class RiskFund implements Serializable { + private static final long serialVersionUID = -3583406084396059152L; + /** + * name : ESTIMATE_ORDER_COST + * amount : 10000 + * description : 就餐的预估费用 + */ + @SerializedName("name") + private String name; + @SerializedName("amount") + private int amount; + @SerializedName("description") + private String description; +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/TimeRange.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/TimeRange.java new file mode 100644 index 000000000..08b86d6ed --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/TimeRange.java @@ -0,0 +1,27 @@ +package com.github.binarywang.wxpay.bean.payscore; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 服务时间范围. + * + * @author doger.wang + * @date 2020-05-19 + */ +@Data +@NoArgsConstructor +public class TimeRange implements Serializable { + private static final long serialVersionUID = 8169562173656314930L; + /** + * start_time : 20091225091010 + * end_time : 20091225121010 + */ + @SerializedName("start_time") + private String startTime; + @SerializedName("end_time") + private String endTime; +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreRequest.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreRequest.java index 7166a31c2..81fd04b8e 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreRequest.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreRequest.java @@ -1,5 +1,6 @@ package com.github.binarywang.wxpay.bean.payscore; +import com.google.gson.annotations.SerializedName; import lombok.Data; import lombok.NoArgsConstructor; @@ -13,118 +14,60 @@ import java.util.List; @NoArgsConstructor @Data public class WxPayScoreRequest implements Serializable { - - private static final long serialVersionUID = 364764508076146082L; + /** - * out_order_no : 1234323JKHDFE1243252 - * appid : wxd678efh567hg6787 - * service_id : 500001 - * service_introduction : 某某酒店 - * post_payments : [{"name":"就餐费用服务费","amount":4000,"description":"就餐人均100元服务费:100/小时","count":1}] - * post_discounts : [{"name":"满20减1元","description":"不与其他优惠叠加"}] - * time_range : {"start_time":"20091225091010","end_time":"20091225121010"} - * location : {"start_location":"嗨客时尚主题展餐厅","end_location":"嗨客时尚主题展餐厅"} - * risk_fund : {"name":"ESTIMATE_ORDER_COST","amount":10000,"description":"就餐的预估费用"} - * attach : Easdfowealsdkjfnlaksjdlfkwqoi&wl3l2sald - * notify_url : https://api.test.com - * openid : oUpF8uMuAJO_M2pxb1Q9zNjWeS6o - * need_user_confirm : true - */ + * out_order_no : 1234323JKHDFE1243252 + * appid : wxd678efh567hg6787 + * service_id : 500001 + * service_introduction : 某某酒店 + * post_payments : [{"name":"就餐费用服务费","amount":4000,"description":"就餐人均100元服务费:100/小时","count":1}] + * post_discounts : [{"name":"满20减1元","description":"不与其他优惠叠加"}] + * time_range : {"start_time":"20091225091010","end_time":"20091225121010"} + * location : {"start_location":"嗨客时尚主题展餐厅","end_location":"嗨客时尚主题展餐厅"} + * risk_fund : {"name":"ESTIMATE_ORDER_COST","amount":10000,"description":"就餐的预估费用"} + * attach : Easdfowealsdkjfnlaksjdlfkwqoi&wl3l2sald + * notify_url : https://api.test.com + * openid : oUpF8uMuAJO_M2pxb1Q9zNjWeS6o + * need_user_confirm : true + */ + @SerializedName("out_order_no") + private String outOrderNo; + @SerializedName("appid") + private String appid; + @SerializedName("service_id") + private String serviceId; + @SerializedName("service_introduction") + private String serviceIntroduction; + @SerializedName("time_range") + private TimeRange timeRange; + @SerializedName("location") + private Location location; + @SerializedName("risk_fund") + private RiskFund riskFund; + @SerializedName("attach") + private String attach; + @SerializedName("notify_url") + private String notifyUrl; + @SerializedName("openid") + private String openid; + @SerializedName("need_user_confirm") + private boolean needUserConfirm; + @SerializedName("profit_sharing") + private boolean profitSharing; + @SerializedName("post_payments") + private List postPayments; + @SerializedName("post_discounts") + private List postDiscounts; + @SerializedName("total_amount") + private int totalAmount; + @SerializedName("reason") + private String reason; + @SerializedName("goods_tag") + private String goodsTag; + @SerializedName("type") + private String type; + @SerializedName("detail") + private Detail detail; - private String out_order_no; - private String appid; - private String service_id; - private String service_introduction; - private TimeRange time_range; - private Location location; - private RiskFund risk_fund; - private String attach; - private String notify_url; - private String openid; - private boolean need_user_confirm; - private boolean profit_sharing; - private List post_payments; - private List post_discounts; - private int total_amount; - private String reason; - private String goods_tag; - private String type; - private Detail detail; - - @NoArgsConstructor - @Data - public static class Detail { - private String paid_time; - } - - - - @NoArgsConstructor - @Data - public static class TimeRange { - /** - * start_time : 20091225091010 - * end_time : 20091225121010 - */ - - private String start_time; - private String end_time; - } - - @NoArgsConstructor - @Data - public static class Location { - /** - * start_location : 嗨客时尚主题展餐厅 - * end_location : 嗨客时尚主题展餐厅 - */ - - private String start_location; - private String end_location; - } - - @NoArgsConstructor - @Data - public static class RiskFund { - /** - * name : ESTIMATE_ORDER_COST - * amount : 10000 - * description : 就餐的预估费用 - */ - - private String name; - private int amount; - private String description; - } - - @NoArgsConstructor - @Data - public static class PostPayments { - /** - * name : 就餐费用服务费 - * amount : 4000 - * description : 就餐人均100元服务费:100/小时 - * count : 1 - */ - - private String name; - private int amount; - private String description; - private int count; - } - - @NoArgsConstructor - @Data - public static class PostDiscounts { - /** - * name : 满20减1元 - * description : 不与其他优惠叠加 - */ - - private String name; - private String description; - private int count; - private int amount; - } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreResult.java index 6f72039bc..506148ad9 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreResult.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/payscore/WxPayScoreResult.java @@ -1,6 +1,6 @@ package com.github.binarywang.wxpay.bean.payscore; -import com.alibaba.fastjson.annotation.JSONField; +import com.google.gson.annotations.SerializedName; import lombok.Data; import lombok.NoArgsConstructor; @@ -15,119 +15,78 @@ import java.util.Map; @NoArgsConstructor @Data public class WxPayScoreResult implements Serializable { - - private static final long serialVersionUID = 8809250065540275770L; - /** - * appid : wxd678efh567hg6787 - * mchid : 1230000109 - * out_order_no : 1234323JKHDFE1243252 - * service_id : 500001 - * service_introduction : 某某酒店 - * state : CREATED - * state_description : MCH_COMPLETE - * post_payments : [{"name":"就餐费用服务费","amount":4000,"description":"就餐人均100元服务费:100/小时","count":1}] - * post_discounts : [{"name":"满20减1元","description":"不与其他优惠叠加"}] - * risk_fund : {"name":" ESTIMATE_ORDER_COST","amount":10000,"description":"就餐的预估费用"} - * time_range : {"start_time":"20091225091010","end_time":"20091225121010"} - * location : {"start_location":"嗨客时尚主题展餐厅","end_location":"嗨客时尚主题展餐厅"} - * attach : Easdfowealsdkjfnlaksjdlfkwqoi&wl3l2sald - * notify_url : https://api.test.com - * order_id : 15646546545165651651 - * package : DJIOSQPYWDxsjdldeuwhdodwxasd_dDiodnwjh9we - */ - private String appid; - private String mchid; - private String out_order_no; - private String service_id; - private String service_introduction; - private String state; - private String state_description; - private RiskFund risk_fund; - private TimeRange time_range; - private Location location; - private String attach; - private String notify_url; - private String order_id; - @JSONField(name = "package") - private String packageX; - private List post_payments; - private List post_discounts; - private boolean need_collection; + /** + * appid : wxd678efh567hg6787 + * mchid : 1230000109 + * out_order_no : 1234323JKHDFE1243252 + * service_id : 500001 + * service_introduction : 某某酒店 + * state : CREATED + * state_description : MCH_COMPLETE + * post_payments : [{"name":"就餐费用服务费","amount":4000,"description":"就餐人均100元服务费:100/小时","count":1}] + * post_discounts : [{"name":"满20减1元","description":"不与其他优惠叠加"}] + * risk_fund : {"name":" ESTIMATE_ORDER_COST","amount":10000,"description":"就餐的预估费用"} + * time_range : {"start_time":"20091225091010","end_time":"20091225121010"} + * location : {"start_location":"嗨客时尚主题展餐厅","end_location":"嗨客时尚主题展餐厅"} + * attach : Easdfowealsdkjfnlaksjdlfkwqoi&wl3l2sald + * notify_url : https://api.test.com + * order_id : 15646546545165651651 + * package : DJIOSQPYWDxsjdldeuwhdodwxasd_dDiodnwjh9we + */ + @SerializedName("appid") + private String appid; + @SerializedName("mchid") + private String mchid; + @SerializedName("out_order_no") + private String outOrderNo; + @SerializedName("service_id") + private String serviceId; + @SerializedName("service_introduction") + private String serviceIntroduction; + @SerializedName("state") + private String state; + @SerializedName("state_description") + private String stateDescription; + @SerializedName("risk_fund") + private RiskFund riskFund; + @SerializedName("time_range") + private TimeRange timeRange; + @SerializedName("location") + private Location location; + @SerializedName("attach") + private String attach; + @SerializedName("notify_url") + private String notifyUrl; + @SerializedName("order_id") + private String orderId; + @SerializedName("package") + private String packageX; + @SerializedName("post_payments") + private List postPayments; + @SerializedName("post_discounts") + private List postDiscounts; + @SerializedName("need_collection") + private boolean needCollection; + /** + * 收款信息 + */ + @SerializedName("collection") private Collection collection; - //用于跳转的sign注意区分需确认模式和无需确认模式的数据差别。创单接口会返回,查询请自行组装 + /** + * 用于跳转的sign注意区分需确认模式和无需确认模式的数据差别。创单接口会返回,查询请自行组装 + */ + @SerializedName("payScoreSignInfo") private Map payScoreSignInfo; - @NoArgsConstructor - @Data - public static class RiskFund { - /** - * name : ESTIMATE_ORDER_COST - * amount : 10000 - * description : 就餐的预估费用 - */ - - private String name; - private int amount; - private String description; - } - - @NoArgsConstructor - @Data - public static class TimeRange { - /** - * start_time : 20091225091010 - * end_time : 20091225121010 - */ - - private String start_time; - private String end_time; - } - - @NoArgsConstructor - @Data - public static class Location { - /** - * start_location : 嗨客时尚主题展餐厅 - * end_location : 嗨客时尚主题展餐厅 - */ - - private String start_location; - private String end_location; - } - - @NoArgsConstructor - @Data - public static class PostPayments { - /** - * name : 就餐费用服务费 - * amount : 4000 - * description : 就餐人均100元服务费:100/小时 - * count : 1 - */ - - private String name; - private int amount; - private String description; - private int count; - } - - @NoArgsConstructor - @Data - public static class PostDiscounts { - /** - * name : 满20减1元 - * description : 不与其他优惠叠加 - */ - - private String name; - private String description; - } - - @NoArgsConstructor + /** + * 收款信息 + */ @Data - public static class Collection { + @NoArgsConstructor + public static class Collection implements Serializable { + private static final long serialVersionUID = 2279516555276133086L; /** * state : USER_PAID * total_amount : 3900 @@ -135,29 +94,16 @@ public class WxPayScoreResult implements Serializable { * paid_amount : 900 * details : [{"seq":1,"amount":900,"paid_type":"NEWTON","paid_time":"20091225091210","transaction_id":"15646546545165651651"}] */ - + @SerializedName("state") private String state; - private int total_amount; - private int paying_amount; - private int paid_amount; - private List
details; - - @NoArgsConstructor - @Data - public static class Details { - /** - * seq : 1 - * amount : 900 - * paid_type : NEWTON - * paid_time : 20091225091210 - * transaction_id : 15646546545165651651 - */ - - private int seq; - private int amount; - private String paid_type; - private String paid_time; - private String transaction_id; - } + @SerializedName("total_amount") + private int totalAmount; + @SerializedName("paying_amount") + private int payingAmount; + @SerializedName("paid_amount") + private int paidAmount; + @SerializedName("details") + private List details; } + } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java index 4371afe10..7b23ea485 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java @@ -9,6 +9,7 @@ import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator; import com.github.binarywang.wxpay.v3.util.PemUtils; import lombok.Data; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.ssl.SSLContexts; @@ -16,6 +17,7 @@ import org.apache.http.ssl.SSLContexts; import javax.net.ssl.SSLContext; import java.io.*; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.security.KeyStore; import java.security.PrivateKey; import java.security.cert.X509Certificate; @@ -107,7 +109,7 @@ public class WxPayConfig { /** * apiV3 秘钥值. */ - private String apiv3Key; + private String apiV3Key; /** * apiV3 证书序列号值 @@ -125,7 +127,7 @@ public class WxPayConfig { */ private String payScoreNotifyUrl; - private CloseableHttpClient apiv3HttpClient; + private CloseableHttpClient apiV3HttpClient; /** @@ -151,6 +153,7 @@ public class WxPayConfig { /** * 返回所设置的微信支付接口请求地址域名. + * * @return 微信支付接口请求地址域名 */ public String getPayBaseUrl() { @@ -184,10 +187,11 @@ public class WxPayConfig { String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!"; String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!"; if (this.getKeyPath().startsWith(prefix)) { - String path = StringUtils.removeFirst(this.getKeyPath(), prefix); + String path = RegExUtils.removeFirst(this.getKeyPath(), prefix); if (!path.startsWith("/")) { path = "/" + path; } + inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); if (inputStream == null) { throw new WxPayException(fileNotFoundMsg); @@ -230,18 +234,17 @@ public class WxPayConfig { /** - * @Author doger.wang - * @Description 初始化api v3请求头 自动签名验签 + * 初始化api v3请求头 自动签名验签 * 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient - * @Date 2020/5/14 10:10 - * @Param [] + * * @return org.apache.http.impl.client.CloseableHttpClient + * @author doger.wang **/ - public CloseableHttpClient initApiV3HttpClient()throws WxPayException { + public CloseableHttpClient initApiV3HttpClient() throws WxPayException { String privateKeyPath = this.getPrivateKeyPath(); String privateCertPath = this.getPrivateCertPath(); String certSerialNo = this.getCertSerialNo(); - String apiv3Key = this.getApiv3Key(); + String apiV3Key = this.getApiV3Key(); if (StringUtils.isBlank(privateKeyPath)) { throw new WxPayException("请确保privateKeyPath已设置"); } @@ -251,55 +254,55 @@ public class WxPayConfig { if (StringUtils.isBlank(certSerialNo)) { throw new WxPayException("请确保certSerialNo证书序列号已设置"); } - if (StringUtils.isBlank(apiv3Key)) { - throw new WxPayException("请确保apiv3Key值已设置"); + if (StringUtils.isBlank(apiV3Key)) { + throw new WxPayException("请确保apiV3Key值已设置"); } - - InputStream keyinputStream=null; - InputStream certinputStream=null; + InputStream keyInputStream = null; + InputStream certInputStream = null; final String prefix = "classpath:"; if (privateKeyPath.startsWith(prefix)) { - String keypath = StringUtils.removeFirst(privateKeyPath, prefix); + String keypath = RegExUtils.removeFirst(privateKeyPath, prefix); if (!keypath.startsWith("/")) { keypath = "/" + keypath; } - keyinputStream = WxPayConfig.class.getResourceAsStream(keypath); - if (keyinputStream == null) { + keyInputStream = WxPayConfig.class.getResourceAsStream(keypath); + if (keyInputStream == null) { throw new WxPayException("证书文件【" + this.getPrivateKeyPath() + "】不存在,请核实!"); } } - if (privateCertPath.startsWith(prefix)) { - String certpath = StringUtils.removeFirst(privateCertPath, prefix); - if (!certpath.startsWith("/")) { - certpath = "/" + certpath; - } - certinputStream = WxPayConfig.class.getResourceAsStream(certpath); - if (certinputStream == null) { - throw new WxPayException("证书文件【" + this.getPrivateCertPath() + "】不存在,请核实!"); - } + if (privateCertPath.startsWith(prefix)) { + String certpath = RegExUtils.removeFirst(privateCertPath, prefix); + if (!certpath.startsWith("/")) { + certpath = "/" + certpath; + } + certInputStream = WxPayConfig.class.getResourceAsStream(certpath); + if (certInputStream == null) { + throw new WxPayException("证书文件【" + this.getPrivateCertPath() + "】不存在,请核实!"); } - CloseableHttpClient httpClient = null; - try { - WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create(); - PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyinputStream); - X509Certificate x509Certificate = PemUtils.loadCertificate(certinputStream); - ArrayList certificates = new ArrayList<>(); - certificates.add(x509Certificate); - builder.withMerchant(mchId, certSerialNo, merchantPrivateKey); - builder.withWechatpay(certificates); - AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( - new WechatPay2Credentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)), - apiv3Key.getBytes("utf-8")); - builder.withValidator(new WechatPay2Validator(verifier)); - httpClient = builder.build(); - this.apiv3HttpClient =httpClient; - } catch (Exception e) { - throw new WxPayException("v3请求构造异常", e); - } - return httpClient; - - } + + CloseableHttpClient httpClient; + try { + WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create(); + PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); + X509Certificate x509Certificate = PemUtils.loadCertificate(certInputStream); + ArrayList certificates = new ArrayList<>(); + certificates.add(x509Certificate); + builder.withMerchant(mchId, certSerialNo, merchantPrivateKey); + builder.withWechatpay(certificates); + AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( + new WechatPay2Credentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)), + apiV3Key.getBytes(StandardCharsets.UTF_8)); + builder.withValidator(new WechatPay2Validator(verifier)); + httpClient = builder.build(); + this.apiV3HttpClient = httpClient; + } catch (Exception e) { + throw new WxPayException("v3请求构造异常!", e); + } + + return httpClient; + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/PayScoreService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/PayScoreService.java index e85df7f88..dafa474be 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/PayScoreService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/PayScoreService.java @@ -1,6 +1,6 @@ package com.github.binarywang.wxpay.service; -import com.github.binarywang.wxpay.bean.payscore.NotifyData; +import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData; import com.github.binarywang.wxpay.bean.payscore.WxPayScoreRequest; import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult; import com.github.binarywang.wxpay.exception.WxPayException; @@ -11,154 +11,120 @@ import java.net.URISyntaxException; *
  *  支付分相关服务类.
  *   微信支付分是对个人的身份特质、支付行为、使用历史等情况的综合计算分值,旨在为用户提供更简单便捷的生活方式。
- *   微信用户可以在具体应用场景中,开通微信支付分。开通后,用户可以在【微信—>钱包—>支付分】中查看分数和使用记录。(即需在应用场景中使用过一次,钱包才会出现支付分入口)
+ *   微信用户可以在具体应用场景中,开通微信支付分。开通后,用户可以在【微信—>钱包—>支付分】中查看分数和使用记录。
+ *   (即需在应用场景中使用过一次,钱包才会出现支付分入口)
  *
  *  Created by doger.wang on 2020/05/12.
  * 
* - * + * @author doger.wang */ public interface PayScoreService { - - - /** *
    * 支付分创建订单API.
-   * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter1_1.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_1.shtml
+   * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_1.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/payscore/serviceorder
    * 
* * @param request 请求对象 - * @return WxPayScoreResult + * @return WxPayScoreResult wx pay score result * @throws WxPayException the wx pay exception */ WxPayScoreResult createServiceOrder(WxPayScoreRequest request) throws WxPayException; - - /** *
    * 支付分查询订单API.
    * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_2.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_2.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/payscore/serviceorder
    * 
* - * @Author doger.wang - * @Description - * @Date 2020/5/14 15:40 - * @Param out_order_no, query_id选填一个 - * @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult - **/ - WxPayScoreResult queryServiceOrder( String out_order_no,String query_id ) throws WxPayException, URISyntaxException; - + * @param outOrderNo the out order no + * @param queryId the query id + * @return the wx pay score result + * @throws WxPayException the wx pay exception + * @throws URISyntaxException the uri syntax exception + */ + WxPayScoreResult queryServiceOrder(String outOrderNo, String queryId) throws WxPayException, URISyntaxException; /** *
    * 支付分取消订单API.
    * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_3.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_3.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/cancel
    * 
* - * @Author doger.wang - * @Description - * @Date 2020/5/14 15:40 - * @Param out_order_no reason - * @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult - **/ - WxPayScoreResult cancelServiceOrder(String out_order_no, String reason) throws WxPayException; + * @param outOrderNo the out order no + * @param reason the reason + * @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult wx pay score result + * @throws WxPayException the wx pay exception + */ + WxPayScoreResult cancelServiceOrder(String outOrderNo, String reason) throws WxPayException; /** *
    * 支付分修改订单金额API.
    * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_4.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_4.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/modify
    * 
* - * @Author doger.wang - * @Description - * @Date 2020/5/14 15:40 - * @Param WxPayScoreRequest - * @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult - **/ + * @param request the request + * @return the wx pay score result + * @throws WxPayException the wx pay exception + */ WxPayScoreResult modifyServiceOrder(WxPayScoreRequest request) throws WxPayException; - /** *
    * 支付分完结订单API.
    * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_5.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_5.shtml
+   * 请求URL:https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/complete
    * 
* - * @Author doger.wang - * @Description - * @Date 2020/5/14 15:40 - * @Param WxPayScoreRequest - * @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult - **/ + * @param request the request + * @return the wx pay score result + * @throws WxPayException the wx pay exception + */ WxPayScoreResult completeServiceOrder(WxPayScoreRequest request) throws WxPayException; - /** *
    * 支付分订单收款API.
    * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_6.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_6.shtml
+   * 请求URL:https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/pay
+   *
    * 
* - * @Author doger.wang - * @Description - * @Date 2020/5/14 15:40 - * @Param out_order_no - * @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult - **/ - WxPayScoreResult payServiceOrder(String out_order_no) throws WxPayException; - + * @param outOrderNo the out order no + * @return the wx pay score result + * @throws WxPayException the wx pay exception + */ + WxPayScoreResult payServiceOrder(String outOrderNo) throws WxPayException; /** *
    * 支付分订单收款API.
    * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_7.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_7.shtml
+   * 请求URL: https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/sync
    * 
* - * @Author doger.wang - * @Description - * @Date 2020/5/14 15:40 - * @Param WxPayScoreRequest - * @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult - **/ + * @param request the request + * @return the wx pay score result + * @throws WxPayException the wx pay exception + */ WxPayScoreResult syncServiceOrder(WxPayScoreRequest request) throws WxPayException; - /** *
-   * 支付分回调内容解密方法
+   * 支付分回调内容解析方法
    * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter5_2.shtml
-   * 接口链接:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter5_2.shtml
    * 
* - * @param NotifyData 请求对象 - * @return WxPayScoreResult + * @param data the data + * @return the wx pay score result + * @throws WxPayException the wx pay exception */ - WxPayScoreResult decryptNotifyData(NotifyData data) throws WxPayException; - - - - - - - - - - - - - - - - - + WxPayScoreResult parseNotifyData(PayScoreNotifyData data) throws WxPayException; } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImpl.java index 2756a712c..79b64fb3f 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImpl.java @@ -1,7 +1,6 @@ package com.github.binarywang.wxpay.service.impl; -import com.alibaba.fastjson.JSONObject; -import com.github.binarywang.wxpay.bean.payscore.NotifyData; +import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData; import com.github.binarywang.wxpay.bean.payscore.WxPayScoreRequest; import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult; import com.github.binarywang.wxpay.config.WxPayConfig; @@ -9,11 +8,13 @@ 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 org.apache.commons.lang3.StringUtils; import org.apache.http.client.utils.URIBuilder; import java.io.IOException; -import java.net.URI; import java.net.URISyntaxException; import java.security.GeneralSecurityException; import java.util.HashMap; @@ -23,35 +24,31 @@ import java.util.Map; * @author doger.wang * @date 2020/5/14 9:43 */ - +@RequiredArgsConstructor public class PayScoreServiceImpl implements PayScoreService { - private WxPayService payService; - - public PayScoreServiceImpl(WxPayService payService) { - this.payService = payService; - } - + private static final Gson GSON = new GsonBuilder().create(); + private final WxPayService payService; @Override public WxPayScoreResult createServiceOrder(WxPayScoreRequest request) throws WxPayException { - boolean need_user_confirm = request.isNeed_user_confirm(); + boolean needUserConfirm = request.isNeedUserConfirm(); WxPayConfig config = this.payService.getConfig(); String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder"; request.setAppid(config.getAppId()); - request.setService_id(config.getServiceId()); - request.setNotify_url(config.getPayScoreNotifyUrl()); - String result = payService.postV3(url, JSONObject.toJSONString(request)); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class); + request.setServiceId(config.getServiceId()); + request.setNotifyUrl(config.getPayScoreNotifyUrl()); + String result = payService.postV3(url, GSON.toJson(request)); + WxPayScoreResult wxPayScoreCreateResult = GSON.fromJson(result, WxPayScoreResult.class); //补充算一下签名给小程序跳转用 String currentTimeMillis = System.currentTimeMillis() + ""; - Map signMap = new HashMap<>(); + Map signMap = new HashMap<>(8); signMap.put("mch_id", config.getMchId()); - if (need_user_confirm){ + if (needUserConfirm) { signMap.put("package", wxPayScoreCreateResult.getPackageX()); - }else { + } else { signMap.put("service_id", config.getServiceId()); - signMap.put("out_order_no", request.getOut_order_no()); + signMap.put("out_order_no", request.getOutOrderNo()); } signMap.put("timestamp", currentTimeMillis); signMap.put("nonce_str", currentTimeMillis); @@ -63,131 +60,96 @@ public class PayScoreServiceImpl implements PayScoreService { } @Override - public WxPayScoreResult queryServiceOrder(String out_order_no, String query_id) throws WxPayException, URISyntaxException { + public WxPayScoreResult queryServiceOrder(String outOrderNo, String queryId) throws WxPayException, URISyntaxException { WxPayConfig config = this.payService.getConfig(); String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder"; - URIBuilder uriBuilder = new URIBuilder(url); - if (StringUtils.isAllEmpty(out_order_no,query_id) || !StringUtils.isAnyEmpty(out_order_no,query_id)){ + URIBuilder uriBuilder = new URIBuilder(url); + if (StringUtils.isAllEmpty(outOrderNo, queryId) || !StringUtils.isAnyEmpty(outOrderNo, queryId)) { throw new WxPayException("out_order_no,query_id不允许都填写或都不填写"); } - if (StringUtils.isNotEmpty(out_order_no)){ - uriBuilder.setParameter("out_order_no", out_order_no); + if (StringUtils.isNotEmpty(outOrderNo)) { + uriBuilder.setParameter("out_order_no", outOrderNo); } - if (StringUtils.isNotEmpty(query_id)){ - uriBuilder.setParameter("query_id", query_id); + if (StringUtils.isNotEmpty(queryId)) { + uriBuilder.setParameter("query_id", queryId); } uriBuilder.setParameter("service_id", config.getServiceId()); uriBuilder.setParameter("appid", config.getAppId()); - URI build = uriBuilder.build(); - String result = payService.getV3(build); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class); - //补充一下加密跳转信息 -/* String currentTimeMillis = System.currentTimeMillis() + ""; - Map signMap = new HashMap(); - signMap.put("mch_id", config.getMchId()); - signMap.put("service_id", config.getServiceId()); - signMap.put("out_order_no", out_order_no); - signMap.put("timestamp", currentTimeMillis); - signMap.put("nonce_str", currentTimeMillis); - signMap.put("sign_type", "HMAC-SHA256"); - String sign = AesUtil.createSign(signMap, config.getMchKey()); - signMap.put("sign", sign); - wxPayScoreCreateResult.setPayScoreSignInfo(signMap);*/ - return wxPayScoreCreateResult; - + String result = payService.getV3(uriBuilder.build()); + return GSON.fromJson(result, WxPayScoreResult.class); } - - @Override - public WxPayScoreResult cancelServiceOrder(String out_order_no, String reason) throws WxPayException { + public WxPayScoreResult cancelServiceOrder(String outOrderNo, String reason) throws WxPayException { WxPayConfig config = this.payService.getConfig(); - String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/cancel"; - HashMap map = new HashMap<>(); - map.put("appid",config.getAppId()); - map.put("service_id",config.getServiceId()); - map.put("reason",reason); - String result = payService.postV3(url, JSONObject.toJSONString(map)); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class); - return wxPayScoreCreateResult; - + String url = String.format("%s/v3/payscore/serviceorder/%s/cancel", this.payService.getPayBaseUrl(), outOrderNo); + HashMap 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); } @Override public WxPayScoreResult modifyServiceOrder(WxPayScoreRequest request) throws WxPayException { WxPayConfig config = this.payService.getConfig(); - String out_order_no = request.getOut_order_no(); - String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/modify"; + String outOrderNo = request.getOutOrderNo(); + String url = String.format("%s/v3/payscore/serviceorder/%s/modify", this.payService.getPayBaseUrl(), outOrderNo); request.setAppid(config.getAppId()); - request.setService_id(config.getServiceId()); - request.setOut_order_no(null); - //request.setNotify_url(config.getPayScoreNotifyUrl()); - String result = payService.postV3(url, JSONObject.toJSONString(request)); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class); - return wxPayScoreCreateResult; - - + request.setServiceId(config.getServiceId()); + request.setOutOrderNo(null); + String result = payService.postV3(url, GSON.toJson(request)); + return GSON.fromJson(result, WxPayScoreResult.class); } @Override public WxPayScoreResult completeServiceOrder(WxPayScoreRequest request) throws WxPayException { WxPayConfig config = this.payService.getConfig(); - String out_order_no = request.getOut_order_no(); - String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/complete"; + String outOrderNo = request.getOutOrderNo(); + String url = String.format("%s/v3/payscore/serviceorder/%s/complete", this.payService.getPayBaseUrl(), outOrderNo); request.setAppid(config.getAppId()); - request.setService_id(config.getServiceId()); - //request.setNotify_url(config.getPayScoreNotifyUrl()); - request.setOut_order_no(null); - String result = payService.postV3(url, JSONObject.toJSONString(request)); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class); - return wxPayScoreCreateResult; - + request.setServiceId(config.getServiceId()); + request.setOutOrderNo(null); + String result = payService.postV3(url, GSON.toJson(request)); + return GSON.fromJson(result, WxPayScoreResult.class); } @Override - public WxPayScoreResult payServiceOrder(String out_order_no) throws WxPayException { + public WxPayScoreResult payServiceOrder(String outOrderNo) throws WxPayException { WxPayConfig config = this.payService.getConfig(); - String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/pay"; - HashMap map = new HashMap<>(); - map.put("appid",config.getAppId()); - map.put("service_id",config.getServiceId()); - String result = payService.postV3(url, JSONObject.toJSONString(map)); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class); - return wxPayScoreCreateResult; - + String url = String.format("%s/v3/payscore/serviceorder/%s/pay", this.payService.getPayBaseUrl(), outOrderNo); + HashMap 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); } @Override public WxPayScoreResult syncServiceOrder(WxPayScoreRequest request) throws WxPayException { WxPayConfig config = this.payService.getConfig(); - String out_order_no = request.getOut_order_no(); - String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/sync"; + String outOrderNo = request.getOutOrderNo(); + String url = String.format("%s/v3/payscore/serviceorder/%s/sync", this.payService.getPayBaseUrl(), outOrderNo); request.setAppid(config.getAppId()); - request.setService_id(config.getServiceId()); - request.setOut_order_no(null); - //request.setNotify_url(config.getPayScoreNotifyUrl()); - String result = payService.postV3(url, JSONObject.toJSONString(request)); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class); - return wxPayScoreCreateResult; - + request.setServiceId(config.getServiceId()); + request.setOutOrderNo(null); + String result = payService.postV3(url, GSON.toJson(request)); + return GSON.fromJson(result, WxPayScoreResult.class); } @Override - public WxPayScoreResult decryptNotifyData(NotifyData data) throws WxPayException{ - NotifyData.Resource resource = data.getResource(); - String ciphertext = resource.getCiphertext(); - String associated_data = resource.getAssociated_data(); + public WxPayScoreResult parseNotifyData(PayScoreNotifyData data) throws WxPayException { + PayScoreNotifyData.Resource resource = data.getResource(); + String cipherText = resource.getCipherText(); + String associatedData = resource.getAssociatedData(); String nonce = resource.getNonce(); - String apiv3Key = this.payService.getConfig().getApiv3Key(); + String apiV3Key = this.payService.getConfig().getApiV3Key(); try { - String s = AesUtils.decryptToString(associated_data, nonce, ciphertext, apiv3Key); - WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(s, WxPayScoreResult.class); - return wxPayScoreCreateResult; - } catch (GeneralSecurityException e) { - throw new WxPayException("解析报文异常",e); - } catch (IOException e) { - throw new WxPayException("解析报文异常",e); + String s = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key); + return GSON.fromJson(s, WxPayScoreResult.class); + } catch (GeneralSecurityException | IOException e) { + throw new WxPayException("解析报文异常!", e); } - } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java index 065a3fc08..af74f06e3 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java @@ -1,18 +1,11 @@ package com.github.binarywang.wxpay.service.impl; -import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.nio.charset.StandardCharsets; import javax.net.ssl.SSLContext; import com.alibaba.fastjson.JSONObject; import com.github.binarywang.wxpay.bean.WxPayApiData; -import com.github.binarywang.wxpay.bean.request.WxPayQueryCommentRequest; -import com.github.binarywang.wxpay.bean.request.WxPayRedpackQueryRequest; -import com.github.binarywang.wxpay.bean.result.WxPayCommonResult; -import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult; -import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.exception.WxPayException; import jodd.util.Base64; import org.apache.commons.lang3.StringUtils; @@ -35,10 +28,6 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; -import com.github.binarywang.wxpay.bean.WxPayApiData; -import com.github.binarywang.wxpay.exception.WxPayException; -import jodd.util.Base64; - /** *
  * 微信支付请求实现类,apache httpclient实现.
@@ -158,7 +147,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
   }
 
   private CloseableHttpClient createApiV3HttpClient() throws WxPayException {
-    CloseableHttpClient apiv3HttpClient = this.getConfig().getApiv3HttpClient();
+    CloseableHttpClient apiv3HttpClient = this.getConfig().getApiV3HttpClient();
     if (null==apiv3HttpClient){
       return this.getConfig().initApiV3HttpClient();
     }
diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImplTest.java
new file mode 100644
index 000000000..fa0c7b944
--- /dev/null
+++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PayScoreServiceImplTest.java
@@ -0,0 +1,46 @@
+package com.github.binarywang.wxpay.service.impl;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+/**
+ * 测试代码,待补充完善.
+ *
+ * @author Binary Wang
+ * @date 2020-05-19
+ */
+public class PayScoreServiceImplTest {
+
+    @Test
+    public void testCreateServiceOrder() {
+    }
+
+    @Test
+    public void testQueryServiceOrder() {
+    }
+
+    @Test
+    public void testCancelServiceOrder() {
+    }
+
+    @Test
+    public void testModifyServiceOrder() {
+    }
+
+    @Test
+    public void testCompleteServiceOrder() {
+    }
+
+    @Test
+    public void testPayServiceOrder() {
+    }
+
+    @Test
+    public void testSyncServiceOrder() {
+    }
+
+    @Test
+    public void testDecryptNotifyData() {
+    }
+}