diff --git a/Api/Data/ValidationInterface.php b/Api/Data/ValidationInterface.php index a7cd256f..9603504f 100644 --- a/Api/Data/ValidationInterface.php +++ b/Api/Data/ValidationInterface.php @@ -23,12 +23,14 @@ interface ValidationInterface * @param string $lastTransactionId * @param string|null $rvvupId * @param string|null $paymentStatus + * @param string|null $origin * @return ValidationInterface */ public function validate( Quote &$quote, string &$lastTransactionId, string $rvvupId = null, - string $paymentStatus = null + string $paymentStatus = null, + string $origin = null ): ValidationInterface; } diff --git a/Controller/Redirect/In.php b/Controller/Redirect/In.php index 0dd6419c..19f07c98 100644 --- a/Controller/Redirect/In.php +++ b/Controller/Redirect/In.php @@ -80,6 +80,7 @@ public function execute() $rvvupId = $this->request->getParam('rvvup-order-id'); $paymentStatus = $this->request->getParam('payment-status'); $quote = $this->captureService->getQuoteByRvvupId($rvvupId); + $origin = 'customer-flow'; if (!$quote) { $quote = $this->checkoutSession->getQuote(); @@ -89,7 +90,13 @@ public function execute() $rvvupPaymentId = $payment->getAdditionalInformation(Method::PAYMENT_ID); $lastTransactionId = (string)$payment->getAdditionalInformation(Method::TRANSACTION_ID); - $validate = $this->captureService->validate($quote, $lastTransactionId, $rvvupId, $paymentStatus); + $validate = $this->captureService->validate( + $quote, + $lastTransactionId, + $rvvupId, + $paymentStatus, + $origin + ); if (!$validate->getIsValid()) { if ($validate->getRestoreQuote()) { @@ -113,14 +120,19 @@ public function execute() $this->checkoutSession->setLastQuoteId($quote->getId()); $this->checkoutSession->setLastOrderId($quote->getReservedOrderId()); $this->checkoutSession->setLastRealOrderId($quote->getReservedOrderId()); - return $this->resultService->processOrderResult(null, $rvvupId); + return $this->resultService->processOrderResult(null, $rvvupId, $origin); } - return $this->resultService->processOrderResult((string)$quote->getReservedOrderId(), $rvvupId, true); + return $this->resultService->processOrderResult( + (string)$quote->getReservedOrderId(), + $rvvupId, + $origin, + true + ); } } $this->captureService->setCheckoutMethod($quote); - $validation = $this->captureService->createOrder($rvvupId, $quote); + $validation = $this->captureService->createOrder($rvvupId, $quote, $origin); $orderId = $validation->getOrderId(); $alreadyExists = $validation->getAlreadyExists(); @@ -129,7 +141,12 @@ public function execute() $this->checkoutSession->setLastQuoteId($quote->getId()); $this->checkoutSession->setLastOrderId($quote->getReservedOrderId()); $this->checkoutSession->setLastRealOrderId($quote->getReservedOrderId()); - return $this->resultService->processOrderResult((string)$orderId, $rvvupId, true); + return $this->resultService->processOrderResult( + (string)$orderId, + $rvvupId, + $origin, + true + ); } if (!$orderId) { @@ -143,7 +160,13 @@ public function execute() return $this->redirectToCart(); } - if (!$this->captureService->paymentCapture($payment, $lastTransactionId, $rvvupPaymentId, $rvvupId)) { + if (!$this->captureService->paymentCapture( + $payment, + $lastTransactionId, + $rvvupPaymentId, + $rvvupId, + $origin + )) { $this->messageManager->addErrorMessage( __( 'An error occurred while capturing your order (ID %1). Please contact us.', @@ -153,7 +176,7 @@ public function execute() return $this->redirectToCart(); } - return $this->resultService->processOrderResult((string)$orderId, $rvvupId); + return $this->resultService->processOrderResult((string)$orderId, $rvvupId, $origin); } /** diff --git a/Controller/Webhook/Index.php b/Controller/Webhook/Index.php index 60fb52fe..4bd62f01 100644 --- a/Controller/Webhook/Index.php +++ b/Controller/Webhook/Index.php @@ -142,7 +142,8 @@ public function execute(): ResultInterface 'payment_id' => $paymentId, 'event_type' => $eventType, 'store_id' => $this->getStoreId(), - 'payment_link_id' => $paymentLinkId + 'payment_link_id' => $paymentLinkId, + 'origin' => 'webhook' ]; if ($payload['event_type'] == Complete::TYPE) { diff --git a/Model/Data/Validation.php b/Model/Data/Validation.php index 83e045b9..037ee069 100644 --- a/Model/Data/Validation.php +++ b/Model/Data/Validation.php @@ -42,11 +42,20 @@ public function __construct( parent::__construct($data); } + /** + * @param Quote $quote + * @param string $lastTransactionId + * @param string|null $rvvupId + * @param string|null $paymentStatus + * @param string|null $origin + * @return ValidationInterface + */ public function validate( Quote &$quote, string &$lastTransactionId, string $rvvupId = null, - string $paymentStatus = null + string $paymentStatus = null, + string $origin = null ): ValidationInterface { $data = $this->getDefaultData(); @@ -57,7 +66,8 @@ public function validate( 'No Rvvup Order ID provided', null, null, - $lastTransactionId + $lastTransactionId, + $origin ); $data[ValidationInterface::IS_VALID] = false; @@ -88,7 +98,8 @@ public function validate( $message, $rvvupId, $lastTransactionId, - $quote->getReservedOrderId() ?? null + $quote->getReservedOrderId() ?? null, + $origin ); $this->setValidationData($data); @@ -104,7 +115,8 @@ public function validate( 'Missing quote for Rvvup payment', $message, $rvvupId, - $lastTransactionId + $lastTransactionId, + $origin ); $data[ValidationInterface::IS_VALID] = false; @@ -124,7 +136,8 @@ public function validate( null, $rvvupId, $lastTransactionId, - $quote->getReservedOrderId() ?? null + $quote->getReservedOrderId() ?? null, + $origin ); $message = __( @@ -144,7 +157,8 @@ public function validate( null, $rvvupId, $lastTransactionId, - $quote->getReservedOrderId() ?? null + $quote->getReservedOrderId() ?? null, + $origin ); $message = __( 'This checkout cannot complete, a new cart was opened in another tab. ' . $errorId diff --git a/Model/Logger.php b/Model/Logger.php index 6de710e0..8cde64d1 100644 --- a/Model/Logger.php +++ b/Model/Logger.php @@ -48,6 +48,7 @@ public function __construct( * @param string|null $rvvupOrderId * @param string|null $rvvupPaymentId * @param string|null $magentoOrderId + * @param string|null $origin * @return bool */ public function addRvvupError( @@ -55,16 +56,24 @@ public function addRvvupError( ?string $cause = null, ?string $rvvupOrderId = null, ?string $rvvupPaymentId = null, - ?string $magentoOrderId = null + ?string $magentoOrderId = null, + ?string $origin = null ) { $result = $this->addRecord( static::ERROR, $message, - [$cause, $rvvupOrderId, $rvvupPaymentId, $magentoOrderId] + [$cause, $rvvupOrderId, $rvvupPaymentId, $magentoOrderId, $origin] ); try { - $data = $this->prepareData($message, $cause, $rvvupOrderId, $rvvupPaymentId, $magentoOrderId); + $data = $this->prepareData( + $message, + $cause, + $rvvupOrderId, + $rvvupPaymentId, + $magentoOrderId, + $origin + ); /** @var LogModel $model */ $model = $this->modelFactory->create(); @@ -84,6 +93,7 @@ public function addRvvupError( * @param string|null $rvvupOrderId * @param string|null $rvvupPaymentId * @param string|null $magentoOrderId + * @param string|null $origin * @return array * @throws NoSuchEntityException */ @@ -92,7 +102,8 @@ private function prepareData( ?string $cause = null, ?string $rvvupOrderId = null, ?string $rvvupPaymentId = null, - ?string $magentoOrderId = null + ?string $magentoOrderId = null, + ?string $origin = null ): array { $payload = json_encode([ @@ -103,7 +114,8 @@ private function prepareData( 'rvvupOrderId' => $rvvupOrderId, 'magento' => [ 'storeId' => $this->storeManager->getStore()->getId(), - 'orderId' => $magentoOrderId + 'orderId' => $magentoOrderId, + 'origin' => $origin ] ], 'cause' => $cause diff --git a/Model/ProcessOrder/Cancel.php b/Model/ProcessOrder/Cancel.php index 63e5c2f3..2901db31 100644 --- a/Model/ProcessOrder/Cancel.php +++ b/Model/ProcessOrder/Cancel.php @@ -77,12 +77,13 @@ public function __construct( } /** - * @param \Magento\Sales\Api\Data\OrderInterface $order + * @param OrderInterface $order * @param array $rvvupData + * @param string $origin * @return \Rvvup\Payments\Api\Data\ProcessOrderResultInterface * @throws \Rvvup\Payments\Exception\PaymentValidationException */ - public function execute(OrderInterface $order, array $rvvupData): ProcessOrderResultInterface + public function execute(OrderInterface $order, array $rvvupData, string $origin): ProcessOrderResultInterface { $this->validateIdExists($rvvupData); $this->validateStatusAllowed($rvvupData['payments'][0], $this->allowedStatuses); diff --git a/Model/ProcessOrder/Comment.php b/Model/ProcessOrder/Comment.php index 673448dd..8ab46238 100644 --- a/Model/ProcessOrder/Comment.php +++ b/Model/ProcessOrder/Comment.php @@ -43,10 +43,11 @@ public function __construct( /** * @param OrderInterface $order * @param array $rvvupData + * @param string $origin * @return ProcessOrderResultInterface * @throws PaymentValidationException */ - public function execute(OrderInterface $order, array $rvvupData): ProcessOrderResultInterface + public function execute(OrderInterface $order, array $rvvupData, string $origin): ProcessOrderResultInterface { if (strpos($order->getPayment()->getMethod(), RvvupConfigProvider::CODE) !== 0) { throw new PaymentValidationException(__('Order is not paid via Rvvup Payment Link')); diff --git a/Model/ProcessOrder/Complete.php b/Model/ProcessOrder/Complete.php index 33ffa8ab..ca2b214b 100644 --- a/Model/ProcessOrder/Complete.php +++ b/Model/ProcessOrder/Complete.php @@ -78,10 +78,11 @@ public function __construct( /** * @param OrderInterface $order * @param array $rvvupData + * @param string $origin * @return ProcessOrderResultInterface * @throws PaymentValidationException */ - public function execute(OrderInterface $order, array $rvvupData): ProcessOrderResultInterface + public function execute(OrderInterface $order, array $rvvupData, string $origin): ProcessOrderResultInterface { if ($order->getPayment() === null || strpos($order->getPayment()->getMethod(), Method::PAYMENT_TITLE_PREFIX) !== 0 @@ -134,7 +135,8 @@ public function execute(OrderInterface $order, array $rvvupData): ProcessOrderRe $e->getMessage(), $rvvupData['id'], $rvvupData['payments'][0]['id'], - $order->getId() + $order->getId(), + $origin ); $processOrderResult->setResultType(ProcessOrderResultInterface::RESULT_TYPE_ERROR); diff --git a/Model/ProcessOrder/Processing.php b/Model/ProcessOrder/Processing.php index f8144687..acc14024 100644 --- a/Model/ProcessOrder/Processing.php +++ b/Model/ProcessOrder/Processing.php @@ -60,10 +60,11 @@ public function __construct( * * @param OrderInterface $order * @param array $rvvupData + * @param string $origin * @return ProcessOrderResultInterface * @throws PaymentValidationException */ - public function execute(OrderInterface $order, array $rvvupData): ProcessOrderResultInterface + public function execute(OrderInterface $order, array $rvvupData, string $origin): ProcessOrderResultInterface { /** @var \Rvvup\Payments\Api\Data\ProcessOrderResultInterface $processOrderResult */ $processOrderResult = $this->processOrderResultFactory->create(); @@ -121,7 +122,8 @@ public function execute(OrderInterface $order, array $rvvupData): ProcessOrderRe $e->getMessage(), $rvvupData['id'], $rvvupData['payments'][0]['id'], - $order->getId() + $order->getId(), + $origin ); $processOrderResult->setResultType(ProcessOrderResultInterface::RESULT_TYPE_ERROR); diff --git a/Model/ProcessOrder/ProcessorInterface.php b/Model/ProcessOrder/ProcessorInterface.php index fcc38446..8b525315 100644 --- a/Model/ProcessOrder/ProcessorInterface.php +++ b/Model/ProcessOrder/ProcessorInterface.php @@ -10,7 +10,8 @@ interface ProcessorInterface /** * @param OrderInterface $order * @param array $rvvupData + * @param string $origin * @return ProcessOrderResultInterface */ - public function execute(OrderInterface $order, array $rvvupData): ProcessOrderResultInterface; + public function execute(OrderInterface $order, array $rvvupData, string $origin): ProcessOrderResultInterface; } diff --git a/Model/ProcessOrder/UpdateOrder.php b/Model/ProcessOrder/UpdateOrder.php index 06bb25f2..7dc9a857 100644 --- a/Model/ProcessOrder/UpdateOrder.php +++ b/Model/ProcessOrder/UpdateOrder.php @@ -29,11 +29,12 @@ public function __construct( /** * @param OrderInterface $order * @param array $rvvupData + * @param string $origin * @return ProcessOrderResultInterface * @throws JsonException * @throws NoSuchEntityException */ - public function execute(OrderInterface $order, array $rvvupData): ProcessOrderResultInterface + public function execute(OrderInterface $order, array $rvvupData, string $origin): ProcessOrderResultInterface { $processOrderResult = $this->processOrderResultFactory->create(); $processOrderResult->setResultType(ProcessOrderResultInterface::RESULT_TYPE_ERROR); diff --git a/Model/Queue/Handler/Handler.php b/Model/Queue/Handler/Handler.php index 31a703bb..4ab5840b 100644 --- a/Model/Queue/Handler/Handler.php +++ b/Model/Queue/Handler/Handler.php @@ -111,6 +111,7 @@ public function execute(string $data) $rvvupOrderId = $payload['order_id']; $rvvupPaymentId = $payload['payment_id']; $storeId = $payload['store_id'] ?? false; + $origin = $payload['origin'] ?? false; if (!$storeId) { return; @@ -121,7 +122,7 @@ public function execute(string $data) if ($paymentLinkId = $payload['payment_link_id']) { $order = $this->captureService->getOrderByRvvupPaymentLinkId($paymentLinkId, $storeId); if ($order && $order->getId()) { - $this->processOrder($order, $rvvupOrderId, $rvvupPaymentId); + $this->processOrder($order, $rvvupOrderId, $rvvupPaymentId, $origin); return; } return; @@ -142,7 +143,7 @@ public function execute(string $data) $payment = $quote->getPayment(); $rvvupPaymentId = $payment->getAdditionalInformation(Method::PAYMENT_ID); $lastTransactionId = (string)$payment->getAdditionalInformation(Method::TRANSACTION_ID); - $validate = $this->captureService->validate($quote, $lastTransactionId, $rvvupOrderId); + $validate = $this->captureService->validate($quote, $lastTransactionId, $rvvupOrderId, $origin); if (!$validate->getIsValid()) { if ($validate->getRedirectToCart()) { return; @@ -152,7 +153,7 @@ public function execute(string $data) } } $this->captureService->setCheckoutMethod($quote); - $validation = $this->captureService->createOrder($rvvupOrderId, $quote); + $validation = $this->captureService->createOrder($rvvupOrderId, $quote, $origin); $alreadyExists = $validation->getAlreadyExists(); $orderId = $validation->getOrderId(); @@ -168,7 +169,8 @@ public function execute(string $data) $payment, $lastTransactionId, $rvvupPaymentId, - $rvvupOrderId + $rvvupOrderId, + $origin ); return; @@ -176,7 +178,7 @@ public function execute(string $data) $order = $this->captureService->getOrderByRvvupId($rvvupOrderId); if ($order && $order->getId()) { - $this->processOrder($order, $rvvupOrderId, $rvvupPaymentId); + $this->processOrder($order, $rvvupOrderId, $rvvupPaymentId, $origin); } return; } catch (\Exception $e) { @@ -190,12 +192,17 @@ public function execute(string $data) * @param OrderInterface $order * @param string $rvvupOrderId * @param string $rvvupPaymentId + * @param string $origin * @return void - * @throws LocalizedException * @throws AlreadyExistsException + * @throws LocalizedException */ - private function processOrder(OrderInterface $order, string $rvvupOrderId, string $rvvupPaymentId): void - { + private function processOrder( + OrderInterface $order, + string $rvvupOrderId, + string $rvvupPaymentId, + string $origin + ): void { // if Payment method is not Rvvup, exit. if (strpos($order->getPayment()->getMethod(), Method::PAYMENT_TITLE_PREFIX) !== 0) { if (strpos($order->getPayment()->getMethod(), RvvupConfigProvider::CODE) !== 0) { @@ -218,12 +225,14 @@ private function processOrder(OrderInterface $order, string $rvvupOrderId, strin if ($order->getPayment()->getMethod() == RvvupConfigProvider::CODE) { $this->processorPool->getPaymentLinkProcessor($rvvupData['payments'][0]['status'])->execute( $order, - $rvvupData + $rvvupData, + $origin ); } else { $this->processorPool->getProcessor($rvvupData['payments'][0]['status'])->execute( $order, - $rvvupData + $rvvupData, + $origin ); } } diff --git a/Service/Capture.php b/Service/Capture.php index 6d3f8464..517038e0 100644 --- a/Service/Capture.php +++ b/Service/Capture.php @@ -178,23 +178,26 @@ public function getOrderListByRvvupId(string $rvvupOrderId): OrderPaymentSearchR * @param string $lastTransactionId * @param string|null $rvvupId * @param string|null $paymentStatus + * @param string|null $origin * @return ValidationInterface */ public function validate( Quote &$quote, string &$lastTransactionId, string $rvvupId = null, - string $paymentStatus = null + string $paymentStatus = null, + string $origin = null ): ValidationInterface { - return $this->validationInterface->validate($quote, $lastTransactionId, $rvvupId, $paymentStatus); + return $this->validationInterface->validate($quote, $lastTransactionId, $rvvupId, $paymentStatus, $origin); } /** * @param string $rvvupId * @param Quote $quote + * @param string $origin * @return ValidationInterface */ - public function createOrder(string $rvvupId, Quote $quote): ValidationInterface + public function createOrder(string $rvvupId, Quote $quote, string $origin): ValidationInterface { $this->quoteResource->beginTransaction(); $lastTransactionId = (string)$quote->getPayment()->getAdditionalInformation('transaction_id'); @@ -250,6 +253,7 @@ public function createOrder(string $rvvupId, Quote $quote): ValidationInterface $rvvupId, null, $quote->getReservedOrderId(), + $origin ); return $this->validationInterfaceFactory->create( [ @@ -267,13 +271,15 @@ public function createOrder(string $rvvupId, Quote $quote): ValidationInterface * @param string $lastTransactionId * @param string $rvvupPaymentId * @param string $rvvupId + * @param string $origin * @return bool */ public function paymentCapture( Quote\Payment $payment, string $lastTransactionId, string $rvvupPaymentId, - string $rvvupId + string $rvvupId, + string $origin ): bool { try { if ($payment->getMethodInstance()->getCaptureType() !== 'MANUAL') { @@ -284,7 +290,8 @@ public function paymentCapture( 'Rvvup order capture failed during payment capture', $e->getMessage(), $rvvupId, - $rvvupPaymentId + $rvvupPaymentId, + $origin ); return false; } diff --git a/Service/Result.php b/Service/Result.php index c754c979..37178b3b 100644 --- a/Service/Result.php +++ b/Service/Result.php @@ -73,15 +73,19 @@ public function __construct( $this->logger = $logger; } - /** @todo refactor - * Update Magento Order based on Rvuup Order and payment statuses - * @param string|null $orderId + /** @param string|null $orderId * @param string $rvvupId + * @param string $origin * @param bool $reservedOrderId * @return Redirect + * Update Magento Order based on Rvuup Order and payment statuses */ - public function processOrderResult(?string $orderId, string $rvvupId, bool $reservedOrderId = false): Redirect - { + public function processOrderResult( + ?string $orderId, + string $rvvupId, + string $origin, + bool $reservedOrderId = false + ): Redirect { if (!$orderId) { return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath( In::SUCCESS, @@ -100,12 +104,12 @@ public function processOrderResult(?string $orderId, string $rvvupId, bool $rese if ($rvvupData['status'] != $rvvupData['payments'][0]['status']) { if ($rvvupData['payments'][0]['status'] !== Method::STATUS_AUTHORIZED) { - $this->processorPool->getProcessor($rvvupData['status'])->execute($order, $rvvupData); + $this->processorPool->getProcessor($rvvupData['status'])->execute($order, $rvvupData, $origin); } } $processor = $this->processorPool->getProcessor($rvvupData['payments'][0]['status']); - $result = $processor->execute($order, $rvvupData); + $result = $processor->execute($order, $rvvupData, $origin); if (get_class($processor) == Cancel::class) { return $this->processResultPage($result, true); }