토스페이먼츠 v2 결제 모듈 연동

This commit is contained in:
chym1217
2025-09-16 16:34:15 +09:00
parent 1eee11e433
commit 46ea2d03b5
44 changed files with 2614 additions and 20 deletions

View File

@ -0,0 +1,6 @@
<?php
include_once('../../../common.php');
if (!defined('G5_USE_SHOP') || !G5_USE_SHOP)
die('<p>쇼핑몰 설치 후 이용해 주십시오.</p>');
define('_SHOP_', true);

View File

@ -0,0 +1,33 @@
<?php
if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
?>
<form name="sm_form" method="POST" action="<?php echo G5_MSHOP_URL; ?>/toss/toss_approval.php">
<input type="hidden" name="method" value="">
<input type="hidden" name="orderId" value="<?php echo isset($od_id) ? $od_id : ''; ?>">
<input type="hidden" name="orderName" value="<?php echo isset($goods) ? $goods : ''; ?>">
<input type="hidden" name="customerName" value="<?php echo isset($od_name) ? $od_name : ''; ?>">
<input type="hidden" name="customerEmail" value="<?php echo isset($od_email) ? $od_email : ''; ?>">
<input type="hidden" name="customerMobilePhone" value="<?php echo isset($od_hp) ? $od_hp : ''; ?>">
<input type="hidden" name="cardUseEscrow" value="false">
<input type="hidden" name="escrowProducts" value=''>
<input type="hidden" name="cardflowMode" value="DEFAULT">
<input type="hidden" name="cardeasyPay" value="PAYCO">
<input type="hidden" name="cardUseCardPoint" value="false">
<input type="hidden" name="cardUseAppCardOnly" value="false">
<input type="hidden" name="amountCurrency" value="KRW">
<input type="hidden" name="amountValue" value="<?php echo isset($tot_price) ? $tot_price : 0; ?>">
<input type="hidden" name="taxFreeAmount" value="<?php echo isset($comm_free_mny) ? $comm_free_mny : 0; ?>">
<input type="hidden" name="windowTarget" value="iframe">
<input type="hidden" name="good_mny" value="<?php echo $tot_price; ?>">
<?php
if($default['de_tax_flag_use']) {
?>
<input type="hidden" name="comm_tax_mny" value="<?php echo isset($comm_tax_mny) ? $comm_tax_mny : 0; ?>"> <!-- 과세금액 -->
<input type="hidden" name="comm_vat_mny" value="<?php echo isset($comm_vat_mny) ? $comm_vat_mny : 0; ?>"> <!-- 부가세 -->
<input type="hidden" name="comm_free_mny" value="<?php echo isset($comm_free_mny) ? $comm_free_mny : 0; ?>"> <!-- 비과세 금액 -->
<?php
}
?>
</form>

View File

@ -0,0 +1,37 @@
<?php
if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
?>
<input type="hidden" name="method" value="">
<input type="hidden" name="orderId" value="<?php echo isset($od_id) ? $od_id : ''; ?>">
<input type="hidden" name="orderName" value="<?php echo isset($goods) ? $goods : ''; ?>">
<input type="hidden" name="customerName" value="<?php echo isset($od_name) ? $od_name : ''; ?>">
<input type="hidden" name="customerEmail" value="<?php echo isset($od_email) ? $od_email : ''; ?>">
<input type="hidden" name="customerMobilePhone" value="<?php echo isset($od_hp) ? $od_hp : ''; ?>">
<input type="hidden" name="cardUseEscrow" value="false">
<input type="hidden" name="escrowProducts" value=''>
<input type="hidden" name="cardflowMode" value="DEFAULT">
<input type="hidden" name="cardeasyPay" value="PAYCO">
<input type="hidden" name="cardUseCardPoint" value="false">
<input type="hidden" name="cardUseAppCardOnly" value="false">
<input type="hidden" name="amountCurrency" value="KRW">
<input type="hidden" name="amountValue" value="<?php echo isset($tot_price) ? $tot_price : 0; ?>">
<input type="hidden" name="taxFreeAmount" value="<?php echo isset($comm_free_mny) ? $comm_free_mny : 0; ?>">
<input type="hidden" name="windowTarget" value="iframe">
<input type="hidden" name="good_mny" value="<?php echo $tot_price; ?>">
<?php
if($default['de_tax_flag_use']) {
?>
<input type="hidden" name="comm_tax_mny" value="<?php echo isset($comm_tax_mny) ? $comm_tax_mny : 0; ?>"> <!-- 과세금액 -->
<input type="hidden" name="comm_vat_mny" value="<?php echo isset($comm_vat_mny) ? $comm_vat_mny : 0; ?>"> <!-- 부가세 -->
<input type="hidden" name="comm_free_mny" value="<?php echo isset($comm_free_mny) ? $comm_free_mny : 0; ?>"> <!-- 비과세 금액 -->
<?php
}
?>
<div id="display_pay_button" class="btn_confirm">
<span id="show_req_btn"><input type="button" name="submitChecked" onClick="pay_approval();" value="결제등록요청" class="btn_submit"></span>
<span id="show_pay_btn" style="display:none;"><input type="button" onClick="forderform_check();" value="주문하기" class="btn_submit"></span>
<a href="<?php echo G5_SHOP_URL; ?>" class="btn_cancel">취소</a>
</div>

