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
declare(strict_types=1);
use LoyaltyRewards\Core\Services\LoyaltyService;
use LoyaltyRewards\Core\Services\AuditService;
use LoyaltyRewards\Core\Services\FraudDetectionService;
use LoyaltyRewards\Core\Engine\RulesEngine;
use LoyaltyRewards\Domain\ValueObjects\{ConversionRate, Currency, CustomerId, Money, Points, TransactionContext};
use LoyaltyRewards\Infrastructure\Database\DatabaseAccountRepository;
use LoyaltyRewards\Infrastructure\Database\DatabaseAuditRepository;
use LoyaltyRewards\Infrastructure\Database\DatabaseConnectionFactory;
use LoyaltyRewards\Infrastructure\Database\DatabaseTransactionRepository;
use LoyaltyRewards\Rules\Earning\CategoryMultiplierRule;
use LoyaltyRewards\Rules\Earning\TierBonusRule;
use LoyaltyRewards\Rules\Redemption\BasicRedemptionRule;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\NullLogger;
$pdo = DatabaseConnectionFactory::create([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$accountRepository = new DatabaseAccountRepository(
$pdo,
new DatabaseTransactionRepository($pdo)
);
$auditRepository = new DatabaseAuditRepository($pdo);
$rulesEngine = new RulesEngine(new NullLogger);
$rulesEngine->addEarningRule(new CategoryMultiplierRule('electronics', 2.0, ConversionRate::standard()));
$rulesEngine->addEarningRule(new TierBonusRule('gold', 1.25, ConversionRate::standard()));
$rulesEngine->addRedemptionRule(new BasicRedemptionRule(Currency::USD(), 100, 100));
$loyaltyService = new LoyaltyService(
$accountRepository,
$rulesEngine,
new FraudDetectionService(new NullLogger),
new AuditService($auditRepository, new NullLogger),
new class implements EventDispatcherInterface {
public function dispatch(object $event): object
{
return $event;
}
},
new NullLogger
);
// Create customer account
$customerId = CustomerId::fromString('customer_12345');
$loyaltyService->createAccount($customerId);
// Earn points from purchase
$result = $loyaltyService->earnPoints(
$customerId,
Money::fromDollars(99.99, Currency::USD()),
TransactionContext::earning('electronics', 'online_store')
);
$loyaltyService->confirmPendingPoints($customerId);
$availableBalance = $loyaltyService->getAccountBalance($customerId);
echo "Earned: {$result->pointsEarned->value()} points\n";
echo "Balance (available): {$availableBalance->value()} points\n";
// Redeem points
$redemption = $loyaltyService->redeemPoints(
$customerId,
Points::fromInt(1000),
TransactionContext::redemption(['channel' => 'store'])
);
echo "Redeemed: {$redemption->redemptionValue->toDollars()} {$redemption->redemptionValue->currency()->code()}";
use LoyaltyRewards\Core\Services\LoyaltyService;
use LoyaltyRewards\Core\Services\AuditService;
use LoyaltyRewards\Core\Services\FraudDetectionService;
use LoyaltyRewards\Core\Engine\RulesEngine;
use LoyaltyRewards\Infrastructure\Database\DatabaseAccountRepository;
use LoyaltyRewards\Infrastructure\Database\DatabaseAuditRepository;
use LoyaltyRewards\Infrastructure\Database\DatabaseConnectionFactory;
use LoyaltyRewards\Infrastructure\Database\DatabaseTransactionRepository;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\NullLogger;
$pdo = DatabaseConnectionFactory::create([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$accountRepository = new DatabaseAccountRepository(
$pdo,
new DatabaseTransactionRepository($pdo)
);
$auditRepository = new DatabaseAuditRepository($pdo);
$loyaltyService = new LoyaltyService(
$accountRepository,
new RulesEngine(new NullLogger),
new FraudDetectionService(new NullLogger),
new AuditService($auditRepository, new NullLogger),
new class implements EventDispatcherInterface {
public function dispatch(object $event): object
{
return $event;
}
},
new NullLogger
);
use LoyaltyRewards\Core\Engine\RulesEngine;
use LoyaltyRewards\Domain\ValueObjects\ConversionRate;
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\Exceptions\FraudDetectedException;
use LoyaltyRewards\Core\Services\FraudDetectionService;
use LoyaltyRewards\Domain\ValueObjects\{Currency, CustomerId, Money, TransactionContext};
$fraudDetection = new FraudDetectionService();
$customerId = CustomerId::fromString('customer_12345');
$account = $accountRepository->findByCustomerId($customerId);
$amount = Money::fromDollars(99.99, Currency::USD());
$context = TransactionContext::earning('electronics', 'online_store');
// Automatic fraud checking on all transactions
$fraudResult = $fraudDetection->analyze($account, $amount, $context);
if ($fraudResult->shouldBlock()) {
throw new FraudDetectedException('Transaction blocked');
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.