81 lines
3.0 KiB
Python
81 lines
3.0 KiB
Python
import stripe
|
|
import json
|
|
import logging
|
|
from app.config import settings, Config
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
stripe.api_key = settings.STRIPE_PRIV_KEY
|
|
|
|
class StripeIntegration:
|
|
STRIPE_SUCCESS_URL = Config.STRIPE_SUCCESS_URL
|
|
STRIPE_CANCEL_URL= Config.STRIPE_CANCEL_URL
|
|
|
|
@staticmethod
|
|
def transction_hx(limit=30):
|
|
logger.info(f"Inside Stripe_Transaction Hx ===== :")
|
|
invoice_items = stripe.InvoiceItem.list(limit)
|
|
return invoice_items
|
|
|
|
@staticmethod
|
|
def create_customer(stripe_customer):
|
|
logger.info(f"Inside Stripe_Customer ===== : {stripe_customer}")
|
|
# customer = stripe.Customer.create(
|
|
# email= stripe_customer["email"],
|
|
# name=stripe_customer["name"],
|
|
# payment_method="pm_card_visa",
|
|
# description="Customer for Merms Subscription",
|
|
# invoice_settings={"default_payment_method": "pm_card_visa"},
|
|
# )
|
|
#
|
|
customer = stripe.Customer.create(
|
|
email= stripe_customer["email"],
|
|
name=stripe_customer["name"],
|
|
description="Customer for MERMS Subscription",
|
|
)
|
|
# payment_method="pm_card_visa", # Replace with a valid payment method ID or attach one later
|
|
return customer
|
|
|
|
@staticmethod
|
|
def create_product(display_name, monthly):
|
|
logger.info(f"Inside Stripe_Product ===== : {display_name}")
|
|
product_name = display_name
|
|
product = stripe.Product.create(name=product_name)
|
|
price = stripe.Price.create(
|
|
unit_amount=monthly, # Amount in cents (e.g., $10.00)
|
|
currency="usd",
|
|
recurring={"interval": "month"},
|
|
product=product.id,
|
|
)
|
|
return {'product_id': product.id, 'price_id': price.id}
|
|
|
|
@staticmethod
|
|
def create_subscription(self, data):
|
|
subscription = stripe.Subscription.create(
|
|
customer='customer.id',
|
|
items=[
|
|
{"price": 'price.id'},
|
|
],
|
|
payment_behavior="default_incomplete", # Recommended for handling initial payment
|
|
expand=["latest_invoice.payment_intent"], # To get details for payment confirmation
|
|
)
|
|
|
|
@staticmethod
|
|
def create_checkout_session_subscription(price_id, customer_id):
|
|
try:
|
|
checkout_session = stripe.checkout.Session.create(
|
|
customer=customer_id, # Pass the existing customer ID here
|
|
line_items=[
|
|
{
|
|
'price': price_id, # Use a pre-defined Stripe Price ID
|
|
'quantity': 1,
|
|
},
|
|
],
|
|
mode='subscription',
|
|
success_url= StripeIntegration.STRIPE_SUCCESS_URL,
|
|
cancel_url=StripeIntegration.STRIPE_CANCEL_URL,
|
|
)
|
|
return checkout_session
|
|
except stripe.error.StripeError as e:
|
|
print(f"Error creating subscription Checkout Session: {e}")
|
|
return None |