package com.taoke.autopay.utils;
|
|
import com.google.gson.Gson;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.jsoup.Connection;
|
import org.jsoup.Jsoup;
|
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
import java.util.Map;
|
|
/**
|
* @author hxh
|
* @title: AlipayUtil
|
* @description: 支付宝工具类
|
* @date 2024/7/23 15:45
|
*/
|
public class AlipayOrderUtil {
|
|
public static class AlipayOrderTradeInfo{
|
|
public final static String STATUS_CANCELED = "BC";
|
public final static String STATUS_PAY = "BS";
|
public final static String STATUS_NOT_PAY = "BI";
|
|
/**
|
* bizIdentity : trade20001
|
* bizNo : 2024072322001415961410745988
|
* bizSuccessDate :
|
* goodsQuantity : 1
|
* goodsTitle : 抖音电商商家 即时到账--抖音电商-多笔订单6932448966275437765等
|
* itemRealAmount : 29.90
|
* salesProductCode : QUICK_MSECURITY_PAY
|
* status : BC
|
*/
|
|
private String bizIdentity;
|
private String bizNo;
|
private String bizSuccessDate;
|
private int goodsQuantity;
|
private String goodsTitle;
|
private String itemRealAmount;
|
private String salesProductCode;
|
// BC: 订单已取消 BS: 订单已支付 BI: 订单未支付
|
private String status;
|
|
public String getBizIdentity() {
|
return bizIdentity;
|
}
|
|
public void setBizIdentity(String bizIdentity) {
|
this.bizIdentity = bizIdentity;
|
}
|
|
public String getBizNo() {
|
return bizNo;
|
}
|
|
public void setBizNo(String bizNo) {
|
this.bizNo = bizNo;
|
}
|
|
public String getBizSuccessDate() {
|
return bizSuccessDate;
|
}
|
|
public void setBizSuccessDate(String bizSuccessDate) {
|
this.bizSuccessDate = bizSuccessDate;
|
}
|
|
public int getGoodsQuantity() {
|
return goodsQuantity;
|
}
|
|
public void setGoodsQuantity(int goodsQuantity) {
|
this.goodsQuantity = goodsQuantity;
|
}
|
|
public String getGoodsTitle() {
|
return goodsTitle;
|
}
|
|
public void setGoodsTitle(String goodsTitle) {
|
this.goodsTitle = goodsTitle;
|
}
|
|
public String getItemRealAmount() {
|
return itemRealAmount;
|
}
|
|
public void setItemRealAmount(String itemRealAmount) {
|
this.itemRealAmount = itemRealAmount;
|
}
|
|
public String getSalesProductCode() {
|
return salesProductCode;
|
}
|
|
public void setSalesProductCode(String salesProductCode) {
|
this.salesProductCode = salesProductCode;
|
}
|
|
public String getStatus() {
|
return status;
|
}
|
|
public void setStatus(String status) {
|
this.status = status;
|
}
|
}
|
|
private static Gson gson=JsonUtil.getSimpleGson();
|
|
private static String getLocationUrl(String alipayUrl) throws IOException {
|
Connection.Response response = Jsoup.connect(alipayUrl)
|
.userAgent("Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36")
|
.timeout(20000)
|
.header("Sec-Ch-Ua","\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"")
|
.header("Sec-Ch-Ua-Mobile","?1")
|
.header("Sec-Ch-Ua-Platform","\"Android\"")
|
.header("Sec-Fetch-Dest","document")
|
.header("Sec-Fetch-Mode","navigate")
|
.header("Sec-Fetch-Site","none")
|
.header("Sec-Fetch-User","?1")
|
.followRedirects(false)
|
.method(Connection.Method.GET)
|
.execute();
|
System.out.println(response.statusCode());
|
|
if (response.statusCode() == 302 || response.statusCode() == 301) {
|
// 获取重定向 URL
|
String redirectUrl = response.header("Location");
|
System.out.println("Redirect URL: " + redirectUrl);
|
return redirectUrl;
|
}
|
return null;
|
}
|
|
private static AlipayOrderTradeInfo parseOrderInfo(String biz_no) throws Exception {
|
String link = "https://mclient.alipay.com/h5/shareppyprepayconsult.htm?biz_no="+ URLEncoder.encode(biz_no,"UTF-8");
|
Connection.Response response = Jsoup.connect(link)
|
.userAgent("Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36")
|
.timeout(20000)
|
.header("Sec-Ch-Ua","\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"")
|
.header("Sec-Ch-Ua-Mobile","?1")
|
.header("Sec-Ch-Ua-Platform","\"Android\"")
|
.header("Sec-Fetch-Dest","document")
|
.header("Sec-Fetch-Mode","navigate")
|
.header("Sec-Fetch-Site","none")
|
.header("Sec-Fetch-User","?1")
|
.ignoreContentType(true)
|
.method(Connection.Method.GET)
|
.execute();
|
if(response.statusCode()==200){
|
String result = response.body();
|
System.out.println(result);
|
JSONObject root = JSONObject.fromObject(result);
|
if(root.optBoolean("success")){
|
JSONObject data = root.optJSONObject("data");
|
if(data!=null){
|
JSONArray tradeInfoList = data.optJSONArray("tradeInfoList");
|
if(tradeInfoList!=null&&tradeInfoList.size()>0){
|
return gson.fromJson(tradeInfoList.optJSONObject(0).toString(),AlipayOrderTradeInfo.class);
|
}
|
}
|
}
|
throw new Exception(result);
|
}
|
throw new Exception("网络请求出错");
|
}
|
|
public static AlipayOrderTradeInfo getTradeInfo(String url) throws Exception {
|
url = getLocationUrl(url);
|
if(url!=null){
|
Map<String, String> params = HttpUtil.getPramsFromUrl(url);
|
AlipayOrderTradeInfo tradeInfo = parseOrderInfo(params.get("biz_no"));
|
return tradeInfo;
|
}
|
return null;
|
}
|
|
public static void main(String[] args) throws Exception {
|
String url = getLocationUrl("https://ur.alipay.com/_5MDnsPbo8TAsDNtNB9Tfia");
|
if(url!=null){
|
Map<String, String> params = HttpUtil.getPramsFromUrl(url);
|
AlipayOrderTradeInfo tradeInfo = parseOrderInfo(params.get("biz_no"));
|
System.out.println("");
|
}
|
// parseOrderInfo("http://www.izxwl.com/admin/index.html");
|
|
|
}
|
|
}
|