JAVA+微信支付APP开发+支付宝支付APP开发
微信支付
网址:https://pay.weixin.qq.com/guide/index.shtml
DEMO下载:https://pay.weixin.qq.com/wiki/doc/api/download/WxPayAPI_JAVA_v3.zip
微信支付 Java SDK
——
对[微信支付开发者文档](https://pay.weixin.qq.com/wiki/doc/api/index.html)中给出的API进行了封装。
com.github.wxpay.sdk.WXPay类下提供了对应的方法:
|方法名 | 说明 |
|——–|——–|
|microPay| 刷卡支付 |
|unifiedOrder | 统一下单|
|orderQuery | 查询订单 |
|reverse | 撤销订单 |
|closeOrder|关闭订单|
|refund|申请退款|
|refundQuery|查询退款|
|downloadBill|下载对账单|
|report|交易保障|
|shortUrl|转换短链接|
|authCodeToOpenid|授权码查询openid|
* 参数为`Map<String, String>`对象,返回类型也是`Map<String, String>`。
* 方法内部会将参数会转换成含有`appid`、`mch_id`、`nonce_str`、`sign\_type`和`sign`的XML;
* 默认使用MD5进行签名;
* 通过HTTPS请求得到返回数据后会对其做必要的处理(例如验证签名,签名错误则抛出异常)。
* 对于downloadBill,无论是否成功都返回Map,且都含有`return_code`和`return_msg`。若成功,其中`return_code`为`SUCCESS`,另外`data`对应对账单数据。
## 安装
maven:
“`
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
“`
## 示例
配置类MyConfig:
“`java
import com.github.wxpay.sdk.WXPayConfig;
import java.io.*;
public class MyConfig implements WXPayConfig{
private byte[] certData;
public MyConfig() throws Exception {
String certPath = “/path/to/apiclient_cert.p12”;
File file = new File(certPath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
public String getAppID() {
return “wx8888888888888888”;
}
public String getMchID() {
return “12888888”;
}
public String getKey() {
return “88888888888888888888888888888888”;
}
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
public int getHttpConnectTimeoutMs() {
return 8000;
}
public int getHttpReadTimeoutMs() {
return 10000;
}
}
“`
统一下单:
“`java
import com.github.wxpay.sdk.WXPay;
import java.util.HashMap;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put(“body”, “腾讯充值中心-QQ会员充值“);
data.put(“out_trade_no”, “2016090910595900000012”);
data.put(“device_info”, “”);
data.put(“fee_type”, “CNY”);
data.put(“total_fee”, “1”);
data.put(“spbill_create_ip”, “123.12.12.123”);
data.put(“notify_url”, “http://www.example.com/wxpay/notify”);
data.put(“trade_type”, “NATIVE”); // 此处指定为扫码支付
data.put(“product_id”, “12”);
try {
Map<String, String> resp = wxpay.unifiedOrder(data);
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
“`
订单查询:
“`java
import com.github.wxpay.sdk.WXPay;
import java.util.HashMap;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put(“out_trade_no”, “2016090910595900000012”);
try {
Map<String, String> resp = wxpay.orderQuery(data);
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
“`
退款查询:
“`java
import com.github.wxpay.sdk.WXPay;
import java.util.HashMap;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put(“out_trade_no”, “2016090910595900000012”);
try {
Map<String, String> resp = wxpay.refundQuery(data);
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
“`
下载对账单:
“`java
import com.github.wxpay.sdk.WXPay;
import java.util.HashMap;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put(“bill_date”, “20140603”);
data.put(“bill_type”, “ALL”);
try {
Map<String, String> resp = wxpay.downloadBill(data);
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
“`
其他API的使用和上面类似。
暂时不支持下载压缩格式的对账单,但可以使用该SDK生成请求用的XML数据:
“`java
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import java.util.HashMap;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put(“bill_date”, “20140603”);
data.put(“bill_type”, “ALL”);
data.put(“tar_type”, “GZIP”);
try {
data = wxpay.fillRequestData(data);
System.out.println(WXPayUtil.mapToXml(data));
} catch (Exception e) {
e.printStackTrace();
}
}
}
“`
收到支付结果通知时,需要验证签名,可以这样做:
“`java
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
String notifyData = “….”; // 支付结果通知的xml格式数据
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> notifyMap = WXPayUtil.xmlToMap(notifyData); // 转换成map
if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {
// 签名正确
// 进行处理。
// 注意特殊情况:订单已经退款,但收到了支付结果成功的通知,不应把商户侧订单状态从退款改成支付成功
}
else {
// 签名错误,如果数据里没有sign字段,也认为是签名错误
}
}
}
“`
HTTPS请求默认使用MD5算法签名,若需要使用HMAC-SHA256:
“`
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config, WXPayConstants.SignType.HMACSHA256);
// ……
}
}
“`
若需要使用sandbox环境:
“`
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5, true);
// ……
}
}
“`
## License
BSD
支付宝支付
网站:https://open.alipay.com/developmentAccess/developmentAccess.htm
系统交互流程:
Maven:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.0.52.ALL</version>
</dependency>
JAVA服务端SDK生成APP支付订单信息示例
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(“https://openapi.alipay.com/gateway.do”, APP_ID, APP_PRIVATE_KEY, “json”, CHARSET, ALIPAY_PUBLIC_KEY, “RSA2”);
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(“我是测试数据”);
model.setSubject(“App支付测试Java”);
model.setOutTradeNo(outtradeno);
model.setTimeoutExpress(“30m”);
model.setTotalAmount(“0.01”);
model.setProductCode(“QUICK_MSECURITY_PAY”);
request.setBizModel(model);
request.setNotifyUrl(“商户外网可以访问的异步地址”);
try {
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。
} catch (AlipayApiException e) {
e.printStackTrace();
}
JAVA服务端验证异步通知信息参数示例
//获取支付宝POST过来反馈信息
Map<String,String> params = new HashMap<String,String>();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = “”;
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length – 1) ? valueStr + values[i]
: valueStr + values[i] + “,”;
}
//乱码解决,这段代码在出现乱码时使用。
//valueStr = new String(valueStr.getBytes(“ISO-8859-1”), “utf-8”);
params.put(name, valueStr);
}
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
boolean flag = AlipaySignature.rsaCheckV1(params, alipaypublicKey, charset,”RSA2″)
交易操作
1.交易查询接口alipay.trade.query:
AlipayClient alipayClient = new DefaultAlipayClient(“https://openapi.alipay.com/gateway.do”, APP_ID, APP_PRIVATE_KEY, “json”, CHARSET, ALIPAY_PUBLIC_KEY, “RSA2”); //获得初始化的AlipayClient
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();//创建API对应的request类
request.setBizContent(“{” +” \”out_trade_no\”:\”20150320010101001\”,” +” \”trade_no\”:\”2014112611001004680073956707\”” +” }”);//设置业务参数
AlipayTradeQueryResponse response = alipayClient.execute(request);//通过alipayClient调用API,获得对应的response类
System.out.print(response.getBody());//根据response中的结果继续业务逻辑处理
2.交易退款接口alipay.trade.refund:
AlipayClient alipayClient = new DefaultAlipayClient(“https://openapi.alipay.com/gateway.do”, APP_ID, APP_PRIVATE_KEY, “json”, CHARSET, ALIPAY_PUBLIC_KEY, “RSA2”); //获得初始化的AlipayClient AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();//创建API对应的request类 request.setBizContent(“{” + ” \”out_trade_no\”:\”20150320010101001\”,” + ” \”trade_no\”:\”2014112611001004680073956707\”,” + ” \”out_request_no\”:\”1000001\”,” + ” \”refund_amount\”:\”1\”” + ” }”);//设置业务参数 AlipayTradeRefundResponse response = alipayClient.execute(request);//通过alipayClient调用API,获得对应的response类 System.out.print(response.getBody()); // 根据response中的结果继续业务逻辑处理
3.查询对账单下载地址接口
alipay.data.dataservice.bill.downloadurl.query:
为了保障交易的正确性,支付宝提供了交易账单数据提供给商户对账,对账说明。
AlipayClient alipayClient = new DefaultAlipayClient(“https://openapi.alipay.com/gateway.do”, APP_ID, APP_PRIVATE_KEY, “json”, CHARSET, ALIPAY_PUBLIC_KEY, “RSA2”); //获得初始化的AlipayClient
AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();//创建API对应的request类
request.setBizContent(“{” +” \”bill_type\”:\”trade\”,” +” \”bill_date\”:\”2016-04-05\”” +” }”);//设置业务参数
AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
System.out.print(response.getBody());//根据response中的结果继续业务逻辑处理
4.下载账单文件:
//将接口返回的对账单下载地址传入urlStr
String urlStr = “http://dwbillcenter.alipay.com/downloadBillFile.resource?bizType=X&userId=X&fileType=X&bizDates=X&downloadFileName=X&fileId=X”;//指定希望保存的文件路径
String filePath = “/Users/fund_bill_20160405.csv”;
URL url = null;
HttpURLConnection httpUrlConnection = null;
InputStream fis = null;
FileOutputStream fos = null;try {
url = new URL(urlStr);
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setConnectTimeout(5 * 1000);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestMethod(“GET”);
httpUrlConnection.setRequestProperty(“Charsert”, “UTF-8”);
httpUrlConnection.connect();
fis = httpUrlConnection.getInputStream();
byte[] temp = new byte[1024];
int b;
fos = new FileOutputStream(new File(filePath));
while ((b = fis.read(temp)) != -1) {
fos.write(temp, 0, b);
fos.flush();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
httpUrlConnection.disconnect();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}