1. Install Libraries
The Python backend uses the requests library to send API requests. Download the module using the PIP package manager:
pip install requests
Import the requests
library and the requests.HTTPBasicAuth
module.
The code example is written in Python. It describes the steps required to perform authentication with the CCBill API and the payment token obtained using the CCBill API Payment Widget to perform the charge by payment token action (non-3DSecure and 3DSecure versions). This script covers the backend portion of the integration with the CCBill's RESTful Transaction API
The Python backend uses the requests library to send API requests. Download the module using the PIP package manager:
pip install requests
Import the requests
library and the requests.HTTPBasicAuth
module.
The endpoints section defines the API endpoints you will use throughout the process. The example uses three endpoints to demonstrate the charge flow:
1. token_endpoint
allows requesting the CCBill Auth (Bearer) token required for authentication in subsequent operations.
2. non_threeds_transaction_endpoint
allows performing a non-3DSecured charge transaction using the payment token.
3. threeds_transaction_endpoint
allows performing a 3DSecured charge transaction using the payment token and previously obtained SCA parameters.
Set your Merchant Application ID as the value of client_id
and the Merchant Secret as the value of client_secret
.
The example code will set the accessToken
value when the script executes.
Provide the appropriate data for the request. To create a charge, the following parameters are obtained using the payment widget:
1. The payment_token
previously obtained using the payment widget.
Ensure the client's IP address is added as a payload (ipAddress
) or header (X-Origin-IP) parameter.
2. transaction_data
, including the client account number, subaccount, the initial price, and period.
3. If using 3DS, provide the threeds_parameters
previously obtained using the payment widget.
Create a function to fetch the OAuth token (get_oauth_token
). The function requires the client ID, client secret, and token endpoint to request the token.
The token helps authenticate the payment requests.
The charge_transaction
function performs the charge based on the provided endpoint, data, payment token, and generated access token.
If calling the function throws no errors, the response is passed to the handle_response
function.
In this case, it only logs the response data. In a realistic scenario, it would allow you to continue processing the transaction and return the response to your client.
The code fetches the OAuth token based on the previously provided data.
If the access token is generated successfully, the code performs a non-3DS transaction and then a 3DSecure transaction using the previously obtained SCA and other transaction data.
# import libraries
import requests
from requests.auth import HTTPBasicAuth
# endpoints base_url =
"https://api.ccbill.com"
token_endpoint = f"{base_url}/ccbill-auth/oauth/token"
non_threeds_transaction_endpoint
= f"{base_url}/transactions/payment-tokens/"
threeds_transaction_endpoint =
f"{non_threeds_transaction_endpoint}threeds/"
# client credentials
client_id = "YOUR-CLIENT-ID"
client_secret = "YOUR-CLIENT-SECRET"
access_token = "" # the access
token will be obtained during script execution
# requests data
payment_token =
"YOUR-PAYMENT-TOKEN";
transaction_data = {
"clientAccnum": 900000,
"clientSubacc": 0,
"initialPrice": 0,
"initialPeriod": 0
}
threeds_parameters = {
"threedsEci": "00",
"threedsError": "",
"threedsStatus": "Y",
"threedsClientTransactionId": "mcn-id-h76oy394utw",
"threedsAcsTransId": "d6f15aae-2c9d-4333-a920-954be07c0c76",
"threedsDsTransId": "d65e93c3-35ab-41ba-b307-767bfc19eae3",
"threedsCurrency": "978",
"threedsAmount": "10",
"threedsCardToken": "01ae5d142g7efb4b",
"threedsVersion": "2.1.0",
"threedsCavv": "",
"threedsXid": "",
"threedsSuccess": "true",
"threedsAuthenticationType": "01",
"threedsAuthenticationValue": "5VdhGOTXBJw9+kEBOTtaJiLUAr8="
}
def
get_oauth_token(token_endpoint, client_id, client_secret):
data = {
"grant_type": "client_credentials"
}
response = requests.post(
token_endpoint,
data=data,
auth=HTTPBasicAuth(client_id, client_secret)
)
if response.status_code == 200:
return response.json().get("access_token")
else:
print("Token request failed with status code:", response.status_code)
print("Response content:", response.content)
return None
def
charge_transaction(endpoint, access_token, payment_token,
transaction_data):
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.post(
f"{endpoint}{payment_token}",
json=transaction_data,
headers=headers
)
handle_response(response)
def
handle_response(response):
if response.status_code == 200:
print("Response: ", response.json())
else:
print("Error: ", response.content)
return None
if __name__ == "__main__":
# get access token
access_token = get_oauth_token(token_endpoint, client_id,
client_secret)
if access_token:
# charge regular transaction
charge_transaction(
non_threeds_transaction_endpoint,
access_token,
payment_token,
transaction_data
)
# charge threeds verified transaction
charge_transaction(threeds_transaction_endpoint,
access_token,
payment_token,
{**transaction_data, **threeds_parameters}
)