Skip to content

Commit

Permalink
Added card validation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Silviana Ghita committed Sep 5, 2023
1 parent d700b5e commit 5b5657f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
6 changes: 5 additions & 1 deletion mangopay/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,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
23 changes: 21 additions & 2 deletions mangopay/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,31 @@ class Meta:
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(Card):
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)

def validate(self, *args, **kwargs):
kwargs['id'] = self.id
insert = InsertQuery(self, **kwargs)
insert.insert_query = self.get_field_dict()
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')
Expand Down
38 changes: 34 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,37 @@ 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_validation = CardValidation()
card_validation.author = user
card_validation.id = updated_registration['card_id']
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()
updated_card = Card.get(updated_registration['card_id'])

self.assertIsNotNone(validation_response)
self.assertEqual(updated_card.validity, 'VALID')

0 comments on commit 5b5657f

Please sign in to comment.