GatePay API官方Python语言客户端代码库。
- GatePay Python SDK适用于Python 3.7及以上版本。
- 在开始使用GatePay SDK之前需要提前去GatePay注册中心注册账号,申请secretKey,密钥信息请妥善保管,如果遗失可能会造成非法用户使用此信息操作您的资源,给您造成数据和财产损失。
- 接口SDK
- 客户端GatePayClient,支持通过HTTP签名发送请求和接收应答。
pip3 install gatepay-sdk-python
-
下载地址: todo
-
您也可以使用以下命令获取安装包,代码会被下载到您本地的目录中。
git clone https://github.com/gatepay2025/gatepay-sdk-python
cd gatepay-sdk-python
pip3 install -r requirements.txt
- Python 3.7+
业务侧调用SDK主要分为如下步骤:
-
引用GatePay Python版本SDK
-
创建GatePayConfig
-
创建GatePayClient
-
设置业务请求参数
-
调用SDK API接口
-
处理得到响应
import time
from src.gatepay.gatepay_config import GatePayConfig
from src.gatepay.infrastructure.credential import Credential
from src.gatepay.client.gatepay_client import GatePayClient
from src.gatepay.api.model.req.env_req import EnvReq
from src.gatepay.api.model.req.goods_req import GoodsReq
from src.gatepay.api.model.req.address.create_order_req import CreateOrderReq as AddressCreateOrderReq
from src.gatepay.common.utils.random_utils import RandomUtils
# env_address 终端地址,不同环境请根据实际情况填写
# client_id 客户端ID
# secret_key 密钥
# api_key 商户api密钥
gate_pay_config = GatePayConfig("env_address", 30, "client_id", Credential("secret_key", "api_key")
)
gatepay_client = GatePayClient(gate_pay_config)
env_req = EnvReq()
env_req.set_terminal_type("MINIAPP")
# 创建商品请求对象
goods_req = GoodsReq()
goods_req.set_goods_name("test")
goods_req.set_goods_detail("testDetail")
# 创建地址订单请求对象
create_order_req = AddressCreateOrderReq()
create_order_req.set_merchant_trade_no(RandomUtils.generate_nonce(24))
create_order_req.set_currency("USDT")
create_order_req.set_order_amount("9.9")
create_order_req.set_env(env_req)
create_order_req.set_goods(goods_req)
create_order_req.set_order_expire_time(int(time.time() * 1000) + 3 * 60 * 60 * 1000)
create_order_req.set_return_url("https://www.gate.com/")
create_order_req.set_cancel_url("https://www.gate.com/")
# your merchantUserId
create_order_req.set_merchant_user_id(6790011)
create_order_req.set_chain("ETH")
create_order_req.set_full_curr_type("USDT_ETH")
create_order_req.set_channel_id("")
print("merchantOrderNo:" + create_order_req.get_merchant_trade_no())
response = create_order_resp = gatepay_client.create_address_order(create_order_req)
print(create_order_resp.get_data().__str__())
# 处理响应
print(f"Status: {response.get_status()}")
print(f"Code: {response.get_code()}")
GatePay SDK包含以下主要API模块:
- ApiAddress: 地址支付接口
- ApiPayment: 普通支付接口
- ApiCheckout: 收银台支付接口
- ApiQrCode: 二维码支付接口
- ApiBill: 账单接口
- ApiChannelManage: 渠道管理接口
- ApiConvert: 闪兑接口
- ApiGift: 礼品卡接口
- ApiWithdraw: 提现接口
SDK会抛出异常来处理错误情况:
try:
self._pre_process(req)
http_response = self._do_process(req)
# 这里将用后置处理器转为snake
to_snake_str = CamelAndSnakeUtils.convert_camel_json_to_snake(http_response.text)
print(to_snake_str)
return self._post_process(to_snake_str, resp_class)
except Exception as e:
raise RuntimeError(str(e)) from e
- 请妥善保管您的secretKey和apiKey
- 不要在客户端代码中硬编码密钥信息
- 使用环境变量或配置文件来管理密钥
- 定期更换密钥以提高安全性
如有技术问题,请联系GatePay技术支持团队:
- 邮箱: support@gatepay.com
- 官方文档: https://docs.gatepay.com
请关注GitHub仓库获取最新版本更新和功能增强。
本SDK遵循MIT许可证协议。