|
@@ -0,0 +1,147 @@
|
|
|
+package com.fdkankan.ucenter.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.fdkankan.ucenter.entity.StripeConfig;
|
|
|
+import com.fdkankan.ucenter.entity.StripeUserEmail;
|
|
|
+import com.fdkankan.ucenter.service.IStripeConfigService;
|
|
|
+import com.fdkankan.ucenter.service.IStripeUserEmailService;
|
|
|
+import com.fdkankan.ucenter.util.DateUserUtil;
|
|
|
+import com.stripe.Stripe;
|
|
|
+import com.stripe.exception.StripeException;
|
|
|
+import com.stripe.model.Customer;
|
|
|
+import com.stripe.model.CustomerSearchResult;
|
|
|
+import com.stripe.model.PaymentIntent;
|
|
|
+import com.stripe.model.PaymentIntentSearchResult;
|
|
|
+import com.stripe.param.CustomerSearchParams;
|
|
|
+import com.stripe.param.PaymentIntentSearchParams;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class StripeService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ IStripeConfigService stripeConfigService;
|
|
|
+ @Autowired
|
|
|
+ IStripeUserEmailService stripeUserEmailService;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void setStripeConfig(){
|
|
|
+ List<StripeConfig> list = stripeConfigService.list();
|
|
|
+ if(CollectionUtil.isEmpty(list)){
|
|
|
+ log.info("setStripeConfig-error:数据库未初始化stripe配置");
|
|
|
+ }
|
|
|
+ StripeConfig stripeConfig = list.get(0);
|
|
|
+ Stripe.apiKey = stripeConfig.getStripePrivate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean checkUserStripePay(Long userId,String userName){
|
|
|
+ List<String> emailList = new ArrayList<>();
|
|
|
+ emailList.add(userName);
|
|
|
+ List<StripeUserEmail> list = stripeUserEmailService.getByUserId(userId);
|
|
|
+ if(CollectionUtil.isNotEmpty(list)){
|
|
|
+ for (StripeUserEmail stripeUserEmail : list) {
|
|
|
+ emailList.add(stripeUserEmail.getStripePayEmail());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String email : emailList) {
|
|
|
+ if(checkEmailStripePay(email)){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public boolean checkEmailStripePay(String email){
|
|
|
+ List<String> customer = this.getCustomerByEmail(email);
|
|
|
+ if(CollectionUtil.isNotEmpty(customer)){
|
|
|
+ for (String customerId : customer) {
|
|
|
+ List<PaymentIntent> paymentIntents = this.searchPaySuccess(customerId);
|
|
|
+ if(CollectionUtil.isNotEmpty(paymentIntents)){
|
|
|
+ for (PaymentIntent paymentIntent : paymentIntents) {
|
|
|
+ Long created = paymentIntent.getCreated();
|
|
|
+ Date date = new Date(created);
|
|
|
+ Date addOneYear = DateUserUtil.dateAddOneYear(date, 1);
|
|
|
+ if(addOneYear.getTime() > new Date().getTime()){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private List<String> getCustomerByEmail(String email) {
|
|
|
+ try {
|
|
|
+ CustomerSearchParams params =
|
|
|
+ CustomerSearchParams
|
|
|
+ .builder()
|
|
|
+ .setQuery("email:'"+email+"'")
|
|
|
+ .setLimit(100L)
|
|
|
+ .build();
|
|
|
+ CustomerSearchResult result = Customer.search(params);
|
|
|
+ List<Customer> data = result.getData();
|
|
|
+ List<String> resultList = new ArrayList<>(data.stream().map(Customer::getId).collect(Collectors.toList()));
|
|
|
+
|
|
|
+ while (StringUtils.isNotBlank(result.getNextPage())){
|
|
|
+ params = CustomerSearchParams
|
|
|
+ .builder()
|
|
|
+ .setQuery("email:'"+email+"'")
|
|
|
+ .setPage(result.getNextPage())
|
|
|
+ .setLimit(100L)
|
|
|
+ .build();
|
|
|
+ result = Customer.search(params);
|
|
|
+ data = result.getData();
|
|
|
+ resultList.addAll(data.stream().map(Customer::getId).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("stripe-getCustomerByEmail-error:",e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private List<PaymentIntent> searchPaySuccess(String customerId) {
|
|
|
+ try {
|
|
|
+ PaymentIntentSearchParams params = PaymentIntentSearchParams
|
|
|
+ .builder()
|
|
|
+ .setQuery("status:'succeeded' AND customer:'" + customerId+"'")
|
|
|
+ //.setQuery("status:'succeeded'")
|
|
|
+ .setLimit(100L)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ PaymentIntentSearchResult result = PaymentIntent.search(params);
|
|
|
+ List<PaymentIntent> resultList = new ArrayList<>(result.getData());
|
|
|
+
|
|
|
+ while (StringUtils.isNotBlank(result.getNextPage())){
|
|
|
+ params = PaymentIntentSearchParams
|
|
|
+ .builder()
|
|
|
+ .setQuery("status:'succeeded' AND customer:'" + customerId+"'")
|
|
|
+ .setPage(result.getNextPage())
|
|
|
+ .setLimit(100L)
|
|
|
+ .build();
|
|
|
+ result = PaymentIntent.search(params);
|
|
|
+ resultList.addAll(result.getData());
|
|
|
+ }
|
|
|
+
|
|
|
+ return resultList;
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("stripe-searchPaySuccess-error:",e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|