<%
include("../../util/constants.jag");
include("util/constants.jag");
include("util/utility.jag");

var log = new Log();
var cookie = request.getParameter("cookie");
var user = request.getParameter("user");
var receiptData = request.getParameter("receiptData");
var carbon = require("carbon");
var tenantUser = carbon.server.tenantUser(user);

session.put('auth-cookie', cookie);
session.put('user', user);

try {
    // Override collectionMethod
    receiptData = parse(receiptData);
    receiptData.collectionMethod = COLLECTION_METHOD_USER_PORTAL;

    var serverCoreUrl = application.get(SERVER_CORE_URL);
    var url  = serverCoreUrl + "/t/" + tenantUser.domain + CONSENT_API_URI;
    var header = {"cookie" : cookie, "Content-Type": "application/json"};
    var consentJSON = post(url, stringify(receiptData), header, "json");
    validateResponse(consentJSON);

    print(consentJSON);

    } catch(e) {
        log.error(e);
        response.sendRedirect("/portal/error_pages/error.jag");
}

%>
