package com.github.wxpay.sdk;
import com.github.wxpay.sdk.WXPayConstants.SignType;
import java.util.HashMap;
import java.util.Map;
public class WXPay {
private WXPayConfig config;
private SignType signType;
private boolean autoReport;
private boolean useSandbox;
private String notifyUrl;
private WXPayRequest wxPayRequest;
public WXPay(final WXPayConfig config) throws Exception {
this(config, null, true, false);
}
public WXPay(final WXPayConfig config, final boolean autoReport) throws Exception {
this(config, null, autoReport, false);
}
public WXPay(final WXPayConfig config, final boolean autoReport, final boolean useSandbox) throws Exception{
this(config, null, autoReport, useSandbox);
}
public WXPay(final WXPayConfig config, final String notifyUrl) throws Exception {
this(config, notifyUrl, true, false);
}
public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport) throws Exception {
this(config, notifyUrl, autoReport, false);
}
public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception {
this.config = config;
this.notifyUrl = notifyUrl;
this.autoReport = autoReport;
this.useSandbox = useSandbox;
if (useSandbox) {
this.signType = SignType.MD5; // 沙箱环境
}
else {
this.signType = SignType.HMACSHA256;
}
this.wxPayRequest = new WXPayRequest(config);
}
private void checkWXPayConfig() throws Exception {
if (this.config == null) {
throw new Exception("config is null");
}
if (this.config.getAppID() == null || this.config.getAppID().trim().length() == 0) {
throw new Exception("appid in config is empty");
}
if (this.config.getMchID() == null || this.config.getMchID().trim().length() == 0) {
throw new Exception("appid in config is empty");
}
if (this.config.getCertStream() == null) {
throw new Exception("cert stream in config is empty");
}
if (this.config.getWXPayDomain() == null){
throw new Exception("config.getWXPayDomain() is null");
}
if (this.config.getHttpConnectTimeoutMs() < 10) {
throw new Exception("http connect timeout is too small");
}
if (this.config.getHttpReadTimeoutMs() < 10) {
throw new Exception("http read timeout is too small");
}
}
/**
* 向 Map 中添加 appid、mch_id、nonce_str、sign_type、sign <br>
* 该函数适用于商户适用于统一下单等接口,不适用于红包、代金券接口
*
* @param reqData
* @return
* @throws Exception
*/
public Map<String, String> fillRequestData(Map<String, String> reqData) throws Exception {
reqData.put("appid", config.getAppID());
reqData.put("mch_id", config.getMchID());
reqData.put("nonce_str", WXPayUtil.generateNonceStr());
if (SignType.MD5.equals(this.signType)) {
reqData.put("sign_type", WXPayConstants.MD5);
}
else if (SignType.HMACSHA256.equals(this.signType)) {
reqData.put("sign_type", WXPayConstants.HMACSHA256);
}
reqData.put("sign", WXPayUtil.generateSignature(reqData, config.getKey(), this.signType));
return reqData;
}
/**
* 判断xml数据的sign是否有效,必须包含sign字段,否则返回false。
*
* @param reqData 向wxpay post的请求数据
* @return 签名是否有效
* @throws Exception
*/
public boolean isResponseSignatureValid(Map<String, String> reqData) throws Exception {
// 返回数据的签名方式和请求中给定的签名方式是一致的
return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), this.signType);
}
/**
* 判断支付结果通知中的sign是否有效
*
* @param reqData 向wxpay post的请求数据
* @return 签名是否有效
* @throws Exception
*/
public boolean isPayResultNotifySignatureValid(Map<String, String> reqData) throws Exception {
String signTypeInData = reqData.get(WXPayConstants.FIELD_SIGN_TYPE);
SignType signType;
if (signTypeInData == null) {
signType = SignType.MD5;
}
else {
signTypeInData = signTypeInData.trim();
if (signTypeInData.length() == 0) {
signType = SignType.MD5;
}
else if (WXPayConstants.MD5.equals(signTypeInData)) {
signType = SignType.MD5;
}
else if (WXPayConstants.HMACSHA256.equals(signTypeInData)) {
signType = SignType.HMACSHA256;
}
else {
throw new Exception(String.format("Unsupported sign_type: %s", signTypeInData));
}
}
return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), signType);
}
/**
* 不需要证书的请求
* @param urlSuffix String
* @param reqData 向wxpay post的请求数据
* @param connectTimeoutMs 超时时间,单位是毫秒
* @param readTimeoutMs 超时时间,单位是毫秒
* @return API返回数据
* @throws Exception
*/
public String requestWithoutCert(String urlSuffix, Map<String, String> reqData,
int connectTimeoutMs, int readTimeoutMs) throws Exception {
String msgUUID = reqData.get("nonce_str");
String reqBody = WXPayUtil.mapToXml(reqData);
String resp = this.wxPayRequest.requestWithoutCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, autoReport);
return resp;
}
/**
* 需要证书的请求
* @param urlSuffix String
* @param reqData 向wxpay post的请求数据 Map
* @param connectTimeoutMs 超时时间,单位是毫秒
* @param readTimeoutMs 超时时间,单位是毫秒
* @return API返回数据
* @throws Exception
*/
public String requestWithCert(String urlSuffix, Map<String, String> reqData,
int connectTimeoutMs, int readTimeoutMs) throws Exception {
String msgUUID= reqData.get("nonce_str");
String reqBody = WXPayUtil.mapToXml(reqData);
String resp = this.wxPayRequest.requestWithCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, this.autoReport);
return resp;
}
/**
* 处理 HTTPS API返回数据,转换成Map对象。return_code为SUCCESS时,验证签名。
* @param xmlStr API返回的XML格式数据
* @return Map类型数据
* @throws Exception
*/
public Map<String, String> processResponseXml(String xmlStr) throws Exception {
String RETURN_CODE = "return_code";
String return_code;
Map<String, String> respData = WXPayUtil.xmlToMap(xmlStr);
if (respData.containsKey(RETURN_CODE)) {
return_code = respData.get(RETURN_CODE);
}
else {
throw new Exception(String.format("No `return_code` in XML: %s", xmlStr));
}
if (return_code.equals(WXPayConstants.FAIL)) {
return respData;
}
else if (return_code.equals(WXPayConstants.SUCCESS)) {
if (this.isResponseSignatureValid(respData)) {
return respData;
}
else {
throw new Exception(String.format("Invalid sign value in XML: %s", xmlStr));
}
}
else {
throw new Exception(String.format("return_code value %s is invalid in XML: %s", return_code, xmlStr));
}
}
/**
* 作用:提交刷卡支付<br>
* 场景:刷卡支付
* @param reqData 向wxpay post的请求数据
* @
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本源码是基于Java开发的二手奢侈品在线交易平台设计与实现项目,包含1885个文件,包括PNG图片、Java源代码、JPG图片、CSS样式表、JavaScript脚本、JSON文件、XML文件、SVG文件、EOT文件和TTF文件。系统功能完善,界面美观,适合用于二手奢侈品在线交易平台。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的二手奢侈品在线交易平台设计与实现源码 (1885个子文件)
User.class 3KB
TestController.class 2KB
BootThymeleafApplication.class 760B
file.conf 1013B
registry.conf 868B
fdfs_client.conf 126B
all.css 319KB
all.css 319KB
all.css 319KB
all.css 319KB
all.css 319KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
widget-jquery.autocomplete.css 16KB
pages-JD-index.css 9KB
pages-JD-index.css 9KB
pages-JD-index.css 9KB
pages-JD-index.css 9KB
pages-JD-index.css 9KB
pages-seckillOrder.css 9KB
pages-seckillOrder.css 9KB
pages-seckillOrder.css 9KB
pages-seckillOrder.css 9KB
pages-seckillOrder.css 9KB
pages-item.css 9KB
pages-item.css 9KB
pages-item.css 9KB
pages-item.css 9KB
pages-item.css 9KB
widget-cartPanelView.css 8KB
widget-cartPanelView.css 8KB
widget-cartPanelView.css 8KB
widget-cartPanelView.css 8KB
widget-cartPanelView.css 8KB
pages-mycomment.css 7KB
pages-mycomment.css 7KB
pages-mycomment.css 7KB
pages-mycomment.css 7KB
pages-mycomment.css 7KB
pages-seckill-item.css 7KB
pages-seckill-item.css 7KB
pages-seckill-item.css 7KB
pages-seckill-item.css 7KB
pages-seckill-item.css 7KB
pages-myOrder.css 7KB
pages-myOrder.css 7KB
pages-myOrder.css 7KB
pages-myOrder.css 7KB
pages-myOrder.css 7KB
pages-list.css 5KB
pages-list.css 5KB
pages-list.css 5KB
pages-list.css 5KB
pages-list.css 5KB
pages-shop.css 4KB
pages-shop.css 4KB
pages-shop.css 4KB
pages-shop.css 4KB
pages-shop.css 4KB
pages-home.css 4KB
pages-home.css 4KB
pages-home.css 4KB
pages-home.css 4KB
pages-home.css 4KB
pages-cart.css 4KB
pages-cart.css 4KB
pages-cart.css 4KB
pages-cart.css 4KB
pages-cart.css 4KB
pages-getOrderInfo.css 4KB
pages-getOrderInfo.css 4KB
pages-getOrderInfo.css 4KB
pages-getOrderInfo.css 4KB
pages-getOrderInfo.css 4KB
pages-weixinpay.css 4KB
pages-weixinpay.css 4KB
pages-weixinpay.css 4KB
pages-weixinpay.css 4KB
pages-weixinpay.css 4KB
pages-seckill-index.css 3KB
pages-seckill-index.css 3KB
pages-seckill-index.css 3KB
pages-seckill-index.css 3KB
pages-seckill-index.css 3KB
pages-login-manage.css 3KB
pages-login-manage.css 3KB
pages-login-manage.css 3KB
pages-login-manage.css 3KB
pages-login-manage.css 3KB
pages-success-cart.css 3KB
pages-success-cart.css 3KB
pages-success-cart.css 3KB
pages-success-cart.css 3KB
pages-success-cart.css 3KB
pages-pay.css 3KB
pages-pay.css 3KB
pages-pay.css 3KB
pages-pay.css 3KB
共 1885 条
- 1
- 2
- 3
- 4
- 5
- 6
- 19
资源评论
沐知全栈开发
- 粉丝: 5022
- 资源: 5025
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功