Skip to content

Commit

Permalink
Merge pull request #337 from Mangopay/feature/card-validation-endpoint
Browse files Browse the repository at this point in the history
feature/Added card validation endpoint
  • Loading branch information
iulian03 authored Sep 18, 2023
2 parents 67d63e7 + b70894c commit f77eb88
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
9 changes: 7 additions & 2 deletions mangopay/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
TRANSACTION_TYPE_CHOICES = Choices(
('PAYIN', 'payin', 'Pay In'),
('PAYOUT', 'payout', 'Pay out'),
('TRANSFER', 'transfer', 'Transfer')
('TRANSFER', 'transfer', 'Transfer'),
('CARD_VALIDATION', 'card_validation', 'Card validation')
)

NATURE_CHOICES = Choices(
Expand Down Expand Up @@ -176,7 +177,11 @@
('DEPOSIT_PREAUTHORIZATION_PAYMENT_EXPIRED', 'deposit_preauthorization_payment_expired', 'Deposit Preauthorization Payment Expired'),
('DEPOSIT_PREAUTHORIZATION_PAYMENT_CANCEL_REQUESTED', 'deposit_preauthorization_payment_cancel_requested', 'Deposit Preauthorization Payment Cancel Requested'),
('DEPOSIT_PREAUTHORIZATION_PAYMENT_CANCELED', 'deposit_preauthorization_payment_canceled', 'Deposit Preauthorization Payment Canceled'),
('DEPOSIT_PREAUTHORIZATION_PAYMENT_VALIDATED', 'deposit_preauthorization_payment_validated', 'Deposit Preauthorization Payment Validated')
('DEPOSIT_PREAUTHORIZATION_PAYMENT_VALIDATED', 'deposit_preauthorization_payment_validated', 'Deposit Preauthorization Payment Validated'),

('CARD_VALIDATION_CREATED', 'card_validation_created', 'Card Validation Created'),
('CARD_VALIDATION_FAILED', 'card_validation_failed', 'Card Validation Failed'),
('CARD_VALIDATION_SUCCEEDED', 'card_validation_succeeded', 'Card Validation Succeeded'),
)

NOTIFICATION_STATUS_CHOICES = Choices(
Expand Down
46 changes: 38 additions & 8 deletions mangopay/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,53 @@ def get_transactions(self, *args, **kwargs):
select.identifier = 'CARD_GET_TRANSACTIONS'
return select.all(*args, **kwargs)

def validate(self, *args, **kwargs):
kwargs['id'] = self.id
insert = InsertQuery(self, **kwargs)
insert.identifier = 'CARD_VALIDATE'
return insert.execute()

class Meta:
verbose_name = 'card'
verbose_name_plural = 'cards'
url = {
SelectQuery.identifier: '/cards',
UpdateQuery.identifier: '/cards',
'CARDS_FOR_FINGERPRINT': '/cards/fingerprints/%(fingerprint)s',
'CARD_VALIDATE': '/cards/%(id)s/validate'}
'CARDS_FOR_FINGERPRINT': '/cards/fingerprints/%(fingerprint)s'
}

def __str__(self):
return '%s of user %s' % (self.card_type, self.user_id)


class CardValidation(BaseModel):
creation_date = DateTimeField(api_name='CreationDate')
author = ForeignKeyField(User, api_name='AuthorId', required=True)
ip_address = CharField(api_name='IpAddress', required=True)
browser_info = BrowserInfoField(api_name='BrowserInfo', required=True)
secure_mode_return_url = CharField(api_name='SecureModeReturnURL', required=True)
secure_mode_redirect_url = CharField(api_name='SecureModeRedirectURL')
secure_mode_needed = BooleanField(api_name='SecureModeNeeded')
validity = CharField(api_name='Validity',
choices=constants.VALIDITY_CHOICES,
default=constants.VALIDITY_CHOICES.unknown)
type = CharField(api_name='Type', choices=constants.TRANSACTION_TYPE_CHOICES, default=None)
applied_3ds_version = CharField(api_name='Applied3DSVersion')
status = CharField(api_name='Status',
choices=constants.STATUS_CHOICES,
default=None)
result_code = CharField(api_name='ResultCode')
result_message = CharField(api_name='ResultMessage')

def validate(self, card_id, **kwargs):
insert = InsertQuery(self, **kwargs)
insert.insert_query = self.get_field_dict()
insert.insert_query['id'] = card_id
insert.identifier = 'CARD_VALIDATE'
return insert.execute()

class Meta:
verbose_name = 'card_validation'
verbose_name_plural = 'card_validations'
url = {
'CARD_VALIDATE': '/cards/%(id)s/validation'
}


class CardRegistration(BaseModel):
user = ForeignKeyField(User, api_name='UserId', required=True, related_name='card_registrations')
currency = CharField(api_name='Currency', required=True)
Expand Down Expand Up @@ -771,6 +799,7 @@ class Meta:
}



class GooglePayDirectPayIn(PayIn):
tag = CharField(api_name='Tag')
author = ForeignKeyField(User, api_name='AuthorId', required=True)
Expand Down Expand Up @@ -904,6 +933,7 @@ class Meta:
SelectQuery.identifier: '/payins'
}


class PreAuthorization(BaseModel):
author = ForeignKeyField(User, api_name='AuthorId', required=True)
debited_funds = MoneyField(api_name='DebitedFunds', required=True)
Expand Down
37 changes: 33 additions & 4 deletions tests/test_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
except ImportError:
import urllib as urlrequest

from mangopay.resources import Card, CardRegistration
from mangopay.resources import Card, CardRegistration, CardValidation
from tests.test_base import BaseTest
from tests.test_base import BaseTestLive

Expand Down Expand Up @@ -279,7 +279,36 @@ def test_getCardTransactions(self):
self.assertIsInstance(transactions_page.data, list)

def test_cardValidation(self):
card = BaseTestLive.get_johns_card()
validatedCard = card.validate()
user = BaseTestLive.get_john()

card_registration = CardRegistration()
card_registration.user = user
card_registration.currency = 'EUR'

self.assertIsNotNone(validatedCard)
saved_registration = card_registration.save()
data = {
'cardNumber': '4970107111111119',
'cardCvx': '123',
'cardExpirationDate': '1224',
'accessKeyRef': card_registration.access_key,
'data': card_registration.preregistration_data
}
headers = {
'content-type': 'application/x-www-form-urlencoded'
}
registration_data_response = requests.post(card_registration.card_registration_url, data=data, headers=headers)
saved_registration['registration_data'] = registration_data_response.text
updated_registration = CardRegistration(**saved_registration).save()
card_id = updated_registration['card_id']

card_validation = CardValidation()
card_validation.author = user
card_validation.tag = "test"
card_validation.secure_mode_return_url = "http://www.example.com/"
card_validation.ip_address = "2001:0620:0000:0000:0211:24FF:FE80:C12C"
card_validation.browser_info = BaseTest.get_browser_info()

validation_response = card_validation.validate(card_id)

self.assertIsNotNone(validation_response)
self.assertIsNotNone(validation_response['id'])

0 comments on commit f77eb88

Please sign in to comment.