"""Python 3 + requests. Running this script ONLY reads account balance. Set PLUS_API_KEY in your server's secret environment, never in source code. Persist request_id and returned IDs yourself. This client never auto-retries writes. """ import os from urllib.parse import quote, urlsplit import requests class ApiError(RuntimeError): def __init__(self,status,code): self.status,self.code=status,code super().__init__(f'API HTTP {status}: {code}') def business_action(data, *, preflight=False): """Return a UI/workflow action only. This function never sends a request. Match error_code separately for customer-facing distinctions. An action cannot grant payment consent or authorize re-sending a payment confirm. Old responses without evidence fields keep conservative query behavior. """ status=data.get('status') if preflight and data.get('order_id'): return 'wait_result' if status=='succeeded': return 'none' if status=='review': return 'wait_result' if status=='cleanup_pending': return 'wait_cleanup' if status in ('failed','expired','closed') and data.get('cleanup_acknowledged') is not True: return 'wait_cleanup' action=data.get('next_action') if action=='retry_preflight': return action if data.get('retryable') is True and data.get('cleanup_acknowledged') is True else 'contact_support' if action in ('renew_session','cancel_renewal_and_renew_session','wait_subscription_expiry','change_channel', 'wait_cleanup','wait_result','none','wait','contact_support'): return action if preflight and status=='ready': return 'submit_order' # Show confirmation UI; do not submit automatically. if status in ('queued','preparing','paying'): return 'wait' if preflight else 'wait_result' return 'contact_support' class PlusClient: base='https://api.666666.homes' def __init__(self,key): if not key:raise ValueError('API Key required') self.http=requests.Session() self.http.headers.update({'Authorization':'Bearer '+key,'Accept':'application/json'}) def close(self):self.http.close() def request(self,method,path,payload=None): # No redirects, no automatic write retries, no secret payload logging. response=self.http.request(method,self.base+path,json=payload,timeout=(5,20),allow_redirects=False) try:data=response.json() except ValueError:raise ApiError(response.status_code,'non_json_response_query_original_order') from None if not response.ok: raise ApiError(response.status_code,(data.get('error') or {}).get('code','request_failed')) if 300<=response.status_code<400:raise ApiError(response.status_code,'unexpected_redirect') return data def account(self):return self.request('GET','/v1/account') def redeem(self,code):return self.request('POST','/v1/credits/redeem',{'code':code}) def ledger(self,before=None):return self.request('GET','/v1/credits/ledger'+('' if before is None else '?before='+str(int(before)))) def cards(self):return self.request('GET','/v1/cards') def import_cards(self,text,use_limit=5):return self.request('POST','/v1/cards/import',{'text':text,'use_limit':use_limit}) def preflight(self,request_id,session_json_string,card=None,*,card_id=None): if card is not None and card_id is not None:raise ValueError('Choose card or card_id, not both') payload={'request_id':request_id,'session':session_json_string} if card is not None:payload['card']=card if card_id is not None:payload['card_id']=card_id return self.request('POST','/v1/preflights',payload) def get_preflight(self,pid):return self.request('GET','/v1/preflights/'+quote(pid,safe='')) def confirm(self,request_id,pid,*,confirmed=False,allow_fallback=False): if confirmed is not True:raise ValueError('Explicit customer payment consent required') return self.request('POST','/v1/orders',{'request_id':request_id,'preflight_id':pid,'confirmed':True,'allow_fallback':allow_fallback}) def order(self,oid):return self.request('GET','/v1/orders/'+quote(oid,safe='')) def orders(self):return self.request('GET','/v1/orders') def invoice_url(self,oid): r=self.http.get(self.base+'/v1/orders/'+quote(oid,safe='')+'/invoice',timeout=(5,20),allow_redirects=False) if r.status_code!=303:raise ApiError(r.status_code,'invoice_not_ready_or_request_error') location=r.headers.get('Location','');u=urlsplit(location) if u.scheme!='https' or not u.hostname or u.username or u.password:raise ValueError('Invalid invoice redirect') # Do NOT use self.http to download: it contains your API Authorization. return location if __name__=='__main__': client=PlusClient(os.environ.get('PLUS_API_KEY','')) try: account=client.account() print({'available':account['available'],'frozen':account['frozen']}) finally:client.close()