Skip to content

Instantly share code, notes, and snippets.

@radzhome
Created February 24, 2017 00:21
Show Gist options
  • Save radzhome/4801824b6351f0424a7073e6f341ba9a to your computer and use it in GitHub Desktop.
Save radzhome/4801824b6351f0424a7073e6f341ba9a to your computer and use it in GitHub Desktop.
orbital mocker flask
def orbital_mocker_api():
request_data = request.data
logging.debug("Orbital Mock API Request headers are: {}".format(request.headers))
logging.info("Orbital Mock got {} Request: {}".format(request.method, request_data))
data = '' # Response value
response_type = ''
if '<Request><Profile>' in request_data:
response_type = 'Profile'
# Customer profile
"""@ivar request_data:
<?xml version="1.0" encoding="UTF-8"?><Request><Profile><OrbitalConnectionUsername>MOCK123
</OrbitalConnectionUsername><OrbitalConnectionPassword>SOMEPASS123</OrbitalConnectionPassword>
<CustomerBin>000002</CustomerBin><CustomerMerchantID>700000005894</CustomerMerchantID>
<CustomerName>John Smith</CustomerName><CustomerRefNum>1</CustomerRefNum>
<CustomerAddress1>160 Elgin St #2600</CustomerAddress1><CustomerAddress2>Central</CustomerAddress2>
<CustomerCity>Ottawa</CustomerCity><CustomerState>ON</CustomerState><CustomerEmail>[email protected]
</CustomerEmail><CustomerCountryCode>CA</CustomerCountryCode>
<CustomerProfileAction>C</CustomerProfileAction>
<CustomerProfileOrderOverrideInd>NO</CustomerProfileOrderOverrideInd>
<CustomerProfileFromOrderInd>S</CustomerProfileFromOrderInd>
<CustomerAccountType>CC</CustomerAccountType><Status>A</Status>
<CCAccountNum>4011361100000012</CCAccountNum><CCExpireDate>0620</CCExpireDate></Profile></Request>"""
if '<CustomerRefNum>1</CustomerRefNum' in request_data: # Existing customer id 1
if '<CustomerEmail>[email protected]</CustomerEmail>' in request_data: # Treat as existing profile customer
status = 9582
message = "Profile: Cannot Create profile. Profile already exists for Cust Ref Num: [1] and " \
"MID: [700000005894]. "
profile_status = ''
else:
status = 0 # Success
message = ''
profile_status = '<Status>U</Status>'
else: # New customer id
if '<CustomerEmail>[email protected]</CustomerEmail>' in request_data: # Customer should not exist
status = 111
profile_status = ''
message = ''
else:
status = 0 # Success
message = ""
profile_status = '<Status>A</Status>'
data = \
'<?xml version="1.0" encoding="UTF-8"?><Response><ProfileResp><CustomerBin></CustomerBin>' \
'<CustomerMerchantID></CustomerMerchantID><CustomerName></CustomerName><CustomerRefNum>' \
'</CustomerRefNum><CustomerProfileAction>' \
'</CustomerProfileAction><ProfileProcStatus>{status}</ProfileProcStatus><CustomerProfileMessage>' \
'{message}' \
'</CustomerProfileMessage><CustomerAddress1></CustomerAddress1><CustomerAddress2></CustomerAddress2>' \
'<CustomerCity></CustomerCity><CustomerState></CustomerState><CustomerZIP></CustomerZIP><CustomerEmail>' \
'</CustomerEmail><CustomerPhone></CustomerPhone><CustomerProfileOrderOverrideInd>' \
'</CustomerProfileOrderOverrideInd><OrderDefaultDescription></OrderDefaultDescription>' \
'<OrderDefaultAmount></OrderDefaultAmount><CustomerAccountType></CustomerAccountType>' \
'{profile_status}' \
'<CCAccountNum></CCAccountNum><CCExpireDate></CCExpireDate><ECPAccountDDA></ECPAccountDDA>' \
'<ECPAccountType></ECPAccountType><ECPAccountRT></ECPAccountRT><ECPBankPmtDlv></ECPBankPmtDlv>' \
'<SwitchSoloStartDate></SwitchSoloStartDate><SwitchSoloIssueNum></SwitchSoloIssueNum><RespTime>' \
'</RespTime></ProfileResp></Response>'.format(status=status,
message=message,
profile_status=profile_status)
elif '<Request><NewOrder>' in request_data:
response_type = 'NewOrder'
# Purchase, Pre-Auth
"""@ivar request_data:
<?xml version="1.0" encoding="UTF-8"?><Request><NewOrder><OrbitalConnectionUsername>MOCK123
</OrbitalConnectionUsername><OrbitalConnectionPassword>XCHANGEPA55</OrbitalConnectionPassword>
<IndustryType>EC</IndustryType><MessageType>A</MessageType><BIN>000002</BIN><MerchantID>700000005894
</MerchantID><TerminalID>001</TerminalID><AccountNum>5500000000000004</AccountNum><Exp>0125</Exp>
<CurrencyCode>840</CurrencyCode><CurrencyExponent>2</CurrencyExponent><CardSecVal>111</CardSecVal>
<AVSzip>M5V2C3</AVSzip><AVSaddress1>30 Duncan St #602</AVSaddress1><AVSaddress2>New Toronto</AVSaddress2>
<AVScity>Toronto</AVScity><AVSstate>ON</AVSstate><AVSphoneNum>+1(416)446-342</AVSphoneNum>
<AVSname>Tyrion Lannister</AVSname><AVScountryCode>CA</AVScountryCode><OrderID>798D77A8D23211E6B48B08</OrderID>
<Amount>10000</Amount><CustomerEmail>[email protected]</CustomerEmail></NewOrder></Request>"""
try:
account_num = re.search('<AccountNum>(.*)</AccountNum>', request_data).group(1)
merchant_id = re.search('<MerchantID>(.*)</MerchantID>', request_data).group(1)
customer_name = re.search('<AVSname>(.*)</AVSname>', request_data).group(1).upper()
except AttributeError:
# Most likely a refund request, use some default fillers since mocker is stateless
account_num = '5105105105105100'
merchant_id = '700000005894'
customer_name = ''
# Get amount
try:
amount = re.search('<Amount>(.*)</Amount>', request_data).group(1)
except AttributeError:
amount = None
# Get message type, A is Auth, R is Refund
try:
message_type = re.search('<MessageType>(.*)</MessageType>', request_data).group(1)
except AttributeError:
message_type = None
if amount == '2200' and message_type == 'R': # Special fail value for refund
data = '<?xml version="1.0" encoding="UTF-8"?><Response><QuickResp><ProcStatus>9807</ProcStatus>' \
'<StatusMsg>Refund Transactions By TxRefNum Amount Must Be Less Then Or Equal To ' \
'Original Transaction Amount.</StatusMsg></QuickResp></Response>'
else:
customer_ref_num = '5CCB0808D22611E6BBFE08' if 'CustomerRefNum' in request_data else ''
tx_ref_num = '586C5F491A9CF85616EEB6ABC017EE8228D15368'
auth_code = '198453'
resp_code = '99' if amount == '2200' else '00'
try:
order_id = re.search('<OrderID>(.*)</OrderID>', request_data).group(1)
except AttributeError:
order_id = ''
data = \
'<?xml version="1.0" encoding="UTF-8"?><Response><NewOrderResp><IndustryType></IndustryType>' \
'<MessageType>A</MessageType><MerchantID>{merchant_id}</MerchantID><TerminalID>001</TerminalID>' \
'<CardBrand>MC</CardBrand><AccountNum>{account_num}</AccountNum><OrderID>{order_id}' \
'</OrderID><TxRefNum>{tx_ref_num}</TxRefNum><TxRefIdx>0</TxRefIdx>' \
'<ProcStatus>0</ProcStatus><ApprovalStatus>1</ApprovalStatus><RespCode>{resp_code}</RespCode>' \
'<AVSRespCode>7 </AVSRespCode><CVV2RespCode>M</CVV2RespCode><AuthCode>{auth_code}</AuthCode>' \
'<RecurringAdviceCd></RecurringAdviceCd><CAVVRespCode></CAVVRespCode><StatusMsg>Approved</StatusMsg>' \
'<RespMsg></RespMsg><HostRespCode>00</HostRespCode><HostAVSRespCode>U</HostAVSRespCode>' \
'<HostCVV2RespCode>M</HostCVV2RespCode><CustomerRefNum>{customer_ref_num}</CustomerRefNum>' \
'<CustomerName>{customer_name}</CustomerName><ProfileProcStatus>0</ProfileProcStatus>' \
'<CustomerProfileMessage>Profile Created</CustomerProfileMessage><RespTime>213449</RespTime>' \
'<PartialAuthOccurred></PartialAuthOccurred><RequestedAmount></RequestedAmount><RedeemedAmount>' \
'</RedeemedAmount><RemainingBalance></RemainingBalance><CountryFraudFilterStatus>' \
'</CountryFraudFilterStatus><IsoCountryCode></IsoCountryCode></NewOrderResp>' \
'</Response>'.format(account_num=account_num,
merchant_id=merchant_id,
tx_ref_num=tx_ref_num,
customer_name=customer_name,
auth_code=auth_code,
resp_code=resp_code,
customer_ref_num=customer_ref_num,
order_id=order_id)
elif '<Request><MarkForCapture>' in request_data:
response_type = 'MarkForCapture'
# Capture a pre-auth
"""@ivar request_data:
<?xml version="1.0" encoding="UTF-8"?><Response><MarkForCaptureResp><MerchantID>700000005894</MerchantID>
<TerminalID>001</TerminalID><OrderID>66BF20E0D23211E69AB008</OrderID><TxRefNum>
586C76A56E51F1F02E92010C6065EF5DF1F45395</TxRefNum><TxRefIdx>1</TxRefIdx><Amount>10000</Amount>
<ProcStatus>0</ProcStatus><StatusMsg></StatusMsg><RespTime>231430</RespTime><ApprovalStatus>1</ApprovalStatus>
<RespCode>00</RespCode><AVSRespCode>7 </AVSRespCode><AuthCode>199069</AuthCode><RespMsg></RespMsg>
<HostRespCode>00</HostRespCode><HostAVSRespCode>U </HostAVSRespCode></MarkForCaptureResp></Response>"""
amount = re.search('<Amount>(.*)</Amount>', request_data).group(1)
host_resp_code = resp_code = '00'
avs_resp_code = 'F '
if amount == '2200':
# Simulate a re-capture when sending this amount
data = '<?xml version="1.0" encoding="UTF-8"?><Response><QuickResp>' \
'<TxRefNum>586C67E0E86405964D5ACF8116B0FA51770A53CE</TxRefNum>' \
'<TxRefIdx>0</TxRefIdx><ProcStatus>882</ProcStatus><StatusMsg>' \
'This transaction is locked down. You cannot mark or unmark it.</StatusMsg>' \
'<ApprovalStatus>2</ApprovalStatus></QuickResp></Response>'
else:
try:
order_id = re.search('<OrderID>(.*)</OrderID>', request_data).group(1)
except AttributeError:
order_id = ''
data = \
'<?xml version="1.0" encoding="UTF-8"?><Response><MarkForCaptureResp><MerchantID>700000005894' \
'</MerchantID><TerminalID>001</TerminalID><OrderID>{order_id}</OrderID>' \
'<TxRefNum>586C641A9C3FA1580330DB9961DD09B0C2EC53B5</TxRefNum><TxRefIdx>1</TxRefIdx>' \
'<Amount>{amount}</Amount><ProcStatus>0</ProcStatus><StatusMsg></StatusMsg>' \
'<RespTime>215522</RespTime>' \
'<ApprovalStatus>1</ApprovalStatus><RespCode>{resp_code}</RespCode>' \
'<AVSRespCode>{avs_resp_code}</AVSRespCode>' \
'<AuthCode>198490</AuthCode><RespMsg></RespMsg><HostRespCode>{host_resp_code}</HostRespCode>' \
'<HostAVSRespCode>A </HostAVSRespCode></MarkForCaptureResp>' \
'</Response>'.format(amount=amount,
host_resp_code=host_resp_code,
resp_code=resp_code,
avs_resp_code=avs_resp_code,
order_id=order_id)
elif '<Request><Reversal>' in request_data:
response_type = 'Reversal'
# Void a transaction
"""@ivar request_data:
<?xml version="1.0" encoding="UTF-8"?><Request><Reversal><OrbitalConnectionUsername>MOCK123
</OrbitalConnectionUsername><OrbitalConnectionPassword>XCHANGEPA55</OrbitalConnectionPassword>
<TxRefNum>586C76C5D4E0FA503CA254E383B99D0213B45307</TxRefNum><AdjustedAmt>10000</AdjustedAmt>
<OrderID>798D77A8D23211E6B48B08</OrderID><BIN>000002</BIN><MerchantID>700000005894</MerchantID>
<TerminalID>001</TerminalID></Reversal></Request>"""
adjusted_amount = re.search('<AdjustedAmt>(.*)</AdjustedAmt>', request_data).group(1)
if adjusted_amount == '9000':
outstanding_amt = '1000'
else:
outstanding_amt = '0'
if adjusted_amount == '2200': # Special value to return failed void
data = '<?xml version="1.0" encoding="UTF-8"?><Response><QuickResp>' \
'<TxRefNum>586D30BB62731341BEAE4CCDBD45C09AF07D537A</TxRefNum><TxRefIdx>-1</TxRefIdx>' \
'<ProcStatus>882</ProcStatus>' \
'<StatusMsg>This transaction is locked down. You cannot mark or unmark it.</StatusMsg>' \
'<ApprovalStatus>2</ApprovalStatus></QuickResp></Response>'
else:
try:
order_id = re.search('<OrderID>(.*)</OrderID>', request_data).group(1)
except AttributeError:
order_id = ''
data = '<?xml version="1.0" encoding="UTF-8"?><Response><ReversalResp><MerchantID>700000005894' \
'</MerchantID><TerminalID>001</TerminalID><OrderID>{order_id}</OrderID>' \
'<TxRefNum>586C76C5D4E0FA503CA254E383B99D0213B45307</TxRefNum><TxRefIdx>1</TxRefIdx>' \
'<OutstandingAmt>{outstanding_amt}</OutstandingAmt>' \
'<ProcStatus>0</ProcStatus><StatusMsg></StatusMsg>' \
'<RespTime>01032017231501</RespTime></ReversalResp>' \
'</Response>'.format(outstanding_amt=outstanding_amt,
order_id=order_id)
logging.info("Orbital Mock API '{}' Response: {}".format(response_type, data))
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment