I-Import API를 통한 결제가 가능하도록 구현
상품이 여러 개일 경우 리스트를 통해 주문하여 수령하도록 하였다.
<script>
$(function(){
$("#buy").click(function (){
var IMP = window.IMP; // 생략가능
IMP.init('imp48507577');
// 'iamport' 대신 부여받은 "가맹점 식별코드"를 사용
// i'mport 관리자 페이지 -> 내정보 -> 가맹점식별코드
$("#selectAddress").change(function(){
$("#addressStatus").val("N");
});
if($("#addressStatus").val()!='Y'){
alert("주소를 선택하세요");
return false;
}
if($("#recipient_request").val()==""){
alert("요청사항을 입력하세요");
return false;
}
IMP.request_pay({
pg: 'html5_inicis',
pay_method: 'card',
merchant_uid: 'merchant_' + new Date().getTime(),
name:'<c:forEach var="pvo" items="${plist}">(${pvo.product_name})</c:forEach>${bvo.product_name}',
amount: $("input(name=total)").val(),//가격
buyer_email: '${uvo.user_email}',
buyer_name: $("#receiver_name").val(),
buyer_tel: $("#receiver_tel").val(),
buyer_postcode: $("#receiver_zipcode").val(),
buyer_addr: $("#receiver_addr").val()
}, function (rsp) {
if(rsp.success) {
var msg = '결제가 완료되었습니다.';
var orderData = {
order_num: rsp.imp_uid,
merchant_uid: rsp.merchant_uid,
genie_id:$("input(name=genie_id)").val(),
cartList: cartList,
product_id:$("input(name=product_id").val(),
order_price: $("input(name=cart_price)").val(),
order_qty: $("input(name=cart_qty)").val(),
recipient_name: $("#receiver_name").val(),
recipient_phone: $("#receiver_tel").val(),
recipient_address: $("#receiver_addr").val(),
recipient_request: $("#recipient_request").val(),
total_price: $("input(name=total)").val(),
payment_method: rsp.card_name,
};//data
//alert(JSON.stringify(orderData));
if(rsp.paid_amount == orderData.total_price){
$.ajax({
url: "/order/orderCompletion", // 예: https://www.myservice.com/payments/complete
data: orderData,
type: "get",
contentType: "application/json",
async: false,
success:function(result){
window.location.replace("/order/completion");
//console.log(orderData);
},error:function(e){
console.log(e.responseText);
}
});
}else {
alert("결제 실패");
}
}else{
var msg = '결제에 실패하였습니다.';
msg += '에러내용 : ' + rsp.error_msg;
}
alert(msg);
});
});
});