StripePriceServiceImpl.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.fdkankan.pay.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.fdkankan.pay.entity.StripePrice;
  4. import com.fdkankan.pay.mapper.IStripePriceMapper;
  5. import com.fdkankan.pay.service.IStripePriceService;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.stripe.model.Price;
  8. import com.stripe.param.checkout.SessionCreateParams;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.stereotype.Service;
  11. import java.math.BigDecimal;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. /**
  15. * <p>
  16. * 服务实现类
  17. * </p>
  18. *
  19. * @author
  20. * @since 2025-04-17
  21. */
  22. @Service
  23. @Slf4j
  24. public class StripePriceServiceImpl extends ServiceImpl<IStripePriceMapper, StripePrice> implements IStripePriceService {
  25. @Override
  26. public String getByParam(BigDecimal orderMoney, String currency, String productId, Integer autoPay,String interval) {
  27. try {
  28. LambdaQueryWrapper<StripePrice> wrapper = new LambdaQueryWrapper<>();
  29. wrapper.eq(StripePrice::getUnitAmount,orderMoney);
  30. wrapper.eq(StripePrice::getCurrency,currency);
  31. wrapper.eq(StripePrice::getProductId,productId);
  32. wrapper.eq(StripePrice::getRecurring,autoPay);
  33. StripePrice one = this.getOne(wrapper);
  34. if(one == null){
  35. //创建价格 https://stripe.com/docs/api/prices/create
  36. Map<String, Object> priceMap = new HashMap<>();
  37. priceMap.put("unit_amount", orderMoney.multiply(new BigDecimal(100)));
  38. priceMap.put("currency", currency);
  39. if(autoPay == 1){
  40. Map<String, Object> recurring = new HashMap<>();
  41. recurring.put("interval", interval);
  42. priceMap.put("recurring", recurring);
  43. }
  44. priceMap.put("product", productId);
  45. Price price = Price.create(priceMap);
  46. this.saveByParam(price.getId(),orderMoney,currency,productId,autoPay);
  47. return price.getId();
  48. }
  49. return one.getPriceId();
  50. }catch (Exception e){
  51. log.info("stripe创建价格失败:{}",e);
  52. }
  53. return null;
  54. }
  55. private void saveByParam(String priceId,BigDecimal orderMoney, String currency, String productId, Integer autoPay) {
  56. StripePrice stripePrice = new StripePrice();
  57. stripePrice.setPriceId(priceId);
  58. stripePrice.setUnitAmount(orderMoney);
  59. stripePrice.setCurrency(currency);
  60. stripePrice.setProductId(productId);
  61. stripePrice.setRecurring(autoPay);
  62. this.save(stripePrice);
  63. }
  64. }