Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.dokuPayment = async (req, res) => {
- // doku payment gateway
- const DOKU_BASE_URL = 'https://api-sandbox.doku.com';
- const client_id = "BRN-xxxx-xxxxxxxxxxx"; // Ganti dengan Client-ID Anda
- const secret_key = "SK-xxxxxxxxxxxx"; // Ganti dengan Secret-Key Anda
- const url = "/checkout/v1/payment";
- // Fungsi untuk mendapatkan timestamp terbaru
- function getCurrentTimestamp() {
- return new Date().toISOString().slice(0, 19) + "Z";
- }
- // Fungsi untuk menghasilkan Digest
- function generateDigest(jsonBody) {
- let jsonStringHash256 = crypto
- .createHash("sha256")
- .update(jsonBody, "utf-8")
- .digest();
- let bufferFromJsonStringHash256 = Buffer.from(jsonStringHash256);
- return bufferFromJsonStringHash256.toString("base64");
- }
- // Fungsi untuk menghasilkan Signature
- function generateSignature(clientId, requestId, requestTarget, digest, secret) {
- let requestTimestamp = getCurrentTimestamp();
- let componentSignature = `Client-Id:${clientId}\n`;
- componentSignature += `Request-Id:${requestId}\n`;
- componentSignature += `Request-Timestamp:${requestTimestamp}\n`;
- componentSignature += `Request-Target:${requestTarget}`;
- if (digest) {
- componentSignature += `\nDigest:${digest}`;
- }
- let hmac256Value = crypto
- .createHmac("sha256", secret)
- .update(componentSignature.toString())
- .digest();
- let bufferFromHmac256Value = Buffer.from(hmac256Value);
- let signature = bufferFromHmac256Value.toString("base64");
- return "HMACSHA256=" + signature;
- }
- const request_id = '12'; // Generate unique request ID
- const requestTimestamp = getCurrentTimestamp();
- let jsonBody = JSON.stringify({
- order: {
- amount: 1000,
- // invoice_number: `INV-${requestTimestamp}`,
- invoice_number: 'INV-100',
- currency: "IDR",
- callback_url: "https://xxx.id/", // Ganti dengan URL callback Anda
- callback_url_cancel: "https://xxx.id/", // Ganti dengan URL callback Anda
- },
- payment: {
- payment_due_date: 2,
- // payment_method_types: ["QRIS"],
- },
- customer: {
- id: "1",
- name: "Zolanda",
- email: "zolanda@example.com",
- phone: "628121212121",
- address: "Jl. Imam Bonjol No 14",
- country: "ID",
- },
- });
- let digest = generateDigest(jsonBody);
- let headerSignature = generateSignature(client_id, request_id, url, digest, secret_key);
- const headers = {
- "Client-Id": client_id,
- "Request-Id": request_id,
- "Request-Timestamp": requestTimestamp,
- Signature: headerSignature,
- "Content-Type": "application/json",
- };
- try {
- const response = await fetch(DOKU_BASE_URL + url, {
- method: "POST",
- headers: headers,
- body: jsonBody,
- });
- const data = await response.json();
- if (response.ok) {
- res.json(data);
- } else {
- res.status(response.status).json(data);
- }
- } catch (error) {
- res.status(500).json({ error: error.message });
- }
- };
Add Comment
Please, Sign In to add comment