def get_info_for_paypal_txn(tx, reqfn=paypal_request): token = cfg.get_config_value('paypal_token') urlencoded = urllib.urlencode( [('cmd', '_notify-synch'), ('tx', tx), ('at', token)]) result = reqfn( cfg.get_config_value('paypal_domain'), urlencoded) body = result.read() if not body.startswith('SUCCESS\n'): raise PaypalError('%s with %s returned %s' % ( cfg.get_config_value('paypal_domain'), urlencoded, body)) success, rest = body.split('\n', 1) response_args = dict( (key, urllib.unquote_plus(val).decode('utf-8')) for (key, val) in [x.split('=', 1) for x in rest.split('\n') if x.strip()]) return response_args
def get_pro_transaction_details(tx, reqfn=paypal_request): stuff = dict( user=urllib.quote(cfg.get_config_value('paypal_pro_user')), pwd=cfg.get_config_value('paypal_pro_password'), signature=cfg.get_config_value('paypal_pro_signature'), tx=tx ) domain = cfg.get_config_value('paypal_pro_domain') result = reqfn( domain, GET_TRANSACTION_DETAILS_TEMPLATE % stuff, '/nvp') body = result.read() response_args = dict( (key, urllib.unquote_plus(val).decode('utf-8')) for (key, val) in [x.split('=', 1) for x in body.split('&') if x.strip()]) if response_args.get('ACK') == 'Failure': raise PaypalError('%s with %s returned %s' % ( cfg.get_config_value('paypal_domain'), GET_TRANSACTION_DETAILS_TEMPLATE % stuff, body)) for (key, key2) in PAYPAL_SUCKS.items(): response_args[key2] = response_args[key] return response_args
def do_paypal_pro(fields, remote_ip, ipn_url, reqfn=paypal_request): new = fields.copy() new.update( dict( user=cfg.get_config_value('paypal_pro_user'), pwd=cfg.get_config_value('paypal_pro_password'), signature=cfg.get_config_value('paypal_pro_signature'), ipaddress=remote_ip, notifyurl=ipn_url)) if 'middlename' not in new: ## Hack so maguire can leave the middle name out of the form new['middlename'] = '' if 'suffix' not in new: new['suffix'] = '' new = dict((key, urllib.quote(value.encode('utf-8'))) for (key, value) in new.items()) body = TEMPLATE % new domain = cfg.get_config_value('paypal_pro_domain') result = reqfn(domain, body, '/nvp') result = cgi.parse_qs(result.read()) return result
def validate_ipn(post_data, reqfn=paypal_request): """ Returns A 2-tuple of a bool and a 2-tuple or None. The bool will be True when the ipn validates, otherwise it will be false. When the bool is false the second item will be a 2-tuple of request and response """ body = urllib.urlencode(post_data) body += '&cmd=_notify-validate' response = reqfn( cfg.get_config_value('paypal_domain'), body) response_body = response.read() if not (response.status == 200 and response_body == 'VERIFIED'): return False, (body, response_body) return True, None
def main(): # listen on UDP udp_socket = udplog.get_socket() udp_socket.bind(udplog.get_addr()) # connect to scribe host, port = cfg.get_config_value('scribe_local').split(':') socket = TSocket.TSocket(host=host, port=int(port)) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False) client = scribe.Client(iprot=protocol, oprot=protocol) pending_logs = [] while True: try: transport.open() while True: data, addr = udp_socket.recvfrom(4096) pending_logs.append(parse_log(data)) # try to write all our logs to scribe at once. this may # not be great if we've accumulated a bunch of logs in # memory. messages = [scribe.LogEntry(category=category, message=msg) for category, msg in pending_logs] result = client.Log(messages=messages) if result == scribe.ResultCode.OK: pending_logs = [] if len(pending_logs) > 10000: print "Something is very wrong. We've got too many logs built up." print "Just dropping them on the floor." sys.exit(1) except (KeyboardInterrupt, MemoryError, SystemExit): # this will cause us to exit. raise except: print "got exception. still trying" traceback.print_exc() transport.close() # don't loop too tightly time.sleep(1)
def get_addr(): host, port = cfg.get_config_value('udplog').split(':') return host, int(port)