1+ import requests
2+
3+
4+ class StreamingService :
5+ def __init__ (self ):
6+ self .subscriptions = {'basic' : 5 , 'standard' : 10 , 'premium' : 15 } # Define subscription types and their costs
7+ self .payment_methods = ['credit_card' , 'debit_card' , 'third_part' ] # Define supported payment methods
8+ self .subscriptions_status = {} # Store subscription status
9+
10+ def purchase_subscription (self , subscription_type , payment_method , payment_amount ):
11+ if subscription_type not in self .subscriptions .keys (): # Check if subscription type is supported
12+ raise ValueError ('Unsupported subscription' )
13+
14+ if payment_method == 'credit_card' :
15+ bank_payment_system = BankPaymentSystem (card_number = '1234567890' , username = 'user123' , password = 'password123' )
16+ bank_payment_system .process_card_payment (payment_amount ) # Process payment using bank payment system
17+ elif payment_method == 'third_part' :
18+ third_part_payment_system = ThirdPartPaymentSystem (api_key = 'api_key123' )
19+ third_part_payment_system .process_payment (payment_amount ) # Process payment using third-party payment system
20+ else :
21+ raise ValueError ('Unsupported payment method' ) # Raise error for unsupported payment method
22+
23+ subscription_cost = self .subscriptions [subscription_type ] # Get subscription cost for the given type
24+ self .subscriptions_status [subscription_type ] = payment_amount // subscription_cost # Update subscription status
25+ return self .subscriptions_status [subscription_type ] # Return updated subscription status
26+
27+ def check_subscription_status (self , subscription_type ):
28+ if subscription_type not in self .subscriptions .keys (): # Check if subscription type is supported
29+ raise ValueError ('Unsupported subscription' )
30+ if subscription_type not in self .subscriptions_status .keys (): # Check if subscription status is available
31+ raise ValueError ('Subscription not found' )
32+ return self .subscriptions_status [subscription_type ] # Return subscription status
33+
34+
35+ class BankPaymentSystem :
36+ def __init__ (self , card_number , username , password ):
37+ self .card_number = card_number
38+ self .username = username
39+ self .password = password
40+
41+ def __authenticate_user (self ):
42+ # Authenticates the user with the bank API
43+ payload = {
44+ 'username' : self .username ,
45+ 'password' : self .password
46+ }
47+ response = requests .post ('https://bank-api.com/authenticate_user' , json = payload )
48+ if response .status_code == 200 :
49+ auth_token = response .json ().get ('auth_token' )
50+ return auth_token
51+ else :
52+ raise Exception ('User authentication error' )
53+
54+ def __check_card_balance (self , amount ):
55+ # Checks if the card has sufficient balance for the payment
56+ auth_token = self .__authenticate_user ()
57+ response = requests .get (f'https://bank-api.com/check_balance?card_number={ self .card_number } ' ,
58+ headers = {'Authorization' : f'Bearer { auth_token } ' })
59+ if response .status_code == 200 :
60+ card_balance = response .json ().get ('balance' , 0 )
61+ return card_balance >= amount
62+ else :
63+ raise Exception ('Error when checking card balance' )
64+
65+ def __debit_card (self , amount , auth_token ):
66+ # Debits the card with the payment amount
67+ payload = {
68+ 'card_number' : self .card_number ,
69+ 'amount' : amount
70+ }
71+ response = requests .post ('https://bank-api.com/debit_card' ,
72+ json = payload ,
73+ headers = {'Authorization' : f'Bearer { auth_token } ' })
74+ if response .status_code != 200 :
75+ raise Exception ('Error when debiting funds from the card' )
76+
77+ def process_card_payment (self , amount ):
78+ # Processes the payment using a bank card
79+ auth_token = self .__authenticate_user ()
80+ if self .__check_card_balance (amount ):
81+ try :
82+ self .__debit_card (amount , auth_token )
83+ print (f'Payment for the amount { amount } $ using a bank card was successful' )
84+ except Exception as e :
85+ print (f'Error when debiting funds from the card: { e } ' )
86+ else :
87+ raise ValueError ('Insufficient funds on the card to complete the payment' )
88+
89+
90+ class ThirdPartPaymentSystem :
91+ def __init__ (self , api_key ):
92+ self .api_key = api_key
93+
94+ # Private method to authenticate the API key
95+ def __authenticate_api_key (self ):
96+ response = requests .post ('https://third-part-payment.com/authenticate_api_key' ,
97+ json = {'api_key' : self .api_key })
98+ if response .status_code == 200 :
99+ auth_token = response .json ().get ('auth_token' )
100+ return auth_token
101+ else :
102+ raise Exception ('Error authenticating API key' )
103+
104+ # Private method to process the payment
105+ def __process_payment (self , amount ):
106+ auth_token = self .__authenticate_api_key ()
107+ payload = {
108+ 'amount' : amount ,
109+ 'api_key' : self .api_key
110+ }
111+ # Make a post request to the payment processing API with the payment details and the authorization token
112+ response = requests .post ('https://third-part-payment.com/process_payment' ,
113+ json = payload ,
114+ headers = {'Authorization' : f'Bearer { auth_token } ' })
115+ # If the payment is successful, print a success message
116+ if response .status_code == 200 :
117+ print (f'Payment for the amount { amount } $ using a third-party service was successful' )
118+ # Otherwise, raise an exception
119+ else :
120+ raise Exception ('Error making payment through a third party service' )
121+
122+ # Public method to process the payment with error handling
123+ def process_payment (self , amount ):
124+ try :
125+ self .__process_payment (amount )
126+ # Catch any exceptions that may occur during payment processing and print an error message
127+ except Exception as e :
128+ print (f'Error making payment via third party service: { e } ' )
0 commit comments