package demo.examples.redirect;

import java.io.IOException;
import java.util.Map;

import com.sun.net.httpserver.HttpExchange;

import demo.config.Secrets;
import demo.server.Server;
import es.redsys.lib.GenerateRedirectForm;
import es.redsys.lib.Merchant;
import es.redsys.lib.Parameters;
import es.redsys.lib.PaymentMethod;
import es.redsys.lib.TransactionType;
import es.redsys.lib.Utils;

public class AuthenticationRedirectBizum {
	public static void authenticationRedirectBizumExample(HttpExchange exchange) throws IOException {
		Server.addCors(exchange);
		  
		final Merchant merchant = Merchant.initWithApiKey(Secrets.REDSYS_API_KEY);
		final Parameters params = new Parameters();
		
		final Map<String, String> receivedParams = Server.getQueryParams(exchange);
		params.setAmount(receivedParams.get("amount"));
		params.setOrder(Utils.randomString(12));
		params.setBizumMobileNumber("+34700000000");

        // Bizum
        params.setPayMethods(PaymentMethod.BIZUM);
		
        final String host = Server.getSchemeAndHost(exchange);
        if(host != null && !host.trim().isEmpty()) {
            // Notification
            params.setMerchantUrl(host + "/backend/bizum/notification?origin=authenticationRedirectBizumExample");

            // OK / KO
//            params.setUrlOk(host + "/frontend/authentication/bizum/redirect/success.html?amount=" + params.getAmount() + "&order=" + params.getOrder() + "&paymethod=" + params.getPayMethods() + "&a=a");
            params.setUrlKo(host + "/frontend/error.html");
            params.setUrlOk(host + "/frontend/success.html?amount=" + params.getAmount() + "&order=" + params.getOrder() 
            	+ "&paymethod=" + PaymentMethod.BIZUM + "&method=redirect"
        		+ "&transactionType=" + TransactionType.AUTHENTICATION + "&a=a");
        }
        		
		final String redirectForm = GenerateRedirectForm.authentication(merchant, params);
		Server.renderHtml(exchange, redirectForm);
	}
	
	public static void authenticationConfirmationRedirectBizumExample(HttpExchange exchange) throws IOException {
		Server.addCors(exchange);
		  
		final Merchant merchant = Merchant.initWithApiKey(Secrets.REDSYS_API_KEY);
		final Parameters params = new Parameters();
		
		final Map<String, String> receivedParams = Server.getQueryParams(exchange);
		params.setAmount(receivedParams.get("amount"));
		params.setOrder(receivedParams.get("order"));
		
        // Bizum
        params.setPayMethods(PaymentMethod.BIZUM);

        final String host = Server.getSchemeAndHost(exchange);
        if(host != null && !host.trim().isEmpty()) {
            // Notification
            params.setMerchantUrl(host + "/backend/bizum/notification?origin=authenticationConfirmationRedirectBizumExample");

            // OK / KO
            params.setUrlOk(host + "/frontend/success.html?amount=" + params.getAmount() + "&order=" + params.getOrder() 
        		+ "&paymethod=" + PaymentMethod.BIZUM + "&method=redirect"
        		+ "&transactionType=" + TransactionType.AUTHENTICATION_CONFIRMATION + "&a=a");
            params.setUrlKo(host + "/frontend/error.html");
        }
		
		final String redirectForm = GenerateRedirectForm.authenticationConfirmation(merchant, params);
		Server.renderHtml(exchange, redirectForm);
	}
}