Skip to content

Commit e4360a7

Browse files
committed
add files
1 parent 886a914 commit e4360a7

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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}')

test 11 subscription/Lib/__init__.py

Whitespace-only changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import unittest
2+
from unittest.mock import patch
3+
from Lib.Subscription import *
4+
5+
6+
class TestStreamingService(unittest.TestCase):
7+
def setUp(self):
8+
self.streaming_service = StreamingService()
9+
10+
def test_purchase_subscription_with_credit_card(self):
11+
payment_amount = 10
12+
subscription_type = 'basic'
13+
payment_method = 'credit_card'
14+
expected_status = payment_amount // self.streaming_service.subscriptions[subscription_type]
15+
16+
# Patch the 'process_card_payment' method of the BankPaymentSystem
17+
with patch.object(BankPaymentSystem, 'process_card_payment') as mock_process_card_payment:
18+
# Call the method being tested
19+
self.assertEqual(self.streaming_service.purchase_subscription(subscription_type, payment_method, payment_amount), expected_status)
20+
# Assert that the 'process_card_payment' method was called once with the expected payment amount
21+
mock_process_card_payment.assert_called_once_with(payment_amount)
22+
23+
def test_purchase_subscription_with_third_part(self):
24+
payment_amount = 45
25+
subscription_type = 'premium'
26+
payment_method = 'third_part'
27+
expected_status = payment_amount // self.streaming_service.subscriptions[subscription_type]
28+
29+
# Patch the 'process_payment' method of the ThirdPartPaymentSystem
30+
with patch.object(ThirdPartPaymentSystem, 'process_payment') as mock_process_payment:
31+
# Call the method being tested
32+
self.assertEqual(self.streaming_service.purchase_subscription(subscription_type, payment_method, payment_amount), expected_status)
33+
# Assert that the 'process_payment' method was called once with the expected payment amount
34+
mock_process_payment.assert_called_once_with(payment_amount)
35+
36+
def test_purchase_subscription_with_wrong_payment_method(self):
37+
payment_amount = 10
38+
subscription_type = 'standard'
39+
payment_method = 'invalid_method'
40+
41+
# Assert that calling the method with an invalid payment method raises a ValueError
42+
with self.assertRaises(ValueError):
43+
self.streaming_service.purchase_subscription(subscription_type, payment_method, payment_amount)
44+
45+
def test_purchase_subscription_with_wrong_subscription_type(self):
46+
payment_amount = 10
47+
subscription_type = 'invalid_subscription_type'
48+
payment_method = 'credit_card'
49+
50+
# Assert that calling the method with an invalid subscription type raises a ValueError
51+
with self.assertRaises(ValueError):
52+
self.streaming_service.purchase_subscription(subscription_type, payment_method, payment_amount)
53+
54+
def test_check_subscription_status(self):
55+
subscription_type = 'standard'
56+
expected_status = 2
57+
self.streaming_service.subscriptions_status[subscription_type] = expected_status
58+
59+
# Call the method being tested and assert that the returned status matches the expected status
60+
self.assertEqual(self.streaming_service.check_subscription_status(subscription_type), expected_status)
61+
62+
def test_check_subscription_status_with_invalid_subscription_type(self):
63+
subscription_type = 'invalid_subscription_type'
64+
65+
# Assert that calling the method with an invalid subscription type raises a ValueError
66+
with self.assertRaises(ValueError):
67+
self.streaming_service.check_subscription_status(subscription_type)
68+
69+
def test_check_subscription_status_with_subscription_not_found(self):
70+
subscription_type = 'premium'
71+
72+
# Assert that calling the method with a subscription type that is not found raises a ValueError
73+
with self.assertRaises(ValueError):
74+
self.streaming_service.check_subscription_status(subscription_type)
75+
76+
77+
if __name__ == '__main__':
78+
unittest.main()

0 commit comments

Comments
 (0)