Skip to content

Commit

Permalink
Merge branch 'develop' into Adyen#54-cronjobs-not-running
Browse files Browse the repository at this point in the history
* develop: (21 commits)
  Only send mail after succesful payment (Adyen#46)
  Adding N/A to houseNumberOrName in Components/Payload/Providers/ShopperInfoProvider.php
  1.5.0 version bump
  Fixing changelog
  Changing API test error code from 500 to 400 (Adyen#52)
  Feature/change refund function (Adyen#48)
  Localizing changelog (Adyen#49)
  Fixes Adyen#13 PW-2748 Add event to order restore (Adyen#42)
  PW-2505 Distinquish fields of test and live (Adyen#44)
  PW-2565 Make paymentsmethod cache configurable (Adyen#43)
  PW-2804 Save form before test (Adyen#36)
  Fixes Adyen#18 PW-2600 Catch exception during backend Test (Adyen#38)
  Fixes Adyen#15 Handle offer_closed notification (Adyen#39)
  Improve exception logging (Adyen#37)
  Fixing Yandex logo (Adyen#40)
  PW-2603 remove customer id from shopperinfo payload (Adyen#33)
  Fix Adyen#22 PW-2743 Show error on 404 (Adyen#35)
  Fix Adyen#19 PW-2601 Update completed state (Adyen#34)
  Update composer.json (Adyen#31)
  [PW-2562] Adjustments for phpcs (Adyen#9)
  ...

# Conflicts:
#	plugin.xml
  • Loading branch information
vancaw1 committed Sep 25, 2020
2 parents b7b5dce + 8c3b08f commit 9ddbab7
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 4 deletions.
13 changes: 13 additions & 0 deletions AdyenPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions Components/OrderMailService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace AdyenPayment\Components;

use Shopware\Components\Model\ModelManager;

class OrderMailService
{
/**
* @var ModelManager
*/
private $modelManager;
/**
* @var BasketService
*/
private $basketService;

public function __construct(
ModelManager $modelManager,
BasketService $basketService
) {
$this->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);
}
}
7 changes: 6 additions & 1 deletion Controllers/Frontend/Adyen.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use AdyenPayment\AdyenPayment;
use AdyenPayment\Components\Adyen\PaymentMethodService;
use AdyenPayment\Components\BasketService;
use AdyenPayment\Components\Calculator\PriceCalculationService;
Expand Down Expand Up @@ -199,7 +200,7 @@ private function prepareTransaction()
}

/**
* @param $transaction
* @param PaymentInfo $transaction
* @return Order
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
Expand All @@ -208,13 +209,17 @@ private function prepareOrder($transaction)
{
$signature = $this->persistBasket();

Shopware()->Session()->offsetSet(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, $transaction->getId());

$orderNumber = $this->saveOrder(
$transaction->getId(),
$signature,
Status::PAYMENT_STATE_OPEN,
false
);

Shopware()->Session()->offsetSet(AdyenPayment::SESSION_ADYEN_RESTRICT_EMAILS, false);

/** @var Order $order */
$order = $this->getModelManager()->getRepository(Order::class)->findOneBy([
'number' => $orderNumber
Expand Down
11 changes: 10 additions & 1 deletion Controllers/Frontend/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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');
}

Expand All @@ -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',
Expand Down
24 changes: 23 additions & 1 deletion Models/PaymentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions Resources/services/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<argument type="service" id="shopware.plugin.cached_config_reader"/>
<argument type="service" id="dbal_connection"/>
</service>
<service id="adyen_payment.components.order_mail_service" class="AdyenPayment\Components\OrderMailService">
<argument type="service" id="models"/>
<argument type="service" id="adyen_payment.components.basket_service"/>
</service>
<service id="adyen_payment.components.adyen.apifactory" class="AdyenPayment\Components\Adyen\ApiFactory">
<argument type="service" id="models"/>
<argument type="service" id="adyen_payment.components.configuration"/>
Expand Down
5 changes: 5 additions & 0 deletions Resources/services/subscribers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<argument type="service" id="models"/>
<argument type="service" id="adyen_payment.components.notification_manager"/>
</service>
<service id="adyen_payment.subscriber.order_email_subscriber" class="AdyenPayment\Subscriber\OrderEmailSubscriber">
<tag name="shopware.event_subscriber"/>
<argument type="service" id="models"/>
<argument type="service" id="adyen_payment.components.order_mail_service"/>
</service>
<service id="adyen_payment.subscriber.checkout" class="AdyenPayment\Subscriber\CheckoutSubscriber">
<tag name="shopware.event_subscriber"/>
<argument type="service" id="adyen_payment.components.configuration"/>
Expand Down
96 changes: 96 additions & 0 deletions Subscriber/OrderEmailSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php declare(strict_types=1);

namespace AdyenPayment\Subscriber;

use AdyenPayment\AdyenPayment;
use AdyenPayment\Components\NotificationManager;
use AdyenPayment\Components\OrderMailService;
use AdyenPayment\Models\PaymentInfo;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityRepository;
use Enlight\Event\SubscriberInterface;
use Enlight_Event_EventArgs;
use MollieShopware\Models\Transaction;
use Shopware\Components\HttpClient\Response;
use Shopware\Components\Model\ModelManager;
use Shopware_Controllers_Frontend_Checkout;

class OrderEmailSubscriber implements SubscriberInterface
{
/**
* @var ModelManager
*/
private $modelManager;

/**
* @var ObjectRepository|EntityRepository
*/
private $paymentInfoRepository;
/**
* @var OrderMailService
*/
private $orderMailService;

public function __construct(
ModelManager $modelManager,
OrderMailService $orderMailService
) {
$this->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']);
}
}
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<label>Adyen Shopware Plugin</label>
<label lang="de">Adyen Shopware Plugin</label>

<version>1.5.0</version>

<version>1.5.1</version>
<copyright>Adyen</copyright>
<author>Adyen</author>
<link>https://adyen.com</link>
Expand Down

0 comments on commit 9ddbab7

Please sign in to comment.