def setUp(self):
        shipping_data = billing_data = {
            "first_name": u"Test",
            "last_name": u"User",
            "address1": u"1 Test St",
            "address2": u"",
            "city": u"Test City",
            "state": u"AK",
            "zip_code": u"12345",
        }

        billing_card_data = {
            "type": u"visa",
            "number": u"4111111111111111",
            "code": u"123",
            "exp_month": u"1",
            "exp_year": u"2018",
        }

        self.profile = Profile.objects.build_and_create(
            username="******",
            email="*****@*****.**",
            password="******",
            shipping_data=shipping_data,
            billing_data=billing_data,
            billing_card_data=billing_card_data,
        )

        aim_data = build_aim_data(
            shipping_data, billing_data, billing_card_data,
            invoice_num="123",
            description="Test Payment",
            email=self.profile.user.email,
            tax=Decimal("1")
        )

        aim = create_aim()
        aim_response = aim.authorize(Decimal("10"), **aim_data)

        self.billing_history = BillingHistory.objects.create(
            user=self.profile.user,
            payment_method=self.profile.get_billing_card_display(),
            description="Test Payment",
            debit=Decimal("10"),
            reason="test-reason",
            type=TransactionType.Unknown,
            card_data=self.profile.get_billing_card_data(),
            tax=Decimal("1"),
            status=TransactionStatus.Authorized,
            aim_transaction_id=aim_response.transaction_id,
            aim_response=aim_response._as_dict,
            message=aim_response.response_reason_text
        )
示例#2
0
    def make_first_payment(self, user, profile, billing_card, customer_ip):
        """
        Being used when we would like to bill user for the first time and he doesn't
        have ``MemberRentalPlan`` associated yet.

        May raise ``PaymentError``.
        """
        billing_data = {
            "first_name": billing_card.first_name,
            "last_name": billing_card.last_name,
            "address1": billing_card.address1,
            "address2": billing_card.address2,
            "city": billing_card.city,
            "state": billing_card.state,
            "county": billing_card.county,
            "zip_code": billing_card.zip,
        }
        billing_card_data = billing_card.data.copy()
        shipping_data = {
            "first_name": user.first_name,
            "last_name": user.last_name,
            "address1": profile.shipping_address1,
            "address2": profile.shipping_address2,
            "city": profile.shipping_city,
            "state": profile.shipping_state,
            "county": profile.shipping_county,
            "zip_code": profile.shipping_zip,
        }
        email = user.email

        first_payment_amount = self.get_next_payment_amount()
        first_payment_type = self.get_next_payment_type()
        tax_amount = get_tax_amount(first_payment_amount, billing_data["state"], billing_data.get("county"))

        billing_history = BillingHistory.objects.create(
            payment_method=get_card_display_number(billing_card_data["number"]),
            debit=Decimal(first_payment_amount),
            reason="rent",
            type=TransactionType.RentPayment,
            card_data=billing_card_data,
            tax=tax_amount,
        )

        invoice_num, description = self.get_first_payment_description(billing_history.pk)
        aim_data = build_aim_data(
            shipping_data, billing_data, billing_card_data, invoice_num, description, email, customer_ip, tax_amount
        )

        aim = create_aim()
        success_status = TransactionStatus.Passed
        if first_payment_type == "AUTH_ONLY":
            aim_response = aim.authorize(first_payment_amount, **aim_data)
            success_status = TransactionStatus.Authorized
        elif first_payment_type == "AUTH_CAPTURE":
            aim_response = aim.capture(first_payment_amount, **aim_data)

        billing_history.description = description
        billing_history.aim_transaction_id = aim_response.transaction_id
        billing_history.aim_response = aim_response._as_dict
        billing_history.message = aim_response.response_reason_text
        if aim_response.response_code == 1:
            billing_history.status = success_status
            billing_history.save()
            return billing_history
        else:
            billing_history.status = TransactionStatus.Declined
            billing_history.save()
            raise PaymentError(aim_response.response_reason_text)