diff --git a/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php b/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php index bde6cad..3608f87 100644 --- a/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php +++ b/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php @@ -150,44 +150,6 @@ class OnlinePaymentRepository return $this; } - /** - * Check payment - */ - public function checkPayment(string $orderId): array - { - /** @var \App\Modules\OnlinePayment\Models\OnlinePayment Find payment order from history */ - $paymentHistory = OnlinePayment::where('orderId', $orderId)->first(); - - /** @var \Illuminate\Database\Eloquent\Model Find related resource */ - $relatedResource = (new $paymentHistory->online_paymantable_type)->find(id: $paymentHistory->online_paymantable_id); - - // Check if resource could not be found or does not exist - if (! $relatedResource) { - return $this->paymentFailed('(RESOURCE NOT FOUND)'); - } - - $relatedResource->load('branch'); - - /** @var \App\Modules\Branch\Models\Branch */ - $bankBranch = $relatedResource->branch; - - if (! $bankBranch) { - return $this->paymentFailed('(BRANCH NOT FOUND)'); - } - - $this->provider->setUsername($bankBranch->billing_username); - $this->provider->setPassword($bankBranch->billing_password); - $response = $this->provider->checkPayment($orderId); - - if ($response['ErrorCode'] == '99') { - return $this->paymentFailed('(REQUEST FAILURE)'); - } - - return $response['ErrorCode'] == '0' - ? $this->paymentSuccessful($relatedResource, $paymentHistory, $bankBranch) - : $this->paymentFailed(__('Payment has failed'), $paymentHistory, $bankBranch); - } - /** * Payment link */ @@ -221,6 +183,59 @@ class OnlinePaymentRepository OnlinePayment::create($data); } + /** + * Check payment + * + * @return array{success: bool, title: string, pnr: string, branch_name: string, price_amount: string, return_url: string} + */ + public function checkPayment(string $orderId): array + { + /** @var \App\Modules\OnlinePayment\Models\OnlinePayment Find payment order from history */ + $paymentHistory = OnlinePayment::where('orderId', $orderId)->first(); + + // Resolve related resource model dynamically + $modelClass = $paymentHistory->online_paymantable_type; + $modelId = $paymentHistory->online_paymantable_id; + + if (! class_exists($modelClass)) { + return $this->paymentFailed('(INVALID RESOURCE TYPE)'); + } + + /** @var \Illuminate\Database\Eloquent\Model Find related resource */ + $relatedResource = $modelClass::find($modelId); + + // Check if resource could not be found or does not exist + if (! $relatedResource) { + return $this->paymentFailed('(RESOURCE NOT FOUND)'); + } + + $relatedResource->load('branch'); + + /** @var \App\Modules\Branch\Models\Branch */ + $bankBranch = $relatedResource->branch; + + if (! $bankBranch) { + return $this->paymentFailed('(BRANCH NOT FOUND)'); + } + + $this->provider->setUsername($bankBranch->billing_username); + $this->provider->setPassword($bankBranch->billing_password); + $response = $this->provider->checkPayment($orderId); + + if ($response['ErrorCode'] == '99') { + return $this->paymentFailed('(REQUEST FAILURE)'); + } + + return $response['ErrorCode'] == '0' + ? $this->paymentSuccessful($relatedResource, $paymentHistory, $bankBranch) + : $this->paymentFailed(__('Payment has failed'), $paymentHistory, $bankBranch); + } + + /** + * Successful payment + * + * @return array{success: bool, title: string, pnr: string, branch_name: string, price_amount: string, return_url: string} + */ public function paymentSuccessful($relatedResource, $paymentHistory, $bankBranch): array { $relatedResource->update([ @@ -241,6 +256,8 @@ class OnlinePaymentRepository /** * Failed payment + * + * @return array{success: bool, title: string, pnr: string, branch_name: string, price_amount: string, return_url: string} */ public function paymentFailed(string $title = '', $paymentHistory = null, $bankBranch = null): array {