From 06c8ae81d24e352de9ca58a76287d7c7fb658617 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sat, 23 May 2020 21:42:37 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20#1532=20=E5=BE=AE=E4=BF=A1=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E6=A8=A1=E5=9D=97=E5=A2=9E=E5=8A=A0=E6=B1=87=E7=8E=87?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wxpay/bean/request/BaseWxPayRequest.java | 9 ++- .../WxPayQueryExchangeRateRequest.java | 61 +++++++++++++++++++ .../result/WxPayQueryExchangeRateResult.java | 58 ++++++++++++++++++ .../wxpay/service/WxPayService.java | 23 +++++-- .../service/impl/BaseWxPayServiceImpl.java | 14 +++++ .../impl/BaseWxPayServiceImplTest.java | 15 ++++- 6 files changed, 172 insertions(+), 8 deletions(-) create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayQueryExchangeRateRequest.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayQueryExchangeRateResult.java diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/BaseWxPayRequest.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/BaseWxPayRequest.java index 4619e52d4f..f19935e7e1 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/BaseWxPayRequest.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/BaseWxPayRequest.java @@ -170,6 +170,13 @@ private void checkFields() throws WxPayException { */ protected abstract void checkConstraints() throws WxPayException; + /** + * 是否需要nonce_str + */ + protected boolean needNonceStr() { + return true; + } + /** * 如果配置中已经设置,可以不设置值. * @@ -363,7 +370,7 @@ public void checkAndSign(WxPayConfig config) throws WxPayException { } } - if (StringUtils.isBlank(getNonceStr())) { + if (needNonceStr() && StringUtils.isBlank(getNonceStr())) { this.setNonceStr(String.valueOf(System.currentTimeMillis())); } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayQueryExchangeRateRequest.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayQueryExchangeRateRequest.java new file mode 100644 index 0000000000..c4b453b9bd --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayQueryExchangeRateRequest.java @@ -0,0 +1,61 @@ +package com.github.binarywang.wxpay.bean.request; + +import com.github.binarywang.wxpay.exception.WxPayException; +import com.thoughtworks.xstream.annotations.XStreamAlias; +import lombok.*; +import lombok.experimental.Accessors; + +import java.util.Map; + +/** + * 查询汇率请求. + * + * @author Binary Wang + * @date 2020-05-23 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@Builder +@Accessors(chain = true) +@NoArgsConstructor +@AllArgsConstructor +@XStreamAlias("xml") +public class WxPayQueryExchangeRateRequest extends BaseWxPayRequest { + private static final long serialVersionUID = -8796516942563060554L; + /** + * 币种 + * fee_type + * 是 + * String(10) + * USD + * 外币币种 + */ + @XStreamAlias("fee_type") + private String feeType; + + /** + * 日期 + * date + * 是 + * String(14) + * 20150807 + * 格式为yyyyMMdd,如2009年12月25日表示为20091225。时区为GMT+8 beijing + */ + @XStreamAlias("date") + private String date; + + @Override + protected void checkConstraints() throws WxPayException { + + } + + @Override + protected void storeMap(Map map) { + + } + + @Override + protected boolean needNonceStr() { + return false; + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayQueryExchangeRateResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayQueryExchangeRateResult.java new file mode 100644 index 0000000000..d44d282e0f --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayQueryExchangeRateResult.java @@ -0,0 +1,58 @@ +package com.github.binarywang.wxpay.bean.result; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.w3c.dom.Document; + +/** + * 汇率查询响应. + * + * @author Binary Wang + * @date 2020-05-23 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +@XStreamAlias("xml") +public class WxPayQueryExchangeRateResult extends BaseWxPayResult { + private static final long serialVersionUID = 2269734222658532364L; + + /** + * 币种 + * fee_type + * 是 + * String(10) + * SUCCESS 外币币种,详细请见参数规定 + */ + @XStreamAlias("fee_type") + private String feeType; + + /** + * 汇率时间 + * rate_time + * 是 + * String(14) + * 20150807131545 + * 格式:yyyyMMddhhmmss + */ + @XStreamAlias("rate_time") + private String rateTime; + + /** + * 现汇卖出价 + * rate + * 是 + * String(15) + * 系统错误 + * 外币标准单位乘以100折算为人民币的金额,保留4位小数(如:100美元按当时汇率折算返回的先汇卖出价是628.2100) + */ + @XStreamAlias("rate") + private String rate; + + @Override + protected void loadXML(Document d) { + + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java index ba9de0ffdf..74958c1f91 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java @@ -65,12 +65,10 @@ public interface WxPayService { */ String postV3(String url, String requestStr) throws WxPayException; - /** * 发送get V3请求,得到响应字符串. * - * @param url 请求地址 - * @param param 请求信息 + * @param url 请求地址 * @return 返回请求结果字符串 string * @throws WxPayException the wx pay exception */ @@ -200,11 +198,11 @@ public interface WxPayService { /** * 调用统一下单接口,并组装生成支付所需参数对象. * - * @see WxPayService#createOrder(WxPayUnifiedOrderRequest) * @param specificTradeType 将使用的交易方式,不能为 null - * @param request 统一下单请求参数,设定的 tradeType 及配置里的 tradeType 将被忽略,转而使用 specificTradeType + * @param request 统一下单请求参数,设定的 tradeType 及配置里的 tradeType 将被忽略,转而使用 specificTradeType * @return 返回 {@link WxPayConstants.TradeType.Specific} 指定的类 * @throws WxPayException the wx pay exception + * @see WxPayService#createOrder(WxPayUnifiedOrderRequest) */ T createOrder(WxPayConstants.TradeType.Specific specificTradeType, WxPayUnifiedOrderRequest request) throws WxPayException; @@ -759,5 +757,18 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri */ WxPayFacepayResult facepay(WxPayFacepayRequest request) throws WxPayException; - + /** + * 查询汇率 + *
+   * 应用场景:商户网站的商品以外币标价时,通过该接口可以实时查询到微信使用的转换汇率。汇率更新时间为北京时间上午10:00,一天更新一次。
+   * 文档地址:https://pay.weixin.qq.com/wiki/doc/api/app/app_jw.php?chapter=9_15&index=12
+   * 接口链接:https://api.mch.weixin.qq.com/pay/queryexchagerate
+   * 
+ * + * @param feeType 外币币种 + * @param date 日期,格式为yyyyMMdd,如2009年12月25日表示为20091225。时区为GMT+8 beijing + * @return . + * @throws WxPayException . + */ + WxPayQueryExchangeRateResult queryExchangeRate(String feeType, String date) throws WxPayException; } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java index 826af6f39a..4d61522f58 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java @@ -831,4 +831,18 @@ public WxPayFacepayResult facepay(WxPayFacepayRequest request) throws WxPayExcep return result; } + @Override + public WxPayQueryExchangeRateResult queryExchangeRate(String feeType, String date) throws WxPayException { + WxPayQueryExchangeRateRequest request = new WxPayQueryExchangeRateRequest(); + request.setFeeType(feeType); + request.setDate(date); + + request.checkAndSign(this.getConfig()); + + String url = this.getPayBaseUrl() + "/pay/queryexchagerate"; + String responseContent = this.post(url, request.toXML(), false); + WxPayQueryExchangeRateResult result = BaseWxPayResult.fromXML(responseContent, WxPayQueryExchangeRateResult.class); + result.checkResult(this, request.getSignType(), true); + return result; + } } diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java index 6350828f6b..9e129bcbeb 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java @@ -626,7 +626,8 @@ public void testGetWxPayFaceAuthInfo() throws WxPayException { } @Test - public void testFacepay() { + public void testFacepay() throws WxPayException { + final WxPayFacepayResult result = this.payService.facepay(WxPayFacepayRequest.newBuilder().build()); } @Test @@ -673,4 +674,16 @@ public void testQueryRedpack() { @Test public void testTestQueryRedpack() { } + + @Test + public void testGetPayScoreService() { + // no need to test + } + + @Test + public void testQueryExchangeRate() throws WxPayException { + final WxPayQueryExchangeRateResult result = this.payService.queryExchangeRate("USD", "20200425"); + assertThat(result).isNotNull(); + System.out.println(result); + } }