Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC implemented code #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions data/Configuration.py → data/input_configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from CyberSource.configuration import Configuration


class Configuration:
class InputConfiguration:
def __init__(self):
self.authentication_type ="http_signature"
self.merchantid = "testrest"
Expand Down Expand Up @@ -46,4 +46,16 @@ def get_configuration(self):
configuration_dictionary["log_directory"] = self.log_directory
#configuration_dictionary["proxy_address"] = self.proxy_address
#configuration_dictionary["proxy_port"] = self.proxy_port
#self.set_config()
return configuration_dictionary

'''def set_config(self):
#logger_file = self.log_directory
config = Configuration()
# config_logger.proxy = True
# http = urllib3.ProxyManager('https://userproxy.visa.com:443', maxsize=1)
# config.proxy = 'http://userproxy.visa.com:80'
config.debug = self.enable_log
#config.logger_file = logger_file
# config_logger.logger_file_handler = 'Logs'
# config_logger.logger_stream_handler = sys.stdout'''
30 changes: 22 additions & 8 deletions samples/flex/coreservices/generate_key.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
from CyberSource import *
import json
import os
import inspect
from importlib.machinery import SourceFileLoader

config_file = os.path.join(os.getcwd(), "data", "Configuration.py")
config_file = os.path.join(os.getcwd(), "data", "input_configuration.py")
configuration = SourceFileLoader("module.name", config_file).load_module()


