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 RefundRedirectBizum {
	public static void refundRedirectBizumExample(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"));

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

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