|
@@ -44,173 +44,12 @@ public class WxMpPayServiceImpl implements WxMpPayService { |
|
|
private static final List<String> TRADE_TYPES = Lists.newArrayList("JSAPI", |
|
|
private static final List<String> TRADE_TYPES = Lists.newArrayList("JSAPI", |
|
|
"NATIVE", "APP"); |
|
|
"NATIVE", "APP"); |
|
|
private final Logger log = LoggerFactory.getLogger(WxMpPayServiceImpl.class); |
|
|
private final Logger log = LoggerFactory.getLogger(WxMpPayServiceImpl.class); |
|
|
private final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid", |
|
|
|
|
|
"mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip", |
|
|
|
|
|
"notify_url", "trade_type" }; |
|
|
|
|
|
private WxMpService wxMpService; |
|
|
private WxMpService wxMpService; |
|
|
|
|
|
|
|
|
public WxMpPayServiceImpl(WxMpService wxMpService) { |
|
|
public WxMpPayServiceImpl(WxMpService wxMpService) { |
|
|
this.wxMpService = wxMpService; |
|
|
this.wxMpService = wxMpService; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Deprecated |
|
|
|
|
|
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, |
|
|
|
|
|
double amt, String body, String tradeType, String ip, |
|
|
|
|
|
String callbackUrl) throws WxErrorException { |
|
|
|
|
|
Map<String, String> packageParams = new HashMap<>(); |
|
|
|
|
|
packageParams.put("appid", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getAppId()); |
|
|
|
|
|
packageParams.put("mch_id", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerId()); |
|
|
|
|
|
packageParams.put("body", body); |
|
|
|
|
|
packageParams.put("out_trade_no", outTradeNo); |
|
|
|
|
|
packageParams.put("total_fee", (int) (amt * 100) + ""); |
|
|
|
|
|
packageParams.put("spbill_create_ip", ip); |
|
|
|
|
|
packageParams.put("notify_url", callbackUrl); |
|
|
|
|
|
packageParams.put("trade_type", tradeType); |
|
|
|
|
|
packageParams.put("openid", openId); |
|
|
|
|
|
|
|
|
|
|
|
return getPrepayId(packageParams); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Deprecated |
|
|
|
|
|
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) |
|
|
|
|
|
throws WxErrorException { |
|
|
|
|
|
final SortedMap<String, String> packageParams = new TreeMap<>(parameters); |
|
|
|
|
|
packageParams.put("appid", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getAppId()); |
|
|
|
|
|
packageParams.put("mch_id", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerId()); |
|
|
|
|
|
packageParams.put("nonce_str", System.currentTimeMillis() + ""); |
|
|
|
|
|
checkParameters(packageParams); |
|
|
|
|
|
|
|
|
|
|
|
String sign = this.createSign(packageParams, |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerKey()); |
|
|
|
|
|
packageParams.put("sign", sign); |
|
|
|
|
|
|
|
|
|
|
|
StringBuilder request = new StringBuilder("<xml>"); |
|
|
|
|
|
for (Map.Entry<String, String> para : packageParams.entrySet()) { |
|
|
|
|
|
request.append(String.format("<%s>%s</%s>", para.getKey(), |
|
|
|
|
|
para.getValue(), para.getKey())); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
request.append("</xml>"); |
|
|
|
|
|
|
|
|
|
|
|
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; |
|
|
|
|
|
String responseContent = this.wxMpService.post(url, request.toString()); |
|
|
|
|
|
XStream xstream = XStreamInitializer.getInstance(); |
|
|
|
|
|
xstream.alias("xml", WxMpPrepayIdResult.class); |
|
|
|
|
|
return (WxMpPrepayIdResult) xstream.fromXML(responseContent); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void checkParameters(Map<String, String> parameters) { |
|
|
|
|
|
for (String para : this.REQUIRED_ORDER_PARAMETERS) { |
|
|
|
|
|
if (!parameters.containsKey(para)) { |
|
|
|
|
|
throw new IllegalArgumentException( |
|
|
|
|
|
"Reqiured argument '" + para + "' is missing."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ("JSAPI".equals(parameters.get("trade_type")) |
|
|
|
|
|
&& !parameters.containsKey("openid")) { |
|
|
|
|
|
throw new IllegalArgumentException( |
|
|
|
|
|
"Reqiured argument 'openid' is missing when trade_type is 'JSAPI'."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ("NATIVE".equals(parameters.get("trade_type")) |
|
|
|
|
|
&& !parameters.containsKey("product_id")) { |
|
|
|
|
|
throw new IllegalArgumentException( |
|
|
|
|
|
"Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Deprecated |
|
|
|
|
|
public Map<String, String> getJsapiPayInfo(String openId, String outTradeNo, |
|
|
|
|
|
double amt, String body, String ip, String callbackUrl) |
|
|
|
|
|
throws WxErrorException { |
|
|
|
|
|
Map<String, String> packageParams = new HashMap<>(); |
|
|
|
|
|
packageParams.put("appid", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getAppId()); |
|
|
|
|
|
packageParams.put("mch_id", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerId()); |
|
|
|
|
|
packageParams.put("body", body); |
|
|
|
|
|
packageParams.put("out_trade_no", outTradeNo); |
|
|
|
|
|
packageParams.put("total_fee", (int) (amt * 100) + ""); |
|
|
|
|
|
packageParams.put("spbill_create_ip", ip); |
|
|
|
|
|
packageParams.put("notify_url", callbackUrl); |
|
|
|
|
|
packageParams.put("trade_type", "JSAPI"); |
|
|
|
|
|
packageParams.put("openid", openId); |
|
|
|
|
|
|
|
|
|
|
|
return getPayInfo(packageParams); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Deprecated |
|
|
|
|
|
public Map<String, String> getNativePayInfo(String productId, |
|
|
|
|
|
String outTradeNo, double amt, String body, String ip, String callbackUrl) |
|
|
|
|
|
throws WxErrorException { |
|
|
|
|
|
Map<String, String> packageParams = new HashMap<>(); |
|
|
|
|
|
packageParams.put("appid", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getAppId()); |
|
|
|
|
|
packageParams.put("mch_id", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerId()); |
|
|
|
|
|
packageParams.put("body", body); |
|
|
|
|
|
packageParams.put("out_trade_no", outTradeNo); |
|
|
|
|
|
packageParams.put("total_fee", (int) (amt * 100) + ""); |
|
|
|
|
|
packageParams.put("spbill_create_ip", ip); |
|
|
|
|
|
packageParams.put("notify_url", callbackUrl); |
|
|
|
|
|
packageParams.put("trade_type", "NATIVE"); |
|
|
|
|
|
packageParams.put("product_id", productId); |
|
|
|
|
|
|
|
|
|
|
|
return getPayInfo(packageParams); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Deprecated |
|
|
|
|
|
public Map<String, String> getPayInfo(Map<String, String> parameters) |
|
|
|
|
|
throws WxErrorException { |
|
|
|
|
|
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters); |
|
|
|
|
|
|
|
|
|
|
|
if (!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getReturn_code()) |
|
|
|
|
|
|| !"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getResult_code())) { |
|
|
|
|
|
WxError error = new WxError(); |
|
|
|
|
|
error.setErrorCode(-1); |
|
|
|
|
|
error.setErrorMsg("return_code:" + wxMpPrepayIdResult.getReturn_code() |
|
|
|
|
|
+ ";return_msg:" + wxMpPrepayIdResult.getReturn_msg() |
|
|
|
|
|
+ ";result_code:" + wxMpPrepayIdResult.getResult_code() + ";err_code" |
|
|
|
|
|
+ wxMpPrepayIdResult.getErr_code() + ";err_code_des" |
|
|
|
|
|
+ wxMpPrepayIdResult.getErr_code_des()); |
|
|
|
|
|
throw new WxErrorException(error); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String prepayId = wxMpPrepayIdResult.getPrepay_id(); |
|
|
|
|
|
if (prepayId == null || prepayId.equals("")) { |
|
|
|
|
|
throw new RuntimeException( |
|
|
|
|
|
String.format("Failed to get prepay id due to error code '%s'(%s).", |
|
|
|
|
|
wxMpPrepayIdResult.getErr_code(), |
|
|
|
|
|
wxMpPrepayIdResult.getErr_code_des())); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> payInfo = new HashMap<>(); |
|
|
|
|
|
payInfo.put("appId", this.wxMpService.getWxMpConfigStorage().getAppId()); |
|
|
|
|
|
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符 |
|
|
|
|
|
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000)); |
|
|
|
|
|
payInfo.put("nonceStr", System.currentTimeMillis() + ""); |
|
|
|
|
|
payInfo.put("package", "prepay_id=" + prepayId); |
|
|
|
|
|
payInfo.put("signType", "MD5"); |
|
|
|
|
|
if ("NATIVE".equals(parameters.get("trade_type"))) { |
|
|
|
|
|
payInfo.put("codeUrl", wxMpPrepayIdResult.getCode_url()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String finalSign = this.createSign(payInfo, |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerKey()); |
|
|
|
|
|
payInfo.put("paySign", finalSign); |
|
|
|
|
|
return payInfo; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public WxMpPayResult getJSSDKPayResult(String transactionId, |
|
|
public WxMpPayResult getJSSDKPayResult(String transactionId, |
|
|
String outTradeNo) throws WxErrorException { |
|
|
String outTradeNo) throws WxErrorException { |
|
@@ -313,37 +152,6 @@ public class WxMpPayServiceImpl implements WxMpPayService { |
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerKey())); |
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerKey())); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Deprecated |
|
|
|
|
|
public WxRedpackResult sendRedpack(Map<String, String> parameters) |
|
|
|
|
|
throws WxErrorException { |
|
|
|
|
|
SortedMap<String, String> packageParams = new TreeMap<>(parameters); |
|
|
|
|
|
packageParams.put("wxappid", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getAppId()); |
|
|
|
|
|
packageParams.put("mch_id", |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerId()); |
|
|
|
|
|
packageParams.put("nonce_str", System.currentTimeMillis() + ""); |
|
|
|
|
|
|
|
|
|
|
|
String sign = this.createSign(packageParams, |
|
|
|
|
|
this.wxMpService.getWxMpConfigStorage().getPartnerKey()); |
|
|
|
|
|
packageParams.put("sign", sign); |
|
|
|
|
|
|
|
|
|
|
|
StringBuilder request = new StringBuilder("<xml>"); |
|
|
|
|
|
for (Map.Entry<String, String> para : packageParams.entrySet()) { |
|
|
|
|
|
request.append(String.format("<%s>%s</%s>", para.getKey(), |
|
|
|
|
|
para.getValue(), para.getKey())); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
request.append("</xml>"); |
|
|
|
|
|
|
|
|
|
|
|
String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; |
|
|
|
|
|
|
|
|
|
|
|
String responseContent = this.wxMpService.post(url, request.toString()); |
|
|
|
|
|
XStream xstream = XStreamInitializer.getInstance(); |
|
|
|
|
|
xstream.processAnnotations(WxRedpackResult.class); |
|
|
|
|
|
return (WxRedpackResult) xstream.fromXML(responseContent); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public WxRedpackResult sendRedpack(WxSendRedpackRequest request) |
|
|
public WxRedpackResult sendRedpack(WxSendRedpackRequest request) |
|
|
throws WxErrorException { |
|
|
throws WxErrorException { |
|
|