relatedModel = $relatedModel; } /** * Status Values * * @return array */ public static function statusValues(): array { return [ null => '-', self::PENDING => __('Pending'), self::PAID => __('Paid'), self::FAILED => __('Cancelled'), ]; } /** * Generate order number for payment */ public function generateOrderNumber(): string { 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; } /** * Set respond results */ public function setResponseResults(bool $result): void { $this->successful = $result; $this->failed = ! $result; } /** * Send request via provider */ public function sendRequest(): self { $this->response = $this->provider->sendRequest(); $this->failed = $this->response->failed(); $this->successful = $this->response->successful(); 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; } /** * Payment link */ public function paymentLink(): string { return $this->paymentLink; } /** * 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, ]; if ($this->relatedModel) { $data['online_paymantable_id'] = $this->relatedModel->id; // @phpstan-ignore-line $data['online_paymantable_type'] = $this->relatedModel::class; } OnlinePayment::create($data); } }