def generate_key():
try:
print("\n[BEGIN] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)
# Reading Merchant details from Configuration file
config_obj = configuration.Configuration()
config_obj = configuration.InputConfiguration()
details_dict1 = config_obj.get_configuration()
key_generation_obj = KeyGenerationApi(details_dict1)
# Setting the json message body
key_generation = GeneratePublicKeyRequest()
key_generation.encryption_type = "RsaOaep256"
key_generation = GeneratePublicKeyRequest(encryption_type="RsaOaep256")
#key_generation.encryption_type = "RsaOaep256"
message_body = json.dumps(key_generation.__dict__)
return_data, status, body = key_generation_obj.generate_public_key(generate_public_key_request=message_body)
print("API RESPONSE CODE : ",status)
print("API RESPONSE BODY : ",body)
# Calling api_client variable in Configuration file
config = Configuration()
print("\nAPI REQUEST BODY: ",
config.api_client.masking(json.dumps(config.api_client.replace_underscore(json.loads(message_body)))))
response_data = key_generation_obj.generate_public_key(generate_public_key_request=message_body)
# Calling api_client variable in Configuration file to access the request_headers
request_headers = config.api_client.request_headers

# Statements to print on console
print("\nAPI REQUEST HEADERS: ", request_headers)
print("\nAPI RESPONSE CODE : ", response_data.status)
print("\nAPI RESPONSE BODY : ", response_data.data)
print("\nAPI RESPONSE HEADERS: ", response_data.getheaders())
except Exception as e:
print("Exception when calling KeyGenerationApi->generate_public_key: %s\n" % e)
print("\nException when calling KeyGenerationApi->generate_public_key: %s\n" % e)
finally:
print("\n[END] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)


if __name__ == "__main__":
Expand Down
32 changes: 24 additions & 8 deletions samples/flex/coreservices/tokenize_card.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from CyberSource import *
import json
import os
import inspect
from importlib.machinery import SourceFileLoader

config_file = os.path.join(os.getcwd(), "data", "Configuration.py")
config_file = os.path.join(os.getcwd(), "data", "input_configuration.py")
configuration = SourceFileLoader("module.name", config_file).load_module()

# Following code is to handle the Sibling Package Import
Expand All @@ -14,16 +15,18 @@
path.append(dir(path[0]))
__package__ = "coreservices"
import keygeneration_noenc
import flex_signature_verification


def tokenize_card():
try:
print("\n[BEGIN] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)
# Setting the json message body
tokenize_card = TokenizeRequest()
card_info = Flexv1tokensCardInfo()
# Getting the key_id dynamically
api_response = keygeneration_noenc.keygeneration_noenc()
tokenize_card.key_id = api_response.key_id
tokenize_card.key_id = api_response['keyId']

card_info.card_expiration_year = "2031"
card_info.card_number = "5555555555554444"
Expand All @@ -32,15 +35,28 @@ def tokenize_card():
tokenize_card.card_info = card_info.__dict__
message_body = json.dumps(tokenize_card.__dict__)
# Reading Merchant details from Configuration file
config_obj = configuration.Configuration()
config_obj = configuration.InputConfiguration()
details_dict1 = config_obj.get_configuration()
tokenize_obj = FlexTokenApi(details_dict1)
return_data, status, body = tokenize_obj.tokenize(tokenize_request=message_body)
print("API RESPONSE CODE : ", status)
print("API RESPONSE BODY : ", body)

# Calling api_client variable in Configuration file
config = Configuration()
print("\nAPI REQUEST BODY: ",
config.api_client.masking(json.dumps(config.api_client.replace_underscore(json.loads(message_body)))))
response_data = tokenize_obj.tokenize(tokenize_request=message_body)
# Calling api_client variable in Configuration file to access the request_headers
request_headers = config.api_client.request_headers

# Statements to print on console
print("\nAPI REQUEST HEADERS: ", request_headers)
print("\nAPI RESPONSE CODE : ", response_data.status)
print("\nAPI RESPONSE BODY : ", response_data.data)
print("\nAPI RESPONSE HEADERS: ", response_data.getheaders())

flex_signature_verification.verify(api_response['der']['publicKey'],json.loads(response_data.data))
except Exception as e:
print("Exception when calling FlexTokenApi->tokenize: %s\n" % e)
print("\nException when calling FlexTokenApi->tokenize: %s\n" % e)
finally:
print("\n[END] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)


if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion samples/flex/flex_signature_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def verify(key, postparams):
raise Flex_Security_Exception("Missing required field: signedFields")

signed_field = (signed_fields.split(","))


for i in signed_field:
signed_list.append(",")
signed_list.append(str(postparams.get("" + i)))


signed_string = "".join(signed_list)
signed_string = signed_string[1:]

print("Flex Signature Verification: ",rsa_verify.verify_sign(key, signature, signed_string))
31 changes: 22 additions & 9 deletions samples/flex/keygeneration_noenc.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
from CyberSource import *
import json
import os
import inspect
from importlib.machinery import SourceFileLoader

config_file = os.path.join(os.getcwd(), "data", "Configuration.py")
config_file = os.path.join(os.getcwd(), "data", "input_configuration.py")
configuration = SourceFileLoader("module.name", config_file).load_module()


def keygeneration_noenc():
try:
print("\n[BEGIN] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)
# Reading Merchant details from Configuration file
config_obj = configuration.Configuration()
config_obj = configuration.InputConfiguration()
details_dict1 = config_obj.get_configuration()
key_generation_obj = KeyGenerationApi(details_dict1)
# Setting the json message body
key_generation = GeneratePublicKeyRequest()
key_generation.encryption_type = "None"
key_generation = GeneratePublicKeyRequest(encryption_type='None')
#key_generation.encryption_type = "None"

message_body = json.dumps(key_generation.__dict__)
# Calling api_client variable in Configuration file
config = Configuration()
print("\nAPI REQUEST BODY: ",
config.api_client.masking(json.dumps(config.api_client.replace_underscore(json.loads(message_body)))))
response_data = key_generation_obj.generate_public_key(generate_public_key_request=message_body)
# Calling api_client variable in Configuration file to access the request_headers
request_headers = config.api_client.request_headers

return_data, status, body = key_generation_obj.generate_public_key(generate_public_key_request=message_body)
print("API RESPONSE CODE : ", status)
print("API RESPONSE BODY : ", body)
return return_data
# Statements to print on console
print("\nAPI REQUEST HEADERS: ", request_headers)
print("\nAPI RESPONSE CODE : ", response_data.status)
print("\nAPI RESPONSE BODY : ", response_data.data)
print("\nAPI RESPONSE HEADERS: ", response_data.getheaders())
return json.loads(response_data.data)
except Exception as e:
print("Exception when calling KeyGenerationApi->generate_public_key: %s\n" % e)
print("\nException when calling KeyGenerationApi->generate_public_key: %s\n" % e)
finally:
print("\n[END] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)


if __name__ == "__main__":
Expand Down
9 changes: 5 additions & 4 deletions samples/flex/rsa_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from base64 import b64decode



def verify_sign(public_key_loc, signature, data):
"""
Verifies with a public key from whom the data came that it was indeed
Expand All @@ -12,15 +13,15 @@ def verify_sign(public_key_loc, signature, data):
param: signature String signature to be verified
return: Boolean. True if the signature is valid; False otherwise.
"""
pub_key = "-----BEGIN PUBLIC KEY-----\n" + public_key_loc + "\n-----END PUBLIC KEY-----"
pub_key = b64decode(public_key_loc )

rsakey = RSA.importKey(pub_key)
signer = PKCS1_v1_5.new(rsakey)
digest = SHA512.new()
# Assumes the data is base64 encoded to begin with
digest.update(b64decode(data))

digest.update(data.encode('utf-8'))

if signer.verify(digest, b64decode(signature)):
return True
else:

return False
31 changes: 23 additions & 8 deletions samples/payments/coreservices/capture_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
import process_payment
import json
import os
import inspect
from importlib.machinery import SourceFileLoader

config_file = os.path.join(os.getcwd(), "data", "Configuration.py")
config_file = os.path.join(os.getcwd(), "data", "input_configuration.py")
configuration = SourceFileLoader("module.name", config_file).load_module()


def capture_a_payment():

try:
print("\n[BEGIN] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)
# Getting the payment_id dynamically using process_a_payment method
api_payment_response = process_payment.process_a_payment(
False)
payment_id = api_payment_response.id
payment_id = api_payment_response['id']
# Setting the json message body
request = CapturePaymentRequest()
client_reference = Ptsv2paymentsClientReferenceInformation()
Expand All @@ -28,16 +31,28 @@ def capture_a_payment():
request.order_information = order_information.__dict__

message_body = (json.dumps(request.__dict__))

# Reading Merchant details from Configuration file
config_obj = configuration.Configuration()
config_obj = configuration.InputConfiguration()
details_dict1 = config_obj.get_configuration()
capture_obj = CaptureApi(details_dict1)
return_data, status, body = capture_obj.capture_payment(message_body, payment_id)
print("API RESPONSE CODE : ", status)
print("API RESPONSE BODY : ", body)
return return_data
# Calling api_client variable in Configuration file
config = Configuration()
print("\nAPI REQUEST BODY: ",
config.api_client.masking(json.dumps(config.api_client.replace_underscore(json.loads(message_body)))))
response_data = capture_obj.capture_payment(message_body, payment_id)
request_headers = config.api_client.request_headers
# Statements to print on console
print("\nAPI REQUEST HEADERS: ", request_headers)
print("\nAPI RESPONSE CODE : ", response_data.status)
print("\nAPI RESPONSE BODY : ", response_data.data)
print("\nAPI RESPONSE HEADERS: ", response_data.getheaders())
return json.loads(response_data.data)
except Exception as e:
print("Exception when calling CaptureApi->capture_payment: %s\n" % e)
print("\nException when calling CaptureApi->capture_payment: %s\n" % e)
finally:
print("\n[END] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)



if __name__ == "__main__":
Expand Down
27 changes: 20 additions & 7 deletions samples/payments/coreservices/process_authorization_reversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
import process_payment
import json
import os
import inspect
from importlib.machinery import SourceFileLoader

config_file = os.path.join(os.getcwd(), "data", "Configuration.py")
config_file = os.path.join(os.getcwd(), "data", "input_configuration.py")
configuration = SourceFileLoader("module.name", config_file).load_module()


def process_an_authorization_reversal():
try:
print("\n[BEGIN] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)
# Getting the payment_id dynamically using process_a_payment method
api_payment_response = process_payment.process_a_payment(False)
payment_id = api_payment_response.id
payment_id = api_payment_response['id']
# Setting the json message body
request = AuthReversalRequest()
client_reference = Ptsv2paymentsClientReferenceInformation()
Expand All @@ -27,14 +29,25 @@ def process_an_authorization_reversal():
request.reversal_information = reversal_information.__dict__
message_body = json.dumps(request.__dict__)
# Reading Merchant details from Configuration file
config_obj = configuration.Configuration()
config_obj = configuration.InputConfiguration()
details_dict1 = config_obj.get_configuration()
reversal_obj = ReversalApi(details_dict1)
return_data, status, body = reversal_obj.auth_reversal(payment_id, message_body)
print("API RESPONSE CODE : ", status)
print("API RESPONSE BODY : ", body)
# Calling api_client variable in Configuration file
config = Configuration()
print("\nAPI REQUEST BODY: ",
config.api_client.masking(json.dumps(config.api_client.replace_underscore(json.loads(message_body)))))
response_data= reversal_obj.auth_reversal(payment_id, message_body)
# Calling api_client variable in Configuration file
request_headers = config.api_client.request_headers
# Statements to print on console
print("\nAPI REQUEST HEADERS: ", request_headers)
print("\nAPI RESPONSE CODE : ", response_data.status)
print("\nAPI RESPONSE BODY : ", response_data.data)
print("\nAPI RESPONSE HEADERS: ", response_data.getheaders())
except Exception as e:
print("Exception when calling ReversalApi->auth_reversal: %s\n" % e)
print("\nException when calling ReversalApi->auth_reversal: %s\n" % e)
finally:
print("\n[END] EXECUTION OF SAMPLE CODE:" + inspect.currentframe().f_code.co_name)


if __name__ == "__main__":
Expand Down
Loading