Skip to content

Commit

Permalink
Minimal amount (#82)
Browse files Browse the repository at this point in the history
* Capture exceptions for not known errors only and add new message

* Fix for #78

* Change required
  • Loading branch information
Ricardo authored and felipao-mx committed Sep 3, 2019
1 parent 64c1e85 commit 28a0fca
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
11 changes: 9 additions & 2 deletions arcusd/arcusactions.py
Expand Up @@ -4,6 +4,7 @@

from arcus.client import Client
from arcus.client import Transaction as ArcusTransaction
from arcus.exc import InvalidAmount

from arcusd.contracts import Bill, Cancellation, Topup, Transaction
from arcusd.data_access.providers_mapping import get_service_provider_code
Expand All @@ -29,6 +30,12 @@ def clean(value: str) -> str:
return re.sub(r'\D', '', value)


def amount_to_unit(cents: int) -> float:
if cents <= 100:
raise InvalidAmount(code='00', message='Min amount is 1 peso')
return cents_to_unit(cents)


def query_bill(biller_id: int, account_number: str) -> Bill:
bill = client.bills.create(biller_id, clean(account_number))
bill_contract = Bill(
Expand Down Expand Up @@ -61,7 +68,7 @@ def pay_bill(biller_id: int, account_number: str,
if amount is None:
transaction = bill.pay()
else:
amount = cents_to_unit(amount)
amount = amount_to_unit(amount)
transaction = bill.pay(amount)
transaction_contract = Transaction(
id=transaction.id,
Expand Down Expand Up @@ -90,7 +97,7 @@ def cancel_transaction(transaction_id: int) -> Cancellation:

def topup(biller_id: int, phone_number: str, amount: int,
currency: str, name_on_account: str) -> Topup:
unit = cents_to_unit(amount)
unit = amount_to_unit(amount)
topup = client.topups.create(biller_id,
clean(phone_number),
unit,
Expand Down
3 changes: 2 additions & 1 deletion arcusd/daemon/utils.py
Expand Up @@ -23,7 +23,8 @@ def execute_op(request_id: str, op_type: OperationType, funct,
op_info.error_message = exc.message
else:
op_info.error_message = format(exc)
capture_exception(exc)
if not op_info.notification:
capture_exception(exc)
else:
op_info.operation = transaction
op_info.status = OperationStatus.success
Expand Down
3 changes: 2 additions & 1 deletion arcusd/errors_dict.py
@@ -1,5 +1,5 @@
from arcus.exc import (AlreadyPaid, DuplicatedPayment, IncompleteAmount,
InvalidAccountNumber, RecurrentPayments)
InvalidAccountNumber, InvalidAmount, RecurrentPayments)

errors_dict = {
RecurrentPayments: 'Esta cuenta tiene pagos domiciliados activos '
Expand All @@ -10,4 +10,5 @@
AlreadyPaid: 'El balance en esta cuenta ya ha sido cubierto',
InvalidAccountNumber: 'Por favor, verifica el número de telefono '
'e intenta de nuevo',
InvalidAmount: 'El monto a pagar es inválido.'
}
32 changes: 32 additions & 0 deletions tests/cassettes/test_tasks/test_pay_bill_with_non_min_value.yaml
@@ -0,0 +1,32 @@
interactions:
- request:
body: '{"biller_id": 40, "account_number": "501000000007"}'
headers:
Accept: [application/vnd.regalii.v3.1+json]
Accept-Encoding: ['gzip, deflate']
Authorization: ['APIAuth 88879c1b066dc9aea6201f27be2bbba9:w2fUEIcBafbcr5RHnIsV3YVEfig=']
Connection: [keep-alive]
Content-Length: ['51']
Content-MD5: [PtZUYQ9bmh3ULNV0W7ZNdw==]
Content-Type: [application/json]
Date: ['Mon, 28 Jan 2019 18:33:23 GMT']
User-Agent: [python-requests/2.20.1]
method: POST
uri: https://api.casiregalii.com/bills
response:
body: {string: '{"type":"bill","id":8390,"biller_id":40,"account_number":"501000000007","name_on_account":null,"due_date":null,"balance":549.0,"balance_currency":"MXN","balance_updated_at":"2019-01-28T18:33:24Z","error_code":null,"error_message":null,"status":"linked","migrated_at":null,"mfa_challenges":[]}'}
headers:
Cache-Control: ['max-age=0, private, must-revalidate']
Connection: [keep-alive]
Content-Type: [application/json; charset=utf-8]
Date: ['Mon, 28 Jan 2019 18:33:25 GMT']
ETag: [W/"1452f7edd87a6eb3759e6d5e454611f2"]
Strict-Transport-Security: [max-age=31536000]
Transfer-Encoding: [chunked]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [SAMEORIGIN]
X-Request-Id: [7bd0638e-cf93-4037-8440-dea1d43ba40d]
X-Runtime: ['1.056615']
X-XSS-Protection: [1; mode=block]
status: {code: 200, message: OK}
version: 1
25 changes: 25 additions & 0 deletions tests/test_tasks.py
Expand Up @@ -233,3 +233,28 @@ def test_notification(send_op_result, phone_number, amount,
assert send_op_result.called
op_info = send_op_result.call_args[0][0]
assert op_info.notification == expected_notification


@pytest.mark.vcr(cassette_library_dir='tests/cassettes/test_tasks')
@patch(SEND_OP_RESULT, return_value=dict(status='ok'))
def test_pay_bill_with_non_min_value(send_op_result):
request_id = 'request-id'
pay_bill(request_id, 'satellite_tv_sky', '501000000007', 1)
assert send_op_result.called
op_info = send_op_result.call_args[0][0]
assert op_info.request_id == request_id
assert op_info.tran_type == OperationType.payment
assert op_info.status == OperationStatus.failed
assert op_info.notification == 'El monto a pagar es inválido.'


@patch(SEND_OP_RESULT, return_value=dict(status='ok'))
def test_topup_with_non_min_value(send_op_result):
request_id = 'request-id'
topup(request_id, 'topup_att', '501000000007', 1)
assert send_op_result.called
op_info = send_op_result.call_args[0][0]
assert op_info.request_id == request_id
assert op_info.tran_type == OperationType.topup
assert op_info.status == OperationStatus.failed
assert op_info.notification == 'El monto a pagar es inválido.'

0 comments on commit 28a0fca

Please sign in to comment.