Skip to main content

Pay Tournament Entry

The pay tournament entry method allows players to pay entry fees for tournaments that require payment. This is typically used for premium tournaments with prize pools.

Overview

The payEntry method:

  • Processes tournament entry fee payments
  • Validates payment amount and currency
  • Updates player's tournament registration status
  • Returns status code indicating success or failure

Method Signature

public function payEntry(string $gameID, string $tournamentID): int

Parameters

Required Parameters

  • gameID (String) - The Game ID for the tournament
  • tournamentID (String) - The Tournament ID to pay entry for

Return Value

Returns an integer status code:

  • Positive value - Payment processed successfully
  • Zero or negative - Payment failed

Basic Usage

Simple Entry Payment

use Veniso\Moitribe\Sdk\modules\classes\MoitribeApi;

$moitribe = new MoitribeApi([
'gameid' => 'your-game-id',
'channelid' => 'your-channel-id',
'playerid' => 'player-123'
]);

try {
$result = $moitribe->tournRequestsHandler->payEntry(
'your-game-id',
'tournament-123'
);

if ($result > 0) {
echo "Entry fee paid successfully!\n";
echo "Payment ID: " . $result . "\n";
} else {
echo "Failed to pay entry fee\n";
echo "Error code: " . $result . "\n";
}

} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}

Entry Payment with Validation

function payTournamentEntry($gameId, $tournamentId, $playerId, $paymentMethod) {
global $moitribe;

try {
// First get tournament details to check entry fee
$tournaments = $moitribe->tournRequestsHandler->getTournaments($gameId);
$targetTournament = null;

foreach ($tournaments as $tournament) {
if ($tournament->getId() === $tournamentId) {
$targetTournament = $tournament;
break;
}
}

if (!$targetTournament) {
return ['success' => false, 'message' => 'Tournament not found'];
}

$entryFee = $targetTournament->getEntryFee();

if ($entryFee <= 0) {
return ['success' => false, 'message' => 'This tournament does not require an entry fee'];
}

if ($targetTournament->getStatus() !== 'active') {
return ['success' => false, 'message' => 'Tournament is not accepting entries'];
}

if ($targetTournament->isFull()) {
return ['success' => false, 'message' => 'Tournament is full'];
}

// Process payment through your payment system
$paymentResult = $this->processPayment($playerId, $entryFee, $paymentMethod);

if (!$paymentResult['success']) {
return ['success' => false, 'message' => 'Payment failed: ' . $paymentResult['message']];
}

// Pay tournament entry
$result = $moitribe->tournRequestsHandler->payEntry($gameId, $tournamentId);

if ($result > 0) {
// Log successful payment
$this->logEntryPayment($tournamentId, $playerId, $entryFee, $paymentResult['transaction_id'], $result);

return [
'success' => true,
'message' => 'Entry fee paid successfully',
'payment_id' => $result,
'amount' => $entryFee,
'transaction_id' => $paymentResult['transaction_id']
];
} else {
// Refund payment if tournament entry failed
$this->refundPayment($paymentResult['transaction_id']);

return ['success' => false, 'message' => 'Failed to process tournament entry'];
}

} catch (Exception $e) {
error_log("Tournament entry payment error: " . $e->getMessage());
return ['success' => false, 'message' => 'Server error occurred'];
}
}

// Usage
$paymentResult = payTournamentEntry(
'your-game-id',
'tournament-456',
'player-789',
'credit_card'
);

if ($paymentResult['success']) {
echo "Success: " . $paymentResult['message'] . "\n";
echo "Amount paid: " . $paymentResult['amount'] . "\n";
echo "Transaction ID: " . $paymentResult['transaction_id'] . "\n";
} else {
echo "Error: " . $paymentResult['message'] . "\n";
}

Integration Examples

Tournament Payment System

class TournamentPaymentManager {
private $moitribe;
private $gameId;
private $paymentGateway;

public function __construct($moitribe, $gameId, $paymentGateway) {
$this->moitribe = $moitribe;
$this->gameId = $gameId;
$this->paymentGateway = $paymentGateway;
}

public function processTournamentEntry($tournamentId, $playerId, $paymentDetails) {
try {
// Validate tournament and get entry fee
$validation = $this->validateTournamentEntry($tournamentId);

if (!$validation['valid']) {
return ['success' => false, 'message' => $validation['message']];
}

$tournament = $validation['tournament'];
$entryFee = $tournament->getEntryFee();

// Check if player has already paid
if ($this->hasPlayerPaidEntry($tournamentId, $playerId)) {
return ['success' => false, 'message' => 'Entry fee already paid for this tournament'];
}

// Process payment
$paymentResult = $this->paymentGateway->charge([
'amount' => $entryFee,
'currency' => $tournament->getCurrency() ?? 'USD',
'player_id' => $playerId,
'tournament_id' => $tournamentId,
'description' => "Entry fee for {$tournament->getName()}",
'payment_method' => $paymentDetails['method'],
'payment_data' => $paymentDetails
]);

if (!$paymentResult['success']) {
return ['success' => false, 'message' => 'Payment failed: ' . $paymentResult['error']];
}

// Register payment with Moitribe
$moitribeResult = $this->moitribe->tournRequestsHandler->payEntry(
$this->gameId,
$tournamentId
);

if ($moitribeResult > 0) {
// Record payment in local database
$this->recordPayment([
'tournament_id' => $tournamentId,
'player_id' => $playerId,
'amount' => $entryFee,
'currency' => $tournament->getCurrency() ?? 'USD',
'payment_gateway_id' => $paymentResult['transaction_id'],
'moitribe_payment_id' => $moitribeResult,
'status' => 'completed',
'created_at' => date('Y-m-d H:i:s')
]);

return [
'success' => true,
'message' => 'Tournament entry paid successfully',
'payment_id' => $moitribeResult,
'amount' => $entryFee,
'transaction_id' => $paymentResult['transaction_id']
];
} else {
// Refund payment if Moitribe registration failed
$this->paymentGateway->refund($paymentResult['transaction_id']);

return ['success' => false, 'message' => 'Failed to register tournament entry'];
}

} catch (Exception $e) {
error_log("Tournament payment error: " . $e->getMessage());
return ['success' => false, 'message' => 'Payment processing error'];
}
}

private function validateTournamentEntry($tournamentId) {
try {
$tournaments = $this->moitribe->tournRequestsHandler->getTournaments($this->gameId);

foreach ($tournaments as $tournament) {
if ($tournament->getId() === $tournamentId) {
if ($tournament->getStatus() !== 'active') {
return ['valid' => false, 'message' => 'Tournament is not active'];
}

if ($tournament->isFull()) {
return ['valid' => false, 'message' => 'Tournament is full'];
}

if ($tournament->getEntryFee() <= 0) {
return ['valid' => false, 'message' => 'Tournament does not require entry fee'];
}

return ['valid' => true, 'tournament' => $tournament];
}
}

return ['valid' => false, 'message' => 'Tournament not found'];

} catch (Exception $e) {
return ['valid' => false, 'message' => 'Error validating tournament'];
}
}

private function hasPlayerPaidEntry($tournamentId, $playerId) {
// Check your local database for existing payment
// This is a placeholder - implement based on your system
return false;
}

private function recordPayment($paymentData) {
// Save payment record to your database
error_log("Payment recorded: " . json_encode($paymentData));
}
}

