Skip to main content

Codecov

CSOB client

Python library for communicating with ČSOB (https://platbakartou.csob.cz/) payment gateway API. The API is described here: https://github.com/csob/paymentgateway.

The library currently implements ČSOB API v.1.9.

Installation

pip install csobpg

Basic usage

API client initialization

The APIClient provides the interface to communicate with the API.

from csobpg.v19 import APIClient

client = APIClient("merchantId", "merch_private.key", "csob.pub", base_url=..., http_client=...)

# Use the client to interact with the API:
client.init_payment(...)

HTTP client

The library uses the httprest library for making HTTP requests. By default it will use httprest.http.urllib_client.UrllibHTTPClient.

But you may use any other httprest's HTTP client, or even write your own client.

Base methods

The library supports all base API methods. For example, that's how to initialize a payment:

from csobpg.v19.models import cart

response = client.init_payment(
    order_no="2233823251",
    total_amount=100,
    return_url="http://127.0.0.1:5000",
    cart=cart.Cart([cart.CartItem("Apples", 1, 100)]),
    merchant_data=b"Hello, World!",
)

OneClick methods

Here are the steps to perform a OneClick payment.

Step 1 - make a regular payment

First, make a regular payment using the "payment/init":

response = client.init_payment(
    ...,
    # this is important. It will tell the bank to create a OneClick template
    payment_operation=PaymentOperation.ONE_CLICK_PAYMENT,
)
# redirect to the payment page and finalize payment
client.get_payment_process_url(pid)

Preserve the response.pay_id, it will be used to refer to the OneClick template.

Step 2 - initialize OneClick payment

Now, having the template ID, initialize the OneClick payment. First, check that the template ID exists (optional, but recommended):

response = client.oneclick_echo(template_id)
if not response.success:
    # OneClick template not found! handle it somehow

If the template exists, initiate the payment:

response = client.oneclick_init_payment(
    pid, # this is the template ID (the ID of the initial payment, retrieved in Step 1)
    ...,
    client_ip="127.0.0.1", # this is mandatory when client_initiated=True
    client_initiated=True, # whether it is initiated in the presence of client or not
)

Step 3 - process OneClick payment

Finally, process the payment:

response = client.oneclick_process(
    pid, # this is the payment ID retrieved in Step 2
    Fingerprint( # mandatory only for client_initiated=True
        Browser( # the following values must be taken from the client's browser
            user_agent="requests",
            accept_header="application/json",
            language="eng",
            js_enabled=False,
        ),
    ),
)

Google Pay methods

WARNING: not tested.

# echo
response = client.googlepay_echo()

# payment Initialization
client.googlepay_init(
    pid, "127.0.0.1", 10000, {"Google Pay": "payload"}, "http://localhost"
)

# payment process
client.googlepay_process(pid, Fingerprint())

Apple Pay methods

WARNING: not tested.

# echo
response = client.applepay_echo()

# payment Initialization
client.applepay_init(
    pid, "127.0.0.1", 10000, {"Apple Pay": "payload"}, "http://localhost"
)

# payment process
client.applepay_process(pid, Fingerprint())

Exceptions handling

from csobpg.v19 import errors as _e
from httprest.http import HTTPRequestError

try:
    response = client.<operation>(...)
except _e.APIError as exc:
    # handle API error
    # it is raised on any API error. You may also catch the specific API error
except _e.APIInvalidSignatureError as exc:
    # handle invalid signature
except _e.APIInvalidResponseError as exc:
    # handle invalid API response
    # it is raised when the API returns something unexpected
    # (e.g. invalid JSON, missing signature, empty signature, malformed resultCode, etc.)

    # You can access the original HTTP response (httprest.http.HTTPResponse) for debugging
    exc.response
except _e.APIClientError as exc:
    # handle API client error. All unhandled exceptions fall into this category
except HTTPRequestError as exc:
    # handle HTTP error
    # it is raised if the HTTP request fails (e.g. connection error, timeout, etc.)

The library does not pre-validate the request parameters against the API specification. Whatever you pass is signed and sent as is, and the gateway is the one to reject it (reported as an APIError).

The library verifies the response signature before reporting the resultCode as an APIError. Mind that the API does not sign the requests it rejects before processing them:

HTTP/1.1 401 Unauthorized

{"resultCode": 100, "resultMessage": "Missing parameter merchantId"}

Such a response cannot be verified, so an APIError alone is not a proof the failure was reported by the API. A successful response is always verified: it is never returned unless its signature matches.

RSA keys management

The simples way to pass RSA keys is to pass their file paths:

from csobpg.v19 import APIClient

client = APIClient(..., "merch_private.key", "csob.pub")

The library will read the private key from the file when needed. The public key will be cached into the RAM.

If you want to change it, use special classes:

from csobpg.v19 import APIClient
from csobpg.v19.key import FileRSAKey, CachedRSAKey

client = APIClient(..., FileRSAKey("merch_private.key"), FileRSAKey("csob.pub"))

You may also override the base RSAKey class to define your own key access strategy:

from csobpg.v19.key import RSAKey

class MyRSAKey(RSAKey):

    def __str__(self) -> str:
        return "my key"

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

csobpg-0.6.0.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

csobpg-0.6.0-py3-none-any.whl (46.5 kB view details)

Uploaded Python 3

File details

Details for the file csobpg-0.6.0.tar.gz.

File metadata

  • Download URL: csobpg-0.6.0.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.10.9 Linux/6.17.0-1022-azure

File hashes

Hashes for csobpg-0.6.0.tar.gz
Algorithm Hash digest
SHA256 26c12f5b95c214c696191743336c6f0f03da18e86320e4486d03c89053cb91a8
MD5 8276823abcc1d262a7e76c1a03434610
BLAKE2b-256 d5d91744998bf51c77506cfcf968b867e349482199606b34c681c256c429147a

See more details on using hashes here.

File details

Details for the file csobpg-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: csobpg-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 46.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.10.9 Linux/6.17.0-1022-azure

File hashes

Hashes for csobpg-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6da1f3269dfc31850851851c132296df04198d93f2e48544de6abb459d4ef089
MD5 4d3bec63e671938777f2054b0a9cc96f
BLAKE2b-256 be4df73dcb8b9a54f0752b01d0b56b95ddf127b19e6fee04269da2338750180d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.1

2 files

This release

0.6.0 This release

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page