View File

@ -0,0 +1,3 @@
<?php
if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
?>

View File

@ -0,0 +1,77 @@
<?php
include_once('./_common.php');
// 결제 실패 처리인 경우
if (isset($_REQUEST['mode']) && $_REQUEST['mode'] === 'fail') {
$code = isset($_REQUEST['code']) ? trim($_REQUEST['code']) : '';
$message = isset($_REQUEST['message']) ? trim($_REQUEST['message']) : '';
alert('결제에 실패하였습니다.\\n\\n[' . $code . '] ' . $message, G5_SHOP_URL . '/orderform.php');
exit;
}
if(!isset($_SESSION['PAYREQ_MAP'])){
alert('세션이 만료 되었거나 유효하지 않은 요청 입니다.', G5_MSHOP_URL);
}
$payReqMap = $_SESSION['PAYREQ_MAP']; //결제 요청시, Session에 저장했던 파라미터 MAP
$g5['title'] = '토스페이먼츠 결제인증 완료처리';
$g5['body_script'] = ' onload="setTossResult();"';
include_once(G5_PATH.'/head.sub.php');
// 토스페이먼츠 결제인증 성공시 인증키 주문 임시데이터에 업데이트
$paymentKey = isset($_REQUEST['paymentKey']) ? trim($_REQUEST['paymentKey']) : '';
$orderId = isset($_REQUEST['orderId']) ? trim($_REQUEST['orderId']) : '';
$amount = isset($_REQUEST['amount']) ? trim($_REQUEST['amount']) : '';
if (empty($paymentKey) || empty($orderId)) {
alert('결제정보가 올바르지 않습니다.', G5_MSHOP_URL);
exit;
}
$sql = " select * from {$g5['g5_shop_order_data_table']} where od_id = '$orderId' ";
$row = sql_fetch($sql);
$data = isset($row['dt_data']) ? unserialize(base64_decode($row['dt_data'])) : array();
// 주문 임시데이터에 paymentKey 업데이트
$data['paymentKey'] = $paymentKey;
$data_new = base64_encode(serialize($data));
$sql = " update {$g5['g5_shop_order_data_table']} set dt_data = '$data_new' where od_id = '$orderId' limit 1 ";
sql_query($sql);
if(isset($data['pp_id']) && $data['pp_id']) {
$order_action_url = G5_HTTPS_MSHOP_URL.'/personalpayformupdate.php';
} else {
$order_action_url = G5_HTTPS_MSHOP_URL.'/orderformupdate.php';
}
?>
<?php
$exclude = array();
echo '<form name="forderform" method="post" action="'.$order_action_url.'" autocomplete="off">'.PHP_EOL;
echo make_order_field($data, $exclude);
echo '</form>'.PHP_EOL;
?>
<div>
<div id="show_progress">
<span style="display:block; text-align:center;margin-top:120px"><img src="<?php echo G5_MOBILE_URL; ?>/shop/img/loading.gif" alt=""></span>
<span style="display:block; text-align:center;margin-top:10px; font-size:14px">주문완료 중입니다. 잠시만 기다려 주십시오.</span>
</div>
</div>
<script type="text/javascript">
function setTossResult() {
setTimeout( function() {
document.forderform.submit();
}, 300);
}
</script>
<?php
include_once(G5_PATH.'/tail.sub.php');

View File

