'How do you call a function in a redirect field?
I think it would be better to show you the code first before I explain:
$(function() {
var IMP=window.IMP;
IMP.init('11111111');
$('.order-form').on('submit', function(e) {
var amount = parseFloat($('.order-form input[name="amount"]').val().replace(',',''));
var type=$('.order-form input[name="type"]:checked').val();
var order_id = AjaxCreateOrder(e);
if(order_id==false) {
alert('주문 생성 실패\n다시 시도해주세요.');
return false;
}
var merchant_id = AjaxStoreTransaction(e, order_id, amount, type);
if(merchant_id!=='') {
IMP.request_pay({
merchant_uid:merchant_id,
name:'Product',
buyer_name:$('input[name="first_name"]').val()+" "+$('input[name="last_name"]').val(),
buyer_email:$('input[name="email"]').val(),
amount:amount,
m_redirect_url: 'mywebsite/payments/complete',
}, function(rsp) {
if(rsp.success) {
var msg = '결제가 완료되었습니다.';
msg += '고유 ID : '+rsp.imp_uid;
// 결제 완료후 보여줄 메시지
ImpTransaction(e, order_id, rsp.merchant_uid, rsp.imp_uid, rsp.paid_amount);
} else {
var msg = '결제에 실패하였습니다.';
msg += '에러내용 : '+ rsp.error_msg;
console.log(msg);
}
});
}
return false;
});
});
function ImpTransaction(e, order_id, merchant_id, imp_id, amount) {
e.preventDefault();
var request = $.ajax({
method:"POST",
url:order_validation_url,
async:false,
data:{
order_id:order_id,
merchant_id:merchant_id,
imp_id:imp_id,
amount:amount,
csrfmiddlewaretoken:csrf_token
}
});
request.done(function(data) {
if(data.works) {
$(location).attr('href',location.origin+order_complete_url+'?order_id='+order_id)
}
});
request.fail(function(jqXHR, textStatus) {
if(jqXHR.status == 404) {
alert("페이지가 존재하지 않습니다.");
} else if(jqXHR.status==403) {
alert("로그인 해주세요.");
} else {
alert("문제가 발생했습니다.\n다시 시도해주세요.");
}
});
}
As you can see, I am using an Iamport(IMP) API, but that is not really important. What I want is when I run the m_redirect_url: 'mywebsite/payments/complete', I want to be able to run the ImpTransaction() simultaneously before reirecting to the url mywebsite/payments/complete. This is because the callback function function(rsp) does not work when I am using my django website on mobile. m_redirect_url is the url I redirect the user to once the transaction is complete in mobile.
Thank you, and please leave any comments or questions you have below.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
