Code Examples
Complete working examples for the most common operations. All examples use the hosted payment flow — the simplest integration path.
Base URL: https://4pay.online/api/v1
Python
Create a payment
import requests
API_KEY = "YOUR_API_KEY"
BASE_URL = "https://4pay.online/api/v1"
response = requests.post(
f"{BASE_URL}/transactions",
headers={
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
json={
"params": {
"type": "payment",
"amount": 1000,
"currency": "USD",
"txid": "order-12345",
"returnUrl": "https://yoursite.com/success",
"failUrl": "https://yoursite.com/fail",
"notifyUrl": "https://yoursite.com/webhooks/payment",
}
},
)
tx = response.json()
print(f"Payment URL: {tx['payment_url']}")
# Redirect your user to tx['payment_url']
Check transaction status
tx_id = "550e8400-e29b-41d4-a716-446655440000"
response = requests.get(
f"{BASE_URL}/transactions/{tx_id}",
headers={"x-api-key": API_KEY},
)
tx = response.json()
print(f"Status: {tx['status']}") # created, charged, failed, etc.
Create a payout
response = requests.post(
f"{BASE_URL}/transactions",
headers={
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
json={
"params": {
"type": "payout",
"amount": 5000,
"currency": "RUB",
"cardnumber": "4111111111111111",
"txid": "payout-001",
}
},
)
tx = response.json()
print(f"Payout ID: {tx['id']}, Status: {tx['status']}")
Issue a refund
tx_id = "550e8400-e29b-41d4-a716-446655440000"
response = requests.put(
f"{BASE_URL}/transactions/{tx_id}",
headers={
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
json={
"params": {
"type": "refund",
"amount": 500, # partial refund; omit for full refund
}
},
)
print(f"Refund status: {response.json()['status']}")
Webhook handler (Flask)
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/webhooks/payment", methods=["POST"])
def webhook():
data = request.get_json()
tx_id = data["id"]
status = data["status"]
if status == "charged":
# Payment successful — fulfill the order
print(f"Order {data['txid']} paid successfully")
elif status == "failed":
print(f"Order {data['txid']} failed: {data.get('error_description')}")
return jsonify({"success": "true"})
if __name__ == "__main__":
app.run(port=3000)
Node.js
Create a payment
const API_KEY = "YOUR_API_KEY";
const BASE_URL = "https://4pay.online/api/v1";
const response = await fetch(`${BASE_URL}/transactions`, {
method: "POST",
headers: {
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
params: {
type: "payment",
amount: 1000,
currency: "USD",
txid: "order-12345",
returnUrl: "https://yoursite.com/success",
failUrl: "https://yoursite.com/fail",
notifyUrl: "https://yoursite.com/webhooks/payment",
},
}),
});
const tx = await response.json();
console.log(`Payment URL: ${tx.payment_url}`);
// Redirect your user to tx.payment_url
Check transaction status
const txId = "550e8400-e29b-41d4-a716-446655440000";
const response = await fetch(`${BASE_URL}/transactions/${txId}`, {
headers: { "x-api-key": API_KEY },
});
const tx = await response.json();
console.log(`Status: ${tx.status}`); // created, charged, failed, etc.
Create a payout
const response = await fetch(`${BASE_URL}/transactions`, {
method: "POST",
headers: {
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
params: {
type: "payout",
amount: 5000,
currency: "RUB",
cardnumber: "4111111111111111",
txid: "payout-001",
},
}),
});
const tx = await response.json();
console.log(`Payout ID: ${tx.id}, Status: ${tx.status}`);
Issue a refund
const txId = "550e8400-e29b-41d4-a716-446655440000";
const response = await fetch(`${BASE_URL}/transactions/${txId}`, {
method: "PUT",
headers: {
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
params: {
type: "refund",
amount: 500, // partial refund; omit for full refund
},
}),
});
const tx = await response.json();
console.log(`Refund status: ${tx.status}`);
Webhook handler (Express)
import express from "express";
const app = express();
app.use(express.json());
app.post("/webhooks/payment", (req, res) => {
const { id, txid, status, error_description } = req.body;
if (status === "charged") {
// Payment successful — fulfill the order
console.log(`Order ${txid} paid successfully`);
} else if (status === "failed") {
console.log(`Order ${txid} failed: ${error_description}`);
}
res.json({ success: "true" });
});
app.listen(3000);
PHP
Create a payment
<?php
$apiKey = "YOUR_API_KEY";
$baseUrl = "https://4pay.online/api/v1";
$ch = curl_init("$baseUrl/transactions");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"x-api-key: $apiKey",
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => json_encode([
"params" => [
"type" => "payment",
"amount" => 1000,
"currency" => "USD",
"txid" => "order-12345",
"returnUrl" => "https://yoursite.com/success",
"failUrl" => "https://yoursite.com/fail",
"notifyUrl" => "https://yoursite.com/webhooks/payment",
],
]),
]);
$response = curl_exec($ch);
curl_close($ch);
$tx = json_decode($response, true);
echo "Payment URL: " . $tx["payment_url"] . "\n";
// Redirect your user to $tx["payment_url"]
Webhook handler
<?php
$payload = json_decode(file_get_contents("php://input"), true);
$txId = $payload["id"];
$status = $payload["status"];
$txid = $payload["txid"];
if ($status === "charged") {
// Payment successful — fulfill the order
error_log("Order $txid paid successfully");
} elseif ($status === "failed") {
error_log("Order $txid failed: " . $payload["error_description"]);
}
header("Content-Type: application/json");
echo json_encode(["success" => "true"]);