diff --git a/Components/Services/PaymentStatusService.php b/Components/Services/PaymentStatusService.php index 50f4cffc..9699b961 100644 --- a/Components/Services/PaymentStatusService.php +++ b/Components/Services/PaymentStatusService.php @@ -11,6 +11,7 @@ use Shopware\Components\Model\ModelManager; use Shopware\Models\Order\Order; use Shopware\Models\Order\Status; +use Shopware\Models\Payment\Payment; use SwagPaymentPayPalUnified\Components\Exception\OrderNotFoundException; use SwagPaymentPayPalUnified\Components\PaymentStatus; @@ -51,4 +52,37 @@ public function updatePaymentStatus($parentPayment, $paymentStateId) $this->modelManager->flush($orderModel); } + + + /** + * @param string $transactionId + * @param int $paymentStateId + */ + public function updatePaymentStatusOldOrders($transactionId, $paymentStateId) + { + /** @var Payment $payment */ + $payment = $this->modelManager->getRepository(Payment::class)->findOneBy(['name' => 'paypal']); + /** @var Order|null $orderModel */ + $orderModel = $this->modelManager->getRepository(Order::class)->findOneBy([ + 'transactionId' => $transactionId, + 'paymentId' => $payment->getId(), + ]); + + if (!($orderModel instanceof Order)) { + return false; + } + + /** @var Status|null $orderStatusModel */ + $orderStatusModel = $this->modelManager->getRepository(Status::class)->find($paymentStateId); + + $orderModel->setPaymentStatus($orderStatusModel); + if ($paymentStateId === PaymentStatus::PAYMENT_STATUS_PAID + || $paymentStateId === PaymentStatus::PAYMENT_STATUS_PARTIALLY_PAID + ) { + $orderModel->setClearedDate(new \DateTime()); + } + + $this->modelManager->flush($orderModel); + return true; + } } diff --git a/WebhookHandlers/SaleRefunded.php b/WebhookHandlers/SaleRefunded.php index b2ed14b2..1a8fb404 100644 --- a/WebhookHandlers/SaleRefunded.php +++ b/WebhookHandlers/SaleRefunded.php @@ -56,6 +56,9 @@ public function invoke(Webhook $webhook) return true; } catch (OrderNotFoundException $e) { + if($this->invokeOld($webhook)) + return true; + $this->logger->error( '[SaleRefunded-Webhook] Could not find associated order with the temporaryID ' . $parentPayment, ['webhook' => $webhook->toArray()] @@ -71,4 +74,23 @@ public function invoke(Webhook $webhook) return false; } } + + public function invokeOld(Webhook $webhook) + { + $transactionId = $webhook->getResource()['sale_id']; + try { + // check if it is an old order + return $this->paymentStatusService->updatePaymentStatusOldOrders( + $transactionId, + PaymentStatus::PAYMENT_STATUS_REFUNDED + ); + }catch (\Exception $ex) { + $this->logger->error( + '[SaleRefunded-Webhook] Could not update entity', + ['message' => $ex->getMessage(), 'stacktrace' => $ex->getTrace()] + ); + + return false; + } + } }