product time changed 5

This commit is contained in:
2026-07-02 21:24:26 +03:30
parent 3d56178e62
commit 1ce2e73ea9
2 changed files with 70 additions and 51 deletions
@@ -268,6 +268,9 @@ class SingleOrderController extends Controller
if ($wallet->cash < $data['payment_amount']) {
throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است');
}
$this->assertProviderOrderAccepted($data);
$data['order'] = $order = Order::create($data);
$abshode = $this->resolveAbshodeProductContext($data);
$productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0);
@@ -285,9 +288,9 @@ class SingleOrderController extends Controller
$buyTransaction['note'] = 'product_id:' . $productId;
}
new TransactionInterface($wallet_interface, $buyTransaction, 1, true, false);
$this->finalizeProviderOrder($data);
$response['1'] = 2;
//send to provider
$this->sendProviderOrder($data, $wallet_interface, (int) $data['direction']);
return $response;
}
@@ -462,6 +465,9 @@ class SingleOrderController extends Controller
if ($wallet->gold < $goldDebit) {
throw new UnprocessableEntityHttpException($this->insufficientGoldBalanceMessage($data));
}
$this->assertProviderOrderAccepted($data);
$data['order'] = $order = Order::create($data);
$abshode = $this->resolveAbshodeProductContext($data);
$productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0);
@@ -478,10 +484,7 @@ class SingleOrderController extends Controller
$sellTransaction['note'] = 'product_id:' . $productId;
}
new TransactionInterface($wallet_interface, $sellTransaction, 2, false, true);
//send to Goldir
$this->sendProviderOrder($data, $wallet_interface, (int) $data['direction']);
$this->finalizeProviderOrder($data);
}
private function processSellTransactionProduct(&$data, $wallet, $wallet_interface)
@@ -689,54 +692,54 @@ class SingleOrderController extends Controller
$this->send($customer->mobile, $customerMessage);
}
private function sendProviderOrder(array &$data, $wallet_interface, int $direction)
private function assertProviderOrderAccepted(array &$data): void
{
if (app(\App\Services\ConfigService::class)->usesMontzarPricing()) {
try {
return app(MontzarService::class)->sendOrder($data);
} catch (UnprocessableEntityHttpException $e) {
$this->rollbackProviderFailedOrder($data, $wallet_interface, $direction);
throw $e;
} catch (\Throwable $e) {
$this->rollbackProviderFailedOrder($data, $wallet_interface, $direction);
throw new UnprocessableEntityHttpException(
'خطا در ثبت سفارش ارائه دهنده: ' . $e->getMessage()
);
}
}
return null;
}
private function rollbackProviderFailedOrder(array $data, $wallet_interface, int $direction): void
{
$order = $data['order'] ?? null;
if (!$order instanceof Order) {
if (!app(ConfigService::class)->usesMontzarPricing()) {
return;
}
if ($direction === 1) {
new TransactionInterface($wallet_interface, [
'cash' => $data['payment_amount'],
'gold' => $data['amount'],
'expire_at' => now(),
'active' => true,
'order_id' => $order->id,
'start_at' => now(),
], 2, false, true);
} else {
new TransactionInterface($wallet_interface, [
'gold' => $data['amount'],
'cash' => $data['total_price'],
'expire_at' => now(),
'active' => true,
'order_id' => $order->id,
], 1, true, false);
$this->assertNoRecentDuplicateLocalOrder($data);
$data['_montzar_parsed'] = app(MontzarService::class)->submitOrder($data);
}
private function finalizeProviderOrder(array &$data): void
{
if (!app(ConfigService::class)->usesMontzarPricing()) {
return;
}
$order->delete();
$parsed = $data['_montzar_parsed'] ?? null;
if (!is_array($parsed)) {
return;
}
app(MontzarService::class)->applyParsedOrder($data, $parsed);
unset($data['_montzar_parsed']);
}
private function assertNoRecentDuplicateLocalOrder(array $data): void
{
$customerId = (int) ($data['customer_id'] ?? 0);
if ($customerId <= 0) {
return;
}
$direction = (int) ($data['direction'] ?? 0);
$amount = round((float) ($data['amount'] ?? 0), 6);
$windowSeconds = 20;
$duplicate = Order::where('customer_id', $customerId)
->where('direction', $direction)
->where('created_at', '>=', now()->subSeconds($windowSeconds))
->whereRaw('ROUND(amount, 6) = ?', [$amount])
->exists();
if ($duplicate) {
throw new UnprocessableEntityHttpException(
'در حال حاضر سفارش مشابه ثبت شده است. لطفاً چند ثانیه دیگر تلاش کنید.'
);
}
}
public function storeBackUp(Request $request)