Skip to main content

Getswish python client library

Project description

Swish - Python client

Client library to integrate with the swish commerce and payout api.

Supported APIs

Installation

pip install getswish

Prerequisites

Swish api integration require communication certificates and keys to integrate with the commerce api and additional signing certificates and keys for signing payout in the payout api. The client setup require that all the certificates are available through file paths which are given on client setup. The communication certificates are create through your bank portal after the appropriate agreements are signed and signing certificates are set up after additional payout agreements are signed, trough https://portal.swish.nu/.

The signing certificate, private key and serial is only required when using the payout API.

Example - Test Client

The example code uses the test environment and certificates in this library.

import getswish

swish_client = getswish.SwishClient()

callback_url = "https://example.com/callback/"

### Example - Commerce API

# Perform a payment request - E-commerce.
payment_e = swish_client.create_payment(
    100.00, callback_url, message="Product name."
)

# Perform a payment request - M-commerce.
payment_m = swish_client.create_payment(
    100.00, callback_url, "46701234567", message="Product name."
)

# Retrieve info about the payment
payment_retrieved = swish_client.retrieve_payment(payment_m.id)

# Cancel the payment
payment_cancelled = swish_client.cancel_payment(payment_m.id)

# Refund payment the whole amount to the previous payer which now is the payee.
payment_refund = swish_client.create_refund(
    payment_e.id, callback_url, payment_e.payer_alias, payment_e.amount
)

# Retrieve info about the refund
payment_refund_retrieved = swish_client.retrieve_refund(payment_refund.id)

### Example - Payout API

# Generate a merchant specific reference.
# This reference could be order id or similar.
# Use generate_transaction_id for convenience.

from getswish.utils import generate_transaction_id

reference_id = generate_transaction_id()

# Perform a payment request
payout = swish_client.create_payout(
    reference_id, "46701234567", "197001019876", 10.00, callback_url, message="Test payout message."
)

# Retrieve info about the payout
payout_retrieved = swish_client.retrieve_payout(payout.payout_instruction_uuid)

Example - Production Client

In production the environment must be set to swish.ProductionEnvironment and all path must be modified to the production certificates that you have generated through your bank and swish company portals. The example below is the default configuration for the test certificates and environment. Replace all paths and files with your generated production instances.

from pathlib import Path
import getswish

cert_base = Path(__file__).parent.parent.parent.resolve()
cert_base = cert_base / "mss_test_2.0" / "Getswish_Test_Certificates"

swish_client = getswish.SwishClient(
    environment=getswish.TestEnvironment,
    certificates=getswish.Certificates(
        communication=getswish.Certificate(
            public=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.pem",
            private_key=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.key",
        ),
        verify=getswish.Certificate(public=f"{cert_base}/Swish_TLS_RootCA.pem"),
    ),
    merchant_swish_number="1234679304",
)

Example - Production Client with payout

Using the payout API require an additional certificate from your bank and called a signing certificate.

from pathlib import Path
import getswish

cert_base = Path(__file__).parent.parent.parent.resolve()
cert_base = cert_base / "mss_test_2.0" / "Getswish_Test_Certificates"

swish_client = getswish.SwishClient(
    environment=getswish.TestEnvironment,
    certificates=getswish.Certificates(
        communication=getswish.Certificate(
            public=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.pem",
            private_key=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.key",
        ),
        verify=getswish.Certificate(public=f"{cert_base}/Swish_TLS_RootCA.pem"),
        signing=getswish.Certificate(
            public=f"{cert_base}/Swish_Merchant_TestSigningCertificate_1234679304.pem",
            private_key=f"{cert_base}/Swish_Merchant_TestSigningCertificate_1234679304.key",
            public_serial="5E24D8820F5B62C7E5CAC75D20D6E754",
        ),
    ),
    merchant_swish_number="1234679304",
)

callback_url = "https://example.com/callback/"

Generating public_serial for signing certificate

The signing certificate public_serial is extracted from the certificate using this command on linux.

openssl x509 -in Swish_Merchant_TestSigningCertificate_1234679304.pem -serial -noout

Development setup

Clone the repository and set up a local virtual environment.

git clone https://github.com/nibon/getswish-python.git && cd getswish-python

python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install flit
flit install --only-deps

Testing installing package and pytest

Symlink getswish and run pytest. You might want to uninstall the getswish library depending on your workflow.

flit install --symlink
pytest

Testing using nox

Isolated testing on configured python versions and running a coverage test.

nox

Project details


Download files

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

Source Distribution

getswish-0.3.0.tar.gz (750.5 kB view details)

Uploaded Source

Built Distribution

getswish-0.3.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file getswish-0.3.0.tar.gz.

File metadata

  • Download URL: getswish-0.3.0.tar.gz
  • Upload date:
  • Size: 750.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for getswish-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2eb94c5cde62597258a929d0b05440998684b52c42f331851113bcc13b1a27a4
MD5 61407c2812ba8f54f4beb7f8c01accb3
BLAKE2b-256 24281f1f4a0fcfe6cad4dcaee25486901bd50c2971c803e82352b9a24d371b59

See more details on using hashes here.

File details

Details for the file getswish-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: getswish-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for getswish-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c14ec809d0ad8fd3f292ef251681d06ca7252ef3c7899822ae91c7c587f0fe81
MD5 e4d20c89f74b997cdec6453cfc4283c2
BLAKE2b-256 601c1e3dd2dca9af3dc54d528d163e1669dd9ca84598d74e2944f4d8e8ae84ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page