94 lines
3.0 KiB
PHP
94 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Customer;
|
|
use App\Models\User;
|
|
use App\Models\Withdraw;
|
|
use App\Services\ConfigService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
|
use Rooyesh\Wallet\interfaces\WalletInterface;
|
|
|
|
/**
|
|
* شارژ idempotent کیف بعد از پرداخت موفق درگاه.
|
|
*/
|
|
class WalletDepositCreditor
|
|
{
|
|
public function __construct(private ConfigService $config)
|
|
{
|
|
}
|
|
|
|
public function findByReference(string $referenceId): ?Withdraw
|
|
{
|
|
if ($referenceId === '') {
|
|
return null;
|
|
}
|
|
|
|
if (preg_match('/^WD-(\d+)$/', $referenceId, $matches)) {
|
|
return Withdraw::with('customer')->find((int) $matches[1]);
|
|
}
|
|
|
|
return Withdraw::with('customer')
|
|
->where('tracked_number', $referenceId)
|
|
->orderByDesc('id')
|
|
->first();
|
|
}
|
|
|
|
public function markPaidAndCredit(Withdraw $withdraw, string $trackNumber, string $note = 'واریز درگاه'): void
|
|
{
|
|
DB::transaction(function () use ($withdraw, $trackNumber, $note) {
|
|
$locked = Withdraw::query()->whereKey($withdraw->id)->lockForUpdate()->first();
|
|
if (!$locked || (int) $locked->sts === 2) {
|
|
return;
|
|
}
|
|
|
|
if ($trackNumber !== '') {
|
|
$locked->tracked_number = $trackNumber;
|
|
}
|
|
$locked->sts = 2;
|
|
$locked->save();
|
|
|
|
$user = Customer::find($locked->customer_id);
|
|
if (!$user) {
|
|
return;
|
|
}
|
|
|
|
$wallet = new WalletInterface($user->id, $user->type);
|
|
new TransactionInterface($wallet, [
|
|
'cash' => (int) $locked->cash,
|
|
'expire_at' => now(),
|
|
'withdraw_id' => $locked->id,
|
|
'active' => true,
|
|
], 3, false, true);
|
|
|
|
try {
|
|
app(\App\Services\PargarSyncService::class)->queueWalletDocument(
|
|
$user,
|
|
'cash',
|
|
(float) $locked->cash,
|
|
[
|
|
'withdraw_id' => $locked->id,
|
|
'note' => $note,
|
|
'source' => 'gateway',
|
|
]
|
|
);
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
}
|
|
|
|
$locked->loadMissing('customer');
|
|
$message = 'کاربر ' . ($locked->customer->name ?? '') . ' با شماره موبایل ' . ($locked->customer->mobile ?? '')
|
|
. ' حساب خود را به مقدار ' . number_format((int) $locked->cash) . ' ریال شارژ کرد. با تشکر، '
|
|
. $this->config->getString('name_fa', '');
|
|
|
|
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
|
|
if (Controller::isSmsNotifEnabled('sms_notif_gateway_charge_admin')) {
|
|
Controller::Send($admin->mobile, $message);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|