{"id":8993,"date":"2024-06-08T16:18:49","date_gmt":"2024-06-08T16:18:49","guid":{"rendered":"https:\/\/ccbill.com\/doc\/?post_type=tjspw&#038;p=8993"},"modified":"2026-04-08T12:39:55","modified_gmt":"2026-04-08T12:39:55","slug":"python","status":"publish","type":"tjspw","link":"https:\/\/ccbill.com\/doc\/tjspw\/python","title":{"rendered":"Python"},"content":{"rendered":"\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"content\">\n<h2>Python<\/h2>\n\t\t\t<p>\n\t\t\tThe 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\n\t\t\t<\/p>\n            <article>\n                <h3 ><span class=\"dropcap\">1.<\/span> Install Libraries<\/h3>\n                <p> The Python backend uses the requests library to send API requests. Download the module using the PIP package manager:\n                <\/p>\n\t\t\t\t<code class=\"snippet-small\">pip install requests<\/code>\n\t\t\t\t<p class=\"highlight\" data-target=\"install-libraries\">Import the <strong><code>requests<\/code><\/strong> library and the <strong><code>requests.HTTPBasicAuth<\/code><\/strong> module.<\/p>\n            <\/article>\n            <article>\n                <h3><span class=\"dropcap\">2.<\/span> Define Endpoints<\/h3>\n                <p> The endpoints section defines the API endpoints you will use throughout the process. The example uses three endpoints to demonstrate the charge flow:\n                <\/p>\n                <p class=\"highlight\" data-target=\"token-endpoint\">\n                    1. <strong><code>token_endpoint<\/code><\/strong> allows requesting the CCBill Auth (Bearer) token required for authentication in subsequent operations.\n                <\/p>\n\t\t\t\t<p class=\"highlight\" data-target=\"non-threeds-endpoint\">\n                    2. <strong><code>non_threeds_transaction_endpoint<\/code><\/strong> allows performing a non-3DSecured charge transaction using the payment token.\n                <\/p>\n\t\t\t\t<p class=\"highlight\" data-target=\"threeds-endpoint\">\n                    3. <strong><code>threeds_transaction_endpoint<\/code><\/strong> allows performing a 3DSecured charge transaction using the payment token and previously obtained SCA parameters.\n                <\/p>\n            <\/article>\n            <article>\n                <h3><span class=\"dropcap\">3.<\/span> Provide Client Credentials<\/h3>\n\t\t\t\t<p class=\"highlight\" data-target=\"merchant-application\">Set your&nbsp;<strong>Merchant Application ID<\/strong>&nbsp;as the value of&nbsp;<strong><code>client_id<\/code><\/strong> and the&nbsp;<strong>Merchant Secret<\/strong>&nbsp;as the value of&nbsp;<strong><code>client_secret<\/code><\/strong>.<\/p>\n\t\t\t\t<p class=\"highlight\" data-target=\"access-token\">The example code will set the&nbsp;<strong><code>accessToken<\/code><\/strong>&nbsp;value when the script executes.<\/p>\n            <\/article>\n            <article>\n                <h3><span class=\"dropcap\">4.<\/span> Add Data<\/h3>\n                <p>Provide the appropriate data for the request. To create a charge, the following parameters are obtained using the payment widget:\n                <\/p>\n\t\t\t\t<p class=\"highlight\" data-target=\"ipaddress-parameter\">1. The&nbsp;<strong><code>payment_token<\/code><\/strong>&nbsp;previously obtained using the payment widget.<\/p><p class=\"has-text-align-left\">Ensure the client's IP address is added as a payload (<strong><code>ipAddress<\/code><\/strong>) or header (<strong>X-Origin-IP<\/strong>) parameter.<\/p>\n\t\t\t\t<p class=\"highlight\" data-target=\"transaction-data\">2. <strong><code>transaction_data<\/code><\/strong>, including the client account number, subaccount, the initial price, and period.<\/p>\n\t\t\t\t<p class=\"highlight\" data-target=\"threeds-parameters\">3. If using 3DS, provide the <strong><code>threeds_parameters<\/code><\/strong> previously obtained using the payment widget.<\/p>\n            <\/article>\n            <article>\n                <h3><span class=\"dropcap\">5.<\/span> Generate Token<\/h3>\n\t\t\t\t<p class=\"highlight\" data-target=\"oauth-token\">Create a function to fetch the OAuth token (<strong><code>get_oauth_token<\/code><\/strong>). The function requires the client ID, client secret, and token endpoint to request the token.<\/p>\n                <p>The token helps authenticate the payment requests.\n                <\/p>\n            <\/article>\n\t\t\t<article>\n                <h3><span class=\"dropcap\">6.<\/span> Charge Transaction and Handle Response<\/h3>\n\t\t\t\t<p class=\"highlight\" data-target=\"charge-transactions\">The&nbsp;<strong><code>charge_transaction<\/code><\/strong>&nbsp;function performs the charge based on the provided endpoint, data, payment token, and generated access token.<\/p>\n                <p data-target=\"handle-response\">If calling the&nbsp;function throws no errors, the response is passed to the&nbsp;<strong><code>handle_response<\/code><\/strong>&nbsp;function.<\/p>\n\t\t\t\t<p>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.<\/p>\n            <\/article>\n\t\t\t<article>\n                <h3><span class=\"dropcap\">7.<\/span> Test Transaction<\/h3>\n\t\t\t\t<p class=\"highlight\" data-target=\"fetches-oauth-token\">The code fetches the OAuth token based on the previously provided data.<\/p>\n                <p class=\"highlight\" data-target=\"performs-transaction\">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.<\/p>\n            <\/article>\n        <\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"examples\">\n  <div class=\"navigation\">\n    <span class=\"nav-item selected\">backend.py<\/span>\n  <\/div>\n  <div class=\"files\">\n    <div class=\"file file-1 active line-numbers\">\n      <div title=\"install-libraries\">\n        <code class=\"language-python code-block\"># import libraries\n          import requests\n          from requests.auth import HTTPBasicAuth\n        <\/code>\n      <\/div>\n      <code class=\"language-python\"># endpoints\nbase_url = \"https:\/\/api.ccbill.com\"<\/code>\n      <div title=\"token-endpoint\">\n        <code class=\"language-python code-block\">token_endpoint = f\"{base_url}\/ccbill-auth\/oauth\/token\"<\/code>\n      <\/div>\n      <div title=\"non-threeds-endpoint\">\n        <code class=\"language-python code-block\">non_threeds_transaction_endpoint = f\"{base_url}\/transactions\/payment-tokens\/\"<\/code>\n      <\/div>\n      <div title=\"threeds-endpoint\">\n        <code class=\"language-python code-block\">threeds_transaction_endpoint = f\"{non_threeds_transaction_endpoint}threeds\/\"<\/code>\n      <\/div>\n      <code class=\"language-python\"># client credentials<\/code>\n      <div title=\"merchant-application\">\n        <code class=\"language-python code-block\">client_id = \"YOUR-CLIENT-ID\"\nclient_secret = \"YOUR-CLIENT-SECRET\"<\/code>\n      <\/div>\n      <div title=\"access-token\">\n        <code class=\"language-python code-block\">access_token = \"\" # the access token will be obtained during script execution<\/code>\n      <\/div>\n      <code class=\"language-python\"># requests data<\/code>\n      <div title=\"ipaddress-parameter\">\n        <code class=\"language-python code-block\">payment_token = \"YOUR-PAYMENT-TOKEN\";<\/code>\n      <\/div>\n      <div title=\"transaction-data\">\n        <code class=\"language-python code-block\">transaction_data = {\n          \"clientAccnum\": 900000,\n          \"clientSubacc\": 0,\n          \"initialPrice\": 0,\n          \"initialPeriod\": 0\n          }<\/code>\n      <\/div>\n      <div title=\"threeds-parameters\">\n        <code class=\"language-python code-block\">threeds_parameters = {\n          \"threedsEci\": \"00\",\n          \"threedsError\": \"\",\n          \"threedsStatus\": \"Y\",\n          \"threedsClientTransactionId\": \"mcn-id-h76oy394utw\",\n          \"threedsAcsTransId\": \"d6f15aae-2c9d-4333-a920-954be07c0c76\",\n          \"threedsDsTransId\": \"d65e93c3-35ab-41ba-b307-767bfc19eae3\",\n          \"threedsCurrency\": \"978\",\n          \"threedsAmount\": \"10\",\n          \"threedsCardToken\": \"01ae5d142g7efb4b\",\n          \"threedsVersion\": \"2.1.0\",\n          \"threedsCavv\": \"\",\n          \"threedsXid\": \"\",\n          \"threedsSuccess\": \"true\",\n          \"threedsAuthenticationType\": \"01\",\n          \"threedsAuthenticationValue\": \"5VdhGOTXBJw9+kEBOTtaJiLUAr8=\"\n          }<\/code>\n      <\/div>\n      <div title=\"oauth-token\">\n        <code class=\"language-python code-block\">def\n          get_oauth_token(token_endpoint, client_id, client_secret):\n\n          data = {\n          \"grant_type\": \"client_credentials\"\n          }\n\n          response = requests.post(\n          token_endpoint,\n          data=data,\n          auth=HTTPBasicAuth(client_id, client_secret)\n          )\n\n          if response.status_code == 200:\n          return response.json().get(\"access_token\")\n          else:\n          print(\"Token request failed with status code:\", response.status_code)\n          print(\"Response content:\", response.content)\n          return None\n        <\/code>\n      <\/div>\n      <div title=\"charge-transactions\">\n        <code class=\"language-python code-block\" data-section=\"section11\">def\n          charge_transaction(endpoint, access_token, payment_token,\n          transaction_data):\n\n          headers = {\n          \"Authorization\": f\"Bearer {access_token}\"\n          }\n\n          response = requests.post(\n          f\"{endpoint}{payment_token}\",\n          json=transaction_data,\n          headers=headers\n          )\n\n          handle_response(response)\n        <\/code>\n      <\/div>\n      <div title=\"handle-response\">\n        <code class=\"language-python code-block\" data-section=\"section12\">def\n          handle_response(response):\n          if response.status_code == 200:\n          print(\"Response: \", response.json())\n          else:\n          print(\"Error: \", response.content)\n          return None\n        <\/code>\n      <\/div>\n      <code class=\"language-python\">if __name__ == \"__main__\":<\/code>\n      <div title=\"fetches-oauth-token\">\n        <code class=\"language-python code-block\" data-section=\"section13\">\n          # get access token\n          access_token = get_oauth_token(token_endpoint, client_id,\n          client_secret)\n        <\/code>\n      <\/div>\n      <div title=\"performs-transaction\">\n        <code class=\"language-python code-block\">\n          if access_token:\n\n          # charge regular transaction\n          charge_transaction(\n          non_threeds_transaction_endpoint,\n          access_token,\n          payment_token,\n          transaction_data\n          )\n\n          # charge threeds verified transaction\n          charge_transaction(threeds_transaction_endpoint,\n          access_token,\n          payment_token,\n          {**transaction_data, **threeds_parameters}\n          )\n        <\/code>\n      <\/div>\n    <\/div>\n  <\/div>\n<\/div>\n<\/div>\n","protected":false},"author":4,"featured_media":0,"template":"","meta":{"_acf_changed":false,"kk_blocks_editor_width":"","_kiokenblocks_attr":"","_kiokenblocks_dimensions":""},"backend":[67],"frontend":[],"class_list":["post-8993","tjspw","type-tjspw","status-publish","hentry","backend-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python - CCBill Doc<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ccbill.com\/doc\/tjspw\/python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - CCBill Doc\" \/>\n<meta property=\"og:description\" content=\"Python The code example is written in Python. It describes the steps required to perform...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ccbill.com\/doc\/tjspw\/python\" \/>\n<meta property=\"og:site_name\" content=\"CCBill Doc\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-08T12:39:55+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ccbill.com\/doc\/tjspw\/python\",\"url\":\"https:\/\/ccbill.com\/doc\/tjspw\/python\",\"name\":\"Python - CCBill Doc\",\"isPartOf\":{\"@id\":\"https:\/\/ccbill.com\/doc\/#website\"},\"datePublished\":\"2024-06-08T16:18:49+00:00\",\"dateModified\":\"2026-04-08T12:39:55+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ccbill.com\/doc\/tjspw\/python\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ccbill.com\/doc\/#website\",\"url\":\"https:\/\/ccbill.com\/doc\/\",\"name\":\"CCBill Doc\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ccbill.com\/doc\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - CCBill Doc","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ccbill.com\/doc\/tjspw\/python","og_locale":"en_US","og_type":"article","og_title":"Python - CCBill Doc","og_description":"Python The code example is written in Python. It describes the steps required to perform...","og_url":"https:\/\/ccbill.com\/doc\/tjspw\/python","og_site_name":"CCBill Doc","article_modified_time":"2026-04-08T12:39:55+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/ccbill.com\/doc\/tjspw\/python","url":"https:\/\/ccbill.com\/doc\/tjspw\/python","name":"Python - CCBill Doc","isPartOf":{"@id":"https:\/\/ccbill.com\/doc\/#website"},"datePublished":"2024-06-08T16:18:49+00:00","dateModified":"2026-04-08T12:39:55+00:00","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ccbill.com\/doc\/tjspw\/python"]}]},{"@type":"WebSite","@id":"https:\/\/ccbill.com\/doc\/#website","url":"https:\/\/ccbill.com\/doc\/","name":"CCBill Doc","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ccbill.com\/doc\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/ccbill.com\/doc\/wp-json\/wp\/v2\/tjspw\/8993"}],"collection":[{"href":"https:\/\/ccbill.com\/doc\/wp-json\/wp\/v2\/tjspw"}],"about":[{"href":"https:\/\/ccbill.com\/doc\/wp-json\/wp\/v2\/types\/tjspw"}],"author":[{"embeddable":true,"href":"https:\/\/ccbill.com\/doc\/wp-json\/wp\/v2\/users\/4"}],"wp:attachment":[{"href":"https:\/\/ccbill.com\/doc\/wp-json\/wp\/v2\/media?parent=8993"}],"wp:term":[{"taxonomy":"backend","embeddable":true,"href":"https:\/\/ccbill.com\/doc\/wp-json\/wp\/v2\/backend?post=8993"},{"taxonomy":"frontend","embeddable":true,"href":"https:\/\/ccbill.com\/doc\/wp-json\/wp\/v2\/frontend?post=8993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}