Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (
Help us improve Python packaging - Donate today!

GoPay's Python SDK for Payments REST API

Project Description



The simplest way to install SDK is to use PIP:

pip install gopay

Basic usage

import gopay

# minimal configuration
payments = gopay.payments({
    'goid': 'my goid',
    'clientId': 'my id',
    'clientSecret': 'my secret',
    'isProductionMode': False

# full configuration
payments = gopay.payments({
    'goid': 'my goid',
    'clientId': 'my id',
    'clientSecret': 'my secret',
    'isProductionMode': False,
    'scope': gopay.TokenScope.ALL,
    'language': gopay.Language.CZECH,
    'timeout': 30


Required fields

Required field Data type Documentation
goid string default GoPay account used in create_payment if target is not specified
clientId string
clientSecret string
isProductionMode boolean test or production environment?

Optional fields

Optional field Data type Default value Documentation
scope string `gopay.enums.TokenScope.ALL </gopay/>`__
language string `gopay.enums.Language.ENGLISH </gopay/>`__ language used in create_payment if lang is not specified + used for localization of errors
timeout int 30 Browser timeout in seconds

 Available methods

API SDK method
Create standard payment payments.create_payment({})
Status of the payment payments.get_status(id_payment)
Refund of the payment payments.refund_payment(id_payment, $amount)
Create recurring payment payments.create_payment({})
Recurring payment on demand payments.create_recurrence(id_payment, {})
Cancellation of the recurring payment payments.void_recurrence(id_payment)
Create pre-authorized payment payments.create_payment({})
Charge of pre-authorized payment payments.capture_authorization(id_payment)
Cancellation of the pre-authorized payment payments.void_authorization(id_payment)

SDK response? Has my call succeed?

SDK returns wrapped API response. Every method returns `gopay.http.response object </gopay/>`__. Structure of json/__str__ should be same as in documentation. SDK throws no exception. Please create an issue if you catch one.

response = payments.create_payment({})
if response.has_succeed():
    print("hooray, API returned " + str(response))
    return response.json['gw_url'] # url for initiation of gateway
    # errors format:
    print("oops, API returned " + str(response.status_code) + ": " + str(response))
Method Description
response.has_succeed() checks if API returns status code 200
response.json decoded response, returned objects are converted into associative arrays
response.status_code HTTP status code
response.__str__() raw body from HTTP response

 Are required fields and allowed values validated?

No. API validates fields pretty extensively so there is no need to duplicate validation in SDK. It would only introduce new type of error. Or we would have to perfectly simulate API error messages. That’s why SDK just calls API which behavior is well documented in

Advanced usage

Initiation of the payment gateway

# create payment and pass url to template
response = payments.create_payment({})
if response.has_succeed():
    templateParameters = {
        'gatewayUrl': response.json['gw_url'],
        'embedJs': gopay.url_to_embedjs()
    # render template

Inline gateway

<form action="<%= $gatewayUrl %>" method="post" id="gopay-payment-button">
  <button name="pay" type="submit">Pay</button>
  <script type="text/javascript" src="<%= $embedJs %>"></script>

Redirect gateway

<form action="<%= $gatewayUrl %>" method="post">
  <button name="pay" type="submit">Pay</button>

Enums (Code lists)

Instead of hardcoding bank codes string you can use predefined enums. Check using enums in create-payment example

Type Description
Language Payment language, localization of error messages
Token scope Authorization scope for OAuth2
Payment enums Enums for creating payment
Response enums Result of creating payment, executing payment operations

Cache access token

Access token expires after 30 minutes so it’s expensive to use new token for every request. Unfortunately it’s default behavior of `gopay.oauth2.InMemoryTokenCache </gopay/>`__. But you can implement your cache and store tokens in Memcache, Redis, files, … It’s up to you.

Your cache must implement template methods get_token and set_token. Be aware that there are two scopes (TokenScope) and SDK can be used for different clients (clientId, isProductionMode). So client passed to methods is unique identifier (string) that is built for current environment. Below you can see example implementation of caching tokens in memory:

# register cache in optional service configuration
payments = gopay.payments(
    {}, # your config
    {'cache': MyCache()}
class MyCache:
    def __init__(self):
        self.tokens = {}

    def get_token(self, client):
        return self.tokens.get(client) # return None if token not exists

    def set_token(self, client, token):
        self.tokens[client] = token

Log HTTP communication

You can log every request and response from communication with API. Check available loggers below. Or you can implement your own logger, just implement function that takes two arguments: `gopay.http.request </gopay/>`__ and `gopay.http.response </gopay/>`__.

# register logger in optional service configuration
payments = gopay.payments(
    {}, # your config
    {'logger': my_logger}

def my_logger(request, response):
Available logger Description
gopay.http.null_logger Default logger which does nothing
tests.remote.debug_logger Prints request and response in remote tests


Contributions from others would be very much appreciated! Send pull request/ issue. Thanks!


Copyright (c) 2015 MIT Licensed, see LICENSE for details.

Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
gopay-1.0.0rc4-py2.py3-none-any.whl (12.8 kB) Copy SHA256 Checksum SHA256 2.7 Wheel Dec 2, 2015
gopay-1.0.0rc4.tar.gz (9.3 kB) Copy SHA256 Checksum SHA256 Source Dec 2, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting