NASER aff gift card controller and models and routes
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Customer;
|
||||
use App\Models\GiftCard;
|
||||
use App\Models\GiftCardCodes;
|
||||
use App\DB\Te\TableEngine;
|
||||
use App\Models\Withdraw;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
||||
use Rooyesh\Wallet\interfaces\WalletInterface;
|
||||
|
||||
|
||||
class GiftCardController extends Controller
|
||||
{
|
||||
public function redeem(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
$count = $request->count;
|
||||
$amount = $request->amount;
|
||||
if(!$user){
|
||||
throw new UnprocessableEntityHttpException('auth failed!');
|
||||
}
|
||||
$codes = [];
|
||||
if((!is_numeric($count) || $count < 0) || (!is_numeric($amount) || $amount <= 0)){
|
||||
return response()->json(['error' => 'تعداد کد را چک کنید!'], 400);
|
||||
}
|
||||
$customer = Customer::select('id', 'name', 'mobile')->where('mobile', $request->mobile)->first();
|
||||
|
||||
$gift = GiftCard::create([
|
||||
'customer_id' => $customer->id ?? null,
|
||||
'count' => $count,
|
||||
'amount' => $amount,
|
||||
'mobile' => $request->mobile,
|
||||
'name' => $customer ? $customer->name : $request->name,
|
||||
'amount' => $amount,
|
||||
'total' => $count*$amount,
|
||||
'sts' => 1,
|
||||
'unit' => $request->unit,
|
||||
'expiry_date' => $request->expiry_date,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
'description' => $request->description,
|
||||
]);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
do {
|
||||
$unique_code = strtoupper(bin2hex(random_bytes(4)));
|
||||
} while (GiftCardCodes::where('code', $unique_code)->exists());
|
||||
|
||||
$codes[] = [
|
||||
'giftcard_id' => $gift->id,
|
||||
'code' => $unique_code,
|
||||
'sts' => 1,
|
||||
'unit' => $request->unit,
|
||||
'amount' => $amount,
|
||||
'expiry_date' => $request->expiry_date,
|
||||
'created_at' => now(),
|
||||
];
|
||||
}
|
||||
GiftCardCodes::insert($codes);
|
||||
|
||||
return response()->json([
|
||||
'customer' => $customer,
|
||||
'order' => $gift,
|
||||
'codes' => $gift->giftcardcodes,
|
||||
'total' => $count*$amount,
|
||||
]);
|
||||
}
|
||||
|
||||
public function useGiftCard(Request $request){
|
||||
$user = auth()->user();
|
||||
if(!$user){
|
||||
throw new UnprocessableEntityHttpException('auth failed!');
|
||||
}
|
||||
|
||||
$redeem = $request->code;
|
||||
$giftcode = GiftCardCodes::where('code', $redeem)->first();
|
||||
$gift = GiftCard::find($giftcode->giftcard_id);
|
||||
|
||||
if(!$giftcode){
|
||||
return response()->json(['error' => 'کد هدیه یافت نشد!'], 404);
|
||||
}
|
||||
|
||||
//gift order update
|
||||
$gift = GiftCard::find($giftcode->giftcard_id);
|
||||
|
||||
if($gift->used_count+1 === $gift->count){
|
||||
$gift->sts = 2;
|
||||
}elseif($gift->expiry_date <= now()){
|
||||
$gift->sts = 3;
|
||||
|
||||
$giftcode->sts = 3;
|
||||
$giftcode->save();
|
||||
$gift->save();
|
||||
|
||||
return response()->json(['error' => 'کد هدیه منقضی شده است!'], 400);
|
||||
}
|
||||
|
||||
$gift->used_count += 1;
|
||||
$gift->save();
|
||||
|
||||
if($giftcode->sts != 1){
|
||||
return response()->json(['error' => 'کد هدیه قبلا استفاده شده است!'], 400);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//tranaction
|
||||
if($giftcode->unit === 1){
|
||||
|
||||
//add IRR to user wallet
|
||||
$Withdraw = Withdraw::create([
|
||||
'customer_id' => auth('customer')->id(),
|
||||
"cash" => $giftcode->amount,
|
||||
"note" => "کارت هدیه",
|
||||
'sts' => 1,
|
||||
'order_id' => $giftcode->id,
|
||||
]);
|
||||
$Withdraw->tracked_number = $giftcode->code; // توکن تراکنش برابر کد گیفت کارت
|
||||
if ($Withdraw->cash == $giftcode->amount) {
|
||||
if ($Withdraw->order_id){
|
||||
$this->verifyOrder($Withdraw->id);
|
||||
}
|
||||
$Withdraw->zify_code = $Withdraw->tracked_number;
|
||||
$Withdraw->tracked_number = $giftcode->id;//gift card id
|
||||
$Withdraw->sts = 6;
|
||||
$Withdraw->save();
|
||||
$wallet_interface_fund = new WalletInterface($user->id, $user->type);
|
||||
$transaction = new TransactionInterface($wallet_interface_fund, $_inp = [
|
||||
"cash" => $giftcode->amount,
|
||||
'expire_at' => now(),
|
||||
'withdraw_id' => $Withdraw->id,
|
||||
'active' => true
|
||||
], 3, false, true);
|
||||
}
|
||||
|
||||
//set text for sms to customer
|
||||
$amount = number_format($giftcode->amount);
|
||||
$message = "$user->name عزیز
|
||||
کیف پول شما به مقدار $amount ریال
|
||||
به واسطه کارت هدیه از طرف $gift->name شارژ شد.
|
||||
قلک طلا";
|
||||
|
||||
}elseif ($giftcode->unit === 2){
|
||||
//add Gold to user wallet
|
||||
return response()->json(['message'=> 'در دست ساخت👷🏻🚧'], 400);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//gift code update
|
||||
$giftcode->assigned_to = $user->id;
|
||||
$giftcode->sts = 2;
|
||||
$giftcode->used_at = now();
|
||||
$giftcode->save();
|
||||
|
||||
$this->Send($user->mobile, $message);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'کد هدیه با موفقیت استفاده شد!',
|
||||
'code' => $giftcode->code,
|
||||
'amount' => $giftcode->amount,
|
||||
'unit' => $giftcode->unit,
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
if(!$user){
|
||||
throw new UnprocessableEntityHttpException('auth failed!');
|
||||
}
|
||||
$records = GiftCard::with([
|
||||
'giftcardcodes',
|
||||
'giftcardcodes.assignedTo'
|
||||
]);
|
||||
TableEngine::create($records, $request->all())
|
||||
->initSearch([])
|
||||
->select([
|
||||
'*', GiftCard::$SELECT_STS_STR
|
||||
])->export($records);
|
||||
return response()->json($records);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\GiftCardCodes;
|
||||
|
||||
class GiftCard extends Model
|
||||
{
|
||||
use HasFactory,Selector;
|
||||
|
||||
protected $table = 'giftcards';
|
||||
|
||||
protected $fillable = [
|
||||
'customer_id',
|
||||
'count',
|
||||
'amount',
|
||||
'total',
|
||||
'unit',
|
||||
'unique_code',
|
||||
'created_at',
|
||||
'sts',
|
||||
'expiry_date',
|
||||
'used_count',
|
||||
'description',
|
||||
'name',
|
||||
'mobile',
|
||||
];
|
||||
|
||||
public function giftcardcodes(){
|
||||
return $this->hasMany(GiftCardCodes::class, 'giftcard_id', 'id');
|
||||
}
|
||||
|
||||
static $STS = [
|
||||
1 => "فعال",
|
||||
2 => "تمام شده",
|
||||
3 => "منقضی شده"
|
||||
|
||||
];
|
||||
static $UNIT = [
|
||||
1=>'IRR',
|
||||
2=>'Golden'
|
||||
];
|
||||
|
||||
public function getStsStrAttribute()
|
||||
{
|
||||
return self::$STS[$this->sts] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\Customer;
|
||||
|
||||
|
||||
class GiftCardCodes extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'giftcard_codes';
|
||||
|
||||
protected $fillable = [
|
||||
'giftcard_id',
|
||||
'code',
|
||||
'sts',
|
||||
'unit',
|
||||
'amount',
|
||||
'assigned_to',
|
||||
'used_at',
|
||||
'expiry_date',
|
||||
];
|
||||
|
||||
static $STS = [
|
||||
1=>'فعال',
|
||||
2=>'استفاده شده',
|
||||
3=>'منقضی شده'
|
||||
];
|
||||
|
||||
static $UNIT = [
|
||||
1=>'IRR',
|
||||
2=>'Golden'
|
||||
];
|
||||
|
||||
public function assignedTo()
|
||||
{
|
||||
return $this->belongsTo(Customer::class, 'assigned_to', 'id')->select('id', 'name', 'mobile');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ use App\Http\Controllers\Admin\TahesabController;
|
||||
use App\Http\Controllers\Admin\FileController;
|
||||
use App\Http\Controllers\Site\OfferCodesController;
|
||||
use App\Http\Controllers\Admin\SettingsController;
|
||||
use \App\Http\Controllers\Admin\GiftCardController;
|
||||
|
||||
|
||||
Route::middleware('auth:user')->group(function (){
|
||||
@@ -154,5 +155,9 @@ Route::middleware('auth:user')->group(function (){
|
||||
//check card
|
||||
Route::post('/check_card',[CustomerController::class, 'cardChecker']);
|
||||
|
||||
//Giftcard
|
||||
Route::post('giftcard/generate/', [GiftCardController::class, 'redeem']);
|
||||
Route::post('giftcard/table/', [GiftCardController::class, 'table']);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,7 @@ use App\Http\Controllers\Admin\ProductController as AdminProductController;
|
||||
use App\Http\Controllers\Customer\ProductController;
|
||||
use App\Http\Controllers\Admin\TahesabController;
|
||||
use App\Http\Controllers\Site\OfferCodesController;
|
||||
use App\Http\Controllers\Admin\GiftCardController;
|
||||
|
||||
|
||||
//----------gateway-verify
|
||||
@@ -97,6 +98,9 @@ Route::middleware('auth:customer')->group(function () {
|
||||
// Route::post('account/tahesab', [TahesabController::class, 'store']);
|
||||
Route::post('account/tahesab/trade', [TahesabController::class, 'storeTrade']);
|
||||
|
||||
//--------------------use gift card
|
||||
Route::post('giftcard/use', [GiftCardController::class, 'useGiftCard']);
|
||||
|
||||
//offer code
|
||||
Route::post('offer_code/check_code', [OfferCodesController::class, 'check_code']);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user