stripe session
This commit is contained in:
@@ -47,9 +47,10 @@ class StripeIntegration:
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def create_checkout_session_subscription(price_id):
|
||||
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
|
||||
@@ -57,10 +58,10 @@ class StripeIntegration:
|
||||
},
|
||||
],
|
||||
mode='subscription',
|
||||
success_url='https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
|
||||
cancel_url='https://example.com/cancel',
|
||||
success_url='https://qa-panel.mermsemr.com/subscription-success?session_id={CHECKOUT_SESSION_ID}',
|
||||
cancel_url='https://qa-panel.mermsemr.com/subscription',
|
||||
)
|
||||
return checkout_session.url
|
||||
return checkout_session
|
||||
except stripe.error.StripeError as e:
|
||||
print(f"Error creating subscription Checkout Session: {e}")
|
||||
return None
|
||||
@@ -195,6 +195,14 @@ def subscription_products():
|
||||
response = SubscriptionService.subscription_available_products(data)
|
||||
return response
|
||||
|
||||
#
|
||||
@api.route("/panel/subscription/start", methods=["POST"])
|
||||
def subscription_sessuib_start():
|
||||
data = request.get_json()
|
||||
logger.info(f"Route Subscription Session Start ==>>>> {data}")
|
||||
response = SubscriptionService.subscription_session_start(data)
|
||||
return response
|
||||
|
||||
|
||||
# /panel/myproduct/subscription
|
||||
@api.route("/panel/myproduct/subscription", methods=["POST"])
|
||||
@@ -219,6 +227,12 @@ def myproduct_provision_actions():
|
||||
response = MyProductsService.process_provision_actions(data)
|
||||
return response
|
||||
|
||||
@api.route("/stripe/payments", methods=["POST"])
|
||||
def stripe_payments_webhook():
|
||||
data = request.get_json()
|
||||
logger.info(f"Route Stripe Webhook ==>>>> {data}")
|
||||
# response = MyProductsService.process_provision_actions(data)
|
||||
return []
|
||||
|
||||
|
||||
# /panel/account/calendar
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
class SubscriptionSession(Schema):
|
||||
token = fields.Str(required=True)
|
||||
uid = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
priceId = fields.Str(required=True)
|
||||
|
||||
# {
|
||||
# "priceId": "price_1RzLIMLjZLojw6IZ95Eijmdd",
|
||||
# "customerId": "cus_StebwHVaA1nUjC",
|
||||
# "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjpbNTFdLCJtZW1iZXJfaWQiOls1MV0sInVpZCI6WyI5NzVlZTQyZS0zMTY5LTQ5NzgtOTJkNy1kMjhlN2UyYWMwMTQiXX0sImV4cCI6MTc1NjE3ODYwM30.rC8KGz7M8PjgMBu-Vh9YcEWgcFp_WDqJUvTEa_OUU8Y",
|
||||
# "uid": "975ee42e-3169-4978-92d7-d28e7e2ac014"
|
||||
# }
|
||||
@@ -4,6 +4,8 @@ from app.api.services.base_service import BaseService
|
||||
from sqlalchemy import func, desc
|
||||
from app.api.helpers.response_helper import ResponseHelper
|
||||
from app.api.schemas.user import UserSchema
|
||||
from app.api.schemas.subscription_session import SubscriptionSession
|
||||
from marshmallow import ValidationError
|
||||
from app.models import Members, MembersProducts, SubscriptionOptions, SubscriptionOptionsItems
|
||||
from app.extensions import db
|
||||
from app.api.integrations import StripeIntegration
|
||||
@@ -145,3 +147,106 @@ class SubscriptionService(BaseService):
|
||||
# return ['Post Jobs 222', 'advanced instructors search', 'invite candidates', 'post events',
|
||||
# 'Cancel anytime']
|
||||
return res_options_items
|
||||
|
||||
@staticmethod
|
||||
def subscription_session_start(data):
|
||||
try:
|
||||
with db.session.begin():
|
||||
logger.info(f"Incoming MyProduct data ==>>>> {data}")
|
||||
validated_data = SubscriptionService.validate_data(data, SubscriptionSession())
|
||||
token = validated_data.get('token')
|
||||
uid = validated_data.get('uid')
|
||||
customerId = validated_data.get('customerId')
|
||||
priceId = validated_data.get('priceId')
|
||||
member_data = Members.get_member_by_uid(uid)
|
||||
member_id = member_data.id
|
||||
|
||||
stripe_session = StripeIntegration.create_checkout_session_subscription(priceId, customerId)
|
||||
logger.info(f"Inside Stripe_Session ===== : {stripe_session}")
|
||||
#
|
||||
# product_id = validated_data.get('product_id')
|
||||
# product_subscription_uid=''
|
||||
# product_subscription_external_url = ''
|
||||
# product_subscription_internal_url = ''
|
||||
# product_data = Products.get_product_by_product_id(product_id)
|
||||
# product_description = ProductsDetails.get_product_details_with_product_id('A000002')
|
||||
# productDataStatus = product_data.status
|
||||
#
|
||||
# memberSubscription = MembersProducts.get_member_product_by_product_member_id(member_id, product_id)
|
||||
# if memberSubscription is not None:
|
||||
# logger.info(f"Incoming MyProduct data ==>>>> {memberSubscription}")
|
||||
# productDataStatus = memberSubscription.status
|
||||
# product_subscription_uid = memberSubscription.uid
|
||||
# product_subscription_external_url = memberSubscription.external_url
|
||||
# product_subscription_internal_url = memberSubscription.internal_url
|
||||
|
||||
|
||||
# "banner": "banner.jpg",
|
||||
# myproduct_data = {
|
||||
# "myproudct": {
|
||||
# "banner": product_data.banner,
|
||||
# "description": product_description.details,
|
||||
# "sale_text" : product_description.sale_text,
|
||||
# "internal_url": product_subscription_internal_url,
|
||||
# "external_url": product_subscription_external_url,
|
||||
# "price_text": "90 days free and 3.95/Month",
|
||||
# "product_id": product_data.product_id,
|
||||
# "product_name": product_data.name,
|
||||
# "product_subscription_uid": product_subscription_uid,
|
||||
# "product_uid": product_data.uid,
|
||||
# "promotion_text": "Start Free Today !",
|
||||
# "subscription_uid": product_subscription_uid,
|
||||
# "status": productDataStatus,
|
||||
# "subscription_text": "Start with your goals in mind and then work possible.ith yand Goals. If the plan doesn\u2019t support the vision then change it!",
|
||||
# "title": "Your personal professional web presence"
|
||||
# }
|
||||
# }
|
||||
|
||||
# Simulate processing
|
||||
response_data = {
|
||||
"stripe_session":stripe_session.url,
|
||||
"member_id": member_id,
|
||||
"uid": uid,
|
||||
}
|
||||
|
||||
return ResponseHelper.success(data=response_data)
|
||||
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
db.session.rollback()
|
||||
return ResponseHelper.unprocessable_entity(result_description="Validation exception")
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
db.session.rollback()
|
||||
return ResponseHelper.error(result_description=str(err))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
db.session.rollback()
|
||||
return ResponseHelper.internal_server_error()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# items_data = SubscriptionOptionsItems.get_subscription_item(option_name)
|
||||
# res_options_items = []
|
||||
# for t in items_data:
|
||||
# res_options_items.append({
|
||||
# 'description': t.description
|
||||
# })
|
||||
#
|
||||
# # return ['Post Jobs 222', 'advanced instructors search', 'invite candidates', 'post events',
|
||||
# # 'Cancel anytime']
|
||||
# return res_options_items
|
||||
|
||||
# {
|
||||
# "priceId": "price_1RzLIMLjZLojw6IZ95Eijmdd",
|
||||
# "customerId": "cus_StebwHVaA1nUjC",
|
||||
# "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjpbNTFdLCJtZW1iZXJfaWQiOls1MV0sInVpZCI6WyI5NzVlZTQyZS0zMTY5LTQ5NzgtOTJkNy1kMjhlN2UyYWMwMTQiXX0sImV4cCI6MTc1NjE3ODYwM30.rC8KGz7M8PjgMBu-Vh9YcEWgcFp_WDqJUvTEa_OUU8Y",
|
||||
# "uid": "975ee42e-3169-4978-92d7-d28e7e2ac014"
|
||||
# }
|
||||
|
||||
Reference in New Issue
Block a user