mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	引入配置方法,使得微信支付能够支持仿真测试环境 #119
This commit is contained in:
		| @ -144,4 +144,8 @@ public interface WxMpConfigStorage { | ||||
|    */ | ||||
|   boolean autoRefreshToken(); | ||||
|  | ||||
|   /** | ||||
|    * 微信支付是否使用仿真测试环境 | ||||
|    */ | ||||
|   boolean useSandboxForWxPay(); | ||||
| } | ||||
|  | ||||
| @ -344,4 +344,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | ||||
|   public boolean autoRefreshToken() { | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean useSandboxForWxPay() { | ||||
|     return false; | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -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(); | ||||
|  | ||||
| @ -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 { | ||||
|   /** | ||||
|    * 返回状态码 | ||||
|    */ | ||||
|  | ||||
| @ -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){ | ||||
|     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.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); | ||||
|   } | ||||
|  | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang