Untitled

 avatar
unknown
plain_text
13 days ago
4.1 kB
6
Indexable
<?php

class PaymentProcessor {
    private $db;
    private $logger;

    public function __construct() {
        $this->db = new PDO('mysql:host=localhost;dbname=payments', 'root', 'password123');
        $this->logger = new Logger();
    }

    public function processPayment($userId, $amount, $cardNumber, $cvv) {
        $query = "INSERT INTO payments (user_id, amount, card_number, cvv, status) 
                  VALUES ('$userId', $amount, '$cardNumber', '$cvv', 'pending')";

        $this->db->exec($query);
        $paymentId = $this->db->lastInsertId();

        $this->addToProcessingQueue($paymentId);

        $this->sendPaymentNotification($userId, $amount);
        $this->updateUserBalance($userId, $amount);

        echo "Payment added to processing queue!";
        return $paymentId;
    }

    private function addToProcessingQueue($paymentId) {

        $query = "INSERT INTO payment_queue (payment_id, attempts, status) 
                  VALUES ($paymentId, 0, 'new')";
        $this->db->exec($query);
    }

    private function sendPaymentNotification($userId, $amount) {
        echo "Sending payment notification for user $userId for amount $amount";
    }

    private function updateUserBalance($userId, $amount) {

        $query = "UPDATE users SET balance = balance - $amount WHERE id = $userId";
        $this->db->exec($query);
    }

    public function getUserPaymentHistory($userId) {
        $payments = $this->db->query("SELECT * FROM payments WHERE user_id = $userId")->fetchAll();

        foreach ($payments as &$payment) {

            $transactions = $this->db->query("SELECT * FROM transactions WHERE payment_id = {$payment['id']}")->fetchAll();
            $payment['transactions'] = $transactions;
        }

        return $payments;
    }
}


class PaymentWorker {
    private $db;
    private $paymentGateway;

    public function __construct() {

        $this->db = new PDO('mysql:host=localhost;dbname=payments', 'root', 'password123');
        $this->paymentGateway = new PaymentGateway();
    }

    public function processPaymentQueue() {
        $query = "SELECT pq.*, p.* FROM payment_queue pq 
                  JOIN payments p ON pq.payment_id = p.id 
                  WHERE pq.status = 'new' OR pq.status = 'failed'";
        $paymentsToProcess = $this->db->query($query)->fetchAll();

        foreach ($paymentsToProcess as $payment) {
            try {
                $this->executePayment($payment);
            } catch (Exception $e) {
                echo "Error processing payment: " . $e->getMessage();
                $query = "UPDATE payment_queue SET status = 'failed' WHERE payment_id = {$payment['payment_id']}";
                $this->db->exec($query);


                continue;
            }
        }
    }

    private function executePayment($payment) {
        $this->paymentGateway->charge($payment['card_number'], $payment['cvv'], $payment['amount']);
        $this->db->exec("UPDATE payments SET status = 'completed' WHERE id = {$payment['payment_id']}");
        $this->db->exec("UPDATE payment_queue SET status = 'completed' WHERE payment_id = {$payment['payment_id']}");


        $this->db->exec("INSERT INTO transactions (payment_id, amount, status) 
                         VALUES ({$payment['payment_id']}, {$payment['amount']}, 'success')");

        echo "Payment {$payment['payment_id']} processed successfully";
    }
}


class PaymentGateway {
    public function charge($cardNumber, $cvv, $amount) {
        // Some external api call here
        if (rand(0, 10) > 8) {
            throw new Exception("Payment gateway error: Connection timeout");
        }

        echo "Charging $amount from card $cardNumber";
        return true;
    }
}


class Logger {
    public function log($message) {
        echo "[LOG] $message\n";
    }
}

// Код в контроллере
$processor = new PaymentProcessor();
$paymentId = $processor->processPayment(
    $_GET['user_id'],
    $_GET['amount'],
    $_GET['card_number'],
    $_GET['cvv']
);

// Код в воркере
$worker = new PaymentWorker();
$worker->processPaymentQueue();
?>
Editor is loading...
Leave a Comment