Integrating with Python

In this section, we will guide you through the process of integrating the Trustist Ecommerce Payments API into your Python application. We will demonstrate how to set up authentication and general usage, as well as how to use the API endpoints through standard HTTP requests.

We’ll be using the Requests library, which is widely used and recommended for Python applications. If you prefer a different HTTP library, please let us know.

1. Installation #

To get started, ensure you have the Requests library installed in your Python project. You can install it using pip:

pip install requests

To handle the Hawk authentication you can use the mohawk project:

pip install mohawk

2. Configuration #

Create a new session object and configure it with your API credentials and the desired environment (sandbox or production). The base URL and Hawk authentication will be set up for all requests made using this session. Here’s an example:

import requests
from mohawk import Sender
import json

BASE_URL = 'https://api-sandbox.trustistecommerce.com'
CLIENT_ID = 'your-client-id'
CLIENT_KEY = 'your-private-key'

class HawkAuth(requests.auth.AuthBase):
    def __call__(self, request):
        sender_kwarg = {
            'credentials': {
                'id': CLIENT_ID,
                'key': CLIENT_KEY,
                'algorithm': 'sha256'
            },
            'url': request.url,
            'method': request.method,
            'always_hash_content': False
        }
        
        if request.method != 'GET':
            sender_kwarg['content'] = request.body
            sender_kwarg['content_type'] = request.headers['Content-Type']

        sender = Sender(**sender_kwarg)
        request.headers['Authorization'] = sender.request_header
        return request

3. Creating a Payment #

To create a new payment, make a POST request to the `/payments` endpoint, passing a dictionary containing the necessary payment details.

Example:

def create_payment(amount, reference, return_url):
    session = requests.Session()
    session.auth = HawkAuth()

    data = {
        'amount': amount,
        'reference': reference,
        'returnUrl': return_url
    }

    response = session.post(BASE_URL + '/v1/payments', json=data)
    response.raise_for_status()
    return response.json()

4. Retrieving a Payment #

To retrieve the details of an existing payment, make a GET request to the `/payments/{paymentId}` endpoint, passing the payment ID as a parameter.

Example:

def get_payment(payment_id):
    session = requests.Session()
    session.auth = HawkAuth()

    response = session.get(BASE_URL + '/v1/payments/' + payment_id)
    response.raise_for_status()
    return response.json()

By following these steps, you should now be able to integrate the Trustist Ecommerce Payments API into your Python application using the Requests library. For further information or assistance, please consult our comprehensive API documentation or contact our support team.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *