Skip to content

Instantly share code, notes, and snippets.

@hangum
Last active June 7, 2016 15:05
Show Gist options
  • Save hangum/9578bed51f8b18a5884329e969162256 to your computer and use it in GitHub Desktop.
Save hangum/9578bed51f8b18a5884329e969162256 to your computer and use it in GitHub Desktop.
Eclipse SWT를 이용한 아임포트(http://www.iamport.kr/) 결제 모듈
/*******************************************************************************
* Copyright (c) 2016 hangum.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* Contributors:
* hangum - initial API and implementation
******************************************************************************/
package com.hangum.tadpole.bill.core.dialogs;
import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.BrowserFunction;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import com.hangum.tadpole.bill.core.Messages;
import com.hangum.tadpole.bill.core.define.BillDefine;
import com.hangum.tadpole.commons.util.GlobalImageUtils;
import com.hangum.tadpole.engine.query.dao.system.bill.UserBillDAO;
/**
* 결제 다이얼로그
* - 아이엠포트(http://www.iamport.kr/)를 사용하여 결제
*
* @author hangum
*
*/
public class IAMPortDialog extends Dialog {
private static final Logger logger = Logger.getLogger(IAMPortDialog.class);
private Browser browser = null;
private UserBillDAO billDao = null;
private BillCallbackFunction callBackFunction = null;
/**
* Create the dialog.
* @param parentShell
*/
public IAMPortDialog(Shell parentShell, UserBillDAO billDao) {
super(parentShell);
this.billDao = billDao;
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(Messages.get().IAMPortDialog_title);
newShell.setImage(GlobalImageUtils.getTadpoleIcon());
}
/**
* Create contents of the dialog.
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
GridLayout gridLayout = (GridLayout) container.getLayout();
gridLayout.verticalSpacing = 5;
gridLayout.horizontalSpacing = 5;
gridLayout.marginHeight = 5;
gridLayout.marginWidth = 5;
Composite composite = new Composite(container, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Label labelExistMsg = new Label(composite, SWT.NONE);
labelExistMsg.setText(Messages.get().BillExistMsg);
browser = new Browser(composite, SWT.NONE);
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
browser.setText(makeHTML());
callBackFunction = new BillCallbackFunction(browser, "BiillResultFunction", this);
return container;
}
/**
* Create contents of the button bar.
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
GridLayout layout = (GridLayout)parent.getLayout();
layout.marginHeight = 0;
parent.setLayout(layout);
}
/**
* make bill html
*
* @return
*/
private String makeHTML() {
StringBuffer billHtml = new StringBuffer("<html><body onbeforeunload=\"closePage()\">").append("\n")
.append("<script src=\"https://code.jquery.com/jquery-1.12.3.min.js\" integrity=\"sha256-aaODHAgvwQW1bFOGXMeX+pC4PZIPsvn2h1sArYOhgXQ=\" crossorigin=\"anonymous\"></script>").append("\n")
.append("<script type=\"text/javascript\" src=\"https://service.iamport.kr/js/iamport.payment-1.1.1.js\"></script>").append("\n")
.append("<script language=\"javaScript\">").append("\n")
.append("function sendResultFail(msg) {").append("\n")
.append(" BiillResultFunction('fail', msg);").append("\n")
.append("}").append("\n")
.append("function sendResultSuccess(imp_uid, pg_tid, apply_num, paid_at) {").append("\n")
.append(" BiillResultFunction('success', imp_uid, pg_tid, apply_num, paid_at);").append("\n")
.append("}").append("\n")
.append("function closePage() {").append("\n")
.append(" BiillResultFunction('closePage');").append("\n")
.append("}").append("\n")
.append("</script>").append("\n")
.append("<script>").append("\n")
.append("var IMP = window.IMP;").append("\n")
.append(String.format("IMP.init('%s');", billDao.getIamport_code())).append("\n")
.append("IMP.request_pay({").append("\n")
.append(String.format("pg : '%s',", billDao.getPg_company())).append("\n")
.append(String.format("pay_method : '%s',", billDao.getPay_method())).append("\n")//, 'card' : 신용카드 | 'trans' : 실시간계좌이체 | 'vbank' : 가상계좌 | 'phone' : 휴대폰소액결제")
.append(String.format("escrow : false, digital : true,")).append("\n")//, 상품이 컨텐츠인 경우(실물이 아닌 경우) true(기본값 : false)
.append(String.format("merchant_uid : '%s',", billDao.getMerchant_uid())).append("\n")
.append(String.format("name : '%s',", billDao.getName())).append("\n")
.append(String.format("amount : %s,", billDao.getTotal_amount())).append("\n")
.append(String.format("buyer_email : '%s',", billDao.getBuyer_email())).append("\n")
.append(String.format("buyer_name : '%s',", billDao.getBuyer_name())).append("\n")
.append(String.format("buyer_tel : '%s',", billDao.getBuyer_tel())).append("\n")
.append(String.format("buyer_addr : '%s',", billDao.getBuyer_addr())).append("\n")
.append(String.format("buyer_postcode : '%s'", billDao.getBuyer_postcode())).append("\n")
.append("}, function(rsp) {").append("\n")
.append("if ( rsp.success ) {").append("\n")
.append("sendResultSuccess(rsp.imp_uid, rsp.pg_tid, rsp.apply_num, rsp.paid_at);").append("\n")
.append("} else {").append("\n")
.append("sendResultFail(rsp.error_msg);").append("\n")
.append("}").append("\n")
.append("});").append("\n")
.append("</script>").append("\n")
.append("</body></html>");
if(logger.isDebugEnabled()) logger.debug(billHtml.toString());
return billHtml.toString();
}
/**
* Return the initial size of the dialog.
*/
@Override
protected Point getInitialSize() {
return new Point(810, 676);
}
/**
*
* @return
*/
public UserBillDAO getBillDao() {
return billDao;
}
}
/**
* Browser callback function
*
* @author hangum
*/
class BillCallbackFunction extends BrowserFunction {
private static final Logger logger = Logger.getLogger(BillCallbackFunction.class);
private IAMPortDialog billDialog;
public BillCallbackFunction(Browser browser, String name, IAMPortDialog billDialog) {
super(browser, name);
this.billDialog = billDialog;
}
@Override
public Object function(Object[] arguments) {
UserBillDAO dao = billDialog.getBillDao();
String strResult = ""+arguments[0];
if("success".equals(strResult)) {
dao.setResult_status(BillDefine.RESULT_STATUS.SUCCESS.name());
dao.setSucc_imp_uid(""+arguments[1]);
dao.setSucc_pg_tid(""+arguments[2]);
dao.setSucc_apply_num(""+arguments[3]);
dao.setSucc_paid_at(""+arguments[4]);
} else if("fail".equals(strResult)) {
dao.setResult_status(BillDefine.RESULT_STATUS.FAIL.name());
dao.setFail_message(""+arguments[1]);
} else if("closePage".equals(strResult)) {
logger.debug("======> cloase page");
}
if(logger.isDebugEnabled()) logger.debug(billDialog.getBillDao());
this.billDialog.close();
return super.function(arguments);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment