Feat add payment with gateway of montify
This commit is contained in:
@@ -30,25 +30,25 @@ class SettingsController extends Controller
|
||||
->orderBy('key');
|
||||
|
||||
if ($request->boolean('system_only')) {
|
||||
return response()->json(
|
||||
$query
|
||||
->where(function ($q) {
|
||||
$q->where('can_edit', 0)->orWhere('can_edit', false);
|
||||
})
|
||||
->where(function ($q) {
|
||||
$q->where('is_active', 1)
|
||||
->orWhere('is_active', true)
|
||||
->orWhere('is_active', '1');
|
||||
})
|
||||
->get()
|
||||
);
|
||||
$rows = $query
|
||||
->where(function ($q) {
|
||||
$q->where('can_edit', 0)->orWhere('can_edit', false);
|
||||
})
|
||||
->where(function ($q) {
|
||||
$q->where('is_active', 1)
|
||||
->orWhere('is_active', true)
|
||||
->orWhere('is_active', '1');
|
||||
})
|
||||
->get();
|
||||
|
||||
return response()->json($this->authorizeSettingsPayload($rows));
|
||||
}
|
||||
|
||||
if ($request->boolean('editable_only')) {
|
||||
return response()->json($query->where('can_edit', 1)->get());
|
||||
return response()->json($this->authorizeSettingsPayload($query->where('can_edit', 1)->get()));
|
||||
}
|
||||
|
||||
return response()->json($query->get());
|
||||
return response()->json($this->authorizeSettingsPayload($query->get()));
|
||||
}
|
||||
|
||||
public function edit(Request $request)
|
||||
@@ -134,4 +134,20 @@ class SettingsController extends Controller
|
||||
{
|
||||
return in_array($key, self::PANEL_MANAGED_KEYS, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* اپ کاربر (بدون auth ادمین) هرگز secret/credential نمیبیند.
|
||||
* پنل ادمین با auth:user همه کلیدها را میبیند.
|
||||
*
|
||||
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Collection|iterable $rows
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
private function authorizeSettingsPayload($rows)
|
||||
{
|
||||
if (auth('user')->check()) {
|
||||
return collect($rows)->values();
|
||||
}
|
||||
|
||||
return Setting::forPublicApi($rows);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\pay;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Withdraw;
|
||||
use App\Services\ConfigService;
|
||||
use App\Services\Payment\WalletPaymentManager;
|
||||
use Illuminate\Http\Request;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
/**
|
||||
* API عمومی شارژ آنلاین کیف — بدون افشای credential به کلاینت.
|
||||
* انتخاب درگاه فقط سمت سرور (wallet_payment_provider).
|
||||
*/
|
||||
class WalletPaymentController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private WalletPaymentManager $payments,
|
||||
private ConfigService $config,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* فقط فلگ نمایش دکمه — بدون نام درگاه و بدون تنظیمات محرمانه.
|
||||
*/
|
||||
public function options()
|
||||
{
|
||||
return response()->json($this->payments->publicOptions());
|
||||
}
|
||||
|
||||
public function initiate(Request $request)
|
||||
{
|
||||
$gateway = $this->payments->resolve();
|
||||
if (!$gateway || !$gateway->isAvailable()) {
|
||||
throw new UnprocessableEntityHttpException('پرداخت آنلاین در حال حاضر فعال نیست');
|
||||
}
|
||||
|
||||
$amount = (int) $request->input('value', $request->input('amount', 0));
|
||||
$minDeposit = $this->config->getInt('min_deposit', 0);
|
||||
if ($amount < 1000) {
|
||||
throw new UnprocessableEntityHttpException('حداقل مبلغ پرداخت ۱۰۰۰ ریال است');
|
||||
}
|
||||
if ($minDeposit > 0 && $amount < $minDeposit) {
|
||||
throw new UnprocessableEntityHttpException(
|
||||
'حداقل مقدار واریز ' . number_format($minDeposit) . ' ریال است'
|
||||
);
|
||||
}
|
||||
|
||||
$customer = auth('customer')->user();
|
||||
$withdraw = Withdraw::create([
|
||||
'customer_id' => $customer->id,
|
||||
'cash' => $amount,
|
||||
'note' => 'واریز آنلاین',
|
||||
'sts' => 1,
|
||||
]);
|
||||
|
||||
$referenceId = 'WD-' . $withdraw->id;
|
||||
|
||||
try {
|
||||
$result = $gateway->initiate([
|
||||
'amount' => $amount,
|
||||
'reference_id' => $referenceId,
|
||||
'description' => 'شارژ کیف پول کاربر ' . ($customer->name ?? $customer->id),
|
||||
'metadata' => [
|
||||
'withdraw_id' => $withdraw->id,
|
||||
'customer_id' => $customer->id,
|
||||
],
|
||||
]);
|
||||
} catch (RuntimeException $e) {
|
||||
$withdraw->sts = 3;
|
||||
$withdraw->save();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'مشکل در ارتباط با درگاه',
|
||||
], 422);
|
||||
}
|
||||
|
||||
if (!empty($result['session_id'])) {
|
||||
$withdraw->tracked_number = (string) $result['session_id'];
|
||||
$withdraw->save();
|
||||
}
|
||||
|
||||
// حداقل اطلاعات برای redirect — بدون credential و بدون جزئیات داخلی درگاه
|
||||
return response()->json([
|
||||
'payment_url' => $result['payment_url'],
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function returnFromGateway(Request $request, string $provider)
|
||||
{
|
||||
try {
|
||||
$gateway = $this->payments->resolveOrFail($provider);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return response('درگاه نامعتبر', 404);
|
||||
}
|
||||
|
||||
$result = $gateway->handleReturn($request);
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return response(
|
||||
(string) ($result['message'] ?? 'خطا'),
|
||||
(int) ($result['http_status'] ?? 400)
|
||||
);
|
||||
}
|
||||
|
||||
$Withdraw = $result['withdraw'];
|
||||
$site_url = $this->config->resolvePublicSiteUrl($request) ?: '/';
|
||||
|
||||
return view('showTransactionReport', compact('Withdraw', 'site_url'));
|
||||
}
|
||||
|
||||
public function webhook(Request $request, string $provider)
|
||||
{
|
||||
try {
|
||||
$gateway = $this->payments->resolveOrFail($provider);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return response()->json(['message' => 'invalid_provider'], 404);
|
||||
}
|
||||
|
||||
$result = $gateway->handleWebhook($request);
|
||||
$status = (int) ($result['http_status'] ?? 200);
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return response()->json([
|
||||
'message' => $result['message'] ?? 'error',
|
||||
], $status >= 400 ? $status : 400);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
'ignored' => (bool) ($result['ignored'] ?? false),
|
||||
]);
|
||||
}
|
||||
|
||||
/** سازگاری با URLهای قبلی external-payment/* → montify */
|
||||
public function returnLegacy(Request $request)
|
||||
{
|
||||
return $this->returnFromGateway($request, WalletPaymentManager::PROVIDER_MONTIFY);
|
||||
}
|
||||
|
||||
public function webhookLegacy(Request $request)
|
||||
{
|
||||
return $this->webhook($request, WalletPaymentManager::PROVIDER_MONTIFY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user