forked from ruofeixu/mugglepay-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmugglepay.py
176 lines (162 loc) · 4.85 KB
/
mugglepay.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'''
Author: ruofei xu
Date: 2021-03-01 14:01:04
LastEditTime: 2021-03-02 09:38:15
LastEditors: Please set LastEditors
Description: mugglepay sdk for payment
FilePath: /mugglepay-python-sdk/mugglepay.py
'''
import requests
class MugglepayClient:
def __init__(
self,
api_key: str,
api_url: str = "https://api.mugglepay.com/v1",
):
if api_key == "":
raise ValueError('application key is not valid')
self.api_key = api_key
self.api_url = api_url
# create mugglepay order
def create_order(
self,
merchat_order_id: str,
price_amount: float,
price_currency: str,
pay_currency: str,
title: str,
description: str,
callback_url: str,
cancel_url: str,
success_url: str,
mobile: bool,
fast: bool,
token: str
):
if merchat_order_id == "":
raise ValueError('merchant order id is not valid')
url = '{}/orders'.format(self.api_url)
headers = {'Content-Type': 'application/json', 'token': self.api_key}
data = {
"merchat_order_id": merchat_order_id,
"price_amount": price_amount,
"price_currency": price_currency,
"pay_currency": pay_currency,
"title": title,
"description": description,
"callback_url": callback_url,
"cancel_url": cancel_url,
"success_url": success_url,
"mobile": mobile,
"fast": fast,
"token": token
}
r = requests.post(
url = url,
headers = headers,
json = data
)
try:
return r.json()
except Exception:
return {
'status': r.status_code
}
# get mugglepay order
def get_order(
self,
order_id: str
):
if order_id == "":
raise ValueError('order id is not valid')
url = '{}/orders/{}'.format(self.api_url, order_id)
headers = {'Content-Type': 'application/json', 'token': self.api_key}
data = {
"order_id": order_id
}
r = requests.get(
url=url,
headers=headers
)
try:
return r.json()
except Exception:
return {
'status': r.status_code
}
# get mugglepay orders
def get_orders(
self,
status: str = None,
limit: int = 10,
offset: int = 0,
):
url = '{}/orders'.format(self.api_url)
headers = {'Content-Type': 'application/json', 'token': self.api_key}
params = {
"status": status,
"limit": limit,
"offset": offset
}
r = requests.get(
url=url,
headers=headers,
params=params
)
try:
return r.json()
except Exception:
return {
'status': r.status_code
}
# checkout mugglepay order
def checkout_order(
self,
order_id: str,
pay_currency: str,
):
if order_id == "":
raise ValueError('order id is not valid')
url = '{}/orders/{}/checkout'.format(self.api_url, order_id)
headers = {'Content-Type': 'application/json', 'token': self.api_key}
data = {
"order_id": order_id,
"pay_currency": pay_currency
}
r = requests.post(
url = url,
headers = headers,
json = data
)
try:
return r.json()
except Exception:
return {
'status': r.status_code
}
if __name__ == "__main__":
# test case
API_KEY = 'your api key' # put your api key
mgp = MugglepayClient(api_key=API_KEY)
print('create order test')
res = mgp.create_order(
merchat_order_id='your_order_id',
price_amount=0.01,
price_currency='USD',
pay_currency='CNY',
title='Monthly Program x 2',
description='test python',
callback_url='https://{your_host}/mugglepay_callback_api', # replace {your_host} with your own host where you deployed callback server, example your_host:http://123.123.123.12:2340
cancel_url='https://{your_host}/mugglepay?status=cancel',
success_url='https://{your_host}/mugglepay?status=success',
mobile=False,
fast=False,
token='test12345'
)
print(res)
print('get order test')
print(mgp.get_order(res['order']['order_id']))
print('get orders test')
print(mgp.get_orders('NEW', 2, 0))
print('checkout order test')
print(mgp.checkout_order(res['order']['order_id'], 'USD'))