def finalize_order(request):
    '''Helper function that actually complete the order when the
    payment provider tells us so.
    '''
    order_id = provider.get_order_id(request)
    order = Order.objects.get(pk=order_id)
    #Order is already completed
    if order.payment_done:
        return

    #Simulate the cart for the order.complete, and order_handler that needs it
    try:
        cart_id = provider.get_cart_id(request)
        request.cart = Cart.objects.get(id=cart_id)
    except (NotImplementedError, Cart.DoesNotExist, __HOLE__):
        pass

    #Recreate an order form for the order handler
    data = checkout.initial_order_data(request)
    data["step"] = checkout.CHECKOUT_STEP_LAST
    order_form_class = get_callable(settings.SHOP_CHECKOUT_FORM_CLASS)
    form = order_form_class(request, step=checkout.CHECKOUT_STEP_LAST, data=data)
    form.instance = order
    form.full_clean()
    request.session["order"] = dict(form.cleaned_data)

    order.transaction_id = provider.get_transaction_id(request)
    order.payment_done = True
    order.complete(request)

    order_handler(request, form, order)
    checkout.send_order_email(request, order)
dataset/ETHPy150Open thomasWajs/cartridge-external-payment/cartridge_external_payment/views.py/finalize_order