Files
nabeh/backend/app/Core/Flows/PaymentFlow.php
2026-06-18 15:04:52 +03:00

113 lines
4.6 KiB
PHP

<?php
namespace App\Core\Flows;
use App\Services\SiroService;
/**
* PaymentFlow — Smart Payment Verification
*
* Flow: start → await_receipt → finished
*
* Automatically:
* • Detects country from phone prefix (963→Syria, 962→Jordan)
* • Sets payment method (Syria→shamcash, Jordan→cliq)
* • Forwards raw receipt image to payment server for AI verification
* • Payment server auto-finds the latest pending invoice by phone
* → no need for the user to type an invoice number
*/
class PaymentFlow extends BaseFlow
{
public function handleStep(string $step, array $messageData, array &$context): FlowResult
{
$phone = $messageData['phone'] ?? '';
$text = $messageData['body'] ?? $messageData['text'] ?? '';
$image = $messageData['image'] ?? '';
$imageMimeType = $messageData['imageMimeType'] ?? 'image/jpeg';
switch ($step) {
// ─────────────────────────────────────────────────
// START: detect country, set method, ask for receipt
// ─────────────────────────────────────────────────
case 'start':
$country = SiroService::detectCountry($phone);
$paymentMethod = match ($country) {
'jordan' => 'cliq',
default => 'shamcash',
};
$context['payment_method'] = $paymentMethod;
$context['country'] = $country;
$context['user_type'] = 'driver';
$methodName = match ($paymentMethod) {
'cliq' => 'كليك (Cliq)',
default => 'شام كاش (ShamCash)',
};
return new FlowResult(
"أهلاً بك. للتحقق من عملية الدفع:\n"
. "💰 طريقة الدفع المتوقعة: {$methodName}\n"
. "📍 الدولة: " . ($country === 'syria' ? 'سوريا' : ($country === 'jordan' ? 'الأردن' : $country))
. "\n\n📸 يرجى إرسال صورة الإيصال أو وصل التحويل للتحقق منه.",
"await_receipt"
);
// ─────────────────────────────────────────────────
// AWAIT_RECEIPT: collect receipt image
// ─────────────────────────────────────────────────
case 'await_receipt':
if (empty($image)) {
return new FlowResult(
"الرجاء إرسال صورة واضحة لوصل الدفع أو صورة الشاشة:",
"await_receipt"
);
}
return $this->sendToVerification($phone, $context, $image, $imageMimeType);
default:
return new FlowResult("حدث خطأ في المسار.", "finished", true);
}
}
/**
* Forward receipt image + phone to payment server for AI verification.
* Payment server internally resolves phone→driverID via Siro backend.
* Nabeh only makes ONE API call (to payment server).
*/
private function sendToVerification(
string $phone,
array &$context,
string $image,
string $imageMimeType
): FlowResult {
$companyId = $context['company_id'] ?? 1;
if (strpos($imageMimeType, ';') !== false) {
$imageMimeType = trim(explode(';', $imageMimeType)[0]);
}
$result = \App\Controllers\WhatsAppController::verifyPaymentSlipStatic(
companyId: $companyId,
phone: $phone,
jsonStr: '',
userType: $context['user_type'] ?? 'driver',
paymentMethod: $context['payment_method'] ?? 'shamcash',
invoiceNumber: '',
receiptImage: $image,
imageMimeType: $imageMimeType,
);
if ($result) {
return new FlowResult($result, "finished", true);
}
return new FlowResult(
"لم نتمكن من التحقق من الدفع حالياً. يرجى المحاولة مرة أخرى أو التواصل مع الدعم الفني.",
"finished",
true
);
}
}