/cybersource-rest-samples-python

Sample code to demonstrate using the CyberSource REST APIs with the Python client library.

Primary LanguagePythonMIT LicenseMIT

Python Sample Code for the CyberSource SDK

Build Status

This repository contains working code samples which demonstrate python integration with the CyberSource REST APIs through the CyberSource Python SDK.

Using the Sample Code

The samples are all completely independent and self-contained. You can analyze them to get an understanding of how a particular method works, or you can use the snippets as a starting point for your own project. The samples are organized into categories and common usage examples, similar to the CyberSource API Reference.

You can run each sample directly from the command line.

Requirements

Running the Samples From the Command Line

  • Clone this repository:

    git clone https://github.com/CyberSource/cybersource-rest-samples-python.git
  • Install the CyberSource Python SDK:

    pip install cybersource-rest-client-python
  • Set the environment variable PYTHONPATH to the base directory of the Sample Codes.

    On Linux, run the following:

    export PYTHONPATH=$(pwd):$PYTHONPATH

    On Windows, run the following:

    set PYTHONPATH=%cd%
  • Install the Sample Codes and its dependencies (required for running Authentication samples only)

    pip install -e .
    pip install requests munch
  • Run the individual samples by name. For example:

    python [DirectoryPath]\[CodeSampleName]

    e.g.

    python samples\Payments\Payments\simple-authorizationinternet.py

Setting your own API credentials for an API Request

Configure the following information in data/configuration.py file:

  • Http Signature

    self.authentication_type      = "http_signature"
    self.merchantid               = "Your Merchant ID"
    self.merchant_keyid           = "Your key id"
    self.merchant_secretkey       = "Your secret key"
  • Jwt

    self.authentication_type      = "jwt"
    self.merchantid               = "Your Merchant ID"
    self.key_alias                = "Your key alias"
    self.key_pass                 = "Your key password"
    self.key_file_name            = "Your key filename"
    self.keys_directory           = os.getcwd()+"\\resources\\"
  • MetaKey Http

    self.authentication_type      = "http_Signature"
    self.merchantid               = "your_child_merchant_id"
    self.merchant_keyid           = "your_metakey_serial_number"
    self.merchant_secretkey       = "your_metakey_shared_secret"
    self.portfolio_id             = "your_portfolio_id"
    self.use_metakey              = true
  • MetaKey JWT

    self.authentication_type      = "jwt"
    self.merchantid               = "your_child_merchant_id"
    self.key_alias                = "your_child_merchant_id"
    self.key_pass                 = "your_portfolio_id"
    self.key_file_name            = "your_portfolio_id"
    self.keys_directory           = os.getcwd()+"\\resources\\"
    self.use_metakey              = true

Switching between the sandbox environment and the production environment

CyberSource maintains a complete sandbox environment for testing and development purposes. This sandbox environment is an exact duplicate of our production environment with the transaction authorization and settlement process simulated. By default, this SDK is configured to communicate with the sandbox environment. To switch to the production environment, set the appropriate environment constant in data/Configuration.py file. For example:

    # For TESTING use
    self.run_environment = "apitest.cybersource.com"
    # For PRODUCTION use
    # self.run_environment = "api.cybersource.com"

API Reference

The API Reference Guide provides examples of what information is needed for a particular request and how that information would be formatted. Using those examples, you can easily determine what methods would be necessary to include that information in a request using this SDK.

Logging

Generic badge

Since v0.0.31, a new logging framework has been introduced in the SDK. This new logging framework makes use of Python's native logging, and standardizes the logging so that it can be integrated with the logging in the client application.

More information about this new logging framework can be found in this file : Logging.md

Run Environments

The run environments that were supported in CyberSource Python SDK have been deprecated. Moving forward, the SDKs will only support the direct hostname as the run environment.

For the old run environments previously used, they should be replaced by the following hostnames:

Old Run Environment New Hostname Value
cybersource.environment.sandbox apitest.cybersource.com
cybersource.environment.production api.cybersource.com
cybersource.in.environment.sandbox apitest.cybersource.com
cybesource.in.environment.production api.in.cybersource.com

For example, replace the following code in the Configuration file:

    # For TESTING use
    self.run_environment = "cybersource.environment.sandbox"
    # For PRODUCTION use
    # self.run_environment = "cybersource.environment.production"

with the following code:

    # For TESTING use
    self.run_environment = "apitest.cybersource.com"
    # For PRODUCTION use
    # self.run_environment = "api.cybersource.com"