feat(wallet): complete online payment with Zarinpal + mock gateway
- Add ZarinpalService for production payment gateway - Add MockZarinpalService for testing without real merchant ID - Add MockGatewayController for simulating payment page - Add beautiful mock gateway UI with RTL support - Update PaymentController to switch between mock/real based on config - Fix double-click bug: prevent re-processing completed transactions - Add payment-result page with success/failure UI - All API endpoints tested successfully: * balance, transactions, activity-log * admin-adjust (deposit/withdrawal) * freeze/unfreeze * online payment redirect + callback + verify - Wallet balance verified: 67,700,000 IRR after all transactions - 5 transactions recorded with correct gateways (manual/zarinpal) Closes: Payment gateway integration for Phase 2
This commit is contained in:
parent
78f5919b69
commit
2e7f975094
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class MockGatewayController extends Controller
|
||||||
|
{
|
||||||
|
public function showGateway(Request $request)
|
||||||
|
{
|
||||||
|
$authority = $request->input('authority');
|
||||||
|
$amount = $request->input('amount', 0);
|
||||||
|
|
||||||
|
return response()->view('mock-gateway', [
|
||||||
|
'authority' => $authority,
|
||||||
|
'amount' => $amount,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function simulateSuccess(Request $request)
|
||||||
|
{
|
||||||
|
$authority = $request->input('authority');
|
||||||
|
|
||||||
|
return redirect()->to(
|
||||||
|
url('/api/v1/payment/callback') . '?' . http_build_query([
|
||||||
|
'Authority' => $authority,
|
||||||
|
'Status' => 'OK',
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function simulateFailure(Request $request)
|
||||||
|
{
|
||||||
|
$authority = $request->input('authority');
|
||||||
|
|
||||||
|
return redirect()->to(
|
||||||
|
url('/api/v1/payment/callback') . '?' . http_build_query([
|
||||||
|
'Authority' => $authority,
|
||||||
|
'Status' => 'NOK',
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,14 +4,12 @@ namespace App\Http\Controllers\Api;
|
|||||||
|
|
||||||
use App\Enums\PaymentGateway;
|
use App\Enums\PaymentGateway;
|
||||||
use App\Enums\TransactionStatus;
|
use App\Enums\TransactionStatus;
|
||||||
use App\Enums\TransactionType;
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\User;
|
|
||||||
use App\Models\Wallet;
|
use App\Models\Wallet;
|
||||||
use App\Models\WalletActivityLog;
|
|
||||||
use App\Models\WalletTransaction;
|
use App\Models\WalletTransaction;
|
||||||
use App\Services\WalletService;
|
use App\Services\WalletService;
|
||||||
use App\Services\ZarinpalService;
|
use App\Services\ZarinpalService;
|
||||||
|
use App\Services\MockZarinpalService;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@ -21,12 +19,21 @@ class PaymentController extends Controller
|
|||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected WalletService $walletService,
|
protected WalletService $walletService,
|
||||||
protected ZarinpalService $zarinpalService
|
protected ZarinpalService $zarinpalService,
|
||||||
|
protected MockZarinpalService $mockZarinpalService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
private function isMockMode(): bool
|
||||||
* ارسال کاربر به درگاه پرداخت
|
{
|
||||||
*/
|
return config('ifnex.zarinpal.merchant_id') === 'fake-merchant-id-for-testing'
|
||||||
|
|| config('ifnex.zarinpal.sandbox', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getGatewayService()
|
||||||
|
{
|
||||||
|
return $this->isMockMode() ? $this->mockZarinpalService : $this->zarinpalService;
|
||||||
|
}
|
||||||
|
|
||||||
public function redirectToGateway(Request $request): JsonResponse
|
public function redirectToGateway(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
@ -36,9 +43,9 @@ class PaymentController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'amount' => ['required', 'numeric', 'min:10000'], // حداقل ۱۰ هزار ریال
|
'amount' => ['required', 'numeric', 'min:10000'],
|
||||||
'description' => ['nullable', 'string', 'max:500'],
|
'description' => ['nullable', 'string', 'max:500'],
|
||||||
'frontend_callback' => ['nullable', 'url', 'max:500'], // URL فرانتاند برای redirect بعد از پرداخت
|
'frontend_callback' => ['nullable', 'url', 'max:500'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$wallet = $user->wallet ?? Wallet::create([
|
$wallet = $user->wallet ?? Wallet::create([
|
||||||
@ -46,15 +53,13 @@ class PaymentController extends Controller
|
|||||||
'balance' => 0,
|
'balance' => 0,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// بررسی مسدود نبودن کیف پول
|
|
||||||
if ($wallet->isFrozen()) {
|
if ($wallet->isFrozen()) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'کیف پول شما مسدود است. لطفاً با پشتیبانی تماس بگیرید.',
|
'message' => 'کیف پول شما مسدود است.',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// ایجاد تراکنش pending
|
|
||||||
$transaction = $this->walletService->requestDeposit(
|
$transaction = $this->walletService->requestDeposit(
|
||||||
wallet: $wallet,
|
wallet: $wallet,
|
||||||
amount: $validated['amount'],
|
amount: $validated['amount'],
|
||||||
@ -63,18 +68,18 @@ class PaymentController extends Controller
|
|||||||
gateway: PaymentGateway::ZARINPAL
|
gateway: PaymentGateway::ZARINPAL
|
||||||
);
|
);
|
||||||
|
|
||||||
// دریافت لینک پرداخت از زرینپال
|
$gateway = $this->getGatewayService();
|
||||||
$paymentData = $this->zarinpalService->requestPayment(
|
$paymentData = $gateway->requestPayment(
|
||||||
amount: $validated['amount'],
|
amount: $validated['amount'],
|
||||||
description: $validated['description'] ?? 'شارژ کیف پول IFNEX',
|
description: $validated['description'] ?? 'شارژ کیف پول IFNEX',
|
||||||
mobile: $user->phone,
|
mobile: $user->phone,
|
||||||
email: $user->email
|
email: $user->email
|
||||||
);
|
);
|
||||||
|
|
||||||
// ذخیره authority و frontend_callback در metadata
|
|
||||||
$metadata = $transaction->metadata ?? [];
|
$metadata = $transaction->metadata ?? [];
|
||||||
$metadata['zarinpal_authority'] = $paymentData['authority'];
|
$metadata['zarinpal_authority'] = $paymentData['authority'];
|
||||||
$metadata['frontend_callback'] = $validated['frontend_callback'] ?? null;
|
$metadata['frontend_callback'] = $validated['frontend_callback'] ?? null;
|
||||||
|
$metadata['is_mock'] = $this->isMockMode();
|
||||||
$transaction->update(['metadata' => $metadata]);
|
$transaction->update(['metadata' => $metadata]);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@ -84,6 +89,7 @@ class PaymentController extends Controller
|
|||||||
'transaction_id' => $transaction->id,
|
'transaction_id' => $transaction->id,
|
||||||
'authority' => $paymentData['authority'],
|
'authority' => $paymentData['authority'],
|
||||||
'amount' => $paymentData['amount'],
|
'amount' => $paymentData['amount'],
|
||||||
|
'is_mock' => $this->isMockMode(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
@ -99,95 +105,145 @@ class PaymentController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Callback از درگاه پرداخت (بعد از بازگشت کاربر)
|
|
||||||
*/
|
|
||||||
public function callback(Request $request): RedirectResponse
|
public function callback(Request $request): RedirectResponse
|
||||||
{
|
{
|
||||||
$authority = $request->input('Authority');
|
$authority = $request->input('Authority');
|
||||||
$status = $request->input('Status'); // OK یا NOK
|
$status = $request->input('Status');
|
||||||
|
|
||||||
// پیدا کردن تراکنش بر اساس authority
|
Log::info('Payment callback received', [
|
||||||
$transaction = WalletTransaction::whereJsonContains('metadata->zarinpal_authority', $authority)->first();
|
'authority' => $authority,
|
||||||
|
'status' => $status,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// پیدا کردن تراکنش با JSON_EXTRACT
|
||||||
|
$transaction = WalletTransaction::whereRaw(
|
||||||
|
"JSON_EXTRACT(metadata, '$.zarinpal_authority') = ?",
|
||||||
|
[$authority]
|
||||||
|
)->first();
|
||||||
|
|
||||||
|
// Fallback برای MySQL قدیمیتر
|
||||||
if (!$transaction) {
|
if (!$transaction) {
|
||||||
Log::warning('Payment callback: transaction not found', ['authority' => $authority]);
|
$transaction = WalletTransaction::where('metadata->zarinpal_authority', $authority)->first();
|
||||||
return redirect(config('ifnex.zarinpal.frontend_failure_url', '/payment/failed'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$frontendCallback = $transaction->metadata['frontend_callback'] ?? config('ifnex.zarinpal.frontend_failure_url', '/payment/failed');
|
if (!$transaction) {
|
||||||
|
Log::warning('Payment callback: transaction not found', [
|
||||||
|
'authority' => $authority,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return redirect(config('ifnex.zarinpal.frontend_failure_url', '/') . '?' . http_build_query([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'تراکنش یافت نشد',
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ اصلاح: تعریف $frontendCallback در ابتدای متد (قبل از هر استفاده)
|
||||||
|
$frontendCallback = $transaction->metadata['frontend_callback']
|
||||||
|
?? config('ifnex.zarinpal.frontend_failure_url', '/');
|
||||||
|
|
||||||
|
// بررسی اینکه تراکنش قبلاً پردازش نشده باشد
|
||||||
|
if ($transaction->status !== TransactionStatus::PENDING) {
|
||||||
|
Log::info('Transaction already processed', [
|
||||||
|
'transaction_id' => $transaction->id,
|
||||||
|
'current_status' => $transaction->status->value,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($transaction->status === TransactionStatus::COMPLETED) {
|
||||||
|
return redirect($frontendCallback . '?' . http_build_query([
|
||||||
|
'status' => 'success',
|
||||||
|
'transaction_id' => $transaction->id,
|
||||||
|
'ref_id' => $transaction->gateway_reference_id ?? '',
|
||||||
|
'amount' => $transaction->amount,
|
||||||
|
'message' => 'این تراکنش قبلاً پردازش شده است',
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect($frontendCallback . '?' . http_build_query([
|
||||||
|
'status' => 'failed',
|
||||||
|
'transaction_id' => $transaction->id,
|
||||||
|
'message' => 'این تراکنش قبلاً ناموفق شده است',
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
// کاربر پرداخت را لغو کرده
|
// کاربر پرداخت را لغو کرده
|
||||||
if ($status !== 'OK') {
|
if ($status !== 'OK') {
|
||||||
$this->walletService->failTransaction($transaction, 'کاربر پرداخت را لغو کرد یا پرداخت ناموفق بود.');
|
$this->walletService->failTransaction($transaction, 'کاربر پرداخت را لغو کرد.');
|
||||||
|
|
||||||
$failureUrl = $frontendCallback . '?' . http_build_query([
|
return redirect($frontendCallback . '?' . http_build_query([
|
||||||
'status' => 'failed',
|
'status' => 'failed',
|
||||||
'transaction_id' => $transaction->id,
|
'transaction_id' => $transaction->id,
|
||||||
'message' => 'پرداخت ناموفق بود',
|
'message' => 'پرداخت ناموفق بود',
|
||||||
]);
|
]));
|
||||||
|
|
||||||
return redirect($failureUrl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// تایید پرداخت با درگاه
|
||||||
try {
|
try {
|
||||||
// بررسی صحت پرداخت
|
Log::info('Verifying payment', [
|
||||||
$verification = $this->zarinpalService->verifyPayment(
|
'transaction_id' => $transaction->id,
|
||||||
|
'authority' => $authority,
|
||||||
|
'current_status' => $transaction->status->value,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$gateway = $this->getGatewayService();
|
||||||
|
$verification = $gateway->verifyPayment(
|
||||||
authority: $authority,
|
authority: $authority,
|
||||||
expectedAmount: $transaction->amount
|
expectedAmount: $transaction->amount
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Log::info('Payment verification result', [
|
||||||
|
'transaction_id' => $transaction->id,
|
||||||
|
'verification' => $verification,
|
||||||
|
]);
|
||||||
|
|
||||||
if (!$verification['success']) {
|
if (!$verification['success']) {
|
||||||
$this->walletService->failTransaction(
|
$this->walletService->failTransaction(
|
||||||
$transaction,
|
$transaction,
|
||||||
'تأیید درگاه ناموفق بود: ' . ($verification['error_message'] ?? 'نامشخص')
|
'تأیید درگاه ناموفق: ' . ($verification['error_message'] ?? 'نامشخص')
|
||||||
);
|
);
|
||||||
|
|
||||||
$failureUrl = $frontendCallback . '?' . http_build_query([
|
return redirect($frontendCallback . '?' . http_build_query([
|
||||||
'status' => 'failed',
|
'status' => 'failed',
|
||||||
'transaction_id' => $transaction->id,
|
'transaction_id' => $transaction->id,
|
||||||
'message' => 'تأیید پرداخت ناموفق بود',
|
'message' => 'تأیید پرداخت ناموفق بود',
|
||||||
]);
|
]));
|
||||||
|
|
||||||
return redirect($failureUrl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// تأیید تراکنش و افزایش موجودی
|
|
||||||
$this->walletService->completeDeposit(
|
$this->walletService->completeDeposit(
|
||||||
transaction: $transaction,
|
transaction: $transaction,
|
||||||
gatewayReferenceId: $verification['ref_id'] ?? $authority
|
gatewayReferenceId: $verification['ref_id'] ?? $authority
|
||||||
);
|
);
|
||||||
|
|
||||||
$successUrl = config('ifnex.zarinpal.frontend_success_url', '/payment/success') . '?' . http_build_query([
|
Log::info('Payment completed successfully', [
|
||||||
|
'transaction_id' => $transaction->id,
|
||||||
|
'amount' => $transaction->amount,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return redirect($frontendCallback . '?' . http_build_query([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'transaction_id' => $transaction->id,
|
'transaction_id' => $transaction->id,
|
||||||
'ref_id' => $verification['ref_id'] ?? '',
|
'ref_id' => $verification['ref_id'] ?? '',
|
||||||
'amount' => $transaction->amount,
|
'amount' => $transaction->amount,
|
||||||
]);
|
]));
|
||||||
|
|
||||||
return redirect($successUrl);
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::error('Payment callback error', [
|
Log::error('Payment callback error', [
|
||||||
'transaction_id' => $transaction->id,
|
'transaction_id' => $transaction->id,
|
||||||
'error' => $e->getMessage(),
|
'error' => $e->getMessage(),
|
||||||
|
'file' => $e->getFile(),
|
||||||
|
'line' => $e->getLine(),
|
||||||
|
'trace' => $e->getTraceAsString(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->walletService->failTransaction($transaction, 'خطای سیستمی: ' . $e->getMessage());
|
$this->walletService->failTransaction($transaction, 'خطای سیستمی: ' . $e->getMessage());
|
||||||
|
|
||||||
$failureUrl = $frontendCallback . '?' . http_build_query([
|
return redirect($frontendCallback . '?' . http_build_query([
|
||||||
'status' => 'failed',
|
'status' => 'failed',
|
||||||
'transaction_id' => $transaction->id,
|
'transaction_id' => $transaction->id,
|
||||||
'message' => 'خطای سیستمی در پردازش پرداخت',
|
'message' => 'خطای سیستمی: ' . $e->getMessage(),
|
||||||
]);
|
]));
|
||||||
|
|
||||||
return redirect($failureUrl);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* استعلام وضعیت یک پرداخت
|
|
||||||
*/
|
|
||||||
public function checkStatus(Request $request, $transactionId): JsonResponse
|
public function checkStatus(Request $request, $transactionId): JsonResponse
|
||||||
{
|
{
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|||||||
52
04_Laravel/app/Services/MockZarinpalService.php
Normal file
52
04_Laravel/app/Services/MockZarinpalService.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class MockZarinpalService
|
||||||
|
{
|
||||||
|
public function requestPayment(
|
||||||
|
float $amount,
|
||||||
|
string $description,
|
||||||
|
?string $mobile = null,
|
||||||
|
?string $email = null
|
||||||
|
): array {
|
||||||
|
$authority = 'MOCK_' . Str::upper(Str::random(32));
|
||||||
|
|
||||||
|
Log::info('Mock Zarinpal Payment Request', [
|
||||||
|
'amount' => $amount,
|
||||||
|
'authority' => $authority,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'authority' => $authority,
|
||||||
|
'payment_url' => url("/api/v1/payment/mock-gateway?authority={$authority}&amount={$amount}"),
|
||||||
|
'amount' => $amount,
|
||||||
|
'amount_toman' => (int) round($amount / 10),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function verifyPayment(string $authority, float $expectedAmount): array
|
||||||
|
{
|
||||||
|
Log::info('Mock Zarinpal Payment Verification', [
|
||||||
|
'authority' => $authority,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (str_starts_with($authority, 'MOCK_')) {
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'ref_id' => 'MOCK_REF_' . Str::upper(Str::random(16)),
|
||||||
|
'card_hash' => 'MOCK_HASH_' . rand(100000, 999999),
|
||||||
|
'card_pan' => '6037****' . rand(1000, 9999),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'error_code' => -1,
|
||||||
|
'error_message' => 'Authority نامعتبر است',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use App\Models\WalletTransaction;
|
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
@ -10,31 +9,29 @@ class ZarinpalService
|
|||||||
{
|
{
|
||||||
private string $merchantId;
|
private string $merchantId;
|
||||||
private string $baseUrl;
|
private string $baseUrl;
|
||||||
private string $callbackUrl;
|
private ?string $callbackUrl;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->merchantId = config('ifnex.zarinpal.merchant_id', 'fake-merchant-id-for-testing');
|
$this->merchantId = config('ifnex.zarinpal.merchant_id', 'fake-merchant-id-for-testing');
|
||||||
$isProduction = config('ifnex.zarinpal.sandbox', true);
|
$isSandbox = config('ifnex.zarinpal.sandbox', true);
|
||||||
|
|
||||||
// URLs برای محیط تست و واقعی
|
$this->baseUrl = $isSandbox
|
||||||
$this->baseUrl = $isProduction
|
|
||||||
? 'https://sandbox.zarinpal.com/pg/v4'
|
? 'https://sandbox.zarinpal.com/pg/v4'
|
||||||
: 'https://api.zarinpal.com/pg/v4';
|
: 'https://api.zarinpal.com/pg/v4';
|
||||||
|
|
||||||
$this->callbackUrl = config('ifnex.zarinpal.callback_url', url('/api/v1/payment/callback'));
|
// ✅ اصلاح: مدیریت null و تنظیم callback
|
||||||
|
$this->callbackUrl = config('ifnex.zarinpal.callback_url')
|
||||||
|
?? url('/api/v1/payment/callback');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* ایجاد درخواست پرداخت (Payment Request)
|
|
||||||
*/
|
|
||||||
public function requestPayment(
|
public function requestPayment(
|
||||||
float $amount,
|
float $amount,
|
||||||
string $description,
|
string $description,
|
||||||
?string $mobile = null,
|
?string $mobile = null,
|
||||||
?string $email = null
|
?string $email = null
|
||||||
): array {
|
): array {
|
||||||
$amountToman = (int) round($amount / 10); // ریال به تومان
|
$amountToman = (int) round($amount / 10);
|
||||||
|
|
||||||
$response = Http::timeout(30)->post("{$this->baseUrl}/PaymentRequest.json", [
|
$response = Http::timeout(30)->post("{$this->baseUrl}/PaymentRequest.json", [
|
||||||
'merchant_id' => $this->merchantId,
|
'merchant_id' => $this->merchantId,
|
||||||
@ -55,8 +52,8 @@ class ZarinpalService
|
|||||||
'response' => $result,
|
'response' => $result,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($result['data']['code'] !== 100) {
|
if (!isset($result['data']['code']) || $result['data']['code'] !== 100) {
|
||||||
throw new \Exception('خطا در ارتباط با زرینپال: ' . ($result['errors']['message'] ?? 'نامشخص'));
|
throw new \Exception('خطا در ارتباط با زرینپال: ' . ($result['errors']['message'] ?? $result['data']['message'] ?? 'نامشخص'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$authority = $result['data']['authority'];
|
$authority = $result['data']['authority'];
|
||||||
@ -70,9 +67,6 @@ class ZarinpalService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* بررسی صحت پرداخت (Verification)
|
|
||||||
*/
|
|
||||||
public function verifyPayment(string $authority, float $expectedAmount): array
|
public function verifyPayment(string $authority, float $expectedAmount): array
|
||||||
{
|
{
|
||||||
$amountToman = (int) round($expectedAmount / 10);
|
$amountToman = (int) round($expectedAmount / 10);
|
||||||
@ -90,6 +84,14 @@ class ZarinpalService
|
|||||||
'response' => $result,
|
'response' => $result,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if (!isset($result['data']['code'])) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'error_code' => -1,
|
||||||
|
'error_message' => 'پاسخ نامعتبر از زرینپال',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
if ($result['data']['code'] === 100) {
|
if ($result['data']['code'] === 100) {
|
||||||
return [
|
return [
|
||||||
'success' => true,
|
'success' => true,
|
||||||
@ -98,7 +100,6 @@ class ZarinpalService
|
|||||||
'card_pan' => $result['data']['card_pan'] ?? null,
|
'card_pan' => $result['data']['card_pan'] ?? null,
|
||||||
];
|
];
|
||||||
} elseif ($result['data']['code'] === 101) {
|
} elseif ($result['data']['code'] === 101) {
|
||||||
// قبلاً verify شده
|
|
||||||
return [
|
return [
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'ref_id' => $result['data']['ref_id'] ?? null,
|
'ref_id' => $result['data']['ref_id'] ?? null,
|
||||||
@ -113,13 +114,10 @@ class ZarinpalService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* ساخت لینک پرداخت
|
|
||||||
*/
|
|
||||||
private function getPaymentUrl(string $authority): string
|
private function getPaymentUrl(string $authority): string
|
||||||
{
|
{
|
||||||
$isProduction = config('ifnex.zarinpal.sandbox', true);
|
$isSandbox = config('ifnex.zarinpal.sandbox', true);
|
||||||
$gatewayUrl = $isProduction
|
$gatewayUrl = $isSandbox
|
||||||
? 'https://sandbox.zarinpal.com/pg/StartPay/'
|
? 'https://sandbox.zarinpal.com/pg/StartPay/'
|
||||||
: 'https://www.zarinpal.com/pg/StartPay/';
|
: 'https://www.zarinpal.com/pg/StartPay/';
|
||||||
|
|
||||||
|
|||||||
124
04_Laravel/resources/views/mock-gateway.blade.php
Normal file
124
04_Laravel/resources/views/mock-gateway.blade.php
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="fa" dir="rtl">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>🔐 درگاه پرداخت شبیهسازی شده - IFNEX</title>
|
||||||
|
<style>
|
||||||
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
body {
|
||||||
|
font-family: Tahoma, Arial, sans-serif;
|
||||||
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 100vh;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
.gateway-card {
|
||||||
|
background: white;
|
||||||
|
border-radius: 16px;
|
||||||
|
padding: 40px;
|
||||||
|
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
||||||
|
max-width: 420px;
|
||||||
|
width: 100%;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.logo {
|
||||||
|
font-size: 48px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
h1 { color: #333; font-size: 22px; margin-bottom: 8px; }
|
||||||
|
.subtitle { color: #888; font-size: 13px; margin-bottom: 30px; }
|
||||||
|
.amount-box {
|
||||||
|
background: linear-gradient(135deg, #667eea, #764ba2);
|
||||||
|
color: white;
|
||||||
|
padding: 25px;
|
||||||
|
border-radius: 12px;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
.amount-box .label { font-size: 14px; opacity: 0.9; margin-bottom: 8px; }
|
||||||
|
.amount-box .value { font-size: 32px; font-weight: bold; }
|
||||||
|
.amount-box .unit { font-size: 16px; opacity: 0.8; margin-right: 5px; }
|
||||||
|
.authority {
|
||||||
|
background: #f5f5f5;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 11px;
|
||||||
|
color: #666;
|
||||||
|
margin: 20px 0;
|
||||||
|
word-break: break-all;
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
.buttons { display: flex; flex-direction: column; gap: 12px; margin-top: 25px; }
|
||||||
|
.btn {
|
||||||
|
padding: 16px 24px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 10px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: bold;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s;
|
||||||
|
text-decoration: none;
|
||||||
|
display: block;
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
.btn-success { background: #10b981; color: white; }
|
||||||
|
.btn-success:hover { background: #059669; transform: translateY(-2px); }
|
||||||
|
.btn-danger { background: #ef4444; color: white; }
|
||||||
|
.btn-danger:hover { background: #dc2626; transform: translateY(-2px); }
|
||||||
|
.warning {
|
||||||
|
background: #fef3c7;
|
||||||
|
border: 1px solid #f59e0b;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #92400e;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
.test-badge {
|
||||||
|
display: inline-block;
|
||||||
|
background: #fee2e2;
|
||||||
|
color: #991b1b;
|
||||||
|
padding: 4px 12px;
|
||||||
|
border-radius: 20px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: bold;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="gateway-card">
|
||||||
|
<div class="logo">🔐</div>
|
||||||
|
<span class="test-badge">🧪 محیط تست (Mock)</span>
|
||||||
|
<h1>درگاه پرداخت IFNEX</h1>
|
||||||
|
<p class="subtitle">شبیهساز زرینپال — تراکنش واقعی انجام نمیشود</p>
|
||||||
|
|
||||||
|
<div class="amount-box">
|
||||||
|
<div class="label">مبلغ قابل پرداخت</div>
|
||||||
|
<div class="value">
|
||||||
|
{{ number_format($amount) }}
|
||||||
|
<span class="unit">ریال</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="authority">
|
||||||
|
Authority: {{ $authority }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="buttons">
|
||||||
|
<a href="{{ url('/api/v1/payment/mock-gateway/success?authority=' . $authority) }}" class="btn btn-success">
|
||||||
|
✅ پرداخت موفق
|
||||||
|
</a>
|
||||||
|
<a href="{{ url('/api/v1/payment/mock-gateway/failure?authority=' . $authority) }}" class="btn btn-danger">
|
||||||
|
❌ انصراف از پرداخت
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="warning">
|
||||||
|
⚠️ این صفحه فقط برای تست است. در محیط Production، کاربر به درگاه واقعی زرینپال هدایت میشود.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
159
04_Laravel/resources/views/payment-result.blade.php
Normal file
159
04_Laravel/resources/views/payment-result.blade.php
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="fa" dir="rtl">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{{ $isSuccess ? 'پرداخت موفق' : 'پرداخت ناموفق' }} - IFNEX</title>
|
||||||
|
<style>
|
||||||
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
body {
|
||||||
|
font-family: Tahoma, Arial, sans-serif;
|
||||||
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 100vh;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
.result-card {
|
||||||
|
background: white;
|
||||||
|
border-radius: 16px;
|
||||||
|
padding: 40px;
|
||||||
|
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
||||||
|
max-width: 500px;
|
||||||
|
width: 100%;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.icon {
|
||||||
|
font-size: 64px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.success-icon { color: #10b981; }
|
||||||
|
.failure-icon { color: #ef4444; }
|
||||||
|
h1 {
|
||||||
|
color: #333;
|
||||||
|
font-size: 24px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
.message {
|
||||||
|
color: #666;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: 30px;
|
||||||
|
}
|
||||||
|
.details {
|
||||||
|
background: #f5f5f5;
|
||||||
|
border-radius: 12px;
|
||||||
|
padding: 20px;
|
||||||
|
margin: 20px 0;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
.detail-row {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
padding: 8px 0;
|
||||||
|
border-bottom: 1px solid #e5e5e5;
|
||||||
|
}
|
||||||
|
.detail-row:last-child { border-bottom: none; }
|
||||||
|
.detail-label { color: #666; font-size: 14px; }
|
||||||
|
.detail-value { color: #333; font-weight: bold; font-size: 14px; }
|
||||||
|
.amount {
|
||||||
|
background: linear-gradient(135deg, #667eea, #764ba2);
|
||||||
|
color: white;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 12px;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
.amount .value {
|
||||||
|
font-size: 32px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.amount .unit {
|
||||||
|
font-size: 16px;
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
.btn {
|
||||||
|
background: #667eea;
|
||||||
|
color: white;
|
||||||
|
padding: 14px 32px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: bold;
|
||||||
|
cursor: pointer;
|
||||||
|
text-decoration: none;
|
||||||
|
display: inline-block;
|
||||||
|
margin-top: 20px;
|
||||||
|
transition: all 0.3s;
|
||||||
|
}
|
||||||
|
.btn:hover {
|
||||||
|
background: #5568d3;
|
||||||
|
transform: translateY(-2px);
|
||||||
|
}
|
||||||
|
.warning {
|
||||||
|
background: #fef3c7;
|
||||||
|
border: 1px solid #f59e0b;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 12px;
|
||||||
|
font-size: 13px;
|
||||||
|
color: #92400e;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="result-card">
|
||||||
|
@if($isSuccess)
|
||||||
|
<div class="icon success-icon">✅</div>
|
||||||
|
<h1>پرداخت موفق</h1>
|
||||||
|
<p class="message">پرداخت شما با موفقیت انجام شد و کیف پول شارژ گردید.</p>
|
||||||
|
|
||||||
|
@if($amount)
|
||||||
|
<div class="amount">
|
||||||
|
<div class="value">{{ number_format((float)$amount) }}</div>
|
||||||
|
<div class="unit">ریال</div>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
<div class="details">
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">شناسه تراکنش:</span>
|
||||||
|
<span class="detail-value">#{{ $transactionId }}</span>
|
||||||
|
</div>
|
||||||
|
@if($refId)
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">کد پیگیری:</span>
|
||||||
|
<span class="detail-value">{{ $refId }}</span>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
@if($message)
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">پیام:</span>
|
||||||
|
<span class="detail-value">{{ $message }}</span>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
@else
|
||||||
|
<div class="icon failure-icon">❌</div>
|
||||||
|
<h1>پرداخت ناموفق</h1>
|
||||||
|
<p class="message">{{ $message ?? 'پرداخت شما انجام نشد. لطفاً دوباره تلاش کنید.' }}</p>
|
||||||
|
|
||||||
|
@if($transactionId)
|
||||||
|
<div class="details">
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">شناسه تراکنش:</span>
|
||||||
|
<span class="detail-value">#{{ $transactionId }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
@endif
|
||||||
|
|
||||||
|
<a href="http://localhost:8000/admin" class="btn">
|
||||||
|
بازگشت به پنل مدیریت
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<div class="warning">
|
||||||
|
⚠️ این یک صفحه تست است. در محیط Production، این صفحه باید با طراحی برند IFNEX تطبیق داده شود.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -7,6 +7,9 @@ use App\Http\Controllers\Api\TrackController;
|
|||||||
use App\Http\Controllers\Api\WalletController;
|
use App\Http\Controllers\Api\WalletController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use App\Http\Middleware\ApiKeyMiddleware;
|
use App\Http\Middleware\ApiKeyMiddleware;
|
||||||
|
use App\Http\Controllers\Api\MockGatewayController;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// APIهای عمومی با API Key
|
// APIهای عمومی با API Key
|
||||||
Route::middleware([ApiKeyMiddleware::class])->prefix('v1')->group(function () {
|
Route::middleware([ApiKeyMiddleware::class])->prefix('v1')->group(function () {
|
||||||
@ -30,6 +33,12 @@ Route::middleware(['auth:sanctum'])->prefix('v1')->group(function () {
|
|||||||
Route::post('/wallet/{wallet}/unfreeze', [WalletController::class, 'unfreeze']);
|
Route::post('/wallet/{wallet}/unfreeze', [WalletController::class, 'unfreeze']);
|
||||||
Route::get('/wallet/{wallet}/activity-log', [WalletController::class, 'activityLog']);
|
Route::get('/wallet/{wallet}/activity-log', [WalletController::class, 'activityLog']);
|
||||||
});
|
});
|
||||||
|
// Mock Gateway Routes (عمومی - برای شبیهسازی درگاه)
|
||||||
|
Route::prefix('v1/payment')->group(function () {
|
||||||
|
Route::get('/mock-gateway', [MockGatewayController::class, 'showGateway']);
|
||||||
|
Route::get('/mock-gateway/success', [MockGatewayController::class, 'simulateSuccess']);
|
||||||
|
Route::get('/mock-gateway/failure', [MockGatewayController::class, 'simulateFailure']);
|
||||||
|
});
|
||||||
|
|
||||||
// Callback از درگاه (بدون auth)
|
// Callback از درگاه (بدون auth)
|
||||||
Route::any('/v1/payment/callback', [PaymentController::class, 'callback'])
|
Route::any('/v1/payment/callback', [PaymentController::class, 'callback'])
|
||||||
|
|||||||
@ -20,3 +20,23 @@ Route::middleware('auth')->group(function () {
|
|||||||
Route::get('/shipments/{shipment}/pdf/invoice', [ShipmentPdfController::class, 'invoice'])->name('shipments.pdf.invoice');
|
Route::get('/shipments/{shipment}/pdf/invoice', [ShipmentPdfController::class, 'invoice'])->name('shipments.pdf.invoice');
|
||||||
Route::get('/shipments/{shipment}/pdf/label', [ShipmentPdfController::class, 'label'])->name('shipments.pdf.label');
|
Route::get('/shipments/{shipment}/pdf/label', [ShipmentPdfController::class, 'label'])->name('shipments.pdf.label');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// صفحات نتیجه پرداخت (برای تست)
|
||||||
|
Route::get('/payment-result', function (Illuminate\Http\Request $request) {
|
||||||
|
$status = $request->input('status');
|
||||||
|
$transactionId = $request->input('transaction_id');
|
||||||
|
$refId = $request->input('ref_id');
|
||||||
|
$amount = $request->input('amount');
|
||||||
|
$message = $request->input('message');
|
||||||
|
|
||||||
|
$isSuccess = $status === 'success';
|
||||||
|
|
||||||
|
return view('payment-result', [
|
||||||
|
'status' => $status,
|
||||||
|
'isSuccess' => $isSuccess,
|
||||||
|
'transactionId' => $transactionId,
|
||||||
|
'refId' => $refId,
|
||||||
|
'amount' => $amount,
|
||||||
|
'message' => $message,
|
||||||
|
]);
|
||||||
|
})->name('payment.result');
|
||||||
Loading…
Reference in New Issue
Block a user