import json
from uncloud_pay.models import Product, Order, ProductToRecurringPeriod


def finalize_order(request, customer, billing_address,
                    one_time_price, pricing_plan,
                    specs):
    product = Product.objects.first()
    recurring_period_product = ProductToRecurringPeriod.objects.filter(product=product, is_default=True).first()
    order = Order.objects.create(
            owner=request.user,
            customer=customer,
            billing_address=billing_address,
            one_time_price=one_time_price,
            pricing_plan=pricing_plan,
            recurring_period= recurring_period_product.recurring_period,
            product = product,
            config=json.dumps(specs)
        )
    return order