Cheat Sheet w/Sample Code - Run a Visa Direct Transaction using Python

Visa Employee

Watch the recording of my How to Run a Visa Direct Transaction using Python webinar as you follow along with this cheat sheet, including helloword Sample Code for Two-Way SSL & Sample Code for Running a Simple Transaction.  

 

 

Create a Visa Direct Project on Visa Developer

 

 

Python Sample Code for Two-way (Mutual) SSL

 

 

 

import requests
import logging
import unittest
import datetime
import json


#DEBUG = False
DEBUG = True


'''
helloworld
Sample Code for API Key – Two-Way (Mutual) SSL

https://developer.visa.com/
https://developer.visa.com/vdpguide#get-started-overview
https://developer.visa.com/pages/working-with-visa-apis
https://developer.visa.com/pages/working-with-visa-apis/two-way-ssl

'''

# 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 Two-Way (Mutual) SSL")

print(datetime.datetime.now())
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")

url = 'https://sandbox.api.visa.com/vdp/helloworld'
headers = {}
body = {}

payload = json.loads('''{}
''')


# THIS IS EXAMPLE ONLY how will user_id and password look like
# user_id = '1WM2TT4IHPXC8DQ5I3CH21n1rEBGK-Eyv_oLdzE2VZpDqRn_U'
# password = '19JRVdej9'

user_id = 'YOUR_USER_ID'
password = 'YOUR_PASSWORD'

# THIS IS EXAMPLE ONLY how will cert and key look like
# cert = 'cert.pem'
# key = 'key_83d11ea6-a22d-4e52-b310-e0558816727d.pem'

cert = 'ABSOLUTE_PATH_TO_CERTIFICATE'
key = 'ABSOLUTE_PATH_TO_PRIVATE_KEY'


timeout = 10

try:
    response = requests.get( url,
                  #verify = ('put the CA certificate pem file path here'),
                  cert = (cert, key),
                  #headers = headers,
                  auth = (user_id, password),
                  #data = body,
                  #json = payload,
                  timeout = timeout
    #if DEBUG: print (response.text)
    )
except Exception as e:
    print str(e)

if DEBUG: print (response.headers)
if DEBUG: print (response.content)
if DEBUG: print (response.text.encode('utf8'))

var1 = str(response.status_code)
var2 = '200'
msg = " Two-Way (Mutual) SSL test failed"
assert var1 == var2, msg


# Extrract timestamp and message
print (response.json());
r = response.json()

timestamp = r["timestamp"]
print("timestamp", timestamp)
print("date", date)

message = r["message"]
print("message", message)

print ("END Sample Code for Two-Way (Mutual) SSL\n\n")

 

 

 

 

 

 

Postman Example

Code Generation

 

  • The underlined part is NOT going to be generated by Postman and needs to be added manually

 

import requests

 

url = "https://sandbox.api.visa.com/vdp/helloworld"

 

payload = {}

 

  • # headers is an Example and it will be generated by Postman using your UserID and Password

 

headers = {

  'Authorization': 'Basic TU40MUFRVjNCYNWDlOPlVLOEc2TjUyMWtNTFdCd0MTYQ1U3eUJCMTVKWnpJTm44NE5INDo1c3VBdzcweDVCSkDP',

  'Content-Type': 'application/json'

}

 

cert = ('/path/to/cert.pem', '/path/to/key.pem');

 

response = requests.request("GET", url, headers=headers, data = payload, cert=cert)

 

print(response.text.encode('utf8'))

 

Running a Simple Transaction using Two-way (Mutual) SSL

 

 

fundstransfer.png

 

 

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

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.

 

 

Sample Code for Running a Simple Transaction Using Two-way (Mutual) SSL

 

 

 

 

import requests
import logging
import unittest
import base64
import json
import sys
import os
import datetime


#DEBUG = False
DEBUG = True

'''
Visa Direct
Running a simple transaction using Two-Way (Mutual) SSL

https://developer.visa.com/capabilities/visa_direct/reference#visa_direct__funds_transfer__v1__pushfunds

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)
url = 'https://sandbox.api.visa.com/visadirect/fundstransfer/v1/pushfundstransactions'

# THIS IS EXAMPLE ONLY how will user_id and password look like
# user_id = '1WM2TT4IHPXC8DQ5I3CH21n1rEBGK-Eyv_oLdzE2VZpDqRn_U'
# password = '19JRVdej9'

user_id = 'YOUR_USER_ID'
password = 'YOUR_PASSWORD'

# THIS IS EXAMPLE ONLY how will cert and key look like
# cert = 'cert.pem'
# key = 'key_83d11ea6-a22d-4e52-b310-e0558816727d.pem'

cert = 'ABSOLUTE_PATH_TO_CERTIFICATE'
key = 'ABSOULUTE_PATH_TO_PRIVATE_KEY'


headers = { "content-type": "application/json",
            'accept': 'application/json'
 }

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")

 

 

 

 

 

blogs

Recent Blogs