manta.store

Library with a basic implementation of a Manta POS.

This module implements a basic Manta Merchant. An usage example of this class can be found in module manta.testing.store.

The API is implemented in the Store class and specifically by the Store.merchant_order_request() method that creates an MerchantOrderRequestMessage and sends it to the Payment Processor that is managing the payment process.

In order to work correctly, the class needs to be instantiated and connected to the MQTT broker. This is implemented using an asyncio coroutine:

import asyncio

from manta.store import Store

store = Store('example_store', host='example.com')

loop = asyncio.get_event_loop()
loop.run_until_complete(store.connect())

Then the a new order needs to be created:

ack = loop.run_until_complete(store.merchant_order_request(amount=10, fiat='eur'))

ack.url  # contains the Manta URL

When that is done, the Manta URL needs to be transmitted to the Wallet to pay, but this is out of the scope of this code. From the Merchant point of view the progress of the payment transaction can be monitored by looking into the AckMessage instances collected by the store.acks queue. The payment is considered complete when a received ack has status == PAID:

from manta.messages import Status

async def wait_for_complete(store):
    while True:
        ack = await store.acks.get()
        if ack.status is Status.PAID:
            break
    return ack

final_ack = loop.run_until_complete(wait_for_complete(store))

Reference

class manta.store.Store(device_id, host='localhost', client_options=None, port=1883)[source]

Implements a Manta POS. This class needs an asyncio loop to run correctly as some of its features are implemented as coroutines.

Parameters:
  • device_id (str) – Device unique identifier (also called application_id) associated with the POS
  • host (str) – Hostname of the Manta broker
  • client_options (Optional[Dict[~KT, ~VT]]) – A Dict of options to be passed to MQTT Client (like username, password)
  • port (int) – port of the Manta broker
Attributes:
  • acks – queue of AckMessage instances
  • device_id – Device unique identifier (also called application_id) associated with the POS
  • loop – the asyncio loop that manages the asynchronous parts of this object
  • session_idsession_id of the ongoing session, if any
clean()[source]

Clean the AckMessage queue and unsubscribe from any active MQTT subscriptions.

close()[source]

Disconnect and stop MQTT client’s processing loop.

coroutine connect()[source]

Connect to the MQTT broker and wait for the connection confirmation.

This is a coroutine.

coroutine merchant_order_request(self, amount, fiat, crypto=None)[source]

Create a new Merchant Order and publish it to the merchant_order_request/{application_id} topic. Raises an exception if an AckMessage isn’t received in less than 3 seconds.

Parameters:
  • amount (Decimal) – Fiat Amount requested
  • fiat (str) – Fiat Currency requested (ex. ‘EUR’)
  • crypto (Optional[str]) – Crypto Currency requested (ex. ‘NANO’)
Return type:

AckMessage

Returns:

ack message with status ‘NEW’ if confirmed by Payment Processor or

Timeout Exception

This is a coroutine.

subscribe(topic)[source]

Subscribe to the given MQTT topic.

Parameters:topic (str) – string containing the topic name.