Files
dpl/scripts/fuc-xb-acceptance/cashier-api.py
T

32 lines
2.2 KiB
Python

import pathlib,json,urllib.request,urllib.parse,urllib.error,ssl,socket,ipaddress,re
from html.parser import HTMLParser
root=pathlib.Path('/var/www/wwwroot/fuc-xb-test/private/live-acceptance-20261104')
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self,*a,**kw):return None
class Title(HTMLParser):
def __init__(self):super().__init__();self.on=False;self.parts=[]
def handle_starttag(self,t,a):
if t=='title':self.on=True
def handle_endtag(self,t):
if t=='title':self.on=False
def handle_data(self,s):
if self.on:self.parts.append(s.strip())
op=urllib.request.build_opener(urllib.request.HTTPSHandler(context=ssl.create_default_context()),NoRedirect());out=[]
for kind in ['alipay','wxpay']:
v=json.loads((root/('mapi-'+kind+'.response')).read_text());assert v.get('code')==1 and v.get('payurl')
url=v['payurl'];chain=[];body=b''
for hop in range(4):
p=urllib.parse.urlsplit(url);assert p.scheme=='https' and p.hostname and not p.username and not p.password and (p.port is None or p.port==443)
assert p.hostname not in ['fuc-xb-test.gemcook.net','fuc-xb-test.tomtojapan.online'],'Do not follow merchant return'
ips={x[4][0] for x in socket.getaddrinfo(p.hostname,443,type=socket.SOCK_STREAM)};assert ips and all(ipaddress.ip_address(ip).is_global for ip in ips)
req=urllib.request.Request(url,headers={'User-Agent':'Mozilla/5.0','Referer':'https://pay.airspay.top/','Accept':'text/html'})
try:res=op.open(req,timeout=25)
except urllib.error.HTTPError as e:res=e
with res:
code=res.status;body=res.read(2097152);location=res.headers.get('Location');chain.append({'host':p.hostname,'status':code})
if code in [301,302,303,307,308] and location:url=urllib.parse.urljoin(url,location);continue
break
(root/('mapi-cashier-'+kind+'.html')).write_bytes(body);parser=Title();parser.feed(body.decode(errors='replace'));title=' '.join(parser.parts)
out.append({'type':kind,'chain':chain,'final_status':code,'title':re.sub(r'[A-Za-z0-9_-]{8,}','<id>',title)[:100],'response_bytes':len(body),'has_qr_markup':bool(re.search(b'qrcode|qr-code|qr_code|erweima',body,re.I)),'real_money_paid':0})
print(json.dumps({'unpaid_cashier_pages_only':True,'cases':out},ensure_ascii=False))