niammuddin

dokuPayment controller nodejs express

Aug 19th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.dokuPayment = async (req, res) => {
  2.  
  3.   // doku payment gateway
  4.   const DOKU_BASE_URL = 'https://api-sandbox.doku.com';
  5.   const client_id = "BRN-xxxx-xxxxxxxxxxx"; // Ganti dengan Client-ID Anda
  6.   const secret_key = "SK-xxxxxxxxxxxx"; // Ganti dengan Secret-Key Anda
  7.   const url = "/checkout/v1/payment";
  8.  
  9.   // Fungsi untuk mendapatkan timestamp terbaru
  10.   function getCurrentTimestamp() {
  11.     return new Date().toISOString().slice(0, 19) + "Z";
  12.   }
  13.  
  14.   // Fungsi untuk menghasilkan Digest
  15.   function generateDigest(jsonBody) {
  16.     let jsonStringHash256 = crypto
  17.       .createHash("sha256")
  18.       .update(jsonBody, "utf-8")
  19.       .digest();
  20.     let bufferFromJsonStringHash256 = Buffer.from(jsonStringHash256);
  21.     return bufferFromJsonStringHash256.toString("base64");
  22.   }
  23.  
  24.   // Fungsi untuk menghasilkan Signature
  25.   function generateSignature(clientId, requestId, requestTarget, digest, secret) {
  26.     let requestTimestamp = getCurrentTimestamp();
  27.     let componentSignature = `Client-Id:${clientId}\n`;
  28.     componentSignature += `Request-Id:${requestId}\n`;
  29.     componentSignature += `Request-Timestamp:${requestTimestamp}\n`;
  30.     componentSignature += `Request-Target:${requestTarget}`;
  31.     if (digest) {
  32.       componentSignature += `\nDigest:${digest}`;
  33.     }
  34.  
  35.     let hmac256Value = crypto
  36.       .createHmac("sha256", secret)
  37.       .update(componentSignature.toString())
  38.       .digest();
  39.     let bufferFromHmac256Value = Buffer.from(hmac256Value);
  40.     let signature = bufferFromHmac256Value.toString("base64");
  41.     return "HMACSHA256=" + signature;
  42.   }
  43.  
  44.   const request_id = '12'; // Generate unique request ID
  45.   const requestTimestamp = getCurrentTimestamp();
  46.  
  47.   let jsonBody = JSON.stringify({
  48.     order: {
  49.       amount: 1000,
  50.       // invoice_number: `INV-${requestTimestamp}`,
  51.       invoice_number: 'INV-100',
  52.       currency: "IDR",
  53.       callback_url: "https://xxx.id/", // Ganti dengan URL callback Anda
  54.       callback_url_cancel: "https://xxx.id/", // Ganti dengan URL callback Anda
  55.     },
  56.     payment: {
  57.       payment_due_date: 2,
  58.       // payment_method_types: ["QRIS"],
  59.     },
  60.     customer: {
  61.       id: "1",
  62.       name: "Zolanda",
  63.       email: "zolanda@example.com",
  64.       phone: "628121212121",
  65.       address: "Jl. Imam Bonjol No 14",
  66.       country: "ID",
  67.     },
  68.   });
  69.  
  70.   let digest = generateDigest(jsonBody);
  71.   let headerSignature = generateSignature(client_id, request_id, url, digest, secret_key);
  72.  
  73.   const headers = {
  74.     "Client-Id": client_id,
  75.     "Request-Id": request_id,
  76.     "Request-Timestamp": requestTimestamp,
  77.     Signature: headerSignature,
  78.     "Content-Type": "application/json",
  79.   };
  80.  
  81.   try {
  82.     const response = await fetch(DOKU_BASE_URL + url, {
  83.       method: "POST",
  84.       headers: headers,
  85.       body: jsonBody,
  86.     });
  87.  
  88.     const data = await response.json();
  89.     if (response.ok) {
  90.       res.json(data);
  91.     } else {
  92.       res.status(response.status).json(data);
  93.     }
  94.   } catch (error) {
  95.     res.status(500).json({ error: error.message });
  96.   }
  97.  
  98. };
Add Comment
Please, Sign In to add comment