Skip to main content

Craftgate Python Client

Gitpod ready-to-code

This repo contains the Python client for Craftgate API.

PyPI package: https://pypi.org/project/craftgate/

Open in Gitpod

Requirements

  • Python 3.6+

Installation

pip install craftgate

Usage

To access the Craftgate API you'll first need to obtain API credentials (API key & secret key). If you don't already have a Craftgate account, you can sign up at https://craftgate.io.

By default the client connects to production https://api.craftgate.io. For testing, use the sandbox URL https://sandbox-api.craftgate.io.

from craftgate import Craftgate, RequestOptions

options = RequestOptions(
    api_key="<YOUR API KEY>",
    secret_key="<YOUR SECRET KEY>",
    base_url="https://sandbox-api.craftgate.io"
)
payment = Craftgate(options).payment()

Example: Credit Card Payment

import uuid
from decimal import Decimal

from craftgate import Craftgate, RequestOptions
from craftgate.model import Currency, PaymentGroup, PaymentPhase
from craftgate.request import CreatePaymentRequest
from craftgate.request.dto import Card, PaymentItem

# Configure client (use sandbox for testing)
options = RequestOptions(
    api_key="<YOUR API KEY>",
    secret_key="<YOUR SECRET KEY>",
    base_url="https://sandbox-api.craftgate.io"
)
craftgate = Craftgate(options)
payment = craftgate.payment()

# Build basket
items = []
for name, price in [("item 1", "30"), ("item 2", "50"), ("item 3", "20")]:
    pi = PaymentItem()
    pi.name = name
    pi.external_id = str(uuid.uuid4())
    pi.price = Decimal(price)
    items.append(pi)

# Card info (sandbox test card)
card = Card()
card.card_holder_name = "Haluk Demir"
card.card_number = "5258640000000001"
card.expire_year = "2044"
card.expire_month = "07"
card.cvc = "000"

# Payment request
req = CreatePaymentRequest()
req.price = Decimal("100")
req.paid_price = Decimal("100")
req.wallet_price = Decimal("0")
req.installment = 1
req.currency = Currency.TRY
req.conversation_id = "456d1297-908e-4bd6-a13b-4be31a6e47d5"
req.payment_group = PaymentGroup.LISTING_OR_SUBSCRIPTION
req.payment_phase = PaymentPhase.AUTH
req.card = card
req.items = items

resp = payment.create_payment(req)
print(f"Create Payment Result: {resp}")

Idempotency

Mutating operations accept an optional idempotency key. Set it on the request object and the client sends it as the x-idempotency-key header, so a request can be safely retried (e.g. after a timeout) without the operation being performed twice — the server returns the result of the first request when it sees a repeated key.

Every request extends BaseRequest, which carries a HeaderOptions object, so the key is available on any request:

import uuid

from craftgate import HeaderOptions

req = CreatePaymentRequest()
req.price = Decimal("100")
req.paid_price = Decimal("100")
req.currency = Currency.TRY
req.payment_group = PaymentGroup.LISTING_OR_SUBSCRIPTION
req.header_options = HeaderOptions(idempotency_key=str(uuid.uuid4()))
# ... other fields

resp = payment.create_payment(req)

with_header_options() sets it inline and returns the request, which is handy for operations whose parameters live in the URL path:

payment.expire_checkout_payment(
    ExpireCheckoutPaymentRequest(token="456d1297-908e-4bd6-a13b-4be31a6e47d5")
    .with_header_options(HeaderOptions(idempotency_key=str(uuid.uuid4()))))

Use a fresh key per distinct operation, and reuse the same key when retrying that operation.

The API honours the key on POST, PATCH and DELETE only. It is ignored on PUT endpoints, so retrying one of those is not de-duplicated.

HeaderOptions is sent as headers only — it never appears in the request body, the query string, or the request signature.

Examples

A variety of end-to-end samples (3DS, Checkout, APM, refunds, stored cards, marketplace, pre/post-auth) live under the tests/ folder.

Run a single test:

python -m unittest tests.test_payment_sample.PaymentSample.test_create_payment

Contributions

For all contributions to this client please see the contribution guide at CONTRIBUTING.md. By participating in this project, you agree to abide by our Code of Conduct.

Security

If you discover a security vulnerability, please review our Security Policy for how to report it responsibly.

License

This project is licensed under the Apache License, Version 2.0 — see the LICENSE and NOTICE files for details.

Metadata

Release files for craftgate 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for craftgate 1.0.6
File Size Uploaded Provenance Actions
craftgate-1.0.6.tar.gz 95.9 kB Without provenance Details for craftgate-1.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for craftgate 1.0.6
File Interpreter ABI Platform Actions
craftgate-1.0.6-py3-none-any.whl Python 3 none any Details for craftgate-1.0.6-py3-none-any.whl

Total release size: 277.7 kB

Release files / craftgate-1.0.6.tar.gz

Download URL craftgate-1.0.6.tar.gz
Size 95.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1f1cbfea3e4ccac3805a0865bc795ae9ffb830ee12a3a59e1735c660f5cddd5a
BLAKE2b-256 checksum
How to use checksums
156e4b67f3938a7709741fb98dde9652401a0583870cfac59e2848cc45f5c9d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / craftgate-1.0.6-py3-none-any.whl

Download URL craftgate-1.0.6-py3-none-any.whl
Size 181.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ad8d55893d1bfbc6f13b58259588ccb13c983b1f6af841b7a17686a5a128eba
BLAKE2b-256 checksum
How to use checksums
182b34f53bf6eddd20324794bc4d83ac2d09daafc72c6ad0c1dd873e36203f6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for craftgate 1.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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