@ -0,0 +1,192 @@
<?php
include_once('./_common.php');
// 토스페이먼츠 class
require_once(G5_SHOP_PATH.'/toss/toss.inc.php');
// 개인결제 ID와 주문 ID 설정
$ss_order_id = isset($_REQUEST['orderId']) ? $_REQUEST['orderId'] : '';
$ss_personalpay_id = get_session('ss_personalpay_id');
// 장바구니 ID 설정 (바로구매 여부 확인)
$ss_cart_id = get_session('ss_direct') ? get_session('ss_cart_direct') : get_session('ss_cart_id');
// WHERE 조건 추가용 변수
$addQuery = "";
if (!empty($ss_order_id)) {
$addQuery .= " AND od_id = '{$ss_order_id}'";
}
if (isset($member['mb_id']) && $member['mb_id'] !== '') {
$addQuery .= " AND mb_id = '{$member['mb_id']}'";
}
// 개인결제가 아닌 경우 장바구니 ID 조건 추가
if (empty($ss_personalpay_id)) {
if (!empty($ss_cart_id)) {
$addQuery .= " AND cart_id = '{$ss_cart_id}'";
}
}
// 최종 검증 (원래 로직 유지)
if (empty($ss_order_id) || (empty($ss_personalpay_id) && empty($ss_cart_id))) {
alert('주문정보가 올바르지 않습니다.');
exit;
}
// 기존 dt_data 가져오기
$sql = "
SELECT * FROM {$g5['g5_shop_order_data_table']}
WHERE 1=1
{$addQuery}
";
$res = sql_fetch($sql);
$dt_data = [];
if (isset($res['dt_data'])) {
$dt_data = unserialize(base64_decode($res['dt_data']));
}
$payReqMap = $dt_data;
$_SESSION['PAYREQ_MAP'] = $payReqMap;
if(isset($payReqMap['pp_id']) && $payReqMap['pp_id']) {
$page_return_url = G5_SHOP_URL.'/personalpayform.php?pp_id='.$payReqMap['pp_id'];
} else {
$page_return_url = G5_SHOP_URL.'/orderform.php';
if ($_SESSION['ss_direct']) {
$page_return_url .= '?sw_direct=1';
}
}
$g5['title'] = '토스페이먼츠 eCredit서비스 결제';
$g5['body_script'] = ' onload="launchCrossPlatform(frm);"';
include_once(G5_PATH.'/head.sub.php');
?>
<form name="forderform">
<?php
foreach($payReqMap as $key => $value) {
if (isset($_REQUEST[$key]) && $_REQUEST[$key]) {
$value = $_REQUEST[$key];
}
if (is_array($value)) {
$value = implode(',', $value);
}
if ($key === 'escrowProducts') {
$value = str_replace("\\", "", $value);
echo '<input type="hidden" name="'.$key.'" value=\''.$value.'\'>'.PHP_EOL;
} else {
echo '<input type="hidden" name="'.$key.'" value="'.$value.'">'.PHP_EOL;
}
}
?>
</form>
<script language="javascript" src="https://js.tosspayments.com/v2/standard"></script>
<script type="text/javascript">
/*
* 수정불가.
*/
const clientKey = "<?php echo $config['cf_toss_client_key']; ?>";
const customerKey = "<?php echo isset($member['mb_id']) ? $member['mb_id'] : ''; ?>";
const tossPayments = TossPayments(clientKey);
const payment = tossPayments.payment({ customerKey });
const frm = document.forderform;
/*
* 수정불가
*/
async function launchCrossPlatform(frm) {
// 필수 값들 체크
if (!frm.amountValue || !frm.amountValue.value) {
alert('결제 금액이 설정되지 않았습니다.');
return;
}
const amount = parseInt(frm.amountValue.value);
if (isNaN(amount) || amount <= 0) {
alert('올바른 결제 금액을 입력해주세요.');
return;
}
// 기본 결제 옵션
const paymentOptions = {
method: frm.method.value,
amount: {
currency: "KRW",
value: parseInt(frm.amountValue.value),
},
taxFreeAmount: parseInt(frm.taxFreeAmount.value),
orderId: frm.orderId.value, // 고유 주문번호
orderName: frm.orderName.value,
successUrl: "<?php echo G5_MSHOP_URL;?>/toss/returnurl.php", // 결제 요청이 성공하면 리다이렉트되는 URL
failUrl: "<?php echo G5_MSHOP_URL;?>/toss/returnurl.php?mode=fail", // 결제 요청이 실패하면 리다이렉트되는 URL
customerEmail: frm.customerEmail.value,
customerName: frm.customerName.value,
customerMobilePhone: frm.customerMobilePhone.value,
};
// escrowProducts 추가 함수
function addEscrowProducts(paymentMethodOptions) {
if (frm.cardUseEscrow.value === "true") {
if (frm.escrowProducts && frm.escrowProducts.value) {
paymentMethodOptions.escrowProducts = JSON.parse(frm.escrowProducts.value);
}
}
}
// 결제 방법에 따른 추가 옵션
if (frm.method.value == 'CARD') {
// 신용카드
paymentOptions.card = {
flowMode: frm.cardflowMode.value, // 통합결제창 여는 옵션
easyPay: frm.cardeasyPay.value,
useCardPoint: frm.cardUseCardPoint.value == "true" ? true : false,
useAppCardOnly: frm.cardUseAppCardOnly.value == "true" ? true : false,
useEscrow: frm.cardUseEscrow.value == "true" ? true : false,
};
// escrowProducts 추가
addEscrowProducts(paymentOptions.card);
} else if (frm.method.value == 'VIRTUAL_ACCOUNT') {
// 가상계좌
paymentOptions.virtualAccount = {
cashReceipt: {
type: "소득공제",
},
useEscrow: frm.cardUseEscrow.value == "true" ? true : false,
validHours: 168,
};
// escrowProducts 추가
addEscrowProducts(paymentOptions.virtualAccount);
} else if (frm.method.value == 'TRANSFER') {
// 계좌이체
paymentOptions.transfer = {
cashReceipt: {
type: "소득공제",
},
useEscrow: frm.cardUseEscrow.value == "true" ? true : false,
};
// escrowProducts 추가
addEscrowProducts(paymentOptions.transfer);
}
await payment.requestPayment(paymentOptions);
}
/*
* FORM 명만 수정 가능
*/
function getFormObject() {
return document.getElementById("forderform");
}
</script>
<?php
include_once(G5_PATH.'/tail.sub.php');