解析所有发起购买的客户端ip地址
This commit is contained in:
@@ -5,7 +5,6 @@ import com.ai.da.common.constant.CommonConstant;
|
|||||||
import com.ai.da.common.context.UserContext;
|
import com.ai.da.common.context.UserContext;
|
||||||
import com.ai.da.common.enums.*;
|
import com.ai.da.common.enums.*;
|
||||||
import com.ai.da.common.utils.DateUtil;
|
import com.ai.da.common.utils.DateUtil;
|
||||||
import com.ai.da.common.utils.RequestInfoUtil;
|
|
||||||
import com.ai.da.common.utils.SendEmailUtil;
|
import com.ai.da.common.utils.SendEmailUtil;
|
||||||
import com.ai.da.mapper.primary.AccountMapper;
|
import com.ai.da.mapper.primary.AccountMapper;
|
||||||
import com.ai.da.mapper.primary.PaymentInfoMapper;
|
import com.ai.da.mapper.primary.PaymentInfoMapper;
|
||||||
@@ -18,7 +17,6 @@ import com.ai.da.model.dto.ProductPurchaseDTO;
|
|||||||
import com.ai.da.model.dto.SubscriptionEmailParamsDTO;
|
import com.ai.da.model.dto.SubscriptionEmailParamsDTO;
|
||||||
import com.ai.da.service.*;
|
import com.ai.da.service.*;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
@@ -38,12 +36,8 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
@@ -85,7 +79,7 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public String pay(ProductPurchaseDTO productPurchaseDTO) {
|
public String pay(ProductPurchaseDTO productPurchaseDTO, HttpServletRequest request) {
|
||||||
Stripe.apiKey = privateKey;
|
Stripe.apiKey = privateKey;
|
||||||
|
|
||||||
ProductEnum productEnum;
|
ProductEnum productEnum;
|
||||||
@@ -102,9 +96,9 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
case "Year":
|
case "Year":
|
||||||
productEnum = ProductEnum.AnnualSubscription;
|
productEnum = ProductEnum.AnnualSubscription;
|
||||||
break;
|
break;
|
||||||
case "Day":
|
/*case "Day":
|
||||||
productEnum = ProductEnum.DailySubscription;
|
productEnum = ProductEnum.DailySubscription;
|
||||||
break;
|
break;*/
|
||||||
default:
|
default:
|
||||||
throw new BusinessException("unknown subscription type");
|
throw new BusinessException("unknown subscription type");
|
||||||
}
|
}
|
||||||
@@ -113,7 +107,8 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
throw new BusinessException("unknown product type");
|
throw new BusinessException("unknown product type");
|
||||||
}
|
}
|
||||||
log.info("生成订单");
|
log.info("生成订单");
|
||||||
OrderInfo orderInfo = orderInfoService.createOrderByProductId(productPurchaseDTO.getQuantity(), PayTypeEnum.STRIPE.getType(), productEnum);
|
OrderInfo orderInfo = orderInfoService.createOrderByProductId(productPurchaseDTO.getQuantity(),
|
||||||
|
PayTypeEnum.STRIPE.getType(), productEnum, request);
|
||||||
String payType;
|
String payType;
|
||||||
if (productPurchaseDTO.getAutoRenewal()){
|
if (productPurchaseDTO.getAutoRenewal()){
|
||||||
payType = "recurring";
|
payType = "recurring";
|
||||||
@@ -356,7 +351,6 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
// 发送续订失败邮件
|
// 发送续订失败邮件
|
||||||
response = sendRenewalFailEmail(invoice.getId(), null, paymentInfo.getOrderNo());
|
response = sendRenewalFailEmail(invoice.getId(), null, paymentInfo.getOrderNo());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}else if (stripeObject instanceof Charge) {
|
}else if (stripeObject instanceof Charge) {
|
||||||
Charge charge = (Charge) stripeObject;
|
Charge charge = (Charge) stripeObject;
|
||||||
@@ -1005,12 +999,11 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo 新建一个订阅 使用不会成功的付款方式
|
// 新建一个订阅 使用不会成功的付款方式(仅供测试使用)
|
||||||
|
|
||||||
public String createSubscriptionTemp(String name, String email){
|
public String createSubscriptionTemp(String name, String email){
|
||||||
Stripe.apiKey = privateKey;
|
Stripe.apiKey = privateKey;
|
||||||
try {
|
try {
|
||||||
OrderInfo orderInfo = orderInfoService.createOrderByProductId(1, PayTypeEnum.STRIPE.getType(), ProductEnum.DailySubscription);
|
OrderInfo orderInfo = orderInfoService.createOrderByProductId(1, PayTypeEnum.STRIPE.getType(), ProductEnum.DailySubscription, null);
|
||||||
|
|
||||||
// String customerId = getCustomer(name, email);
|
// String customerId = getCustomer(name, email);
|
||||||
String paymentMethodCode = "pm_card_mastercard";
|
String paymentMethodCode = "pm_card_mastercard";
|
||||||
@@ -1030,8 +1023,7 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
.setInvoiceSettings(
|
.setInvoiceSettings(
|
||||||
CustomerUpdateParams.InvoiceSettings.builder()
|
CustomerUpdateParams.InvoiceSettings.builder()
|
||||||
.setDefaultPaymentMethod(paymentMethod.getId())
|
.setDefaultPaymentMethod(paymentMethod.getId())
|
||||||
.build()
|
.build())
|
||||||
)
|
|
||||||
.build();
|
.build();
|
||||||
updatedCustomer.update(params);
|
updatedCustomer.update(params);
|
||||||
|
|
||||||
@@ -1041,8 +1033,7 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
.addItem(
|
.addItem(
|
||||||
SubscriptionCreateParams.Item.builder()
|
SubscriptionCreateParams.Item.builder()
|
||||||
.setPrice("price_1QFXkf02n1TEydyNtA4TQ3Yz") // 替换为实际的价格 ID
|
.setPrice("price_1QFXkf02n1TEydyNtA4TQ3Yz") // 替换为实际的价格 ID
|
||||||
.build()
|
.build())
|
||||||
)
|
|
||||||
.setDescription("AiDA - " + orderInfo.getOrderNo())
|
.setDescription("AiDA - " + orderInfo.getOrderNo())
|
||||||
.build();
|
.build();
|
||||||
Subscription subscription = Subscription.create(subscriptionParams);
|
Subscription subscription = Subscription.create(subscriptionParams);
|
||||||
@@ -1151,96 +1142,12 @@ public class StripeServiceImpl implements StripeService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getIp2(HttpServletRequest request) {
|
// public String getIp(HttpServletRequest request) {
|
||||||
/*String ip = request.getHeader("X-Forwarded-For");
|
// String ipAddress = RequestInfoUtil.getIpAddress(request);
|
||||||
String ipAddress = "";
|
// if (!StringUtil.isNullOrEmpty(ipAddress)) {
|
||||||
if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
|
// return getIPLocation(ipAddress);
|
||||||
//多次反向代理后会有多个ip值,第一个ip才是真实ip
|
// }
|
||||||
int index = ip.indexOf(",");
|
//
|
||||||
if(index != -1){
|
// return request.getRemoteAddr();
|
||||||
ipAddress = ip.substring(0,index);
|
// }
|
||||||
}else{
|
|
||||||
ipAddress = ip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ip = request.getHeader("X-Real-IP");
|
|
||||||
if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
|
|
||||||
ipAddress = ip;
|
|
||||||
}
|
|
||||||
if (!StringUtil.isNullOrEmpty(ipAddress)) {
|
|
||||||
getIPLocation(ipAddress);
|
|
||||||
}*/
|
|
||||||
String ipAddress = RequestInfoUtil.getIpAddress(request);
|
|
||||||
if (!StringUtil.isNullOrEmpty(ipAddress)) {
|
|
||||||
return getIPLocation(ipAddress);
|
|
||||||
}
|
|
||||||
|
|
||||||
return request.getRemoteAddr();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 免费 API 服务可能有请求频率限制,如果你需要处理大量 IP 地址,可能需要考虑使用付费服务或购买 IP 地理位置数据库。此外,始终要遵守 API 提供商的使用条款和隐私政策。*/
|
|
||||||
|
|
||||||
public String getIPLocation(String ip) {
|
|
||||||
// String ip = "117.143.125.1"; // 替换为你想查询的 IP 地址
|
|
||||||
// String ip = "194.5.48.180"; // 替换为你想查询的 IP 地址
|
|
||||||
String apiURL = "http://ip-api.com/json/" + ip;
|
|
||||||
|
|
||||||
try {
|
|
||||||
URL url = new URL(apiURL);
|
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setRequestMethod("GET");
|
|
||||||
conn.setRequestProperty("Accept", "application/json");
|
|
||||||
|
|
||||||
if (conn.getResponseCode() != 200) {
|
|
||||||
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
||||||
String output;
|
|
||||||
StringBuilder outputBuilder = new StringBuilder();
|
|
||||||
System.out.println("Output from Server .... \n");
|
|
||||||
while ((output = br.readLine()) != null) {
|
|
||||||
outputBuilder.append(output);
|
|
||||||
System.out.println(output);
|
|
||||||
}
|
|
||||||
conn.disconnect();
|
|
||||||
Map map = JSONObject.parseObject(outputBuilder.toString(), Map.class);
|
|
||||||
log.info("map: {}", map);
|
|
||||||
return JSON.toJSONString(map);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String ip = "117.143.125.1"; // 替换为你想查询的 IP 地址
|
|
||||||
// String ip = "194.5.48.180"; // 替换为你想查询的 IP 地址
|
|
||||||
String apiURL = "http://ip-api.com/json/" + ip;
|
|
||||||
|
|
||||||
try {
|
|
||||||
URL url = new URL(apiURL);
|
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setRequestMethod("GET");
|
|
||||||
conn.setRequestProperty("Accept", "application/json");
|
|
||||||
|
|
||||||
if (conn.getResponseCode() != 200) {
|
|
||||||
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
||||||
String output;
|
|
||||||
System.out.println("Output from Server .... \n");
|
|
||||||
StringBuilder outputBuilder = new StringBuilder();
|
|
||||||
while ((output = br.readLine()) != null) {
|
|
||||||
outputBuilder.append(output);
|
|
||||||
System.out.println(output);
|
|
||||||
}
|
|
||||||
conn.disconnect();
|
|
||||||
Map map = JSONObject.parseObject(outputBuilder.toString(), Map.class);
|
|
||||||
log.info("map: {}", map);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user