Skip to content

Commit 06c8ae8

Browse files
committed
🆕 #1532 微信支付模块增加汇率查询的接口
1 parent 4eb1d1d commit 06c8ae8

File tree

6 files changed

+172
-8
lines changed

6 files changed

+172
-8
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/BaseWxPayRequest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ private void checkFields() throws WxPayException {
170170
*/
171171
protected abstract void checkConstraints() throws WxPayException;
172172

173+
/**
174+
* 是否需要nonce_str
175+
*/
176+
protected boolean needNonceStr() {
177+
return true;
178+
}
179+
173180
/**
174181
* 如果配置中已经设置,可以不设置值.
175182
*
@@ -363,7 +370,7 @@ public void checkAndSign(WxPayConfig config) throws WxPayException {
363370
}
364371
}
365372

366-
if (StringUtils.isBlank(getNonceStr())) {
373+
if (needNonceStr() && StringUtils.isBlank(getNonceStr())) {
367374
this.setNonceStr(String.valueOf(System.currentTimeMillis()));
368375
}
369376

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.binarywang.wxpay.bean.request;
2+
3+
import com.github.binarywang.wxpay.exception.WxPayException;
4+
import com.thoughtworks.xstream.annotations.XStreamAlias;
5+
import lombok.*;
6+
import lombok.experimental.Accessors;
7+
8+
import java.util.Map;
9+
10+
/**
11+
* 查询汇率请求.
12+
*
13+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
14+
* @date 2020-05-23
15+
*/
16+
@Data
17+
@EqualsAndHashCode(callSuper = true)
18+
@Builder
19+
@Accessors(chain = true)
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
@XStreamAlias("xml")
23+
public class WxPayQueryExchangeRateRequest extends BaseWxPayRequest {
24+
private static final long serialVersionUID = -8796516942563060554L;
25+
/**
26+
* 币种
27+
* fee_type
28+
* 是
29+
* String(10)
30+
* USD
31+
* 外币币种
32+
*/
33+
@XStreamAlias("fee_type")
34+
private String feeType;
35+
36+
/**
37+
* 日期
38+
* date
39+
* 是
40+
* String(14)
41+
* 20150807
42+
* 格式为yyyyMMdd,如2009年12月25日表示为20091225。时区为GMT+8 beijing
43+
*/
44+
@XStreamAlias("date")
45+
private String date;
46+
47+
@Override
48+
protected void checkConstraints() throws WxPayException {
49+
50+
}
51+
52+
@Override
53+
protected void storeMap(Map<String, String> map) {
54+
55+
}
56+
57+
@Override
58+
protected boolean needNonceStr() {
59+
return false;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.github.binarywang.wxpay.bean.result;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
import lombok.NoArgsConstructor;
7+
import org.w3c.dom.Document;
8+
9+
/**
10+
* 汇率查询响应.
11+
*
12+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
13+
* @date 2020-05-23
14+
*/
15+
@Data
16+
@EqualsAndHashCode(callSuper = true)
17+
@NoArgsConstructor
18+
@XStreamAlias("xml")
19+
public class WxPayQueryExchangeRateResult extends BaseWxPayResult {
20+
private static final long serialVersionUID = 2269734222658532364L;
21+
22+
/**
23+
* 币种
24+
* fee_type
25+
* 是
26+
* String(10)
27+
* SUCCESS 外币币种,详细请见参数规定
28+
*/
29+
@XStreamAlias("fee_type")
30+
private String feeType;
31+
32+
/**
33+
* 汇率时间
34+
* rate_time
35+
* 是
36+
* String(14)
37+
* 20150807131545
38+
* 格式:yyyyMMddhhmmss
39+
*/
40+
@XStreamAlias("rate_time")
41+
private String rateTime;
42+
43+
/**
44+
* 现汇卖出价
45+
* rate
46+
* 是
47+
* String(15)
48+
* 系统错误
49+
* 外币标准单位乘以100折算为人民币的金额,保留4位小数(如:100美元按当时汇率折算返回的先汇卖出价是628.2100)
50+
*/
51+
@XStreamAlias("rate")
52+
private String rate;
53+
54+
@Override
55+
protected void loadXML(Document d) {
56+
57+
}
58+
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,10 @@ public interface WxPayService {
6565
*/
6666
String postV3(String url, String requestStr) throws WxPayException;
6767

68-
6968
/**
7069
* 发送get V3请求,得到响应字符串.
7170
*
72-
* @param url 请求地址
73-
* @param param 请求信息
71+
* @param url 请求地址
7472
* @return 返回请求结果字符串 string
7573
* @throws WxPayException the wx pay exception
7674
*/
@@ -200,11 +198,11 @@ public interface WxPayService {
200198
/**
201199
* 调用统一下单接口,并组装生成支付所需参数对象.
202200
*
203-
* @see WxPayService#createOrder(WxPayUnifiedOrderRequest)
204201
* @param specificTradeType 将使用的交易方式,不能为 null
205-
* @param request 统一下单请求参数,设定的 tradeType 及配置里的 tradeType 将被忽略,转而使用 specificTradeType
202+
* @param request 统一下单请求参数,设定的 tradeType 及配置里的 tradeType 将被忽略,转而使用 specificTradeType
206203
* @return 返回 {@link WxPayConstants.TradeType.Specific} 指定的类
207204
* @throws WxPayException the wx pay exception
205+
* @see WxPayService#createOrder(WxPayUnifiedOrderRequest)
208206
*/
209207
<T> T createOrder(WxPayConstants.TradeType.Specific<T> specificTradeType, WxPayUnifiedOrderRequest request) throws WxPayException;
210208

@@ -759,5 +757,18 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
759757
*/
760758
WxPayFacepayResult facepay(WxPayFacepayRequest request) throws WxPayException;
761759

762-
760+
/**
761+
* 查询汇率
762+
* <pre>
763+
* 应用场景:商户网站的商品以外币标价时,通过该接口可以实时查询到微信使用的转换汇率。汇率更新时间为北京时间上午10:00,一天更新一次。
764+
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/app/app_jw.php?chapter=9_15&index=12
765+
* 接口链接:https://api.mch.weixin.qq.com/pay/queryexchagerate
766+
* </pre>
767+
*
768+
* @param feeType 外币币种
769+
* @param date 日期,格式为yyyyMMdd,如2009年12月25日表示为20091225。时区为GMT+8 beijing
770+
* @return .
771+
* @throws WxPayException .
772+
*/
773+
WxPayQueryExchangeRateResult queryExchangeRate(String feeType, String date) throws WxPayException;
763774
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

+14
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,18 @@ public WxPayFacepayResult facepay(WxPayFacepayRequest request) throws WxPayExcep
831831
return result;
832832
}
833833

834+
@Override
835+
public WxPayQueryExchangeRateResult queryExchangeRate(String feeType, String date) throws WxPayException {
836+
WxPayQueryExchangeRateRequest request = new WxPayQueryExchangeRateRequest();
837+
request.setFeeType(feeType);
838+
request.setDate(date);
839+
840+
request.checkAndSign(this.getConfig());
841+
842+
String url = this.getPayBaseUrl() + "/pay/queryexchagerate";
843+
String responseContent = this.post(url, request.toXML(), false);
844+
WxPayQueryExchangeRateResult result = BaseWxPayResult.fromXML(responseContent, WxPayQueryExchangeRateResult.class);
845+
result.checkResult(this, request.getSignType(), true);
846+
return result;
847+
}
834848
}

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,8 @@ public void testGetWxPayFaceAuthInfo() throws WxPayException {
626626
}
627627

628628
@Test
629-
public void testFacepay() {
629+
public void testFacepay() throws WxPayException {
630+
final WxPayFacepayResult result = this.payService.facepay(WxPayFacepayRequest.newBuilder().build());
630631
}
631632

632633
@Test
@@ -673,4 +674,16 @@ public void testQueryRedpack() {
673674
@Test
674675
public void testTestQueryRedpack() {
675676
}
677+
678+
@Test
679+
public void testGetPayScoreService() {
680+
// no need to test
681+
}
682+
683+
@Test
684+
public void testQueryExchangeRate() throws WxPayException {
685+
final WxPayQueryExchangeRateResult result = this.payService.queryExchangeRate("USD", "20200425");
686+
assertThat(result).isNotNull();
687+
System.out.println(result);
688+
}
676689
}

0 commit comments

Comments
 (0)