-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchargebee_payment.py
100 lines (78 loc) · 2.81 KB
/
chargebee_payment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import chargebee
from dotenv import load_dotenv
load_dotenv()
CHARGEBEE_SITE = os.getenv('CHARGEBEE_SITE') # Here you puy your Chargebee site identifier
CHARGEBEE_API_KEY = os.getenv('CHARGEBEE_API_KEY')
chargebee.configure(CHARGEBEE_API_KEY, CHARGEBEE_SITE)
def create_chargebee_subscription(customer_id, plan_id):
"""
Create a subscription in Chargebee for a customer.
Args:
customer_id (str): The unique customer ID.
plan_id (str): The ID of the plan to subscribe the customer to.
Returns:
dict: Chargebee subscription response.
"""
try:
result = chargebee.Subscription.create({
"plan_id": plan_id,
"customer": {
"id": customer_id
}
})
print(f"Subscription created successfully: {result['subscription']['id']}")
return result
except chargebee.APIError as e:
print(f"Error creating subscription: {e}")
return None
# Example usage (create a new subscription)
if __name__ == "__main__":
subscription = create_chargebee_subscription("customer_12345", "premium_plan")
print(subscription)
def create_chargebee_customer(email, first_name, last_name):
"""
Create a new customer in Chargebee.
Args:
email (str): The customer's email address.
first_name (str): Customer's first name.
last_name (str): Customer's last name.
Returns:
dict: Chargebee customer response.
"""
try:
result = chargebee.Customer.create({
"email": email,
"first_name": first_name,
"last_name": last_name
})
print(f"Customer created successfully: {result['customer']['id']}")
return result
except chargebee.APIError as e:
print(f"Error creating customer: {e}")
return None
# Example usage (create a new customer)
if __name__ == "__main__":
customer = create_chargebee_customer("[email protected]", "John", "Doe")
print(customer)
def cancel_chargebee_subscription(subscription_id):
"""
Cancel a subscription in Chargebee.
Args:
subscription_id (str): The ID of the subscription to cancel.
Returns:
dict: Chargebee subscription cancellation response.
"""
try:
result = chargebee.Subscription.cancel(subscription_id, {
"end_of_term": True
})
print(f"Subscription canceled: {result['subscription']['id']}")
return result
except chargebee.APIError as e:
print(f"Error canceling subscription: {e}")
return None
# Example usage (cancel a subscription)
if __name__ == "__main__":
cancellation = cancel_chargebee_subscription("sub_12345")
print(cancellation)