def create(request, data, profile): order = BuyOrder(user=profile.user, tax=Tax.get_value(data.get('billing_state'), data.get('billing_county')), **data) order.save() order.history.create() for cart_item in request.cart.items.all(): for i in xrange(cart_item.quantity): #@UnusedVariable item = BuyOrderItem( order=order, item = cart_item.item, is_new = cart_item.is_new, price = cart_item.get_price(request) ) item.save() order.discounts = request.cart.get_discount_amount() order.recalc_total() available_store_credits = min(request.cart.applied_credits, profile.unlocked_store_credits) if available_store_credits > 0: total = order.get_order_total() if total >= available_store_credits: order.applied_credits = available_store_credits else: order.applied_credits = total order.save() return order
def get_tax_amount(amount, state, county=None): tax = Tax.get_value(state, county) return Decimal('%.2f' % (amount * tax / Decimal('100.0')))
def clean(self): data = super(ConfirmPlanChangingForm, self).clean() if not self._errors: request = self.request wizard = self.request.rent_wizard f = wizard.get_form(0, self.request.POST) f.is_valid() # it's a long way to get info from here plan = int(f.cleaned_data['plan']) _profile = request.user.get_profile() current_plan = MemberRentalPlan.get_current_plan(_profile.user) self.current_plan = current_plan if current_plan and current_plan.plan == plan: self.rent_plan = None return data card = self.cached_card['data'] card['display_number'] = self.cached_card['display_number'] aim_data = { 'x_customer_ip': self.request.META.get('REMOTE_ADDR'), 'x_cust_id': _profile.user.id, 'x_email': _profile.user.email, } shipping_address = _profile.get_shipping_address_data() shipping_address.update(_profile.get_name_data()) shipping_address['country'] = 'USA' billing_address = _profile.get_billing_data() billing_address['country'] = 'USA' downgrade = current_plan and plan <= current_plan.plan self.downgrade = downgrade if downgrade: return data self.rent_plan = MemberRentalPlan.create(_profile.user, plan, downgrade) self.billing_history = None amount = RentalPlan.get_start_payment_amount(plan) #malcala: changs to capture money #do_capture = False do_capture = True if current_plan: last_payment = current_plan.get_last_payment() if last_payment and last_payment.status == TransactionStatus.Passed: orders = RentOrder.objects.filter( user=current_plan.user, date_rent__gte=current_plan.created ).exclude(status__in=[RentOrderStatus.Prepared, RentOrderStatus.Canceled]) if orders.count(): if last_payment.debit < amount: amount -= (current_plan.next_payment_amount or RentalPlan.get_start_payment_amount(current_plan.plan)) do_capture = True tax = Tax.get_value(billing_address['state'], billing_address['county']) tax_amount = decimal.Decimal('%.2f' % (amount * tax / decimal.Decimal('100.0'))) aim_data['x_tax'] = tax_amount self.billing_history = BillingHistory.create(_profile.user, card['display_number'], debit=amount, reason='rent', type=TransactionType.RentPayment) self.billing_history.tax = tax_amount invoice_num, description = self.rent_plan.get_payment_description(False, self.billing_history.id) self.billing_history.description = description self.billing_history.save() if do_capture: res, aim_response, applied_credits, applied_amount = self.rent_plan.take_money(amount, tax_amount, invoice_num, description, card=card, shipping_data=shipping_address, billing_data=billing_address, aim_data=aim_data, profile=_profile) self.billing_history.applied_credits = applied_credits self.billing_history.status = TransactionStatus.Passed self.billing_history.setted = True else: res, aim_response = self.rent_plan.authorize_money(amount, tax_amount, invoice_num, description, card=card, shipping_data=shipping_address, billing_data=billing_address, aim_data=aim_data) self.billing_history.status = TransactionStatus.Authorized self.billing_history.setted = False if aim_response: self.billing_history.aim_transaction_id = aim_response.transaction_id self.billing_history.aim_response = aim_response._as_dict self.billing_history.message = aim_response.response_reason_text if not res: self.billing_history.status = TransactionStatus.Declined self.billing_history.save() self.rent_plan.delete() raise forms.ValidationError(self.rent_plan.status_message) self.billing_history.card_data = self.cached_card['data'] self.billing_history.save() return data