'186691761268d6f85579c84532234998155044887968d6f85579c891167595247', 'base_url' => 'https://pay.wifibills.com/api', 'gateway_name' => 'PipraPay' ]; public static function create_transaction($trx, $user) { try { _log("=== PIPRAPAY CREATE TRANSACTION ===", 'PipraPay'); // Create ALL required URLs $success_url = U . 'callback/piprapay?invoice_id=' . $trx['id']; $cancel_url = U . 'order/view/' . $trx['id']; $webhook_url = U . 'callback/piprapay'; // FIXED: Handle null values for database fields $routers_id = $trx['routers'] ?? 0; $routers_name = $trx['name'] ?? 'Unknown'; $plan_name = $trx['plan_name'] ?? 'Unknown Plan'; _log("Routers ID: " . $routers_id . ", Routers Name: " . $routers_name, 'PipraPay'); // COMPLETE payload with ALL required fields $payload = [ 'full_name' => $user['fullname'] ?: $user['username'], 'email_mobile' => $user['email'] ?: ($user['phonenumber'] ?: 'user@example.com'), 'amount' => number_format($trx['price'], 2, '.', ''), 'redirect_url' => $success_url, 'return_type' => 'GET', 'cancel_url' => $cancel_url, 'webhook_url' => $webhook_url, 'currency' => 'BDT', 'metadata' => [ 'invoice_id' => $trx['id'], 'customer_id' => $user['id'], 'plan_id' => $trx['plan_id'], 'username' => $user['username'], 'plan_name' => $plan_name ] ]; $headers = [ 'accept: application/json', 'content-type: application/json', 'mh-piprapay-api-key: ' . self::$config['api_key'] ]; $create_charge_url = self::$config['base_url'] . '/create-charge'; _log("Creating payment for invoice: " . $trx['id'], 'PipraPay'); // API call $response = Http::postJsonData($create_charge_url, $payload, $headers); $result = json_decode($response, true); _log("API Response: " . $response, 'PipraPay'); if (isset($result['status']) && $result['status'] === true && isset($result['pp_url'])) { $transaction_id = $result['pp_id'] ?? ('piprapay_' . $trx['id'] . '_' . time()); _log("Payment created successfully. Transaction ID: " . $transaction_id, 'PipraPay'); // FIXED: Create NEW payment gateway record with safe values $pg_data = ORM::for_table('tbl_payment_gateway')->create(); $pg_data->username = $user['username'] ?? ''; $pg_data->user_id = $user['id'] ?? 0; $pg_data->gateway_trx_id = $transaction_id; $pg_data->plan_id = $trx['plan_id'] ?? 0; $pg_data->plan_name = $plan_name; $pg_data->routers_id = $routers_id; $pg_data->routers = $routers_name; $pg_data->price = $trx['price'] ?? '0'; $pg_data->pg_url_payment = $result['pp_url']; $pg_data->pg_request = json_encode($result); $pg_data->expired_date = date('Y-m-d H:i:s', strtotime('+24 HOURS')); $pg_data->gateway = 'piprapay'; $pg_data->trx_invoice = $trx['id']; $pg_data->status = 1; $pg_data->verification_attempts = 0; $pg_data->created_date = date('Y-m-d H:i:s'); // Save payment gateway record $save_result = $pg_data->save(); if ($save_result) { $pg_id = $pg_data->id(); _log("Payment gateway record created successfully. ID: " . $pg_id, 'PipraPay'); // Update transaction with gateway_trx_id $trx->gateway_trx_id = $transaction_id; $trx->save(); _log("Redirecting to payment page: " . $result['pp_url'], 'PipraPay'); _log("=== PIPRAPAY CREATE SUCCESS ===", 'PipraPay'); header('Location: ' . $result['pp_url']); exit(); } else { throw new Exception("Failed to save payment gateway record to database"); } } else { $error_msg = $result['message'] ?? 'Payment initiation failed'; _log("API Error: " . $error_msg, 'PipraPay'); r2(U . 'order/package', 'e', Lang::T("Payment failed: ") . $error_msg); } } catch (Exception $e) { _log("EXCEPTION in create_transaction: " . $e->getMessage(), 'PipraPay'); r2(U . 'order/package', 'e', Lang::T("System error. Please try again.")); } } public static function callback_handler() { _log("PipraPay Callback - GET: " . json_encode($_GET), 'PipraPay'); try { // Handle payment success redirect if (isset($_GET['invoice_id'])) { $invoice_id = intval($_GET['invoice_id']); _log("PipraPay Callback - Processing Invoice: " . $invoice_id, 'PipraPay'); // Find transaction $trx = ORM::for_table('tbl_transactions')->find_one($invoice_id); if (!$trx) { _log("PipraPay Callback - Transaction not found: " . $invoice_id, 'PipraPay'); r2(U . 'order/package', 'e', Lang::T("Transaction not found.")); } // Find user $user = ORM::for_table('tbl_customers')->find_one($trx['customer_id']); if (!$user) { _log("PipraPay Callback - User not found: " . $trx['customer_id'], 'PipraPay'); r2(U . 'order/package', 'e', Lang::T("User not found.")); } _log("PipraPay Callback - Found transaction: " . $trx['id'] . " for user: " . $user['username'], 'PipraPay'); // IMMEDIATE Auto-Verify $verification_result = self::immediate_verify_payment($trx, $user); if ($verification_result['success']) { _log("PipraPay Callback - Payment SUCCESS", 'PipraPay'); r2(U . "order/view/" . $trx['id'], 's', Lang::T("Payment successful! Package activated.")); } else { _log("PipraPay Callback - Payment verification failed: " . $verification_result['message'], 'PipraPay'); r2(U . "order/view/" . $trx['id'], 'w', Lang::T("Payment processing: ") . $verification_result['message']); } } // Handle cancelled payment if (isset($_GET['status']) && $_GET['status'] == 'cancel') { r2(U . 'order/package', 'w', Lang::T("Payment was cancelled. Please try again.")); } // Default redirect r2(U . 'order/package'); } catch (Exception $e) { _log("PipraPay Callback Exception: " . $e->getMessage(), 'PipraPay'); r2(U . 'order/package', 'e', Lang::T("System error occurred.")); } } /** * IMMEDIATE Payment Verification */ public static function immediate_verify_payment($trx, $user) { _log("PipraPay Immediate Verify - Starting for Invoice: " . $trx['id'], 'PipraPay'); try { // Check if already paid if ($trx['status'] == 2) { _log("PipraPay Immediate Verify - Already paid: " . $trx['id'], 'PipraPay'); return ['success' => true, 'message' => 'Already paid']; } // Get gateway transaction ID $gateway_trx_id = $trx['gateway_trx_id']; if (empty($gateway_trx_id)) { _log("PipraPay Immediate Verify - No gateway transaction ID", 'PipraPay'); return ['success' => false, 'message' => 'No transaction ID found']; } // API call to verify payment $verify_url = self::$config['base_url'] . '/verify-payments'; $payload = ['transaction_id' => $gateway_trx_id]; $headers = [ 'accept: application/json', 'content-type: application/json', 'mh-piprapay-api-key: ' . self::$config['api_key'] ]; _log("PipraPay Immediate Verify - API Request for: " . $gateway_trx_id, 'PipraPay'); $response = Http::postJsonData($verify_url, $payload, $headers); $result = json_decode($response, true); _log("PipraPay Immediate Verify - API Response: " . $response, 'PipraPay'); // Update verification attempts $pg_trx = ORM::for_table('tbl_payment_gateway') ->where('gateway_trx_id', $gateway_trx_id) ->find_one(); if ($pg_trx) { $pg_trx->verification_attempts = ($pg_trx->verification_attempts ?? 0) + 1; $pg_trx->last_verification = date('Y-m-d H:i:s'); $pg_trx->save(); } if (isset($result['status']) && $result['status'] === true) { // Check payment status $payment_status = ''; $is_paid = false; if (isset($result['payment_status'])) { $payment_status = strtolower(trim($result['payment_status'])); } elseif (isset($result['data']['payment_status'])) { $payment_status = strtolower(trim($result['data']['payment_status'])); } $is_paid = in_array($payment_status, ['success', 'completed', 'paid', 'successful', 'processed', 'complete']); if ($is_paid) { _log("PipraPay Immediate Verify - PAYMENT SUCCESS, activating package", 'PipraPay'); // Update payment gateway record to PAID if ($pg_trx) { $pg_trx->pg_paid_response = json_encode($result); $pg_trx->payment_method = $result['payment_method'] ?? self::$config['gateway_name']; $pg_trx->payment_channel = $result['payment_channel'] ?? self::$config['gateway_name']; $pg_trx->paid_date = date('Y-m-d H:i:s'); $pg_trx->status = 2; // PAID $pg_trx->save(); _log("PipraPay Immediate Verify - Updated payment gateway to PAID", 'PipraPay'); } // ACTIVATE USER PACKAGE _log("PipraPay Immediate Verify - Activating package for user: " . $user['id'], 'PipraPay'); $activation_result = Package::rechargeUser( $user['id'], $trx['routers'], $trx['plan_id'], $trx['id'], self::$config['gateway_name'] ); if ($activation_result) { // Update transaction status to PAID $trx->pg_paid_response = json_encode($result); $trx->payment_method = $result['payment_method'] ?? self::$config['gateway_name']; $trx->payment_channel = $result['payment_channel'] ?? self::$config['gateway_name']; $trx->paid_date = date('Y-m-d H:i:s'); $trx->status = 2; // PAID $trx->save(); _log("PipraPay Immediate Verify - Package activated successfully!", 'PipraPay'); return ['success' => true, 'message' => 'Payment verified and package activated']; } else { _log("PipraPay Immediate Verify - PACKAGE ACTIVATION FAILED", 'PipraPay'); return ['success' => false, 'message' => 'Package activation failed']; } } else { _log("PipraPay Immediate Verify - Payment not completed. Status: " . $payment_status, 'PipraPay'); return ['success' => false, 'message' => 'Payment status: ' . $payment_status]; } } else { $error_msg = $result['message'] ?? 'Verification failed'; _log("PipraPay Immediate Verify - API Error: " . $error_msg, 'PipraPay'); return ['success' => false, 'message' => $error_msg]; } } catch (Exception $e) { _log("PipraPay Immediate Verify Exception: " . $e->getMessage(), 'PipraPay'); return ['success' => false, 'message' => 'Verification error: ' . $e->getMessage()]; } } } // Global functions function piprapay_validate_config() { return true; } function piprapay_show_config() { echo '