def update_settled_braintree_payments(): today = datetime.datetime.today() yesterday = datetime.datetime.today() - datetime.timedelta(1) #today = datetime.datetime(2010, 8, 25) gw = BraintreeGateway(settings.MERCHANT_ID, settings.PUBLIC_KEY, settings.PRIVATE_KEY) transactions = gw.get_daily_transactions(yesterday) logging.info("%s transactions were found yesterday %s" % (len(transactions), yesterday)) for transaction in transactions: if gw.is_settled(transaction.id): logging.info("Transaction ID=%s was SETTLED! Updating the associated sell instance..." % transaction.id) try: btx = BrainTreeTransaction.objects.filter(transaction_id=transaction.id).get() if btx.sell.payment.state_actual.state != "PA": btx.sell.payment.pay() logging.info("%s was marked as paid..." % btx.sell) else: logging.info("%s was already marked as paid, nothing to do..." % btx.sell) except BrainTreeTransaction.DoesNotExist: logging.critical("Transaction ID=%s is not associated to any Sell!!" % transaction.id) elif gw.is_submitted_for_settlement(transaction.id): logging.info("Transaction ID=%s is SUBMITTED_FOR_SETTLEMENT yet! Nothing to do, wait to status change to SETTLED" % transaction.id) logging.info("Check how long is in this situation... do something if several days have passed") elif gw.is_authorized(transaction.id): logging.info("Transaction ID=%s is AUTHORIZED! This should not be never happend because we programatically set the transaction to be submitted_for_settlement" % transaction.id) else: logging.info("Transaction ID=%s has status = %s" % (transaction.id, transaction.status)) try: btx = BrainTreeTransaction.objects.filter(transaction_id=transaction.id).get() btx.sell.payment.pay() except BrainTreeTransaction.DoesNotExist: logging.critical("Transaction ID=%s is not associated to any Sell!!" % transaction.id)
def send_daily_invoice_notification(): today = datetime.datetime.today() #today = datetime.datetime(2010, 7, 15) gw = BraintreeGateway(settings.MERCHANT_ID, settings.PUBLIC_KEY, settings.PRIVATE_KEY) transactions = gw.get_daily_transactions(today) logging.info("%s transactions were found today %s" % (len(transactions), today)) for transaction in transactions: if transaction.subscription_id is not None: invoice = Invoice() invoice.cc_mask = '*'*12 + transaction.credit_card_details.masked_number[12:] invoice.cc_type = transaction.credit_card_details.card_type invoice.charge = decimal.Decimal(transaction.amount) invoice.currency = transaction.currency_iso_code invoice.transaction_status_response = transaction.processor_response_text invoice.transaction_type = transaction.type.lower() invoice.transaction_id = transaction.id invoice.customer_name = "%s %s" % (transaction.customer_details.first_name, transaction.customer_details.last_name) invoice.customer_email = transaction.customer_details.email invoice.customer_id = transaction.customer_details.id invoice.shop_dns = "<Unspecified Shop>" if transaction.customer_details.website is None else transaction.customer_details.website try: shop_id = None if transaction.vault_customer.custom_fields is '' else transaction.vault_customer.custom_fields.get("shop_id", None) if shop_id is not None: try: shop = Shop.objects.get(id=shop_id) invoice.shop = shop invoice.market_place = shop.marketplace.name except Shop.DoesNotExist: logging.error("Shop ID = %s not exist for user %s" % (shop_id, invoice.customer_name)) pass else: logging.error("User %s has not setted shop_id property in braintree" % invoice.customer_name) except Exception, e: logging.error(e) pass invoice.subscription_id = transaction.subscription_id subscription = gw.get_subscription_details(invoice.subscription_id) invoice.plan_id = subscription.plan_id invoice.valid_from = subscription.billing_period_start_date invoice.valid_up_to = subscription.billing_period_end_date invoice.next_billing_date = subscription.next_billing_date invoice.save() msg = invoice.to_text() logging.info("Sending email to %s. tx=%s, charge=%s, " % (invoice.customer_name, invoice.transaction_id, invoice.charge)) mail = EmailMessage(subject='%s | Notification Invoice' % invoice.market_place, body=msg, from_email=settings.EMAIL_FROM, to=[invoice.customer_email]+[mail for name, mail in settings.STAFF], headers={'X-SMTPAPI': '{\"category\": \"Notification Invoice\"}'}) mail.send(fail_silently=True)
def update_settled_braintree_payments(): today = datetime.datetime.today() yesterday = datetime.datetime.today() - datetime.timedelta(1) #today = datetime.datetime(2010, 8, 25) gw = BraintreeGateway(settings.MERCHANT_ID, settings.PUBLIC_KEY, settings.PRIVATE_KEY) transactions = gw.get_daily_transactions(yesterday) logging.info("%s transactions were found yesterday %s" % (len(transactions), yesterday)) for transaction in transactions: if gw.is_settled(transaction.id): logging.info( "Transaction ID=%s was SETTLED! Updating the associated sell instance..." % transaction.id) try: btx = BrainTreeTransaction.objects.filter( transaction_id=transaction.id).get() if btx.sell.payment.state_actual.state != "PA": btx.sell.payment.pay() logging.info("%s was marked as paid..." % btx.sell) else: logging.info( "%s was already marked as paid, nothing to do..." % btx.sell) except BrainTreeTransaction.DoesNotExist: logging.critical( "Transaction ID=%s is not associated to any Sell!!" % transaction.id) elif gw.is_submitted_for_settlement(transaction.id): logging.info( "Transaction ID=%s is SUBMITTED_FOR_SETTLEMENT yet! Nothing to do, wait to status change to SETTLED" % transaction.id) logging.info( "Check how long is in this situation... do something if several days have passed" ) elif gw.is_authorized(transaction.id): logging.info( "Transaction ID=%s is AUTHORIZED! This should not be never happend because we programatically set the transaction to be submitted_for_settlement" % transaction.id) else: logging.info("Transaction ID=%s has status = %s" % (transaction.id, transaction.status)) try: btx = BrainTreeTransaction.objects.filter( transaction_id=transaction.id).get() btx.sell.payment.pay() except BrainTreeTransaction.DoesNotExist: logging.critical( "Transaction ID=%s is not associated to any Sell!!" % transaction.id)
def send_daily_invoice_notification(): today = datetime.datetime.today() #today = datetime.datetime(2010, 7, 15) gw = BraintreeGateway(settings.MERCHANT_ID, settings.PUBLIC_KEY, settings.PRIVATE_KEY) transactions = gw.get_daily_transactions(today) logging.info("%s transactions were found today %s" % (len(transactions), today)) for transaction in transactions: if transaction.subscription_id is not None: invoice = Invoice() invoice.cc_mask = '*' * 12 + transaction.credit_card_details.masked_number[ 12:] invoice.cc_type = transaction.credit_card_details.card_type invoice.charge = decimal.Decimal(transaction.amount) invoice.currency = transaction.currency_iso_code invoice.transaction_status_response = transaction.processor_response_text invoice.transaction_type = transaction.type.lower() invoice.transaction_id = transaction.id invoice.customer_name = "%s %s" % ( transaction.customer_details.first_name, transaction.customer_details.last_name) invoice.customer_email = transaction.customer_details.email invoice.customer_id = transaction.customer_details.id invoice.shop_dns = "<Unspecified Shop>" if transaction.customer_details.website is None else transaction.customer_details.website try: shop_id = None if transaction.vault_customer.custom_fields is '' else transaction.vault_customer.custom_fields.get( "shop_id", None) if shop_id is not None: try: shop = Shop.objects.get(id=shop_id) invoice.shop = shop invoice.market_place = shop.marketplace.name except Shop.DoesNotExist: logging.error("Shop ID = %s not exist for user %s" % (shop_id, invoice.customer_name)) pass else: logging.error( "User %s has not setted shop_id property in braintree" % invoice.customer_name) except Exception, e: logging.error(e) pass invoice.subscription_id = transaction.subscription_id subscription = gw.get_subscription_details(invoice.subscription_id) invoice.plan_id = subscription.plan_id invoice.valid_from = subscription.billing_period_start_date invoice.valid_up_to = subscription.billing_period_end_date invoice.next_billing_date = subscription.next_billing_date invoice.save() msg = invoice.to_text() logging.info("Sending email to %s. tx=%s, charge=%s, " % (invoice.customer_name, invoice.transaction_id, invoice.charge)) mail = EmailMessage(subject='%s | Notification Invoice' % invoice.market_place, body=msg, from_email=settings.EMAIL_FROM, to=[invoice.customer_email] + [mail for name, mail in settings.STAFF], headers={ 'X-SMTPAPI': '{\"category\": \"Notification Invoice\"}' }) mail.send(fail_silently=True)