diff --git a/AdyenPayment.php b/AdyenPayment.php index b73ebb55..eb646556 100644 --- a/AdyenPayment.php +++ b/AdyenPayment.php @@ -18,6 +18,7 @@ use Shopware\Components\Plugin\Context\DeactivateContext; use Shopware\Components\Plugin\Context\InstallContext; use Shopware\Components\Plugin\Context\UninstallContext; +use Shopware\Components\Plugin\Context\UpdateContext; use Shopware\Components\Plugin\PaymentInstaller; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -36,6 +37,7 @@ class AdyenPayment extends Plugin const SESSION_ADYEN_PAYMENT = 'adyenPayment'; const SESSION_ADYEN_PAYMENT_VALID = 'adyenPaymentValid'; const SESSION_ADYEN_PAYMENT_DATA = 'adyenPaymentData'; + const SESSION_ADYEN_RESTRICT_EMAILS = 'adyenRestrictEmail'; /** * @return bool @@ -95,6 +97,17 @@ public function install(InstallContext $context) $tool->updateSchema($classes, true); } + public function update(UpdateContext $context) + { + $this->installAttributes(); + + $tool = new SchemaTool($this->container->get('models')); + $classes = $this->getModelMetaData(); + $tool->updateSchema($classes, true); + + parent::update($context); + } + /** * @param UninstallContext $context * @throws Exception diff --git a/Components/OrderMailService.php b/Components/OrderMailService.php new file mode 100644 index 00000000..28d8aab9 --- /dev/null +++ b/Components/OrderMailService.php @@ -0,0 +1,60 @@ +modelManager = $modelManager; + $this->basketService = $basketService; + } + + /** + * Sends the mail after a payment is confirmed + * + * @param \Shopware\Models\Order\Order $order + */ + public function sendOrderConfirmationMail($orderNumber) + { + $order = $this->basketService->getOrderByOrderNumber($orderNumber); + if (!$order) { + return; + } + + $paymentInfoRepository = $this->modelManager->getRepository(\AdyenPayment\Models\PaymentInfo::class); + /** @var \AdyenPayment\Models\PaymentInfo $paymentInfo */ + $paymentInfo = $paymentInfoRepository->findOneBy([ + 'orderId' => $order->getId() + ]); + + if (!$paymentInfo) { + return; + } + + $variables = json_decode($paymentInfo->getOrdermailVariables(), true); + + if (is_array($variables)) { + $sOrder = Shopware()->Modules()->Order(); + $sOrder->sUserData = $variables; + $sOrder->sendMail($variables); + } + + $paymentInfo->setOrdermailVariables(null); + $this->modelManager->persist($paymentInfo); + $this->modelManager->flush($paymentInfo); + } +} diff --git a/Components/Payload/Providers/ApplicationInfoProvider.php b/Components/Payload/Providers/ApplicationInfoProvider.php index bdd25af8..4569146b 100644 --- a/Components/Payload/Providers/ApplicationInfoProvider.php +++ b/Components/Payload/Providers/ApplicationInfoProvider.php @@ -49,8 +49,10 @@ public function provide(PaymentContext $context): array 'executeThreeD' => true, 'allow3DS2' => true, ], - "channel" => Channel::WEB, + 'channel' => Channel::WEB, 'origin' => $context->getOrigin(), + 'redirectFromIssuerMethod' => 'GET', + 'redirectToIssuerMethod' => 'POST', 'returnUrl' => $returnUrl, 'merchantAccount' => $this->configuration->getMerchantAccount(), 'applicationInfo' => [ diff --git a/Components/Payload/Providers/OrderInfoProvider.php b/Components/Payload/Providers/OrderInfoProvider.php index 73be8356..a16eeeb4 100644 --- a/Components/Payload/Providers/OrderInfoProvider.php +++ b/Components/Payload/Providers/OrderInfoProvider.php @@ -23,8 +23,8 @@ public function provide(PaymentContext $context): array return [ 'amount' => [ - "currency" => $currencyCode, - "value" => $adyenCurrency->sanitize($context->getOrder()->getInvoiceAmount(), $currencyCode), + 'currency' => $currencyCode, + 'value' => $adyenCurrency->sanitize($context->getOrder()->getInvoiceAmount(), $currencyCode), ], 'reference' => $context->getOrder()->getNumber(), ]; diff --git a/Controllers/Frontend/Adyen.php b/Controllers/Frontend/Adyen.php index f31c17fd..28348a3e 100644 --- a/Controllers/Frontend/Adyen.php +++ b/Controllers/Frontend/Adyen.php @@ -1,5 +1,6 @@ persistBasket(); + Shopware()->Session()->offsetSet(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, $transaction->getId()); + $orderNumber = $this->saveOrder( $transaction->getId(), $signature, @@ -215,6 +218,8 @@ private function prepareOrder($transaction) false ); + Shopware()->Session()->offsetSet(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, false); + /** @var Order $order */ $order = $this->getModelManager()->getRepository(Order::class)->findOneBy([ 'number' => $orderNumber diff --git a/Controllers/Frontend/Process.php b/Controllers/Frontend/Process.php index 26fb3dc0..a4f4900e 100644 --- a/Controllers/Frontend/Process.php +++ b/Controllers/Frontend/Process.php @@ -28,11 +28,17 @@ class Shopware_Controllers_Frontend_Process extends Shopware_Controllers_Fronten */ private $basketService; + /** + * @var \AdyenPayment\Components\OrderMailService + */ + private $orderMailService; + /** * @var Logger */ private $logger; + /** * Whitelist notifyAction */ @@ -41,12 +47,12 @@ public function getWhitelistedCSRFActions() return ['return']; } - public function preDispatch() { $this->adyenManager = $this->get('adyen_payment.components.manager.adyen_manager'); $this->adyenCheckout = $this->get('adyen_payment.components.adyen.payment.method'); $this->basketService = $this->get('adyen_payment.components.basket_service'); + $this->orderMailService = $this->get('adyen_payment.components.order_mail_service'); $this->logger = $this->get('adyen_payment.logger'); } @@ -67,6 +73,9 @@ public function returnAction() case PaymentResultCodes::AUTHORISED: case PaymentResultCodes::PENDING: case PaymentResultCodes::RECEIVED: + if (!empty($result['merchantReference'])) { + $this->orderMailService->sendOrderConfirmationMail($result['merchantReference']); + } $this->redirect([ 'controller' => 'checkout', 'action' => 'finish', diff --git a/Models/PaymentInfo.php b/Models/PaymentInfo.php index f1b133c7..13262dc7 100644 --- a/Models/PaymentInfo.php +++ b/Models/PaymentInfo.php @@ -59,8 +59,12 @@ class PaymentInfo extends ModelEntity private $resultCode; /** - * PaymenntInfo constructor + * @var string + * + * @ORM\Column(name="ordermail_variables", type="text", nullable=true) */ + private $ordermailVariables; + public function __construct() { $this->setCreatedAt(new \DateTime('now')); @@ -195,4 +199,22 @@ public function setResultCode(string $resultCode) $this->resultCode = $resultCode; return $this; } + + /** + * @return string|null + */ + public function getOrdermailVariables() + { + return $this->ordermailVariables; + } + + /** + * @param string|null $ordermailVariables + * @return $this + */ + public function setOrdermailVariables($ordermailVariables) + { + $this->ordermailVariables = $ordermailVariables; + return $this; + } } diff --git a/Resources/services/components.xml b/Resources/services/components.xml index 0b109363..00f4515e 100644 --- a/Resources/services/components.xml +++ b/Resources/services/components.xml @@ -19,6 +19,10 @@ + + + + @@ -80,6 +84,7 @@ + @@ -94,6 +99,7 @@ + diff --git a/Resources/services/subscribers.xml b/Resources/services/subscribers.xml index 46a75234..f300a0e2 100644 --- a/Resources/services/subscribers.xml +++ b/Resources/services/subscribers.xml @@ -38,6 +38,11 @@ + + + + + diff --git a/Subscriber/OrderEmailSubscriber.php b/Subscriber/OrderEmailSubscriber.php new file mode 100644 index 00000000..5ad97f34 --- /dev/null +++ b/Subscriber/OrderEmailSubscriber.php @@ -0,0 +1,96 @@ +modelManager = $modelManager; + $this->paymentInfoRepository = $this->modelManager->getRepository(PaymentInfo::class); + $this->orderMailService = $orderMailService; + } + + public static function getSubscribedEvents() + { + return [ + 'Shopware_Modules_Order_SendMail_Send' => 'shouldStopEmailSending', + 'Enlight_Controller_Action_PostDispatch_Frontend_Checkout' => 'onCheckoutDispatch' + ]; + } + + public function shouldStopEmailSending(Enlight_Event_EventArgs $args) + { + $orderId = $args->get('orderId'); + $variables = $args->get('variables'); + + if ($variables['additional']['payment']['name'] === AdyenPayment::ADYEN_GENERAL_PAYMENT_METHOD && + Shopware()->Session()->get(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, true) === false) { + Shopware()->Session()->offsetSet(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, true); + + /** @var PaymentInfo $paymentInfo */ + $paymentInfo = $this->paymentInfoRepository->findOneBy([ + 'orderId' => $orderId + ]); + + if ($paymentInfo && empty($paymentInfo->getOrdermailVariables())) { + $paymentInfo->setOrdermailVariables(json_encode($variables)); + + $this->modelManager->persist($paymentInfo); + $this->modelManager->flush($paymentInfo); + } + + return false; + } + + return null; + } + + public function onCheckoutDispatch(Enlight_Event_EventArgs $args) + { + + /** @var Shopware_Controllers_Frontend_Checkout $subject */ + $subject = $args->getSubject(); + + if ($subject->Request()->getActionName() !== 'finish') { + return; + } + + $data = $subject->View()->getAssign(); + + if (!$data['sOrderNumber']) { + return; + } + + $this->orderMailService->sendOrderConfirmationMail($data['sOrderNumber']); + } +} diff --git a/plugin.xml b/plugin.xml index 0558ccc2..00591329 100644 --- a/plugin.xml +++ b/plugin.xml @@ -4,8 +4,7 @@ - - 1.5.0 + 1.5.1 Adyen Adyen https://adyen.com @@ -52,4 +51,20 @@ Live/Test config values + + + Fixes: + Only send mail after successful payment + Adding N/A to houseNumberOrName + Cronjobs not running fix + Add redirect data to payments request + + + Fixes: + Only send mail after successful payment + Adding N/A to houseNumberOrName + Cronjobs not running fix + Add redirect data to payments request + +