shubraVeil/includes/Payment.php
2024-12-25 14:31:31 +02:00

110 lines
4.2 KiB
PHP

<?php
class Payment {
private $db;
private $stripe;
private $paypal;
public function __construct($db) {
$this->db = $db;
// Initialize Stripe
if (getenv('STRIPE_SECRET_KEY')) {
\Stripe\Stripe::setApiKey(getenv('STRIPE_SECRET_KEY'));
$this->stripe = new \Stripe\StripeClient(getenv('STRIPE_SECRET_KEY'));
}
// Initialize PayPal
if (getenv('PAYPAL_CLIENT_ID') && getenv('PAYPAL_CLIENT_SECRET')) {
$this->paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
getenv('PAYPAL_CLIENT_ID'),
getenv('PAYPAL_CLIENT_SECRET')
)
);
}
}
public function processStripePayment($amount, $currency, $token, $description) {
try {
$charge = \Stripe\Charge::create([
'amount' => $amount * 100, // Convert to cents
'currency' => $currency,
'source' => $token,
'description' => $description,
]);
$this->saveTransaction($charge->id, 'stripe', $amount, $currency, $charge->status);
return ['success' => true, 'transaction_id' => $charge->id];
} catch (\Stripe\Exception\CardException $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
public function processPayPalPayment($amount, $currency, $returnUrl, $cancelUrl) {
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setTotal($amount);
$amount->setCurrency($currency);
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl($returnUrl)
->setCancelUrl($cancelUrl);
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
try {
$payment->create($this->paypal);
return ['success' => true, 'approval_url' => $payment->getApprovalLink()];
} catch (\PayPal\Exception\PayPalConnectionException $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
public function executePayPalPayment($paymentId, $payerId) {
$payment = \PayPal\Api\Payment::get($paymentId, $this->paypal);
$execution = new \PayPal\Api\PaymentExecution();
$execution->setPayerId($payerId);
try {
$result = $payment->execute($execution, $this->paypal);
$this->saveTransaction($result->getId(), 'paypal', $result->getTransactions()[0]->getAmount()->getTotal(), $result->getTransactions()[0]->getAmount()->getCurrency(), $result->getState());
return ['success' => true, 'transaction_id' => $result->getId()];
} catch (\PayPal\Exception\PayPalConnectionException $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
private function saveTransaction($transaction_id, $provider, $amount, $currency, $status) {
$stmt = $this->db->prepare("INSERT INTO transactions (transaction_id, provider, amount, currency, status, created_at) VALUES (?, ?, ?, ?, ?, NOW())");
$stmt->bind_param('ssdss', $transaction_id, $provider, $amount, $currency, $status);
$stmt->execute();
}
public function getTransaction($transaction_id) {
$stmt = $this->db->prepare("SELECT * FROM transactions WHERE transaction_id = ?");
$stmt->bind_param('s', $transaction_id);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
public function getUserTransactions($user_id) {
$stmt = $this->db->prepare("SELECT * FROM transactions WHERE user_id = ? ORDER BY created_at DESC");
$stmt->bind_param('i', $user_id);
$stmt->execute();
return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
}