class TransactionCharge(models.Model): charge = fields.BigForeignKey(Charge) transaction = fields.BigForeignKey(Transaction, null=True) amount = models.DecimalField(max_digits=5, decimal_places=2, null=True) created = models.DateTimeField("Started at", auto_now_add=True) def __unicode__(self): return u'Ch %s for tr %s' % (self.charge, self.transaction)
class PlanSellingPrice(models.Model): carrier = fields.BigForeignKey(Carrier) plan = fields.BigForeignKey(Plan) company = fields.BigForeignKey(CompanyProfile) price_level = models.ForeignKey(SellingPriceLevel) selling_price = models.DecimalField(max_digits=5, decimal_places=2, null=True) created = models.DateTimeField('Created at', auto_now_add=True) updated = models.DateTimeField(verbose_name='Updated at', auto_now=True) def __unicode__(self): return u'%s level price for %s' % (self.price_level.level, self.plan.plan_id) def get_absolute_url(self): return reverse('plan_selling_price_list')
class SpamMessage(models.Model): ALL = 'A' ENABLED = 'E' DISABLED = 'D' MAIL = 'EM' SMS = 'SM' SMS_EMAIL = 'SE' CUSTOMER_TYPE_CHOICES = ( (ALL, 'All customers'), (ENABLED, 'Enabled customers'), (DISABLED, 'Disabled customers'), ) SEND_TYPE_CHOICES = ( (MAIL, 'Email'), (SMS, 'Sms'), (SMS_EMAIL, 'Sms via email'), ) company = fields.BigForeignKey(CompanyProfile) message = models.CharField(max_length=500) send_with = models.CharField(max_length=2, choices=SEND_TYPE_CHOICES, default=SMS) customer_type = models.CharField(max_length=1, choices=CUSTOMER_TYPE_CHOICES, default=ALL) created = models.DateTimeField(auto_now_add=True) def __unicode__(self): return self.message def get_absolute_url(self): return reverse('sms_create')
class ChargeError(models.Model): charge = fields.BigForeignKey(Charge) step = models.CharField(max_length=50) message = models.CharField(max_length=500) created = models.DateTimeField(auto_now=True) def __unicode__(self): return u'%s [%s]' % (self.step, self.created)
class CustomPreChargeMessage(models.Model): company = fields.BigForeignKey(CompanyProfile) message = models.TextField(blank=True) use_message = models.BooleanField(default=False) def __unicode__(self): return self.message def get_absolute_url(self): return reverse('custom_message')
class ChargeStep(models.Model): SUCCESS = 'S' ERROR = 'E' STATUS_TYPE_CHOICES = ( (SUCCESS, 'Success'), (ERROR, 'Error'), ) charge = fields.BigForeignKey(Charge) action = models.CharField(max_length=200) status = models.CharField(max_length=3, choices=STATUS_TYPE_CHOICES) adv_status = models.CharField(max_length=500, null=True) created = models.DateTimeField("Timestamp", auto_now=True) def __unicode__(self): return self.action
class Charge(models.Model): SUCCESS = 'S' ERROR = 'E' REFUND = 'R' VOID = 'V' PROCESS = 'P' STATUS_TYPE_CHOICES = ( (SUCCESS, 'Success'), (ERROR, 'Error'), (REFUND, 'Refund'), (VOID, 'Void'), ) AUTHORIZE = 'A' USAEPAY = 'U' DOLLARPHONE = 'DP' CASH_PREPAYMENT = 'CP' CASH = 'CA' CHARGE_GETAWAY_CHOICES = ( (AUTHORIZE, 'Authorize'), (USAEPAY, 'USAePay'), (DOLLARPHONE, 'DollarPhone'), (CASH_PREPAYMENT, 'Cash(PrePayment)'), (CASH, 'Cash'), ) REFUND_STATUS = ( (SUCCESS, 'Success'), (ERROR, 'Error'), ) id = fields.BigAutoField(primary_key=True) company = fields.BigForeignKey(CompanyProfile, null=True) autorefill = fields.BigForeignKey(AutoRefill, null=True) customer = fields.BigForeignKey(Customer, null=True) company_informed = models.BooleanField(default=False) creditcard = models.CharField(max_length=20, blank=True) used = models.BooleanField(default=False) amount = models.DecimalField(max_digits=5, decimal_places=2, default=decimal.Decimal(0.0)) tax = models.DecimalField(max_digits=5, decimal_places=2, default=decimal.Decimal(0.0)) summ = models.DecimalField(max_digits=5, decimal_places=2, default=decimal.Decimal(0.0)) atransaction = models.CharField(max_length=30, blank=True) payment_getaway = models.CharField(max_length=3, choices=CHARGE_GETAWAY_CHOICES) status = models.CharField(max_length=3, choices=STATUS_TYPE_CHOICES, blank=True) adv_status = models.CharField(max_length=500, blank=True) pin = models.CharField(max_length=256, blank=True) pin_used = models.BooleanField(default=False) refund_id = models.CharField(max_length=30, blank=True) refund_status = models.CharField(max_length=1, choices=REFUND_STATUS, blank=True) refunded = models.DateTimeField(null=True) created = models.DateTimeField("Timestamp", auto_now_add=True) note = models.TextField(null=True, blank='') def __unicode__(self): return u'%s' % self.id def get_full_url(self): return '%s%s' % (settings.SITE_DOMAIN, reverse('charge_detail', args=[self.pk])) def is_refundable(self): return self.created.replace(tzinfo=None) + datetime.timedelta( days=3) < datetime.datetime.now() def transaction_list(self): return TransactionCharge.objects.filter(charge=self) def check_getaway(self): charge_gateway = self.customer.get_charge_getaway if self.payment_getaway != charge_gateway: previous_getaway = self.get_payment_getaway_display() self.payment_getaway = charge_gateway self.save() self.add_charge_step( 'check payment', ChargeStep.SUCCESS, 'Customer getaway changed from "%s" to "%s"' % (previous_getaway, self.get_payment_getaway_display())) return self def make_charge(self): try: # charge card to USAePAY if Charge.USAEPAY == self.payment_getaway: if not self.customer.usaepay_customer_id: raise Exception( u'Customer credit card didn\'t added to USAePay') self.atransaction = self.add_transaction_to_usaepay() # charge card to Authorize elif Charge.AUTHORIZE == self.payment_getaway: if not self.customer.authorize_id: raise Exception( u'Customer credit card didn\'t added to Authorize') self.atransaction = self.add_transaction_to_authorize() elif Charge.DOLLARPHONE == self.payment_getaway: if not self.customer.has_local_cards: raise Exception( u'Customer credit card didn\'t added to system') self.atransaction = self.make_dollar_phone_charge() self.status = Charge.SUCCESS self.used = False self.adv_status = "Charge ended successfully" except Exception, e: logger.error("Exception: %s. Trace: %s." % (e, traceback.format_exc(limit=10))) self.used = True self.status = Charge.ERROR self.adv_status = 'CC Charge failed with error: "%s"' % e raise Exception(e) finally:
class Notification(models.Model): MAIL = 'EM' SMS = 'SM' SMS_EMAIL = 'SE' BOTH = 'B' SUCCESS = 'S' ERROR = 'E' SEND_TYPE_CHOICES = ( (MAIL, 'Email'), (SMS, 'Sms'), (SMS_EMAIL, 'Sms via email'), (BOTH, 'Sms and email'), ) STATUS_TYPE_CHOICES = ( (SUCCESS, 'Success'), (ERROR, 'Error'), ) company = fields.BigForeignKey(CompanyProfile) customer = fields.BigForeignKey(Customer, null=True) email = models.EmailField(blank=True) phone_number = models.CharField(max_length=10, blank=True) subject = models.TextField() body = models.TextField() send_with = models.CharField(max_length=2, choices=SEND_TYPE_CHOICES, null=True) status = models.CharField(max_length=3, choices=STATUS_TYPE_CHOICES, null=True) adv_status = models.TextField() created = models.DateTimeField('Created at', auto_now_add=True) def __unicode__(self): return u'%s' % self.subject def send_twilio_sms(self, twilio_sid, twilio_auth_token, twilio_number): if not settings.TEST_MODE: client = TwilioRestClient(twilio_sid, twilio_auth_token) client.messages.create(from_="+1%s" % twilio_number, to="+1%s" % self.phone_number, body=self.body.replace('<br/>', '\n')) def send_mandrill_email(self, mandrill_key, mandrill_email, body=None): if not body: body = self.body form_fields = { "key": mandrill_key, "message": { "html": body, "subject": self.subject, "from_email": mandrill_email, "to": [{ "email": self.email, "type": "to", }], } } if not settings.TEST_MODE: result = requests.post( 'https://mandrillapp.com/api/1.0/messages/send.json', data=json.dumps(form_fields), headers={'Content-Type': 'application/json'}) return result def send_sms_email(self, sms_email, mandrill_key, mandrill_email): self.email = sms_email self.subject = '' self.body = self.body.replace('<br/>', ' ') self.save() separator = 140 for part in [ self.body[i:i + separator] for i in range(0, len(self.body), separator) ]: self.send_mandrill_email(mandrill_key, mandrill_email, body=part) def send_notification(self): try: if self.MAIL == self.send_with or self.BOTH == self.send_with: self.send_mandrill_email( self.company.mandrill_key, self.company.mandrill_email, ) elif self.SMS_EMAIL == self.send_with or self.BOTH == self.send_with: for phone_number in self.customer.sms_email.split(','): self.send_sms_email( phone_number + '@' + self.customer.sms_gateway.gateway, self.company.mandrill_key, self.company.mandrill_email, ) elif self.SMS == self.send_with or self.BOTH == self.send_with: self.body = '%s\nPlease, do not reply' % self.body self.send_twilio_sms(self.company.twilio_sid, self.company.twilio_auth_token, self.company.twilio_number) self.status = self.SUCCESS self.adv_status = 'Notification sent succesfully' except Exception, e: self.adv_status = 'Notification not sent because: "%s"' % e self.status = self.ERROR raise Exception(e) finally: