Installation:
composer require palpalani/bayrewards-laravel
Publish the config file:
php artisan vendor:publish --provider="Palpalani\BayRewards\BayRewardsServiceProvider" --tag="config"
Configuration:
Update .env with your BayRewards credentials:
BAYREWARDS_API_KEY=your_api_key_here
BAYREWARDS_API_SECRET=your_api_secret_here
BAYREWARDS_ENVIRONMENT=sandbox # or 'live'
First Use Case: Trigger a reward redemption in a controller:
use Palpalani\BayRewards\Facades\BayRewards;
public function redeemReward(Request $request)
{
$response = BayRewards::redeem($request->input('reward_id'), $request->input('customer_id'));
return response()->json($response);
}
BayRewards facade for quick access to all methods.config/bayrewards.php for available endpoints and default settings.Reward Redemption:
// Basic redemption
$result = BayRewards::redeem($rewardId, $customerId);
// With custom payload
$payload = [
'reward_id' => $rewardId,
'customer_id' => $customerId,
'metadata' => ['order_id' => 12345]
];
$result = BayRewards::redeem($payload);
Customer Rewards Balance:
$balance = BayRewards::getCustomerBalance($customerId);
Event-Based Integration:
Listen to bayrewards.reward.redeemed events for post-redemption logic:
BayRewards::redeem($rewardId, $customerId)
->then(function ($response) {
// Post-redemption logic (e.g., update user profile)
});
Middleware: Use middleware to validate API keys or enforce rate limits:
public function handle(Request $request, Closure $next)
{
if (!BayRewards::isApiKeyValid()) {
abort(403, 'Invalid API key');
}
return $next($request);
}
Queue Jobs: Offload reward processing to queues for async handling:
BayRewards::dispatchRedeemJob($rewardId, $customerId);
Webhooks: Set up webhook endpoints in routes/web.php:
Route::post('/bayrewards/webhook', [BayRewardsWebhookController::class, 'handle']);
Testing: Use the sandbox environment for local testing:
BayRewards::setEnvironment('sandbox');
API Key/Secret Mismatch:
.env values match the BayRewards dashboard.BayRewards::getLastError(); // Check for auth failures
Rate Limiting:
BayRewards::withRetry(3, 1000)->redeem($rewardId, $customerId);
Payload Validation:
reward_id, customer_id) before calling SDK methods.ValidatesRequests trait for consistency.Webhook Verification:
if (!BayRewards::verifyWebhookSignature($request->header('X-Signature'), $request->getContent())) {
abort(403);
}
BayRewards::enableLogging(); // Logs to storage/logs/bayrewards.log
BayRewards::mockResponse('redeem', ['success' => true]);
Custom Responses: Override default responses by binding a custom handler:
BayRewards::bindResponseHandler(function ($response, $method) {
return ['custom' => 'data'];
});
Event Extensions: Extend events to trigger custom logic:
event(new \Palpalani\BayRewards\Events\RewardRedeemed($rewardId, $customerId));
API Endpoint Overrides:
Configure custom endpoints in config/bayrewards.php:
'endpoints' => [
'redeem' => 'https://custom-api.bayrewards.com/redeem',
],
Environment-Specific Settings:
Use config/bayrewards.php to define environment-specific keys:
'environments' => [
'sandbox' => [
'api_key' => env('BAYREWARDS_SANDBOX_KEY'),
'api_secret' => env('BAYREWARDS_SANDBOX_SECRET'),
],
'live' => [
'api_key' => env('BAYREWARDS_LIVE_KEY'),
'api_secret' => env('BAYREWARDS_LIVE_SECRET'),
],
],
Timeout Handling: Default timeout is 30 seconds. Adjust in config:
'timeout' => 60, // seconds
How can I help you explore Laravel packages today?