package com.fdkankan.pay.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.pay.entity.StripePrice;
import com.fdkankan.pay.mapper.IStripePriceMapper;
import com.fdkankan.pay.service.IStripePriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stripe.model.Price;
import com.stripe.param.checkout.SessionCreateParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
*
* 服务实现类
*
*
* @author
* @since 2025-04-17
*/
@Service
@Slf4j
public class StripePriceServiceImpl extends ServiceImpl implements IStripePriceService {
@Override
public String getByParam(BigDecimal orderMoney, String currency, String productId, Integer autoPay,String interval) {
try {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(StripePrice::getUnitAmount,orderMoney);
wrapper.eq(StripePrice::getCurrency,currency);
wrapper.eq(StripePrice::getProductId,productId);
wrapper.eq(StripePrice::getRecurring,autoPay);
StripePrice one = this.getOne(wrapper);
if(one == null){
//创建价格 https://stripe.com/docs/api/prices/create
Map priceMap = new HashMap<>();
priceMap.put("unit_amount", orderMoney.multiply(new BigDecimal(100)));
priceMap.put("currency", currency);
if(autoPay == 1){
Map recurring = new HashMap<>();
recurring.put("interval", interval);
priceMap.put("recurring", recurring);
}
priceMap.put("product", productId);
Price price = Price.create(priceMap);
this.saveByParam(price.getId(),orderMoney,currency,productId,autoPay);
return price.getId();
}
return one.getPriceId();
}catch (Exception e){
log.info("stripe创建价格失败:{}",e);
}
return null;
}
private void saveByParam(String priceId,BigDecimal orderMoney, String currency, String productId, Integer autoPay) {
StripePrice stripePrice = new StripePrice();
stripePrice.setPriceId(priceId);
stripePrice.setUnitAmount(orderMoney);
stripePrice.setCurrency(currency);
stripePrice.setProductId(productId);
stripePrice.setRecurring(autoPay);
this.save(stripePrice);
}
}