Created
June 9, 2015 11:41
-
-
Save pgjones/41f7aa6f9f264f8a7761 to your computer and use it in GitHub Desktop.
Example client class to transfer in and out of a Neteller wallet using the REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import requests | |
log = logging.getLogger(__name__) | |
# Currency without decimal places according to Neteller | |
NETELLER_NOT_DECIMAL = ['HUF', 'JPY'] | |
class NetellerUnavailableError(Exception): | |
pass | |
class NetellerPaymentFailedError(Exception): | |
pass | |
class NetellerAPIClient(object): | |
""" Allows transfer between Neteller wallets. | |
Transfer In transfers money into the APP's wallet. | |
Transfer Out transfers money out of the APP's wallet. | |
""" | |
def __init__(self, client_id, client_secret, | |
url="https://api.neteller.com/v1"): | |
""" Construct a Neteller REST API client. | |
Args: | |
client_id (string): The Neteller APP ID | |
client_secret (string): The Neteller APP secret token | |
url (string, optional): The base url, https://api.neteller.com/v1 | |
""" | |
self._url = url | |
self._client_id = client_id | |
self._client_secret = client_secret | |
def transfer_in(self, amount, currency, reference, | |
neteller_account, neteller_secret): | |
""" Transfer money from the neteller_account into the APP's wallet. | |
Args: | |
amount (decimal.Decimal): Amount with decimal places. | |
currency (string): 3 Character currency code. | |
reference (string): Unique reference for this transaction. | |
neteller_account (string): Neteller account ID or email address. | |
neteller_secret (string): 6 character secret code. | |
""" | |
log.info("Transfering %s %s with reference %s", | |
amount, currency, reference) | |
data = { | |
"paymentMethod": { | |
"type": "neteller", | |
"value": neteller_account, | |
}, | |
"transaction": self._make_transaction(amount, currency, reference), | |
"verificationCode": neteller_secret, | |
} | |
headers = {'Authorization': 'Bearer %s' % self._get_bearer_token()} | |
response = requests.post("%s/transferIn?expand=customer" % self._url, | |
headers=headers, | |
json=data) | |
response_data = response.json() | |
if response_data.get('error'): | |
raise NetellerPaymentFailedError(response_data['error']['message']) | |
log.info("Transfer complete") | |
return response_data | |
def transfer_out(self, amount, currency, reference, message, | |
neteller_email=None, neteller_account=None): | |
""" Transfer money from the APP's wallet to the neteller_email wallet. | |
Args: | |
amount (decimal.Decimal): Amount with decimal places. | |
currency (string): 3 Character currency code. | |
reference (string): Unique reference for this transaction. | |
message (string): Message for the user. | |
neteller_email (string, optional): Neteller email address. | |
neteller_account (string, optional): Neteller account ID. | |
Notes: | |
Either pass the neteller_email or the neteller_account ID. | |
""" | |
log.info("Transfering %s %s with reference %s", | |
amount, currency, reference) | |
if neteller_email is None and neteller_account is not None: | |
payee = {"email": neteller_email} | |
log.info("Transfering to %s", neteller_email) | |
elif neteller_account is not None and neteller_email is None: | |
payee = {"accountID": neteller_account} | |
log.info("Transfering to %s", neteller_account) | |
else: | |
NetellerPaymentFailedError("Missing or duplicated account details") | |
data = { | |
"payeeProfile": payee, | |
"transaction": self._make_transaction(amount, currency, reference), | |
"message": message, | |
} | |
headers = {'Authorization': 'Bearer %s' % self._get_bearer_token()} | |
response = requests.post("%s/transferOut" % self._url, | |
headers=headers, | |
json=data) | |
response_data = response.json() | |
if response_data.get('error'): | |
raise NetellerPaymentFailedError(response_data['error']['message']) | |
log.info("Transfer complete") | |
response_data = response.json() | |
return response_data | |
def _get_bearer_token(self): | |
""" Retrieve a Bearer authorisation token from neteller. | |
This Bearer token authorises the actual transfer requests. | |
""" | |
log.debug("Retrieving Bearer Token") | |
url = "%s/oauth2/token?grant_type=client_credentials" % self._url | |
response = requests.post(url, | |
auth=(self._client_id, self._client_secret), | |
headers={'Content-Type': 'application/json'}) | |
if response.status_code != requests.codes.ok: | |
raise NetellerUnavailableError() | |
return response.json()['accessToken'] | |
def _make_transaction(self, amount, currency, reference): | |
""" Return a transaction dictionary for neteller. | |
Args: | |
amount (decimal.Decimal): Amount with decimal places. | |
currency (string): 3 Character currency code. | |
reference (string): Unique reference for this transaction. | |
""" | |
if currency not in NETELLER_NOT_DECIMAL: | |
amount = amount * 100 | |
assert amount == int(amount) | |
return { | |
"merchantRefId": reference, | |
"amount": "%d" % amount, | |
"currency": currency, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment