//
//  VeriTrans BuySmart Flex
//  adapter kit sample　CCInvoice.java　Version 1.0.1
//  Copyright (c) 2001-2002 VeriTrans Inc.
//  Note: CCInvoice for BuySmart Flex.
//

// CUSTOM:
//   URL を変えたいときはカスタマイズしてください
package CCAdapterSamples;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import CCKK.CCBSXLib.*;

//
// PURPOSE:
//   カード情報入力画面を表示する Servlet
//
// ATTENTION:
//
public class CCInvoice extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // Content-Type を設定する.
        response.setContentType(CCUtil.getContent());

        // 出力先をえる
        PrintWriter out = null;
        try {
	    out = CCUtil.getWriter(response);
	}
	catch (Exception e) {
	    CCCustom.temporaryDifficulties(out, "レスポンスエラー。");
	    return;
	}

	// order-id をえる.
	String id = CCCustom.genOrderID(request);

	if (id == null || id.length() <= 0) {
	    CCCustom.temporaryDifficulties(out, "オーダー ID の取得に失敗しました。");
	    return;
	}

	// price と note をえる
	Hashtable cartInfo = CCCustom.getCart(request);
	String price = (String)cartInfo.get("price");
	if (price == null) {
	    CCCustom.temporaryDifficulties(out, "金額の取得に失敗しました。");
	    return;
	}
	String jprice = "jpy " + price;
	String note = (String)cartInfo.get("note");
	if (note == null) {
	    CCCustom.temporaryDifficulties(out, "備考の取得に失敗しました。");
	    return;
	}

	// MDK インスタンスの生成
	CCTransaction t = new CCTransaction(CCCustom.config);

	// payrequest をなげる
	Hashtable param = new Hashtable();
	param.put("order-id", id + "in");
	param.put("amount", jprice);
	param.put("note", note);
	Hashtable result = t.sendMServer("payrequest", param);

	// MI の作成
	Hashtable mi = new Hashtable();
	mi.put("version", "1.0.1");
	mi.put("auth-type", CCCustom.authType);
	mi.put("order-id", id);
	mi.put("price", price);
	mi.put("payto", (String)result.get("url-pay-to"));
	String ccMi = CCUtil.urlEncode(mi);

	// MI の署名
	String ccSig = t.genHash(CCCustom.merchantID, CCCustom.key, ccMi);

	// create token
	Hashtable token = new Hashtable();
	token.put("CC_MI", ccMi);
	token.put("CC_SIG", ccSig);
	token.put("order-id", id);
	token.put("price", price);
	token.put("jprice", jprice);
	token.put("note", note);
	token.put("payto", (String)result.get("url-pay-to"));
	token.put("brandlogo", CCCustom.getCardLogo((String)result.get("accepts")));
	token.put("cclogo", CCUtil.getCClogo());

	// カード情報入力画面の表示.
	try {
	    CCUtil.printTemplate(out, CCCustom.wallet, token);
	}
	catch (IOException e) {
	    CCCustom.temporaryDifficulties(out, CCCustom.wallet + "&nbsp;が開けませんでした。");
	}

	// 支払前処理の実行
	CCCustom.preStoreFulfillmentInfo();
    }

}

