def handle_cancellation(ta: StoreTransaction): if not ta.time_cancelled: ta.time_cancelled = timezone.now() ta.save() logger.info('Store transaction {} cancelled.'.format(ta.id)) else: logger.warning('Attempted to mark store transaction {} as cancelled twice'.format(ta.id))
def handle_pending(ta: StoreTransaction): if ta.time_cancelled: logger.warning('Cannot mark store transaction {} pending; is already cancelled.'.format(ta.id)) elif not ta.time_pending: ta.time_pending = timezone.now() ta.save() logger.info('Store transaction {} paid, pending confirmation.'.format(ta.id)) else: logger.warning('Attempted to mark store transaction {} as pending twice'.format(ta.id))
def handle_cancellation(ta: StoreTransaction): if not ta.time_cancelled: ta.time_cancelled = timezone.now() ta.save() logger.info('Store transaction {} cancelled.'.format(ta.id)) else: logger.warning( 'Attempted to mark store transaction {} as cancelled twice'.format( ta.id))
def handle_pending(ta: StoreTransaction): if ta.time_cancelled: logger.warning( 'Cannot mark store transaction {} pending; is already cancelled.'. format(ta.id)) elif not ta.time_pending: ta.time_pending = timezone.now() ta.save() logger.info('Store transaction {} paid, pending confirmation.'.format( ta.id)) else: logger.warning( 'Attempted to mark store transaction {} as pending twice'.format( ta.id))
def handle_payment(ta: StoreTransaction): # If not yet marked as pending, mark it now if not ta.time_pending: ta.time_pending = timezone.now() # Mark as paid right away, so failures later cannot change this ta.time_paid = timezone.now() ta.save() # Deliver email. params = ReceiptParams() params.order_number(ta.id) params.order_date(ta.time_created) params.receipt_date(ta.time_paid) params.first_name(ta.firstname) params.last_name(ta.lastname) params.email(ta.email) params.company(ta.company) params.mobile(ta.mobile) params.telephone(ta.telephone) params.street(ta.street) params.city(ta.city) params.postal_code(ta.postalcode) params.country(ta.country) params.transaction_url(get_url(reverse('store:ta_view', args=(ta.key, )))) # Add items to email for item, variant, purchase_price in ta.get_distinct_storeitems_and_prices( ): i_amount = ta.get_storeitem_count(item, variant=variant) i_name = '{}, {}'.format(item.name, variant.name) if variant else item.name i_id = '{}:{}'.format(item.id, variant.id) if variant else item.id params.add_item(i_id, i_name, purchase_price, i_amount, '0%') # Send mail try: receipt = Receipt.create( mail_to=ta.email, mail_from='"Instanssi" <*****@*****.**>', subject='Instanssi.org: Kuitti tilaukselle #{}'.format(ta.id), params=params) receipt.send() except Exception as ex: logger.exception('Store: {}.'.format(ex)) return False logger.info('Store transaction {} confirmed.'.format(ta.id)) return True
def handle_payment(ta: StoreTransaction): # If not yet marked as pending, mark it now if not ta.time_pending: ta.time_pending = timezone.now() # Mark as paid right away, so failures later cannot change this ta.time_paid = timezone.now() ta.save() # Deliver email. params = ReceiptParams() params.order_number(ta.id) params.order_date(ta.time_created) params.receipt_date(ta.time_paid) params.first_name(ta.firstname) params.last_name(ta.lastname) params.email(ta.email) params.company(ta.company) params.mobile(ta.mobile) params.telephone(ta.telephone) params.street(ta.street) params.city(ta.city) params.postal_code(ta.postalcode) params.country(ta.country) params.transaction_url(get_url(reverse('store:ta_view', args=(ta.key,)))) # Add items to email for item, variant, purchase_price in ta.get_distinct_storeitems_and_prices(): i_amount = ta.get_storeitem_count(item, variant=variant) i_name = '{}, {}'.format(item.name, variant.name) if variant else item.name i_id = '{}:{}'.format(item.id, variant.id) if variant else item.id params.add_item(i_id, i_name, purchase_price, i_amount, '0%') # Send mail try: receipt = Receipt.create( mail_to=ta.email, mail_from='"Instanssi" <*****@*****.**>', subject='Instanssi.org: Kuitti tilaukselle #{}'.format(ta.id), params=params) receipt.send() except Exception as ex: logger.exception('Store: {}.'.format(ex)) return False logger.info('Store transaction {} confirmed.'.format(ta.id)) return True
def create_store_transaction(data): # Handle creation of the order in a transaction to avoid creating crap to db in errors try: with transaction.atomic(): ta = StoreTransaction() ta.firstname = data['first_name'] ta.lastname = data['last_name'] ta.company = data['company'] ta.email = data['email'] ta.telephone = data['telephone'] ta.mobile = data['mobile'] ta.street = data['street'] ta.postalcode = data['postal_code'] ta.city = data['city'] ta.country = data['country'] ta.information = data['information'] ta.time_created = datetime.now() ta.key = uuid.uuid4().hex ta.save() # Check items for item in data['items']: # First, make sure that the ordered item exists and is available store_item = StoreItem.items_available().get(pk=item['item_id']) store_variant = store_item.variants.get(pk=item['variant_id']) if item['variant_id'] else None # Find the price with discounts (if any) amount = item['amount'] purchase_price = store_item.get_discounted_unit_price(amount) # Form the transaction item(s) for m in range(amount): ta_item = TransactionItem() ta_item.transaction = ta ta_item.item = store_item ta_item.variant = store_variant ta_item.key = uuid.uuid4().hex ta_item.purchase_price = purchase_price ta_item.original_price = store_item.price ta_item.save() return ta except Exception as e: logger.error("Unable to save store transaction: %s", str(e)) raise
def create_store_transaction(data: dict) -> StoreTransaction: # Handle creation of the order in a transaction to avoid creating crap to db in errors try: with transaction.atomic(): ta = StoreTransaction() ta.firstname = data['first_name'] ta.lastname = data['last_name'] ta.company = data['company'] ta.email = data['email'] ta.telephone = data['telephone'] ta.mobile = data['mobile'] ta.street = data['street'] ta.postalcode = data['postal_code'] ta.city = data['city'] ta.country = data['country'] ta.information = data['information'] ta.time_created = timezone.now() ta.key = uuid.uuid4().hex ta.save() # Check items for item in data['items']: store_item, store_variant = get_item_and_variant(item) # Find the price with discounts (if any) purchase_price = store_item.get_discounted_unit_price( item['amount']) # Form the transaction item(s) for m in range(item['amount']): ta_item = TransactionItem() ta_item.transaction = ta ta_item.item = store_item ta_item.variant = store_variant ta_item.key = uuid.uuid4().hex ta_item.purchase_price = purchase_price ta_item.original_price = store_item.price ta_item.save() return ta except Exception as e: logger.error("Unable to save store transaction: %s", str(e)) raise
def create_store_transaction(data: dict) -> StoreTransaction: # Handle creation of the order in a transaction to avoid creating crap to db in errors try: with transaction.atomic(): ta = StoreTransaction() ta.firstname = data['first_name'] ta.lastname = data['last_name'] ta.company = data['company'] ta.email = data['email'] ta.telephone = data['telephone'] ta.mobile = data['mobile'] ta.street = data['street'] ta.postalcode = data['postal_code'] ta.city = data['city'] ta.country = data['country'] ta.information = data['information'] ta.time_created = timezone.now() ta.key = uuid.uuid4().hex ta.save() # Check items for item in data['items']: store_item, store_variant = get_item_and_variant(item) # Find the price with discounts (if any) purchase_price = store_item.get_discounted_unit_price(item['amount']) # Form the transaction item(s) for m in range(item['amount']): ta_item = TransactionItem() ta_item.transaction = ta ta_item.item = store_item ta_item.variant = store_variant ta_item.key = uuid.uuid4().hex ta_item.purchase_price = purchase_price ta_item.original_price = store_item.price ta_item.save() return ta except Exception as e: logger.error("Unable to save store transaction: %s", str(e)) raise