Browse Source

引入配置方法,使得微信支付能够支持仿真测试环境 #119

master
Binary Wang 8 years ago
parent
commit
245bec709a
7 changed files with 61 additions and 18 deletions
  1. +4
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java
  2. +5
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java
  3. +25
    -12
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java
  4. +1
    -2
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java
  5. +15
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayCommonResult.java
  6. +5
    -0
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxXmlMpInMemoryConfigStorage.java
  7. +6
    -4
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java

+ 4
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java View File

@@ -144,4 +144,8 @@ public interface WxMpConfigStorage {
*/
boolean autoRefreshToken();

/**
* 微信支付是否使用仿真测试环境
*/
boolean useSandboxForWxPay();
}

+ 5
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java View File

@@ -344,4 +344,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
public boolean autoRefreshToken() {
return true;
}

@Override
public boolean useSandboxForWxPay() {
return false;
}
}

+ 25
- 12
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java View File

@@ -52,6 +52,14 @@ public class WxMpPayServiceImpl implements WxMpPayService {
return wxMpService.getWxMpConfigStorage();
}

private String getPayBaseUrl(){
if(this.getConfig().useSandboxForWxPay()){
return PAY_BASE_URL + "/sandboxnew";
}

return PAY_BASE_URL;
}

@Override
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorException {
this.initRequest(request);
@@ -61,7 +69,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
this.checkParameters(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());
WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent, WxPayRefundResult.class);
this.checkResult(result);
@@ -83,7 +91,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
request.setRefundId(StringUtils.trimToNull(refundId));
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());
WxPayRefundQueryResult result = WxPayRefundQueryResult.fromXML(responseContent, WxPayRefundQueryResult.class);
result.composeRefundRecords();
@@ -150,10 +158,10 @@ public class WxMpPayServiceImpl implements WxMpPayService {
this.initRequest(request);
request.setSign(this.createSign(request));

String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack";
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendredpack";
if (request.getAmtType() != null) {
//裂变红包
url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack";
url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendgroupredpack";
}
String responseContent = this.executeWithKey(url, request.toXML());
WxPaySendRedpackResult result = WxPaySendRedpackResult.fromXML(responseContent, WxPaySendRedpackResult.class);
@@ -169,7 +177,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
initRequest(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());
WxPayRedpackQueryResult result = WxPayRedpackQueryResult.fromXML(responseContent, WxPayRedpackQueryResult.class);
this.checkResult(result);
@@ -189,7 +197,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
initRequest(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());
WxPayOrderQueryResult result = WxPayOrderQueryResult.fromXML(responseContent, WxPayOrderQueryResult.class);
result.composeCoupons();
@@ -208,7 +216,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
initRequest(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());
WxPayOrderCloseResult result = WxPayOrderCloseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
this.checkResult(result);
@@ -230,7 +238,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
checkParameters(request);//校验参数
request.setSign(this.createSign(request));

String url = PAY_BASE_URL + "/pay/unifiedorder";
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
String xmlParam = request.toXML();
log.debug("微信统一下单接口,URL:{},参数:{}", url, xmlParam);

@@ -298,7 +306,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
BeanUtils.checkRequiredFields(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());
WxEntPayResult result = WxEntPayResult.fromXML(responseContent, WxEntPayResult.class);
@@ -312,7 +320,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
this.initRequest(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());
WxEntPayQueryResult result = WxEntPayQueryResult.fromXML(responseContent, WxEntPayQueryResult.class);
this.checkResult(result);
@@ -359,9 +367,9 @@ public class WxMpPayServiceImpl implements WxMpPayService {
this.initRequest(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());
WxPayBaseResult result = WxPayBaseResult.fromXML(responseContent, WxPayBaseResult.class);
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
this.checkResult(result);
}

@@ -435,6 +443,11 @@ public class WxMpPayServiceImpl implements WxMpPayService {

@Override
public String createSign(Map<String, String> params, String signKey) {
if(this.getConfig().useSandboxForWxPay()){
//使用仿真测试环境
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456";
}

SortedMap<String, String> sortedMap = new TreeMap<>(params);

StringBuilder toSign = new StringBuilder();


+ 1
- 2
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java View File

@@ -24,8 +24,7 @@ import java.util.Map;
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
@XStreamAlias("xml")
public class WxPayBaseResult {
public abstract class WxPayBaseResult {
/**
* 返回状态码
*/


+ 15
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayCommonResult.java View File

@@ -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 {
}

+ 5
- 0
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxXmlMpInMemoryConfigStorage.java View File

@@ -64,4 +64,9 @@ public class WxXmlMpInMemoryConfigStorage
public void setAccessTokenLock(Lock lock){
super.accessTokenLock = lock;
}

@Override
public boolean useSandboxForWxPay() {
return true;
}
}

+ 6
- 4
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java View File

@@ -8,13 +8,14 @@ import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.bean.pay.request.*;
import me.chanjar.weixin.mp.bean.pay.result.*;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.nio.file.Files;
import java.nio.file.Path;

import static org.testng.Assert.*;

/**
* 测试支付相关接口
* Created by Binary Wang on 2016/7/28.
@@ -52,6 +53,7 @@ public class WxMpPayServiceImplTest {
WxXmlMpInMemoryConfigStorage config = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage();
config.setSslContextFilePath(config.getKeyPath());
}

/**
* 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());
System.out.println(qrcodeContent);

Assert.assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?"));
Assert.assertTrue(qrcodeContent.contains("product_id=" + productId));
assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?"));
assertTrue(qrcodeContent.contains("product_id=" + productId));
}

@Test
@@ -174,7 +176,7 @@ public class WxMpPayServiceImplTest {
Path qrcodeFilePath = Files.createTempFile("qrcode_", ".jpg");
Files.write(qrcodeFilePath, bytes);

Assert.assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
}




Loading…
Cancel
Save