feat(giftcard): redeem metal unit=2 into main wallet gold
This commit is contained in:
@@ -13,6 +13,7 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
||||
use Rooyesh\Wallet\interfaces\WalletInterface;
|
||||
use App\Services\ConfigService;
|
||||
use App\Services\PargarSyncService;
|
||||
|
||||
|
||||
class GiftCardController extends Controller
|
||||
@@ -88,20 +89,17 @@ class GiftCardController extends Controller
|
||||
|
||||
$redeem = $request->code;
|
||||
$giftcode = GiftCardCodes::where('code', $redeem)->first();
|
||||
$gift = GiftCard::find($giftcode->giftcard_id);
|
||||
|
||||
if(!$giftcode){
|
||||
return response()->json(['message' => 'کد هدیه یافت نشد!'], 404);
|
||||
}
|
||||
|
||||
//gift order update
|
||||
$gift = GiftCard::find($giftcode->giftcard_id);
|
||||
if(!$gift){
|
||||
return response()->json(['message' => 'کد هدیه یافت نشد!'], 404);
|
||||
}
|
||||
|
||||
if($gift->used_count+1 === $gift->count){
|
||||
$gift->sts = 2;
|
||||
}elseif($gift->expiry_date <= now()){
|
||||
if($gift->expiry_date && $gift->expiry_date <= now()){
|
||||
$gift->sts = 3;
|
||||
|
||||
$giftcode->sts = 3;
|
||||
$giftcode->save();
|
||||
$gift->save();
|
||||
@@ -109,19 +107,18 @@ class GiftCardController extends Controller
|
||||
return response()->json(['message' => 'کد هدیه منقضی شده است!'], 400);
|
||||
}
|
||||
|
||||
$gift->used_count += 1;
|
||||
$gift->save();
|
||||
|
||||
if($giftcode->sts != 1){
|
||||
if((int) $giftcode->sts !== 1){
|
||||
return response()->json(['message' => 'کد هدیه قبلا استفاده شده است!'], 400);
|
||||
}
|
||||
|
||||
if((int) $gift->count > 0 && (int) $gift->used_count >= (int) $gift->count){
|
||||
return response()->json(['message' => 'کد هدیه قبلا استفاده شده است!'], 400);
|
||||
}
|
||||
|
||||
$unit = (int) $giftcode->unit;
|
||||
$message = null;
|
||||
|
||||
|
||||
//tranaction
|
||||
if($giftcode->unit === 1){
|
||||
|
||||
if($unit === 1){
|
||||
//add IRR to user wallet
|
||||
$Withdraw = Withdraw::create([
|
||||
'customer_id' => auth('customer')->id(),
|
||||
@@ -148,7 +145,7 @@ class GiftCardController extends Controller
|
||||
], 3, false, true);
|
||||
|
||||
try {
|
||||
app(\App\Services\PargarSyncService::class)->queueWalletDocument(
|
||||
app(PargarSyncService::class)->queueWalletDocument(
|
||||
$user,
|
||||
'cash',
|
||||
(float) $giftcode->amount,
|
||||
@@ -163,28 +160,130 @@ class GiftCardController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
//set text for sms to customer
|
||||
$amount = number_format($giftcode->amount);
|
||||
$amount = number_format((float) $giftcode->amount);
|
||||
$message = "$user->name عزیز
|
||||
کیف پول شما به مقدار $amount ریال
|
||||
به واسطه کارت هدیه از طرف $gift->name شارژ شد.
|
||||
قلک طلا";
|
||||
".$this->Config('name_fa');
|
||||
|
||||
}elseif ($giftcode->unit === 2){
|
||||
//add Gold to user wallet
|
||||
return response()->json(['message'=> 'در دست ساخت👷🏻🚧'], 400);
|
||||
}elseif ($unit === 2){
|
||||
// شارژ کیف فلزی (طلا / مس — فیلد gold کیف اصلی)
|
||||
$goldAmount = (float) $giftcode->amount;
|
||||
if ($goldAmount <= 0) {
|
||||
return response()->json(['message' => 'مقدار کارت هدیه نامعتبر است!'], 400);
|
||||
}
|
||||
|
||||
$configService = app(ConfigService::class);
|
||||
$representative = $configService->mainWalletRepresentativeProduct();
|
||||
|
||||
$Withdraw = Withdraw::create([
|
||||
'customer_id' => $user->id,
|
||||
'gold' => $goldAmount,
|
||||
'note' => 'کارت هدیه',
|
||||
'sts' => 6,
|
||||
'tracked_number' => $giftcode->id,
|
||||
]);
|
||||
|
||||
$wallet_interface = new WalletInterface($user->id, $user->type);
|
||||
new TransactionInterface($wallet_interface, [
|
||||
'cash' => 0,
|
||||
'gold' => $goldAmount,
|
||||
'expire_at' => now(),
|
||||
'note' => 'کارت هدیه',
|
||||
'withdraw_id' => $Withdraw->id,
|
||||
'product_id' => $representative?->id,
|
||||
'active' => true,
|
||||
], 7, true, false);
|
||||
|
||||
$syncPayload = [
|
||||
'customer_id' => $user->id,
|
||||
'value' => $goldAmount,
|
||||
'direction' => 1,
|
||||
'withdraw_id' => $Withdraw->id,
|
||||
'note' => 'کارت هدیه',
|
||||
];
|
||||
if ($representative) {
|
||||
$syncPayload['product_id'] = $representative->id;
|
||||
$syncPayload['product'] = [
|
||||
'id' => $representative->id,
|
||||
'title' => $representative->title,
|
||||
'group_type' => $representative->group_type,
|
||||
'metal_id' => $representative->metal_id,
|
||||
'amount' => $representative->amount,
|
||||
'unit' => $representative->unit,
|
||||
'trade_by_quantity' => $representative->trade_by_quantity,
|
||||
];
|
||||
}
|
||||
|
||||
if ($configService->isTahesabAccounting()) {
|
||||
try {
|
||||
app(TahesabController::class)->walletGold($syncPayload);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
if ($configService->isZarinAccounting()) {
|
||||
try {
|
||||
app(ZarinController::class)->walletGold($syncPayload);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
app(PargarSyncService::class)->queueWalletDocument(
|
||||
$user,
|
||||
'gold',
|
||||
$goldAmount,
|
||||
[
|
||||
'withdraw_id' => $Withdraw->id,
|
||||
'note' => 'کارت هدیه',
|
||||
'product_id' => $representative?->id,
|
||||
'source' => 'gift_card',
|
||||
]
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
|
||||
$weightUnit = self::tradeWeightUnit();
|
||||
$metalLabel = self::mainWalletMetalLabel();
|
||||
$formattedGold = self::formatSmsGoldWeight($goldAmount);
|
||||
$message = "$user->name عزیز
|
||||
کیف $metalLabel شما به مقدار $formattedGold $weightUnit
|
||||
به واسطه کارت هدیه از طرف $gift->name شارژ شد.
|
||||
".$this->Config('name_fa');
|
||||
|
||||
try {
|
||||
app(\App\Services\CustomerAppNotifyService::class)->notifyCustomer(
|
||||
(int) $user->id,
|
||||
'واریز کارت هدیه',
|
||||
"کیف $metalLabel شما به اندازه $formattedGold $weightUnit شارژ شد.",
|
||||
'success'
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
|
||||
} else {
|
||||
return response()->json(['message' => 'نوع کارت هدیه پشتیبانی نمیشود.'], 400);
|
||||
}
|
||||
|
||||
if((int) $gift->used_count + 1 >= (int) $gift->count){
|
||||
$gift->sts = 2;
|
||||
}
|
||||
$gift->used_count = (int) $gift->used_count + 1;
|
||||
$gift->save();
|
||||
|
||||
|
||||
|
||||
//gift code update
|
||||
$giftcode->assigned_to = $user->id;
|
||||
$giftcode->sts = 2;
|
||||
$giftcode->used_at = now();
|
||||
$giftcode->save();
|
||||
|
||||
$this->Send($user->mobile, $message);
|
||||
if ($message) {
|
||||
$this->Send($user->mobile, $message);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'کد هدیه با موفقیت استفاده شد!',
|
||||
|
||||
Reference in New Issue
Block a user