PHP code example of mohammadmehrabani / conditional-coupon
1. Go to this page and download the library: Download mohammadmehrabani/conditional-coupon 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/ */
mohammadmehrabani / conditional-coupon example snippets
namespace App;
use App\Models\Order;
use MohammadMehrabani\ConditionalCoupon\CheckConditionAbstract;
use MohammadMehrabani\ConditionalCoupon\Exceptions\CouponException;
class CheckUsageLimitCondition extends CheckConditionAbstract
{
/**
* @throws CouponException|\Exception
*/
public function handle(mixed $payload, \Closure $next): mixed
{
$orderCount = Order::where([ // only for example
'coupon_id' => $this->coupon->id,
'user_id' => auth()->id(),
'status' => 'fulfilled',
])->count();
if ($orderCount > $this->condition->data) {
throw new \Exception(__('Coupon user usage limit reached.'), 400);
}
return $next($payload);
}
}
class CheckPaymentTypesCondition extends CheckConditionAbstract
{
public function handle(mixed $payload, \Closure $next): mixed
{
if (empty(array_intersect($this->condition->data, ['cash', 'credit']))) {
throw new \Exception('payment types do not match.');
}
return $next($payload);
}
}
use MohammadMehrabani\ConditionalCoupon\Models\Coupon;
use MohammadMehrabani\ConditionalCoupon\Models\CouponCondition;
$coupon = Coupon::create([
'code' => 'AMZ100',
// fill other columns
]);
use Illuminate\Support\Facades\DB;
use App\Models\Order;
use MohammadMehrabani\ConditionalCoupon\InquiryCoupon;
DB::transaction(function () {
$inquiryCoupon = new InquiryCoupon();
[
$currency,
$amount,
$discountAmount,
$payableAmount,
$coupon
] = $inquiryCoupon->handle(code: request()->get('coupon_code'), amount: 1000000, locked: true);
$order = Order::create([ // only for example
'discount' => $discountAmount,
'amount' => $payableAmount,
'user_id' => auth()->id(),
'status' => 'initial',
'coupon_id' => $coupon->id,
# Other fields as needed:
// 'coupon_code' => $coupon->code,
// 'discount_percentage' => $coupon->discount_percentage,
// 'discount_amount' => $coupon->discount_amount,
]);
$coupon->increment('used_count');
});