import requests
import logging
import unittest
import base64
import json
import sys
import os
import datetime
from dotenv import load_dotenv
load_dotenv()
#DEBUG = False
DEBUG = True
'''
Visa Direct
Running a simple transaction using Two-Way (Mutual) SSL
PushFundsTransactions POST
The PushFundsTransactions resource credits (pushes) funds to a recipient's
Visa account by initiating a financial message called an Original Credit Transaction (OCT).
Funds Transfer API
Latest Version v1
keyboard_arrow_down
The Funds Transfer API can pull funds from the sender's Visa account (in preparation for pushing funds
to a recipient's account) in an Account Funding Transaction (AFT). Additionally, the Funds Transfer
API also provides functionality to push funds to the recipient's Visa account in an Original Credit Transaction (OCT).
If a transaction is declined, the Funds Transfer API can also return the funds to the sender's funding source in an
Account Funding Transaction Reversal (AFTR). The API has been enhanced to allow originators to send their
PushFundsTransactions(OCTs) and PullFundsTransactions(AFTs) to Visa for routing to multiple U.S. debit networks.
'''
# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
print ("START Sample Code for Running a simple transaction using Two-Way (Mutual) SSL")
# PullFundsTransactions POST
# The PullFundsTransactions Resource debits (pulls) funds from a sender's Visa account
# (in preparation for pushing funds to a recipient's account) by initiating a financial message called an
# Account Funding Transaction (AFT)
# THIS IS EXAMPLE ONLY how will user_id and password look like
# user_id = '1WM2TT4IHPXC8DQ5I3CH21n1rEBGK-Eyv_oLdzE2VZpDqRn_U'
# password = '19JRVdej9'
user_id = os.getenv('VISA_USER_ID')
password = os.getenv('VISA_PASSWORD')
# THIS IS EXAMPLE ONLY how will cert and key look like
# cert = 'cert.pem'
# key = 'key_83d11ea6-a22d-4e52-b310-e0558816727d.pem'
cert = os.getenv('VISA_CERT_PATH')
key = os.getenv('VISA_KEY_PATH')
headers = { "content-type": "application/json",
'accept': 'application/json',
'keyid': os.getenv('VISA_KEY_ID'),
}
timeout = 10
# 2020-03-25T19:11:18
#"localTransactionDateTime": "''' + date + '''",
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
print(date)
# in our case we are not running AFT transaction
# We have to delete
# "transactionIdentifier": "381228649430015",
payload = json.loads('''
{
"acquirerCountryCode": "840",
"acquiringBin": "408999",
"amount": "124.05",
"businessApplicationId": "AA",
"cardAcceptor": {
"address": {
"country": "USA",
"county": "San Mateo",
"state": "CA",
"zipCode": "94404"
},
"idCode": "CA-IDCode-77765",
"name": "Visa Inc. USA-Foster City",
"terminalId": "TID-9999"
},
"localTransactionDateTime": "''' + date + '''",
"merchantCategoryCode": "6012",
"pointOfServiceData": {
"motoECIIndicator": "0",
"panEntryMode": "90",
"posConditionCode": "00"
},
"recipientName": "rohan",
"recipientPrimaryAccountNumber": "4957030420210496",
"retrievalReferenceNumber": "412770451018",
"senderAccountNumber": "4653459515756154",
"senderAddress": "901 Metro Center Blvd",
"senderCity": "Foster City",
"senderCountryCode": "124",
"senderName": "Mohammed Qasim",
"senderReference": "",
"senderStateCode": "CA",
"sourceOfFundsCode": "05",
"systemsTraceAuditNumber": "451018",
"transactionCurrencyCode": "USD",
"settlementServiceIndicator": "9",
"colombiaNationalServiceData": {
"countryCodeNationalService": "170",
"nationalReimbursementFee": "20.00",
"nationalNetMiscAmountType": "A",
"nationalNetReimbursementFeeBaseAmount": "20.00",
"nationalNetMiscAmount": "10.00",
"addValueTaxReturn": "10.00",
"taxAmountConsumption": "10.00",
"addValueTaxAmount": "10.00",
"costTransactionIndicator": "0",
"emvTransactionIndicator": "1",
"nationalChargebackReason": "11"
}
}
''')
try:
response = requests.post(url,
cert = (cert, key),
headers = headers,
auth = (user_id, password),
json = payload,
timeout=timeout
#if DEBUG: print (response.text)
)
except Exception as e:
print (e)
if DEBUG: print (response.headers)
if DEBUG: print (response.content)
var1 = str(response.status_code)
var2 = '200'
msg = " Two-Way (Mutual) SSL test failed"
assert var1 == var2, msg
print ("END Sample Code for Running a simple transaction using Two-Way (Mutual) SSL\n\n")