PageRenderTime 15ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/satchmo-0.6.0/satchmo/payment/models.py

https://github.com/davemerwin/satchmo
Python | 82 lines | 60 code | 7 blank | 15 comment | 0 complexity | de61a94523c04057a538fe751edfae1e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Stores details about the available payment options.
  3. Also stores credit card info in an encrypted format.
  4. """
  5. from Crypto.Cipher import Blowfish
  6. from django.conf import settings
  7. from django.db import models
  8. from django.utils.translation import ugettext_lazy as _
  9. from satchmo.contact.models import OrderPayment
  10. from satchmo.payment.config import payment_choices, credit_choices
  11. import base64
  12. import config
  13. import logging
  14. log = logging.getLogger('payment.models')
  15. class PaymentOption(models.Model):
  16. """
  17. If there are multiple options - CC, Cash, COD, etc this class allows
  18. configuration.
  19. """
  20. description = models.CharField(_("Description"), max_length=20)
  21. active = models.BooleanField(_("Active"),
  22. help_text=_("Should this be displayed as an option for the user?"))
  23. optionName = models.CharField(_("Option Name"), max_length=20,
  24. choices = payment_choices(), unique=True,
  25. help_text=_("The class name as defined in payment.py"))
  26. sortOrder = models.IntegerField(_("Sort Order"))
  27. class Admin:
  28. list_display = ['optionName','description','active']
  29. ordering = ['sortOrder']
  30. class Meta:
  31. verbose_name = "Payment Option"
  32. verbose_name_plural = "Payment Options"
  33. class CreditCardDetail(models.Model):
  34. """
  35. Stores an encrypted CC number, its information, and its
  36. displayable number.
  37. """
  38. orderpayment = models.ForeignKey(OrderPayment, unique=True, edit_inline=True,
  39. num_in_admin=1, max_num_in_admin=1, related_name="creditcards")
  40. creditType = models.CharField(_("Credit Card Type"), max_length=16,
  41. choices=credit_choices())
  42. displayCC = models.CharField(_("CC Number (Last 4 digits)"),
  43. max_length=4, core=True)
  44. encryptedCC = models.CharField(_("Encrypted Credit Card"),
  45. max_length=40, blank=True, null=True, editable=False)
  46. expireMonth = models.IntegerField(_("Expiration Month"))
  47. expireYear = models.IntegerField(_("Expiration Year"))
  48. ccv = models.IntegerField(_("CCV"), blank=True, null=True)
  49. def storeCC(self, ccnum):
  50. # Take as input a valid cc, encrypt it and store the last 4 digits in a visible form
  51. # Must remember to save it after calling!
  52. secret_key = settings.SECRET_KEY
  53. encryption_object = Blowfish.new(secret_key)
  54. # block cipher length must be a multiple of 8
  55. padding = ''
  56. if (len(ccnum) % 8) <> 0:
  57. padding = 'X' * (8 - (len(ccnum) % 8))
  58. self.encryptedCC = base64.b64encode(encryption_object.encrypt(ccnum + padding))
  59. self.displayCC = ccnum[-4:]
  60. def _decryptCC(self):
  61. secret_key = settings.SECRET_KEY
  62. encryption_object = Blowfish.new(secret_key)
  63. # strip padding from decrypted credit card number
  64. ccnum = encryption_object.decrypt(base64.b64decode(self.encryptedCC)).rstrip('X')
  65. return (ccnum)
  66. decryptedCC = property(_decryptCC)
  67. def _expireDate(self):
  68. return(str(self.expireMonth) + "/" + str(self.expireYear))
  69. expirationDate = property(_expireDate)
  70. class Meta:
  71. verbose_name = _("Credit Card")
  72. verbose_name_plural = _("Credit Cards")