checkoutSession = $checkoutSession; $this->cookieMetadataFactory = $cookieMetadataFactory; $this->cookieManager = $cookieManager; $this->sessionManager = $sessionManager; $this->btcPayService = $btcPayService; $this->customerSession = $customerSession; } // public function __construct(RedirectInterface $redirect, ResponseInterface $response, OrderRepository $orderRepository, , ) { // // $this->redirect = $redirect; // $this->response = $response; // $this->orderRepository = $orderRepository; // } private function setCookie($name, $value, $duration) { $path = $this->sessionManager->getCookiePath(); $domain = $this->sessionManager->getCookieDomain(); $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration($duration)->setPath($path)->setDomain($domain); $this->cookieManager->setPublicCookie($name, $value, $metadata); } public function execute() { $order = $this->checkoutSession->getLastRealOrder(); $resultRedirect = $this->resultRedirectFactory->create(); if ($order) { $btcpayInvoice = $this->btcPayService->createInvoice($order); $invoiceId = $btcpayInvoice['id']; if ($invoiceId) { $savedInvoiceInDb = $this->btcPayService->saveInvoiceInDb($btcpayInvoice); $pendingPaymentStatus = OrderStatuses::STATUS_CODE_PENDING_PAYMENT; $order->addCommentToStatusHistory('Pending BTC payment', $pendingPaymentStatus); $order->save(); if (!$this->customerSession->isLoggedIn()) { // Set cookies for the order/returns page $duration = 30 * 24 * 60 * 60; $this->setCookie('oar_order_id', $order->getIncrementId(), $duration); $this->setCookie('oar_billing_lastname', $order->getBillingAddress()->getLastName(), $duration); $this->setCookie('oar_email', $order->getCustomerEmail(), $duration); } $invoiceUrl = $btcpayInvoice['checkoutLink']; $resultRedirect->setUrl($invoiceUrl); } else { throw new \RuntimeException('Could not create the invoice in BTCPay Server'); } } else { $resultRedirect->setUrl($order->getStore()->getUrl('checkout/cart')); } return $resultRedirect; } }