Repository navigation
Expand file tree
/
Copy pathp2p_merchant_api.py
More file actions
275 lines (246 loc) · 11 KB
/
Copy pathp2p_merchant_api.py
File metadata and controls
275 lines (246 loc) · 11 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
# coding: utf-8
import logging
from gate_api import (
ApiClient, Configuration, P2pApi,
AdsDetailRequest, AdsListRequest, AdsUpdateStatus,
CancelOrder, ConfirmPayment, ConfirmReceipt,
GetChatsListRequest, GetCompletedTransactionListRequest,
GetCounterpartyUserInfoRequest, GetMyselfPaymentRequest,
GetPendingTransactionListRequest, GetTransactionDetailsRequest,
MyAdsListRequest, PlaceBizPushOrder,
PreUploadChatFileRequest, SendChatMessageRequest, SetMerchantWorkHoursRequest,
)
from gate_api.exceptions import ApiException
from config import RunConfig
logger = logging.getLogger(__name__)
def p2p_merchant_demo(run_config):
# type: (RunConfig) -> None
"""
P2P Merchant API demo. Covers current endpoints in p2p-merchant-api.yaml in business order:
Account -> Ads -> Orders (list & detail) -> Order actions -> Chat.
"""
config = Configuration(
key=run_config.api_key,
secret=run_config.api_secret,
host=run_config.host_used,
)
api = P2pApi(ApiClient(config))
try:
# ============================================================
# 1. Account - user info, payment methods, and work hours
# ============================================================
logger.info("=== 1. P2P Merchant Account ===")
user_info = api.p2p_merchant_account_get_user_info()
logger.info("User info: %s", user_info)
myself_payment = api.p2p_merchant_account_get_myself_payment(
get_myself_payment_request=GetMyselfPaymentRequest(fiat="USD")
)
logger.info("Payment methods: %s", myself_payment)
# Set merchant work hours (write op, use with care)
# api.p2p_merchant_account_set_merchant_work_hours(
# SetMerchantWorkHoursRequest(
# work_status=2,
# cycle_type="Weekly",
# day_of_week="1,2,3,4,5",
# time_zone="+8",
# start_time="09:00",
# end_time="18:00",
# )
# )
# ============================================================
# 2. Ads - my ads, market ads, ad detail; place/update (write ops commented)
# ============================================================
logger.info("=== 2. P2P Ads ===")
my_ads = api.p2p_merchant_books_my_ads_list(
my_ads_list_request=MyAdsListRequest(asset="USDT", fiat_unit="USD", trade_type="sell")
)
logger.info("My ads list: %s", my_ads)
ads = api.p2p_merchant_books_ads_list(
AdsListRequest(asset="USDT", fiat_unit="USD", trade_type="sell")
)
logger.info("Market ads list: %s", ads)
if my_ads and getattr(my_ads, "data", None) and getattr(my_ads.data, "lists", None) and my_ads.data.lists:
first_ad_id = getattr(my_ads.data.lists[0], "id", None) or getattr(my_ads.data.lists[0], "orderid", None)
if first_ad_id is not None:
ad_detail = api.p2p_merchant_books_ads_detail(
AdsDetailRequest(adv_no=str(first_ad_id))
)
logger.info("Ad detail: %s", ad_detail)
# Example when no my ads (replace with real adv_no):
# ad_detail = api.p2p_merchant_books_ads_detail(
# AdsDetailRequest(adv_no="2124000001")
# )
# Place/edit ad (write op, use with care).
# Content risk rejection returns code 70305102 with data.risk_event.
# api.p2p_merchant_books_place_biz_push_order(
# PlaceBizPushOrder(
# currency_type="USDT",
# exchange_type="USD",
# type="0",
# unit_price="7.20",
# number="1000",
# limit_basis=1,
# fiat_min_amount="100",
# fiat_max_amount="1000",
# pay_type="bank",
# pay_type_json='{"bank":"10001"}',
# rate_fixed="1",
# polymarket_limit=0,
# expire_min="20",
# trade_tips="Please pay from an account under your own name.",
# auto_reply="Please tap Paid after completing the transfer.",
# )
# )
# Update ad status: 1=active, 3=inactive, 4=closed
# api.p2p_merchant_books_ads_update_status(
# AdsUpdateStatus(adv_no=2124000001, adv_status=3)
# )
# ============================================================
# 3. Orders - pending list, completed list, order detail
# ============================================================
logger.info("=== 3. P2P Orders ===")
pending_list = api.p2p_merchant_transaction_get_pending_transaction_list(
GetPendingTransactionListRequest(
crypto_currency="USDT",
fiat_currency="USD",
order_tab="pending",
select_type="sell",
# status="OPEN",
# txid=40000001,
# start_time=1700000000,
# end_time=1700086399,
)
)
logger.info("Pending orders: %s", pending_list)
completed_list = api.p2p_merchant_transaction_get_completed_transaction_list(
GetCompletedTransactionListRequest(
crypto_currency="USDT",
fiat_currency="USD",
# select_type="sell",
# status="ACCEPT",
# txid=40000001,
# start_time=1700000000,
# end_time=1700086399,
# query_dispute=0,
# page=1,
# per_page=20,
)
)
logger.info("Completed orders: %s", completed_list)
txid_for_detail = None
for resp in (pending_list, completed_list):
if not resp or not getattr(resp, "data", None):
continue
data = resp.data
lst = getattr(data, "list", None) or getattr(data, "lists", None)
if lst and len(lst) > 0:
first = lst[0]
txid_for_detail = getattr(first, "txid", None)
if txid_for_detail is not None:
break
if txid_for_detail is not None:
order_detail = api.p2p_merchant_transaction_get_transaction_details(
GetTransactionDetailsRequest(
txid=txid_for_detail,
# channel="",
)
)
logger.info("Order detail: %s", order_detail)
if order_detail and getattr(order_detail, "data", None):
biz_uid = getattr(order_detail.data, "its_uid", None)
if biz_uid:
counterparty = api.p2p_merchant_account_get_counterparty_user_info(
GetCounterpartyUserInfoRequest(biz_uid=biz_uid)
)
logger.info("Counterparty info: %s", counterparty)
chats = api.p2p_merchant_chat_get_chats_list(
GetChatsListRequest(
txid=txid_for_detail,
# lastreceived=1700000000,
# firstreceived=1700000000,
)
)
logger.info("Chat list: %s", chats)
# Example when no orders (replace with real txid):
# order_detail = api.p2p_merchant_transaction_get_transaction_details(
# GetTransactionDetailsRequest(txid=40000001)
# )
# chats = api.p2p_merchant_chat_get_chats_list(
# GetChatsListRequest(txid=40000001)
# )
# ============================================================
# 4. Order actions - confirm payment, confirm receipt, cancel (write ops, commented)
# ============================================================
logger.info("=== 4. P2P Order actions (examples commented) ===")
# Buyer confirms payment
# api.p2p_merchant_transaction_confirm_payment(
# ConfirmPayment(txid="40000001", payment_method="bank")
# )
# Seller confirms receipt and release
# api.p2p_merchant_transaction_confirm_receipt(
# ConfirmReceipt(txid="40000001")
# )
# Cancel order
# api.p2p_merchant_transaction_cancel(
# CancelOrder(
# txid="40000001",
# reason_id="1",
# reason_memo="Canceled after agreement with the counterparty.",
# )
# )
# ============================================================
# 5. Chat - send message, upload file (write ops, commented)
# ============================================================
logger.info("=== 5. P2P Chat write ops (examples commented) ===")
# Send text message (requires real txid).
# Content risk matches return code=0 with data.risk_type=1 and data.toast_msg.
# api.p2p_merchant_chat_send_chat_message(
# SendChatMessageRequest(
# txid=40000001,
# message="Payment completed, please check.",
# type=0,
# )
# )
# Direct S3 upload and file message (write ops, requires a real txid).
# Install requests before enabling this example: pip install requests
# import requests
#
# content_type = "image/png"
# pre_upload = api.p2p_merchant_chat_pre_upload_chat_file(
# PreUploadChatFileRequest(content_type=content_type)
# )
# if pre_upload.code != 0 or not pre_upload.data:
# raise RuntimeError("Could not get chat file upload credentials")
# upload = pre_upload.data
# if not upload.url or not upload.fields or not upload.file_key:
# raise RuntimeError("Incomplete chat file upload credentials")
#
# # Use credentials within 45 minutes; maximum file size is 100 MiB.
# # Preserve every returned form field, including casing and any STS token.
# # requests encodes data fields before files, placing the file field last.
# with open("receipt.png", "rb") as chat_file:
# s3_response = requests.post(
# upload.url,
# data=upload.fields,
# files={"file": ("receipt.png", chat_file, content_type)},
# timeout=(5, 120),
# allow_redirects=False,
# )
# s3_response.raise_for_status()
# if s3_response.status_code != 204:
# raise RuntimeError("Unexpected S3 upload response")
#
# # Send only after upload succeeds. file_key is already plain text.
# api.p2p_merchant_chat_send_chat_message(
# SendChatMessageRequest(
# txid=40000001,
# message=upload.file_key,
# type=1,
# )
# )
logger.info("=== P2P Merchant demo finished ===")
except ApiException as e:
logger.error("Gate API exception: %s", e)
except Exception as e:
logger.exception("Unexpected exception: %s", e)