PHP code example of mbsoft31 / loyalty-rewards

1. Go to this page and download the library: Download mbsoft31/loyalty-rewards library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mbsoft31 / loyalty-rewards example snippets


use LoyaltyRewards\Core\Services\LoyaltyService;
use LoyaltyRewards\Domain\ValueObjects\{CustomerId, Money, Currency, TransactionContext};
use LoyaltyRewards\Rules\Earning\CategoryMultiplierRule;

// Setup
$loyaltyService = new LoyaltyService(/* dependencies */);

// Create customer account
$customerId = CustomerId::fromString('customer_12345');
$account = $loyaltyService->createAccount($customerId);

// Configure earning rules
$rulesEngine->addEarningRule(
    new CategoryMultiplierRule('electronics', 2.0, ConversionRate::standard())
);

// Earn points from purchase
$result = $loyaltyService->earnPoints(
    $customerId,
    Money::fromDollars(99.99, Currency::USD()),
    TransactionContext::earning('electronics', 'online_store')
);

echo "Earned: {$result->pointsEarned->value()} points";
echo "Balance: {$result->newAvailableBalance->value()} points";

// Redeem points
$redemption = $loyaltyService->redeemPoints(
    $customerId,
    Points::fromInt(1000)
);

echo "Redeemed: {$redemption->redemptionValue} value";


// Setup category-based earning
$rulesEngine->addEarningRule(new CategoryMultiplierRule('electronics', 3.0));
$rulesEngine->addEarningRule(new CategoryMultiplierRule('books', 2.0));
$rulesEngine->addEarningRule(new CategoryMultiplierRule('groceries', 1.5));

// Tier-based bonuses
$rulesEngine->addEarningRule(new TierBonusRule('gold', 1.25));
$rulesEngine->addEarningRule(new TierBonusRule('platinum', 1.5));

// Customer purchases $200 laptop (electronics + gold tier)
$result = $loyaltyService->earnPoints(
    $goldCustomerId,
    Money::fromDollars(200.00, Currency::USD()),
    TransactionContext::earning('electronics')
);
// Earns: 200 * 100 * 3.0 * 1.25 = 75,000 points

### Restaurant Chain Program

// Happy hour promotions
$happyHourRule = new TimeBasedRule(
    new DateTimeImmutable('2025-01-01 14:00:00'),
    new DateTimeImmutable('2025-12-31 17:00:00'),
    2.0, // Double points
    ConversionRate::standard(),
    ['monday', 'tuesday', 'wednesday']
);

$rulesEngine->addEarningRule($happyHourRule);

// Customer orders during happy hour
$result = $loyaltyService->earnPoints(
    $customerId,
    Money::fromDollars(12.50, Currency::USD()),
    TransactionContext::earning('food', 'mobile_app')
);
// Earns: 12.50 * 100 * 2.0 = 2,500 points

### SaaS Referral Program

// Referral bonus
$result = $loyaltyService->earnPoints(
    $referrerId,
    Money::zero(Currency::USD()), // No monetary transaction
    TransactionContext::create([
        'type' => 'referral_conversion',
        'referred_customer' => 'new_customer_789',
        'subscription_tier' => 'pro'
    ])
);

use LoyaltyRewards\Infrastructure\Database\DatabaseConnectionFactory;

$pdo = DatabaseConnectionFactory::create([
    'driver' => 'pgsql',
    'host' => 'localhost',
    'database' => 'loyalty_rewards',
    'username' => 'your_user',
    'password' => 'your_password',
]);

use LoyaltyRewards\Core\Services\LoyaltyService;
use LoyaltyRewards\Infrastructure\Repositories\{AccountRepository, TransactionRepository};
use LoyaltyRewards\Infrastructure\Audit\AuditLogger;

$accountRepo = new AccountRepository($pdo);
$transactionRepo = new TransactionRepository($pdo);
$auditLogger = new AuditLogger($pdo);

$loyaltyService = new LoyaltyService($accountRepo, $transactionRepo, $auditLogger, $rulesEngine);

use LoyaltyRewards\Core\Engine\RulesEngine;
use LoyaltyRewards\Rules\Earning\{CategoryMultiplierRule, TierBonusRule};

$rulesEngine = new RulesEngine();

// Base earning: 1 point per cent spent
$rulesEngine->addEarningRule(
    new CategoryMultiplierRule('default', 1.0, ConversionRate::standard())
);

// Category bonuses
$rulesEngine->addEarningRule(
    new CategoryMultiplierRule('premium', 5.0, ConversionRate::standard())
);

// Tier bonuses stack with category multipliers
$rulesEngine->addEarningRule(
    new TierBonusRule('vip', 2.0, ConversionRate::standard())
);

use LoyaltyRewards\Core\Services\FraudDetectionService;

$fraudDetection = new FraudDetectionService();

// Automatic fraud checking on all transactions
$fraudResult = $fraudDetection->analyze($account, $amount, $context);

if ($fraudResult->shouldBlock()) {
    throw new FraudDetectedException('Transaction blocked');
}