fix dont HMAC request send to montify

This commit is contained in:
mahmood
2026-07-27 00:41:25 +03:30
parent 94a501078b
commit 14089fab55
2 changed files with 54 additions and 8 deletions
@@ -73,8 +73,13 @@ class WalletPaymentController extends Controller
$withdraw->sts = 3;
$withdraw->save();
\Log::warning('wallet.payment.initiate_failed', [
'withdraw_id' => $withdraw->id,
'message' => $e->getMessage(),
]);
return response()->json([
'message' => 'مشکل در ارتباط با درگاه',
'message' => $e->getMessage() ?: 'مشکل در ارتباط با درگاه',
], 422);
}
@@ -84,18 +84,41 @@ class MontifyPartnerGateway implements WalletPaymentGatewayInterface
}
$signer = $this->signer();
$rawBody = $signer->stableStringify($body);
$headers = $signer->signRequest('POST', $path, $body);
$response = Http::withHeaders($headers)
->withBody($signer->stableStringify($body), 'application/json')
->acceptJson()
->post($this->baseUrl() . $path);
try {
// مهم: post($url, []) بدنهٔ withBody را خالی می‌کند — حتماً send بدون data
$response = Http::withHeaders($headers)
->withBody($rawBody, 'application/json')
->acceptJson()
->timeout(30)
->send('POST', $this->baseUrl() . $path);
} catch (\Throwable $e) {
report($e);
throw new RuntimeException('اتصال به سرور درگاه برقرار نشد');
}
$data = $response->json();
if (!$response->successful() || empty($data['payment_url'])) {
$message = is_array($data)
if (!$response->successful() || !is_array($data) || empty($data['payment_url'])) {
$errorCode = is_array($data) ? (string) ($data['error'] ?? '') : '';
$remoteMessage = is_array($data)
? (string) ($data['message'] ?? $data['error'] ?? 'initiate failed')
: 'initiate failed';
throw new RuntimeException($message);
\Log::warning('montify.initiate_failed', [
'status' => $response->status(),
'error' => $errorCode,
'message' => $remoteMessage,
'return_url' => $body['return_url'] ?? null,
'webhook_url' => $body['webhook_url'] ?? null,
'gateway' => $body['gateway'] ?? null,
'reference_id' => $body['reference_id'] ?? null,
'amount' => $body['amount'] ?? null,
'body' => is_array($data) ? $data : $response->body(),
]);
throw new RuntimeException($this->mapInitiateError($errorCode, $remoteMessage));
}
return [
@@ -106,6 +129,24 @@ class MontifyPartnerGateway implements WalletPaymentGatewayInterface
];
}
private function mapInitiateError(string $errorCode, string $fallback): string
{
return match ($errorCode) {
'invalid_signature' => 'امضای درخواست درگاه نامعتبر است (secret یا ساعت سرور را بررسی کنید)',
'expired_timestamp' => 'ساعت سرور با درگاه همخوان نیست',
'replay_detected' => 'درخواست تکراری شناسایی شد؛ دوباره تلاش کنید',
'invalid_return_url' => 'آدرس برگشت (return_url) در whitelist پارتنر Montify ثبت نشده است',
'invalid_gateway' => 'نام درگاه داخل Montify نامعتبر است',
'amount_too_low' => 'مبلغ کمتر از حد مجاز درگاه است',
'duplicate_reference' => 'شناسه پرداخت تکراری است؛ دوباره تلاش کنید',
'gateway_not_configured' => 'درگاه انتخاب‌شده در Montify پیکربندی نشده است',
'missing_auth_headers' => 'هدرهای احراز هویت به درگاه ارسال نشد',
default => $fallback !== '' && $fallback !== 'initiate failed'
? $fallback
: 'مشکل در ارتباط با درگاه',
};
}
public function handleReturn(Request $request): array
{
$query = $request->query();