|
@@ -1,10 +1,13 @@
|
|
|
package cn.com.ty.lift.common.aliservice.alipay;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alipay.api.*;
|
|
|
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
|
|
|
+import com.alipay.api.request.AlipaySystemOauthTokenRequest;
|
|
|
import com.alipay.api.request.AlipayTradeCreateRequest;
|
|
|
import com.alipay.api.request.AlipayTradeQueryRequest;
|
|
|
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
|
|
|
+import com.alipay.api.response.AlipaySystemOauthTokenResponse;
|
|
|
import com.alipay.api.response.AlipayTradeCreateResponse;
|
|
|
import com.alipay.api.response.AlipayTradeQueryResponse;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
@@ -32,75 +35,63 @@ public class AlipayUtil {
|
|
|
//卖家id
|
|
|
private static String sellerId = "2088102179670843";
|
|
|
|
|
|
+ private static DefaultAlipayClient client;
|
|
|
+
|
|
|
/*** @param 买家id,自定义订单号,商品名,商品说明,商品总价
|
|
|
* @return 支付宝的订单编号
|
|
|
* @description
|
|
|
* @date 2019/12/25 9:14
|
|
|
*/
|
|
|
- public static String create(String buyerId, String outTradeNo, String subject, String body, BigDecimal totalAmount) throws AlipayApiException {
|
|
|
- //构造client
|
|
|
+ static {
|
|
|
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
|
|
|
certAlipayRequest.setServerUrl(gateway);
|
|
|
certAlipayRequest.setAppId(appId);
|
|
|
certAlipayRequest.setPrivateKey(privateKey);
|
|
|
certAlipayRequest.setFormat("json");
|
|
|
- certAlipayRequest.setCharset(charSet);
|
|
|
- certAlipayRequest.setSignType(signType);
|
|
|
- String path = "D:/Documents/支付宝开放平台开发助手/CSR/";
|
|
|
+ certAlipayRequest.setCharset("utf-8");
|
|
|
+ certAlipayRequest.setSignType("RSA2");
|
|
|
+ //String path = "D:/Documents/支付宝开放平台开发助手/CSR/";
|
|
|
+ String path = "C:/Users/admin/Desktop/temp/";
|
|
|
certAlipayRequest.setCertPath(path + "appCertPublicKey_2016101500691469.crt");
|
|
|
certAlipayRequest.setAlipayPublicCertPath(path + "alipayCertPublicKey_RSA2.crt");
|
|
|
certAlipayRequest.setRootCertPath(path + "alipayRootCert.crt");
|
|
|
- DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
|
|
|
+ try {
|
|
|
+ client = new DefaultAlipayClient(certAlipayRequest);
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ System.out.println("支付宝连接失败");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- //发送API请求
|
|
|
- AlipayRequest request = new AlipayTradeQueryRequest();
|
|
|
- AlipayResponse response = alipayClient.certificateExecute(request);
|
|
|
- System.out.println("===========");
|
|
|
- Map<String, String> sign = response.getParams("alipay_trade_query_response");
|
|
|
- System.out.println(sign);
|
|
|
- return "132";
|
|
|
-// AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, privateKey, "json", charSet, publicKey, signType);
|
|
|
-// AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
|
|
|
-// request.setBizContent("{" +
|
|
|
-// "\"app_id\":\"" + appId + "\"," +
|
|
|
-// "\"charset\":\"" + charSet + "\"," +
|
|
|
-// "\"sign_type\":\"" + signType + "\"," +
|
|
|
-// "\"method\":\"alipay.trade.app.pay\"," +
|
|
|
-// "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
|
|
|
-// "\"out_trade_no\":\"" + outTradeNo + "\"," +
|
|
|
-// "\"seller_id\":\"" + sellerId + "\"," +
|
|
|
-// "\"total_amount\":\"" + totalAmount.toString() + "\"," +
|
|
|
-// "\"buyer_id\":\"" + buyerId + "\"," +
|
|
|
-// "\"subject\":\"" + subject + "\"," +
|
|
|
-// "\"body\":\"" + body + "\"" +
|
|
|
-// " }");
|
|
|
-// AlipayTradeCreateResponse response = alipayClient.execute(request);
|
|
|
-// if (response.isSuccess()) {
|
|
|
-// return response.getTradeNo();
|
|
|
-// } else {
|
|
|
-// return null;
|
|
|
-// }
|
|
|
+ public static String create(String buyerId, String outTradeNo, String subject, String body, BigDecimal totalAmount) throws AlipayApiException {
|
|
|
+ AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
|
|
|
+ request.setBizContent("{" +
|
|
|
+ "\"method\":\"alipay.trade.app.pay\"," +
|
|
|
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
|
|
|
+ "\"out_trade_no\":\"" + outTradeNo + "\"," +
|
|
|
+ "\"seller_id\":\"" + sellerId + "\"," +
|
|
|
+ "\"total_amount\":\"" + totalAmount.toString() + "\"," +
|
|
|
+ "\"buyer_id\":\"" + buyerId + "\"," +
|
|
|
+ "\"subject\":\"" + subject + "\"," +
|
|
|
+ "\"body\":\"" + body + "\"" +
|
|
|
+ " }");
|
|
|
+ client.certificateExecute(request);
|
|
|
+
|
|
|
+ AlipayTradeCreateResponse response = client.certificateExecute(request);
|
|
|
+ if (response.isSuccess()) {
|
|
|
+ return response.getTradeNo();
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param
|
|
|
- * @return true提现成功 false提现失败
|
|
|
+ * @return 提现成功返回单号 提现失败返回null
|
|
|
* @description 收款人id,自定义提现单号,提现标题,提现总金额,备注
|
|
|
* @date 2019/12/26 14:51
|
|
|
*/
|
|
|
public static String withdraw(String identity, String outBizNo, String orderTitle, BigDecimal transAmount, String remark) throws AlipayApiException {
|
|
|
- CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
|
|
|
- certAlipayRequest.setServerUrl(gateway);
|
|
|
- certAlipayRequest.setAppId(appId);
|
|
|
- certAlipayRequest.setPrivateKey(privateKey);
|
|
|
- certAlipayRequest.setFormat("json");
|
|
|
- certAlipayRequest.setCharset(charSet);
|
|
|
- certAlipayRequest.setSignType("RSA2");
|
|
|
- String path = "D:/Documents/支付宝开放平台开发助手/CSR/";
|
|
|
- certAlipayRequest.setCertPath(path + "appCertPublicKey_2016101500691469.crt");
|
|
|
- certAlipayRequest.setAlipayPublicCertPath(path + "alipayCertPublicKey_RSA2.crt");
|
|
|
- certAlipayRequest.setRootCertPath(path + "alipayRootCert.crt");
|
|
|
- DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
|
|
|
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
|
|
|
request.setBizContent("{" +
|
|
|
"\"out_biz_no\":\"" + outBizNo + "\"," +
|
|
@@ -115,8 +106,7 @@ public class AlipayUtil {
|
|
|
"\"remark\":\"" + remark + "\"," +
|
|
|
"\"business_params\":\"{\\\"payer_show_name\\\":\\\"天域梯业\\\"}\"," +
|
|
|
" }");
|
|
|
- AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
|
|
|
-
|
|
|
+ AlipayFundTransUniTransferResponse response = client.certificateExecute(request);
|
|
|
if (response.isSuccess()) {
|
|
|
return response.getOrderId();
|
|
|
} else {
|
|
@@ -132,57 +122,37 @@ public class AlipayUtil {
|
|
|
* @date 2019/12/25 11:00
|
|
|
*/
|
|
|
public static boolean order(String tradeNo) throws AlipayApiException {
|
|
|
- AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, privateKey, "json", charSet, publicKey, "RSA2"); //获得初始化的AlipayClient
|
|
|
- //创建API对应的request类
|
|
|
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
|
|
|
- //设置业务参数
|
|
|
-// " \"out_trade_no\":\"" + outTradeNo + "\"," +
|
|
|
request.setBizContent("{" +
|
|
|
" \"trade_no\":\"" + tradeNo + "\"" +
|
|
|
" }");
|
|
|
- //通过alipayClient调用API,获得对应的response类
|
|
|
- AlipayTradeQueryResponse response = alipayClient.execute(request);
|
|
|
- //根据response中的结果继续业务逻辑处理
|
|
|
+ AlipayTradeQueryResponse response = client.certificateExecute(request);
|
|
|
if (response.getTradeStatus() == null) {
|
|
|
return false;
|
|
|
}
|
|
|
return response.getTradeStatus().equals("TRADE_SUCCESS");
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws AlipayApiException, IOException {
|
|
|
-// String buyerLogonId = "bdlcph6736@sandbox.com";
|
|
|
-// if (AlipayUtil.order("1209664525224103938", "2019122522001479571000085482")) {
|
|
|
+ public static void main(String[] args) throws AlipayApiException {
|
|
|
+// if (AlipayUtil.order("2019122822001479571000089922")) {
|
|
|
// System.out.println("已支付");
|
|
|
// } else {
|
|
|
// System.out.println("未支付");
|
|
|
// }
|
|
|
-// AlipayUtil.create(buyerLogonId, IdWorker.getIdStr(), "电梯5号", "电梯性能说明", "100.00");
|
|
|
- AlipayUtil.withdraw(buyerId, IdWorker.getIdStr(), "电梯5号", new BigDecimal("100.00"), "电梯提现");
|
|
|
+// String 电梯性能说明 = AlipayUtil.create(buyerId, IdWorker.getIdStr(), "电梯12.28号", "电梯性能说明", new BigDecimal("100.00"));
|
|
|
+// System.out.println(电梯性能说明);
|
|
|
+ // AlipayUtil.withdraw(buyerId, IdWorker.getIdStr(), "电梯5号", new BigDecimal("100.00"), "电梯提现");
|
|
|
// AlipayUtil.getUserId();
|
|
|
}
|
|
|
|
|
|
-// public static String getUserId() throws IOException {
|
|
|
-// String authCode = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=" + appId + "&scope=SCOPE&redirect_uri=https%253A%252F%252Fresponse";
|
|
|
-// HttpClient aNew = HttpClient.New(new URL(authCode));
|
|
|
-
|
|
|
-
|
|
|
- //https://openapi.alipaydev.com/gateway.do 。莫要跟正式环境弄混。正式的不带dev
|
|
|
- //APP_ID位沙箱里面写的appid。APP_PRIVATE_KEY为自己的私钥,用于报文签名. ALIPAY_PUBLIC_KEY 这个是支付宝公钥,用于验签
|
|
|
-// AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, privateKey, "json", "utf-8", publicKey, "RSA2");
|
|
|
+// public static String getUserId() throws AlipayApiException {
|
|
|
// AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
|
|
-// //这个就是第一步获取的auth_code
|
|
|
-// request.setCode("2079dd25baf145808b1082ff27a5ZX57");
|
|
|
-// //这个固定值,参考https://docs.open.alipay.com/api_9/alipay.system.oauth.token
|
|
|
-// request.setGrantType("authorization_code");
|
|
|
-// //request.setBizModel(new BizOrderQueryResponse());
|
|
|
-// try {
|
|
|
-// AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
|
|
|
-// System.out.println(oauthTokenResponse.getBody());
|
|
|
-// System.out.println(oauthTokenResponse.getUserId());
|
|
|
-// } catch (AlipayApiException e) {
|
|
|
-// //处理异常
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
+// request.setBizModel(new AlipayObject() {
|
|
|
+// String method = "alipay.user.info.share";
|
|
|
+// });
|
|
|
+//
|
|
|
+// AlipaySystemOauthTokenResponse response = client.certificateExecute(request);
|
|
|
+// response.getUserId();
|
|
|
// return "123";
|
|
|
// }
|
|
|
}
|