PHP code example of esanj / discount-client

1. Go to this page and download the library: Download esanj/discount-client 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/ */

    

esanj / discount-client example snippets


use Esanj\DiscountClient\Contracts\DiscountClientInterface;

class CheckoutController
{
    public function __construct(private DiscountClientInterface $discount) {}

    public function apply()
    {
        $usage = $this->discount->coupons()->validate('WELCOME', new CouponValidationData(
            userId: 42, amount: 1_000_000, currency: 'IRR',
        ));
    }
}

use Esanj\DiscountClient\Facades\Coupon;
use Esanj\DiscountClient\Facades\GiftCard;
use Esanj\DiscountClient\Facades\Discount;   // Discount::coupons() / Discount::giftCards()

use Esanj\DiscountClient\Facades\Coupon;
use Esanj\DiscountClient\DTOs\CouponValidationData;

$order = new CouponValidationData(userId: 42, amount: 1_000_000, currency: 'IRR', productId: 193);

// 1) Validate against the order — computes the discount and reserves a usage.
$usage = Coupon::validate('WELCOME', $order);
$usage->usageId;          // keep this for the next steps
$usage->discountAmount;   // e.g. 150000
$usage->finalAmount;      // e.g. 850000

// 2) Redeem — holds the coupon for this user while payment is in progress.
Coupon::redeem('WELCOME', $usage->usageId, $order);

// 3a) Confirm once the order is paid (commits the coupon)…
Coupon::confirm('WELCOME', $usage->usageId);

// 3b) …or cancel to release the reservation.
Coupon::cancel('WELCOME', $usage->usageId);

use Esanj\DiscountClient\Facades\GiftCard;

$usage = GiftCard::validate('GC-8H2K', userId: 42);
$usage->amount;    // gift card balance/value
$usage->currency;

GiftCard::redeem('GC-8H2K', $usage->usageId, userId: 42);

use Esanj\DiscountClient\Facades\Coupon;
use Esanj\DiscountClient\Facades\GiftCard;
use Esanj\DiscountClient\DTOs\CouponData;
use Esanj\DiscountClient\DTOs\CouponProductData;
use Esanj\DiscountClient\DTOs\GiftCardData;
use Esanj\DiscountClient\Enums\CouponAmountType;

$coupon = Coupon::create(new CouponData(
    name: 'Welcome 15%',
    amountType: CouponAmountType::Percentage,
    amount: 15,                       // 15% off…
    currency: ['IRR'],
    maxAmount: 200_000,               // …capped at 200,000
    usageLimit: 1000,
    usageLimitPerUser: 1,
    products: [new CouponProductData(serviceId: 3, productId: 193)],
    expiredAt: '2026-12-31 23:59:59',
));
$coupon->code;                        // generated when you don't pass one

$giftCard = GiftCard::create(new GiftCardData(
    name: 'Yalda 500k',
    amount: 500_000,
    currency: 'IRR',
    usageLimit: 1,
));

use Esanj\DiscountClient\Exceptions\DiscountApiException;
use Esanj\DiscountClient\Exceptions\DiscountAuthenticationException;

try {
    $usage = Coupon::validate('WELCOME', $order);
} catch (DiscountApiException $e) {
    if ($e->isBusinessRuleFailure()) {           // 400 — a coupon/gift-card rule rejected it
        match ($e->getErrorCode()) {
            'COUPON_EXPIRED'             => /* … */,
            'COUPON_MAX_USAGE_REACHED'   => /* … */,
            'MINIMUM_ORDER_AMOUNT_NOT_MET' => /* … */,
            default                      => report($e),
        };
    } elseif ($e->isNotFound())        { /* unknown code */ }
    elseif ($e->isValidationError())   { $errors = $e->getErrors(); }   // 422
    else                               { report($e); }
} catch (DiscountAuthenticationException $e) {
    // credentials / OAuth problem
}
bash
php artisan vendor:publish --tag=discount-config