SagarRajput2056 / DjangoEcommerce

0 stars 0 forks source link

esewa integration #1

Open igaurab opened 2 months ago

igaurab commented 2 months ago
<body>
    <form action="https://rc-epay.esewa.com.np/api/epay/main/v2/form" method="POST" onsubmit="generateSignature()">
    <input type="text" id="amount" name="amount" value="100" required>
    <input type="text" id="tax_amount" name="tax_amount" value ="10" required>
    <input type="text" id="total_amount" name="total_amount" value="110" required>
    <input type="text" id="transaction_uuid" name="transaction_uuid" value="1234">
    <input type="text" id="product_code" name="product_code" value ="EPAYTEST" required>
    <input type="text" id="product_service_charge" name="product_service_charge" value="0" required>
    <input type="text" id="product_delivery_charge" name="product_delivery_charge" value="0" required>
    <input type="text" id="success_url" name="success_url" value="https://esewa.com.np" required>
    <input type="text" id="failure_url" name="failure_url" value="https://google.com" required>
    <input type="text" id="signed_field_names" name="signed_field_names" value="total_amount,transaction_uuid,product_code" required>
    <input type="text" id="signature" name="signature">
    <input value="Submit" type="submit">
    </form>
   </body>

   <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1
/crypto-js.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1
/hmac-sha256.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1
/enc-base64.min.js"></script>

<script>
// Function to auto-generate signature
function generateSignature() {
    var currentTime = new Date();
    var formattedTime = currentTime.toISOString().slice(2, 10).replace(/-/g, '') + '-' + currentTime.getHours() +
        currentTime.getMinutes() + currentTime.getSeconds();
    document.getElementById("transaction_uuid").value = formattedTime;
    var total_amount = document.getElementById("total_amount").value;
    var transaction_uuid = document.getElementById("transaction_uuid").value;
    var product_code = document.getElementById("product_code").value;
    var secret = "8gBm/:&EnhH.1/q"
    // document.getElementById("secret").value;

    var hash = CryptoJS.HmacSHA256(
        `total_amount=${total_amount},transaction_uuid=${transaction_uuid},product_code=${product_code}`,
        `${secret}`);
    var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
    document.getElementById("signature").value = hashInBase64;
}

// Event listeners to call generateSignature() when inputs are changed
document.getElementById("total_amount").addEventListener("input", generateSignature);
document.getElementById("transaction_uuid").addEventListener("input", generateSignature);
document.getElementById("product_code").addEventListener("input", generateSignature);
</script>
igaurab commented 2 months ago

When the payment is succcessful esewa will send a GET request to the success_url defined above with the data

Eg:

https://webhook.site/47b86944-5062-46b1-9ead-7eea3d827db4?data=eyJ0cmFuc2FjdGlvbl9jb2RlIjoiMDAwN0IwQiIsInN0YXR1cyI6IkNPTVBMRVRFIiwidG90YWxfYW1vdW50IjoiMTEwLjAiLCJ0cmFuc2FjdGlvbl91dWlkIjoiMjQwNDE4LTExNTEyOCIsInByb2R1Y3RfY29kZSI6IkVQQVlURVNUIiwic2lnbmVkX2ZpZWxkX25hbWVzIjoidHJhbnNhY3Rpb25fY29kZSxzdGF0dXMsdG90YWxfYW1vdW50LHRyYW5zYWN0aW9uX3V1aWQscHJvZHVjdF9jb2RlLHNpZ25lZF9maWVsZF9uYW1lcyIsInNpZ25hdHVyZSI6IkI3VlE0ZDUzWnJNM2lMTTJjcnpwa1FZS1pCV0pCcitGMzgwYThuL1RiaGM9In0=

This data is base64 encoded. When decoded

{"transaction_code":"0007B0B","status":"COMPLETE","total_amount":"110.0","transaction_uuid":"240418-115128","product_code":"EPAYTEST","signed_field_names":"transaction_code,status,total_amount,transaction_uuid,product_code,signed_field_names","signature":"B7VQ4d53ZrM3iLM2crzpkQYKZBWJBr+F380a8n/Tbhc="}