diff --git a/app/Filament/Clusters/Cards/CardOrders/Pages/CreateCardOrder.php b/app/Filament/Clusters/Cards/CardOrders/Pages/CreateCardOrder.php index 5b017ee..5b64f3f 100644 --- a/app/Filament/Clusters/Cards/CardOrders/Pages/CreateCardOrder.php +++ b/app/Filament/Clusters/Cards/CardOrders/Pages/CreateCardOrder.php @@ -3,8 +3,8 @@ namespace App\Filament\Clusters\Cards\CardOrders\Pages; use App\Filament\Clusters\Cards\CardOrders\CardOrderResource; +use App\Modules\CardOrder\Repositories\CardOrderRepository; use Filament\Resources\Pages\CreateRecord; -use Illuminate\Support\Facades\URL; class CreateCardOrder extends CreateRecord { @@ -14,12 +14,14 @@ class CreateCardOrder extends CreateRecord { $defaultUrl = $this->getResource()::getUrl('index'); - return $defaultUrl; + /** @var \App\Modules\CardOrder\Models\CardOrder */ + $record = $this->record; - // $payment = (new OnlinePaymentRepo)->payCardOrder($resource); + $OnlinePayment = CardOrderRepository::make() + ->createOnlinePaymentOrder($record); - // $payment['status'] === 'success' - // ? URL::remote($payment['url']) - // : $defaultUrl; + return $OnlinePayment->successful() + ? $OnlinePayment->paymentLink() + : $defaultUrl; } } diff --git a/app/Modules/AppHelpers/Contracts/HasFailedMethod.php b/app/Modules/AppHelpers/Contracts/HasFailedMethod.php new file mode 100644 index 0000000..d4176b2 --- /dev/null +++ b/app/Modules/AppHelpers/Contracts/HasFailedMethod.php @@ -0,0 +1,10 @@ +belongsToMany(User::class); } + + /** + * Get billing username + */ + public function billingUsername(): string + { + return $this->billing_username ?: '-'; + } + + /** + * Get billing password + */ + public function billingPassword(): string + { + return $this->billing_password ?: '-'; + } } diff --git a/app/Modules/CardOrder/Models/CardOrder.php b/app/Modules/CardOrder/Models/CardOrder.php index 1830d49..276acf5 100644 --- a/app/Modules/CardOrder/Models/CardOrder.php +++ b/app/Modules/CardOrder/Models/CardOrder.php @@ -111,4 +111,12 @@ class CardOrder extends Model implements BelongsToBranch, HasStatus static::creating(LoanOrderRepository::creating()); static::created(LoanOrderRepository::created()); } + + /** + * Price for card + */ + public function priceAmount(): int|float|string + { + return $this->cardState->price ?? 32; + } } diff --git a/app/Modules/CardOrder/Repositories/CardOrderRepository.php b/app/Modules/CardOrder/Repositories/CardOrderRepository.php index 06bc0cd..0098aa7 100644 --- a/app/Modules/CardOrder/Repositories/CardOrderRepository.php +++ b/app/Modules/CardOrder/Repositories/CardOrderRepository.php @@ -2,4 +2,32 @@ namespace App\Modules\CardOrder\Repositories; -class CardOrderRepository {} +use App\Modules\CardOrder\Models\CardOrder; +use App\Modules\HalkbankOnlinePayment\Repositories\HalkbankOnlinePaymentRepository; +use App\Modules\Makeable; +use App\Modules\OnlinePayment\Repositories\OnlinePaymentRepository; + +class CardOrderRepository +{ + use Makeable; + + /** + * Create online payment order + */ + public function createOnlinePaymentOrder(CardOrder $record): OnlinePaymentRepository + { + /** @var \App\Modules\Branch\Models\Branch */ + $branch = $record->branch; + + return OnlinePaymentRepository::make() + ->paymentProvider( + HalkbankOnlinePaymentRepository::make() + ->setUsername($branch->billingUsername()) + ->setPassword($branch->billingPassword()) + ->setAmount($record->priceAmount()) + ->setReturnUrl(route('halkbank-online-payment.store')) + ->setDescription('Kart tölegi') + ) + ->sendRequest(); + } +} diff --git a/app/Modules/HalkbankOnlinePayment/Controllers/HalkbankOnlinePaymentController.php b/app/Modules/HalkbankOnlinePayment/Controllers/HalkbankOnlinePaymentController.php new file mode 100644 index 0000000..2ecb599 --- /dev/null +++ b/app/Modules/HalkbankOnlinePayment/Controllers/HalkbankOnlinePaymentController.php @@ -0,0 +1,19 @@ +validate([ + 'orderId' => ['required', 'string', 'max:50', 'exists:online_payment_histories,orderId'], + ]); + + return view('welcome'); + } +} diff --git a/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php b/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php index a7e1aa6..a62ede0 100644 --- a/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php +++ b/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php @@ -3,26 +3,25 @@ namespace App\Modules\HalkbankOnlinePayment\Repositories; use App\Modules\Makeable; -use Closure; +use App\Modules\OnlinePayment\Contracts\PaymentProviderContract; use Exception; +use GuzzleHttp\Psr7\Response as GuzzleResponse; +use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -class HalkbankOnlinePaymentRepository +class HalkbankOnlinePaymentRepository implements PaymentProviderContract { use Makeable; public function __construct( protected string $username = '', protected string $password = '', - protected string $amount = '', + protected int|float|string $amount = '', protected int|string $orderNumber = '', protected string $returnUrl = '', protected string $description = '', - protected ?Closure $onRequestError = null, - protected ?Closure $onResponseFail = null, - protected ?Closure $onResponseSuccess = null, ) { $this->username = config()->string('module.halkbank-online-payment.username'); $this->password = config()->string('module.halkbank-online-payment.password'); @@ -33,7 +32,7 @@ class HalkbankOnlinePaymentRepository /** * Send request to gatewat */ - public function sendRequest() + public function sendRequest(): Response { try { $paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [ @@ -62,7 +61,11 @@ class HalkbankOnlinePaymentRepository ], ]); - return emptyClass([]); + return new Response(new GuzzleResponse( + 503, + ['Content-Type' => 'application/json'], + sprintf('{"error":"Payment provider connection failed","exception_message":"%s"}', $e->getMessage()) + )); } return $paymentResponse; @@ -95,7 +98,7 @@ class HalkbankOnlinePaymentRepository /** * Get amount */ - public function amount(): string + public function amount(): int|float|string { return $this->amount; } @@ -147,7 +150,7 @@ class HalkbankOnlinePaymentRepository /** * Set amoutn */ - public function setAmount(string $amount): self + public function setAmount(int|float|string $amount): self { $this->amount = $amount; @@ -184,36 +187,6 @@ class HalkbankOnlinePaymentRepository return $this; } - /** - * Set callback on request error - */ - public function setOnRequestError(Closure $onRequestError): self - { - $this->onRequestError = $onRequestError; - - return $this; - } - - /** - * Set callback on Response Fail - */ - public function setOnResponseFail(Closure $onResponseFail): self - { - $this->onResponseFail = $onResponseFail; - - return $this; - } - - /** - * Set callback on Response Success - */ - public function setOnResponseSuccess(Closure $onResponseSuccess): self - { - $this->onResponseSuccess = $onResponseSuccess; - - return $this; - } - /** * Generate order number for payment */ diff --git a/app/Modules/HalkbankOnlinePayment/Routes/halkbank-online-payment-routes.php b/app/Modules/HalkbankOnlinePayment/Routes/halkbank-online-payment-routes.php new file mode 100644 index 0000000..9e0e3d7 --- /dev/null +++ b/app/Modules/HalkbankOnlinePayment/Routes/halkbank-online-payment-routes.php @@ -0,0 +1,7 @@ +name('halkbank-online-payment.store'); diff --git a/app/Modules/OnlinePayment/Contracts/PaymentProviderContract.php b/app/Modules/OnlinePayment/Contracts/PaymentProviderContract.php new file mode 100644 index 0000000..6322d2e --- /dev/null +++ b/app/Modules/OnlinePayment/Contracts/PaymentProviderContract.php @@ -0,0 +1,10 @@ + $api_response * @property \Illuminate\Support\Carbon $created_at * @property \Illuminate\Support\Carbon $updated_at */ diff --git a/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php b/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php index 520b01b..8a77f7d 100644 --- a/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php +++ b/app/Modules/OnlinePayment/Repositories/OnlinePaymentRepository.php @@ -2,14 +2,17 @@ namespace App\Modules\OnlinePayment\Repositories; +use App\Modules\AppHelpers\Contracts\HasOnlinePaymentStatusFields; use App\Modules\CardOrder\Models\CardOrder; -use App\Modules\HalkbankOnlinePayment\Repositories\HalkbankOnlinePaymentRepository; +use App\Modules\Makeable; +use App\Modules\OnlinePayment\Contracts\PaymentProviderContract; use App\Modules\OnlinePayment\Models\OnlinePayment; -use Illuminate\Support\Facades\Http; -use Illuminate\Support\Facades\Log; +use Illuminate\Http\Client\Response; -class OnlinePaymentRepository +class OnlinePaymentRepository implements HasOnlinePaymentStatusFields { + use Makeable; + /** * Pending online payments */ @@ -25,6 +28,31 @@ class OnlinePaymentRepository */ public const PAID = 'paid'; + /** + * Payment provider + */ + protected PaymentProviderContract $provider; + + /** + * Response + */ + protected Response $response; + + /** + * If payment is successful + */ + protected bool $successful; + + /** + * If payment has failed + */ + protected bool $failed; + + /** + * Online payment link + */ + protected string $paymentLink; + /** * Status Values * @@ -40,75 +68,6 @@ class OnlinePaymentRepository ]; } - /** - * Pay card order - * - * @param mixed $resource - * @return array - */ - public function payCardOrder() - { - $orderNumber = $this->generateOrderNumber(); - - $response = HalkbankOnlinePaymentRepository::make() - ->setUsername() - ->setPassword() - ->setAmount() - ->setOrderNumber() - ->returnUrl() - ->description() - ->onError() - ->onResponseFail() - ->onResponseSuccess() - ->sendRequest(); - - // $paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [ - // 'orderNumber' => $orderNumber, - // 'amount' => $this->getPrice($resource->priceAmount()), - // 'currency' => 934, - // 'language' => 'ru', - // 'userName' => $resource->branch->billing_username, - // 'password' => $resource->branch->billing_password, - // 'returnUrl' => route('online-payment-store'), - // 'pageView' => 'DESKTOP', - // 'description' => 'Kart tölegi', - // ])->onError(function ($response) { - // Log::channel('halkbank_payment_error') - // ->error('Payment error', [ - // 'response' => [ - // 'body' => $response->body(), - // ], - // ]); - // }); - - // if ($paymentResponse->failed()) { - // return [ - // 'status' => 'failed', - // 'url' => '', - // ]; - // } - - // $onlinePaymentHistory = new OnlinePayment; - // $onlinePaymentHistory->online_paymantable_id = $resource->really(); - // $onlinePaymentHistory->online_paymantable_type = CardOrder::class; - // $onlinePaymentHistory->amount = $resource->priceAmount(); - // $onlinePaymentHistory->orderNumber = $orderNumber; - // $onlinePaymentHistory->description = 'Kart tölegi'; - // $onlinePaymentHistory->orderId = $paymentResponse['orderId']; - // $onlinePaymentHistory->formUrl = $paymentResponse['formUrl']; - // $onlinePaymentHistory->successUrl = route('online-payment-store'); - // $onlinePaymentHistory->errorUrl = route('online-payment-store'); - // $onlinePaymentHistory->api_client = 'billing_username'; - // $onlinePaymentHistory->username = $resource->branch->billing_username; - // $onlinePaymentHistory->paymentStatus = self::PENDING; - // $onlinePaymentHistory->save(); - - // return [ - // 'status' => 'success', - // 'url' => $paymentResponse['formUrl'], - // ]; - } - /** * Generate order number for payment */ @@ -116,4 +75,83 @@ class OnlinePaymentRepository { return date('dmyHis'); } + + /** + * Payment provder + */ + public function paymentProvider(PaymentProviderContract $provider): self + { + $this->provider = $provider; + + return $this; + } + + /** + * If payment has been successfull + */ + public function successful(): bool + { + return $this->successful; + } + + /** + * If payment has been a failure + */ + public function failed(): bool + { + return $this->failed; + } + + /** + * Send request via provider + */ + public function sendRequest(): self + { + $response = $this->provider->sendRequest(); + + $this->failed = $response->failed(); + $this->successful = $response->successful(); + + $this->response = $response; + + return $this; + } + + /** + * Payment link + */ + public function paymentLink(): string + { + return $this->paymentLink; + } + + // public function payCardOrder() + // { + // if ($paymentResponse->failed()) { + // return [ + // 'status' => 'failed', + // 'url' => '', + // ]; + // } + + // $onlinePaymentHistory = new OnlinePayment; + // $onlinePaymentHistory->online_paymantable_id = $resource->really(); + // $onlinePaymentHistory->online_paymantable_type = CardOrder::class; + // $onlinePaymentHistory->amount = $resource->priceAmount(); + // $onlinePaymentHistory->orderNumber = $orderNumber; + // $onlinePaymentHistory->description = 'Kart tölegi'; + // $onlinePaymentHistory->orderId = $paymentResponse['orderId']; + // $onlinePaymentHistory->formUrl = $paymentResponse['formUrl']; + // $onlinePaymentHistory->successUrl = route('online-payment-store'); + // $onlinePaymentHistory->errorUrl = route('online-payment-store'); + // $onlinePaymentHistory->api_client = 'billing_username'; + // $onlinePaymentHistory->username = $resource->branch->billing_username; + // $onlinePaymentHistory->paymentStatus = self::PENDING; + // $onlinePaymentHistory->save(); + + // return [ + // 'status' => 'success', + // 'url' => $paymentResponse['formUrl'], + // ]; + // } }