open-withdrawal-demo/python/demo.py

86 lines
2.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""代付 Demobalance / submit / query"""
from __future__ import annotations
import json
import os
import sys
import time
import urllib.request
from sign import build_sign
BASE_URL = os.environ.get("BASE_URL", "https://example.com").rstrip("/")
ACCESS_KEY = os.environ.get("ACCESS_KEY", "your_access_key")
SECRET_KEY = os.environ.get("SECRET_KEY", "your_secret_key")
PAY_CHANNEL_ID = os.environ.get("PAY_CHANNEL_ID", "821")
def post(path: str, body: dict) -> dict:
body = dict(body)
body["Sign"] = build_sign(body, SECRET_KEY)
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
req = urllib.request.Request(
BASE_URL + path,
data=data,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=30) as resp:
return json.loads(resp.read().decode("utf-8"))
def cmd_balance() -> None:
print(json.dumps(post("/api/apiv1/open/withdrawal/querybalance", {
"Timestamp": int(time.time()),
"AccessKey": ACCESS_KEY,
}), ensure_ascii=False, indent=2))
def cmd_submit() -> None:
order_no = 'DEMO' + time.strftime('%Y%m%d%H%M%S') + f'{__import__("random").randint(0,9999):04d}'
print(json.dumps(post("/api/apiv1/open/withdrawal/submit", {
"Timestamp": int(time.time()),
"AccessKey": ACCESS_KEY,
"PayChannelId": PAY_CHANNEL_ID,
"OrderNo": order_no,
"Amount": "100.01",
"CallbackUrl": "https://xxxxx.com/xxxx/callback",
"Ext": "-",
"Payee": "张三",
"PayeeNo": "6235753100001261596",
"PayeeAddress": "招商银行",
}), ensure_ascii=False, indent=2))
def cmd_query(order_no: str) -> None:
print(json.dumps(post("/api/apiv1/open/withdrawal/queryorder", {
"Timestamp": int(time.time()),
"AccessKey": ACCESS_KEY,
"OrderNo": order_no,
}), ensure_ascii=False, indent=2))
def main() -> None:
if len(sys.argv) < 2:
print("usage: demo.py balance|submit|query <OrderNo>")
sys.exit(1)
cmd = sys.argv[1]
if cmd == "balance":
cmd_balance()
elif cmd == "submit":
cmd_submit()
elif cmd == "query":
if len(sys.argv) < 3:
print("usage: demo.py query <OrderNo>")
sys.exit(1)
cmd_query(sys.argv[2])
else:
print("unknown command:", cmd)
sys.exit(1)
if __name__ == "__main__":
main()