diff --git a/app/Modules/CardOrder/CardOrderModule.php b/app/Modules/CardOrder/CardOrderModule.php index 0723c5d..17b204b 100644 --- a/app/Modules/CardOrder/CardOrderModule.php +++ b/app/Modules/CardOrder/CardOrderModule.php @@ -59,6 +59,16 @@ class CardOrderModule implements ModuleContract name: 'Branch', message: 'This module is used for branches.', ), + new ModulePackage( + type: ModulePackageType::MODULE, + name: 'OrderStatus', + message: 'Supports orders.', + ), + new ModulePackage( + type: ModulePackageType::MODULE, + name: 'OnlinePayment', + message: 'Is online payable.', + ), ]; } diff --git a/app/Modules/CardOrder/Models/CardOrder.php b/app/Modules/CardOrder/Models/CardOrder.php index 276acf5..18ad64c 100644 --- a/app/Modules/CardOrder/Models/CardOrder.php +++ b/app/Modules/CardOrder/Models/CardOrder.php @@ -6,6 +6,7 @@ use App\Models\User; use App\Modules\Branch\Interfaces\BelongsToBranch; use App\Modules\Branch\Models\Branch; use App\Modules\LoanOrder\Repositories\LoanOrderRepository; +use App\Modules\OnlinePayment\Contracts\HasOnlinePayments; use App\Modules\OrderStatus\Interfaces\HasStatus; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; diff --git a/app/Modules/CardOrder/Repositories/CardOrderRepository.php b/app/Modules/CardOrder/Repositories/CardOrderRepository.php index 0098aa7..561550f 100644 --- a/app/Modules/CardOrder/Repositories/CardOrderRepository.php +++ b/app/Modules/CardOrder/Repositories/CardOrderRepository.php @@ -19,7 +19,7 @@ class CardOrderRepository /** @var \App\Modules\Branch\Models\Branch */ $branch = $record->branch; - return OnlinePaymentRepository::make() + return OnlinePaymentRepository::make(relatedModel: $record) ->paymentProvider( HalkbankOnlinePaymentRepository::make() ->setUsername($branch->billingUsername()) diff --git a/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php b/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php index a62ede0..f52c9a3 100644 --- a/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php +++ b/app/Modules/HalkbankOnlinePayment/Repositories/HalkbankOnlinePaymentRepository.php @@ -64,7 +64,7 @@ class HalkbankOnlinePaymentRepository implements PaymentProviderContract return new Response(new GuzzleResponse( 503, ['Content-Type' => 'application/json'], - sprintf('{"error":"Payment provider connection failed","exception_message":"%s"}', $e->getMessage()) + sprintf('{"errorCode":"1","errorMessage":"%s"}', $e->getMessage()) )); } diff --git a/app/Modules/OnlinePayment/Contracts/HasOnlinePayments.php b/app/Modules/OnlinePayment/Contracts/HasOnlinePayments.php new file mode 100644 index 0000000..dba41d7 --- /dev/null +++ b/app/Modules/OnlinePayment/Contracts/HasOnlinePayments.php @@ -0,0 +1,8 @@ +relatedModel = $relatedModel; + } + /** * Status Values * @@ -102,17 +118,35 @@ class OnlinePaymentRepository implements HasOnlinePaymentStatusFields return $this->failed; } + /** + * Set respond results + */ + public function setResponseResults(bool $result): void + { + $this->successful = $result; + $this->failed = ! $result; + } + /** * Send request via provider */ public function sendRequest(): self { - $response = $this->provider->sendRequest(); + $this->response = $this->provider->sendRequest(); - $this->failed = $response->failed(); - $this->successful = $response->successful(); + $this->failed = $this->response->failed(); + $this->successful = $this->response->successful(); - $this->response = $response; + if ($this->response['errorCode'] != 0) { + $this->setResponseResults(false); + } + + if ($this->successful) { + $this->orderId = string($this->response['orderId']); + $this->paymentLink = string($this->response['formUrl']); + + $this->createHistory(); + } return $this; } @@ -125,33 +159,28 @@ class OnlinePaymentRepository implements HasOnlinePaymentStatusFields return $this->paymentLink; } - // public function payCardOrder() - // { - // if ($paymentResponse->failed()) { - // return [ - // 'status' => 'failed', - // 'url' => '', - // ]; - // } + /** + * Create online payment history + */ + public function createHistory(): void + { + $data = [ + 'amount' => $this->provider->amount(), + 'orderNumber' => $this->provider->orderNumber(), + 'description' => $this->provider->description(), + 'orderId' => $this->orderId, + 'formUrl' => $this->paymentLink, + 'successUrl' => $this->provider->returnUrl(), + 'errorUrl' => $this->provider->returnUrl(), + 'username' => $this->provider->username(), + 'paymentStatus' => self::PENDING, + ]; - // $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(); + if ($this->relatedModel) { + $data['online_paymantable_id'] = $this->relatedModel->id; // @phpstan-ignore-line + $data['online_paymantable_type'] = $this->relatedModel::class; + } - // return [ - // 'status' => 'success', - // 'url' => $paymentResponse['formUrl'], - // ]; - // } + OnlinePayment::create($data); + } } diff --git a/app/Modules/module-helpers.php b/app/Modules/module-helpers.php index 84f5f43..0b263e0 100644 --- a/app/Modules/module-helpers.php +++ b/app/Modules/module-helpers.php @@ -54,6 +54,18 @@ function module_exists(string $moduleName): bool return modular()->moduleExists($moduleName); } +/** + * Cast to string + */ +function string(mixed $value): string +{ + if (! is_string($value)) { + throw new Exception('!!!Make it string!!!'); + } + + return $value; +} + /** * Create an anonymous dynamic object with both properties and callable methods. *