// Usage
$paymentManager = new TournamentPaymentManager($moitribe, 'your-game-id', $paymentGateway);

$paymentDetails = [
'method' => 'credit_card',
'card_number' => '****-****-****-1234',
'expiry' => '12/25',
'cvv' => '***'
];

$result = $paymentManager->processTournamentEntry('tournament-456', 'player-789', $paymentDetails);

if ($result['success']) {
echo "Payment successful! Amount: {$result['amount']}\n";
} else {
echo "Payment failed: {$result['message']}\n";
}

Web Payment Handler

function handleTournamentPaymentRequest() {
global $moitribe;

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
return;
}

$input = json_decode(file_get_contents('php://input'), true);

$requiredFields = ['tournament_id', 'player_id', 'payment_method'];
foreach ($requiredFields as $field) {
if (!isset($input[$field])) {
http_response_code(400);
echo json_encode(['error' => "Missing required field: $field"]);
return;
}
}

$tournamentId = $input['tournament_id'];
$playerId = $input['player_id'];
$gameId = $input['game_id'] ?? 'your-game-id';

try {
// Validate request
if (empty($tournamentId) || empty($playerId)) {
throw new InvalidArgumentException('Tournament ID and Player ID are required');
}

// Process payment with Moitribe
$result = $moitribe->tournRequestsHandler->payEntry($gameId, $tournamentId);

if ($result > 0) {
http_response_code(200);
echo json_encode([
'success' => true,
'message' => 'Tournament entry paid successfully',
'payment_id' => $result
]);
} else {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => 'Failed to process tournament entry',
'error_code' => $result
]);
}

} catch (InvalidArgumentException $e) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Server error occurred']);
error_log("Tournament payment API error: " . $e->getMessage());
}
}

// Route handler
if (strpos($_SERVER['REQUEST_URI'], '/api/tournament/pay-entry') !== false) {
handleTournamentPaymentRequest();
}

Error Handling

Common Error Scenarios

  • Tournament not found - Invalid tournament ID
  • No entry fee required - Tournament is free to enter
  • Tournament not active - Tournament hasn't started or has ended
  • Tournament full - Maximum participants reached
  • Payment failed - Payment processing issues
  • Already paid - Entry fee already paid by player
  • Network issues - API communication problems

Error Handling Example

function safePayTournamentEntry($gameId, $tournamentId) {
global $moitribe;

try {
if (empty($gameId) || empty($tournamentId)) {
throw new InvalidArgumentException("Game ID and Tournament ID are required");
}

$result = $moitribe->tournRequestsHandler->payEntry($gameId, $tournamentId);

if ($result > 0) {
return ['success' => true, 'payment_id' => $result];
} else {
$errorMessage = $this->getPaymentErrorMessage($result);
return ['success' => false, 'message' => $errorMessage, 'error_code' => $result];
}

} catch (InvalidArgumentException $e) {
return ['success' => false, 'message' => $e->getMessage()];
} catch (Exception $e) {
error_log("Tournament entry payment error: " . $e->getMessage());
return ['success' => false, 'message' => 'Failed to process payment'];
}
}

function getPaymentErrorMessage($errorCode) {
switch ($errorCode) {
case 0:
return 'Payment processing failed';
case -1:
return 'Tournament not found';
case -2:
return 'Tournament does not require entry fee';
case -3:
return 'Tournament is not active';
case -4:
return 'Tournament is full';
case -5:
return 'Entry fee already paid';
case -6:
return 'Payment method not supported';
default:
return 'Unknown payment error occurred';
}
}

Best Practices

  1. Validate Before Payment

    • Check tournament status and availability
    • Verify entry fee requirements
    • Confirm player hasn't already paid
  2. Secure Payment Processing

    • Use secure payment gateways
    • Never store raw payment details
    • Implement proper PCI compliance
  3. Handle Payment Failures

    • Provide clear error messages
    • Implement retry mechanisms
    • Refund failed payments when appropriate
  4. Maintain Audit Trails

    • Log all payment attempts
    • Track transaction IDs
    • Monitor for fraudulent activity