mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-25 01:14:36 +08:00
引入配置方法,使得微信支付能够支持仿真测试环境 #119
This commit is contained in:
parent
fa7360d0ca
commit
245bec709a
@ -144,4 +144,8 @@ public interface WxMpConfigStorage {
|
|||||||
*/
|
*/
|
||||||
boolean autoRefreshToken();
|
boolean autoRefreshToken();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信支付是否使用仿真测试环境
|
||||||
|
*/
|
||||||
|
boolean useSandboxForWxPay();
|
||||||
}
|
}
|
||||||
|
@ -344,4 +344,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
|
|||||||
public boolean autoRefreshToken() {
|
public boolean autoRefreshToken() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean useSandboxForWxPay() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,14 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
return wxMpService.getWxMpConfigStorage();
|
return wxMpService.getWxMpConfigStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getPayBaseUrl(){
|
||||||
|
if(this.getConfig().useSandboxForWxPay()){
|
||||||
|
return PAY_BASE_URL + "/sandboxnew";
|
||||||
|
}
|
||||||
|
|
||||||
|
return PAY_BASE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorException {
|
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorException {
|
||||||
this.initRequest(request);
|
this.initRequest(request);
|
||||||
@ -61,7 +69,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
this.checkParameters(request);
|
this.checkParameters(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/secapi/pay/refund";
|
String url = this.getPayBaseUrl() + "/secapi/pay/refund";
|
||||||
String responseContent = this.executeWithKey(url, request.toXML());
|
String responseContent = this.executeWithKey(url, request.toXML());
|
||||||
WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent, WxPayRefundResult.class);
|
WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent, WxPayRefundResult.class);
|
||||||
this.checkResult(result);
|
this.checkResult(result);
|
||||||
@ -83,7 +91,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
request.setRefundId(StringUtils.trimToNull(refundId));
|
request.setRefundId(StringUtils.trimToNull(refundId));
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/pay/refundquery";
|
String url = this.getPayBaseUrl() + "/pay/refundquery";
|
||||||
String responseContent = this.executeRequest(url, request.toXML());
|
String responseContent = this.executeRequest(url, request.toXML());
|
||||||
WxPayRefundQueryResult result = WxPayRefundQueryResult.fromXML(responseContent, WxPayRefundQueryResult.class);
|
WxPayRefundQueryResult result = WxPayRefundQueryResult.fromXML(responseContent, WxPayRefundQueryResult.class);
|
||||||
result.composeRefundRecords();
|
result.composeRefundRecords();
|
||||||
@ -150,10 +158,10 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
this.initRequest(request);
|
this.initRequest(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack";
|
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendredpack";
|
||||||
if (request.getAmtType() != null) {
|
if (request.getAmtType() != null) {
|
||||||
//裂变红包
|
//裂变红包
|
||||||
url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack";
|
url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendgroupredpack";
|
||||||
}
|
}
|
||||||
String responseContent = this.executeWithKey(url, request.toXML());
|
String responseContent = this.executeWithKey(url, request.toXML());
|
||||||
WxPaySendRedpackResult result = WxPaySendRedpackResult.fromXML(responseContent, WxPaySendRedpackResult.class);
|
WxPaySendRedpackResult result = WxPaySendRedpackResult.fromXML(responseContent, WxPaySendRedpackResult.class);
|
||||||
@ -169,7 +177,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
initRequest(request);
|
initRequest(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/mmpaymkttransfers/gethbinfo";
|
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gethbinfo";
|
||||||
String responseContent = this.executeWithKey(url, request.toXML());
|
String responseContent = this.executeWithKey(url, request.toXML());
|
||||||
WxPayRedpackQueryResult result = WxPayRedpackQueryResult.fromXML(responseContent, WxPayRedpackQueryResult.class);
|
WxPayRedpackQueryResult result = WxPayRedpackQueryResult.fromXML(responseContent, WxPayRedpackQueryResult.class);
|
||||||
this.checkResult(result);
|
this.checkResult(result);
|
||||||
@ -189,7 +197,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
initRequest(request);
|
initRequest(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/pay/orderquery";
|
String url = this.getPayBaseUrl() + "/pay/orderquery";
|
||||||
String responseContent = this.executeRequest(url, request.toXML());
|
String responseContent = this.executeRequest(url, request.toXML());
|
||||||
WxPayOrderQueryResult result = WxPayOrderQueryResult.fromXML(responseContent, WxPayOrderQueryResult.class);
|
WxPayOrderQueryResult result = WxPayOrderQueryResult.fromXML(responseContent, WxPayOrderQueryResult.class);
|
||||||
result.composeCoupons();
|
result.composeCoupons();
|
||||||
@ -208,7 +216,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
initRequest(request);
|
initRequest(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/pay/closeorder";
|
String url = this.getPayBaseUrl() + "/pay/closeorder";
|
||||||
String responseContent = this.executeRequest(url, request.toXML());
|
String responseContent = this.executeRequest(url, request.toXML());
|
||||||
WxPayOrderCloseResult result = WxPayOrderCloseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
|
WxPayOrderCloseResult result = WxPayOrderCloseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
|
||||||
this.checkResult(result);
|
this.checkResult(result);
|
||||||
@ -230,7 +238,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
checkParameters(request);//校验参数
|
checkParameters(request);//校验参数
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/pay/unifiedorder";
|
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
|
||||||
String xmlParam = request.toXML();
|
String xmlParam = request.toXML();
|
||||||
log.debug("微信统一下单接口,URL:{},参数:{}", url, xmlParam);
|
log.debug("微信统一下单接口,URL:{},参数:{}", url, xmlParam);
|
||||||
|
|
||||||
@ -298,7 +306,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
BeanUtils.checkRequiredFields(request);
|
BeanUtils.checkRequiredFields(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers";
|
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers";
|
||||||
|
|
||||||
String responseContent = this.executeWithKey(url, request.toXML());
|
String responseContent = this.executeWithKey(url, request.toXML());
|
||||||
WxEntPayResult result = WxEntPayResult.fromXML(responseContent, WxEntPayResult.class);
|
WxEntPayResult result = WxEntPayResult.fromXML(responseContent, WxEntPayResult.class);
|
||||||
@ -312,7 +320,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
this.initRequest(request);
|
this.initRequest(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo";
|
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo";
|
||||||
String responseContent = this.executeWithKey(url, request.toXML());
|
String responseContent = this.executeWithKey(url, request.toXML());
|
||||||
WxEntPayQueryResult result = WxEntPayQueryResult.fromXML(responseContent, WxEntPayQueryResult.class);
|
WxEntPayQueryResult result = WxEntPayQueryResult.fromXML(responseContent, WxEntPayQueryResult.class);
|
||||||
this.checkResult(result);
|
this.checkResult(result);
|
||||||
@ -359,9 +367,9 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
this.initRequest(request);
|
this.initRequest(request);
|
||||||
request.setSign(this.createSign(request));
|
request.setSign(this.createSign(request));
|
||||||
|
|
||||||
String url = PAY_BASE_URL + "/payitil/report";
|
String url = this.getPayBaseUrl() + "/payitil/report";
|
||||||
String responseContent = this.wxMpService.post(url, request.toXML());
|
String responseContent = this.wxMpService.post(url, request.toXML());
|
||||||
WxPayBaseResult result = WxPayBaseResult.fromXML(responseContent, WxPayBaseResult.class);
|
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
|
||||||
this.checkResult(result);
|
this.checkResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,6 +443,11 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String createSign(Map<String, String> params, String signKey) {
|
public String createSign(Map<String, String> params, String signKey) {
|
||||||
|
if(this.getConfig().useSandboxForWxPay()){
|
||||||
|
//使用仿真测试环境
|
||||||
|
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456";
|
||||||
|
}
|
||||||
|
|
||||||
SortedMap<String, String> sortedMap = new TreeMap<>(params);
|
SortedMap<String, String> sortedMap = new TreeMap<>(params);
|
||||||
|
|
||||||
StringBuilder toSign = new StringBuilder();
|
StringBuilder toSign = new StringBuilder();
|
||||||
|
@ -24,8 +24,7 @@ import java.util.Map;
|
|||||||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
@XStreamAlias("xml")
|
public abstract class WxPayBaseResult {
|
||||||
public class WxPayBaseResult {
|
|
||||||
/**
|
/**
|
||||||
* 返回状态码
|
* 返回状态码
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package me.chanjar.weixin.mp.bean.pay.result;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 微信支付结果仅包含有return 和result等相关信息的的属性类
|
||||||
|
* Created by Binary Wang on 2017-01-09.
|
||||||
|
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
|
||||||
|
@XStreamAlias("xml")
|
||||||
|
public class WxPayCommonResult extends WxPayBaseResult {
|
||||||
|
}
|
@ -64,4 +64,9 @@ public class WxXmlMpInMemoryConfigStorage
|
|||||||
public void setAccessTokenLock(Lock lock){
|
public void setAccessTokenLock(Lock lock){
|
||||||
super.accessTokenLock = lock;
|
super.accessTokenLock = lock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean useSandboxForWxPay() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,14 @@ import me.chanjar.weixin.mp.api.WxMpService;
|
|||||||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
|
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
|
||||||
import me.chanjar.weixin.mp.bean.pay.request.*;
|
import me.chanjar.weixin.mp.bean.pay.request.*;
|
||||||
import me.chanjar.weixin.mp.bean.pay.result.*;
|
import me.chanjar.weixin.mp.bean.pay.result.*;
|
||||||
import org.testng.Assert;
|
|
||||||
import org.testng.annotations.Guice;
|
import org.testng.annotations.Guice;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import static org.testng.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试支付相关接口
|
* 测试支付相关接口
|
||||||
* Created by Binary Wang on 2016/7/28.
|
* Created by Binary Wang on 2016/7/28.
|
||||||
@ -52,6 +53,7 @@ public class WxMpPayServiceImplTest {
|
|||||||
WxXmlMpInMemoryConfigStorage config = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage();
|
WxXmlMpInMemoryConfigStorage config = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage();
|
||||||
config.setSslContextFilePath(config.getKeyPath());
|
config.setSslContextFilePath(config.getKeyPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#refund(WxPayRefundRequest)} .
|
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#refund(WxPayRefundRequest)} .
|
||||||
*/
|
*/
|
||||||
@ -163,8 +165,8 @@ public class WxMpPayServiceImplTest {
|
|||||||
String qrcodeContent = QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile());
|
String qrcodeContent = QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile());
|
||||||
System.out.println(qrcodeContent);
|
System.out.println(qrcodeContent);
|
||||||
|
|
||||||
Assert.assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?"));
|
assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?"));
|
||||||
Assert.assertTrue(qrcodeContent.contains("product_id=" + productId));
|
assertTrue(qrcodeContent.contains("product_id=" + productId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -174,7 +176,7 @@ public class WxMpPayServiceImplTest {
|
|||||||
Path qrcodeFilePath = Files.createTempFile("qrcode_", ".jpg");
|
Path qrcodeFilePath = Files.createTempFile("qrcode_", ".jpg");
|
||||||
Files.write(qrcodeFilePath, bytes);
|
Files.write(qrcodeFilePath, bytes);
|
||||||
|
|
||||||
Assert.assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
|
assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user