Node.js
Node.js ile Paylox API entegrasyonu. Bu rehber, fetch API kullanarak temel işlemleri gösterir.
Kurulum
Ek bir paket gerekmez. Node.js 18+ sürümü fetch API'yi dahili olarak destekler. Daha eski sürümlerde node-fetch kullanabilirsiniz.
npm install node-fetch
Yapılandırma
const PAYLOX_API_URL = process.env.PAYLOX_API_URL || "https://api.jetcheckout.com/api/v1";
const PAYLOX_API_KEY = process.env.PAYLOX_API_KEY;
async function payloxRequest(endpoint, body) {
const response = await fetch(`${PAYLOX_API_URL}${endpoint}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || `HTTP ${response.status}`);
}
return response.json();
}
Session Oluşturma
async function createSession(amount, customer, urls) {
return payloxRequest("/embedded/session", {
merchant_api_key: PAYLOX_API_KEY,
amount,
currency: "TRY",
customer,
success_url: urls.success,
fail_url: urls.fail
});
}
const session = await createSession(15000, {
name: "Ahmet",
surname: "Yılmaz",
email: "[email protected]"
}, {
success: "https://yoursite.com/payment/success",
fail: "https://yoursite.com/payment/fail"
});
Ödeme Yapma
async function makePayment(sessionId, cardData) {
return payloxRequest("/payment/pay", {
session_id: sessionId,
payment_type: "creditcard",
card_holder_name: cardData.holderName,
card_number: cardData.number,
expire_month: cardData.expMonth,
expire_year: cardData.expYear,
cvc: cardData.cvc,
is_3d: true,
installment_count: cardData.installments || "1"
});
}
Ödeme Durumu Sorgulama
async function getPaymentStatus(orderId, sessionId) {
const response = await fetch(
`${PAYLOX_API_URL}/payment/status/${orderId}?session_id=${sessionId}`
);
return response.json();
}
Express.js Örneği
const express = require("express");
const app = express();
app.post("/api/checkout", async (req, res) => {
const { amount, customer } = req.body;
const session = await createSession(amount, customer, {
success: `${req.protocol}://${req.get("host")}/payment/success`,
fail: `${req.protocol}://${req.get("host")}/payment/fail`
});
res.json({
session_id: session.session_id,
checkout_url: session.checkout_url
});
});
app.get("/payment/success", async (req, res) => {
const { order_id, session_id } = req.query;
const status = await getPaymentStatus(order_id, session_id);
if (status.success && status.status === "completed") {
res.render("success", { order_id });
} else {
res.render("error", { message: "Odeme dogrulanamadi" });
}
});
app.listen(3000);