Skip to content

Commit

Permalink
Release 28-08-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
Slawomir Rostek committed Aug 28, 2024
1 parent e61436d commit dfeb3d9
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 106 deletions.
5 changes: 5 additions & 0 deletions Controller/Webhook/CancelCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ protected function beforeSave()
$this->order->setStatus('canceled');
}
}

protected function getSuccessComment(): string
{
return 'Cancel created for payment ID: %s';
}
}
30 changes: 30 additions & 0 deletions Controller/Webhook/CheckoutCompleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,34 @@ protected function beforeSave()
$this->order->getPayment()->setAdditionalInformation($additionalInformation);
}
}

/**
* @inheritDoc
*/
protected function afterSave()
{
// update reference if on order submit was to early to update
$this->dibsCheckoutContext->getDibsOrderHandler()->updatePaymentReference($this->order);

// To sent email for Swish payment method
if (strtoupper($this->paymentMethod) === 'SWISH') {
try {
$this->dibsCheckoutContext->getOrderSender()->send($this->order);
$this->logInfo("Sent order confirmation");
} catch (\Exception $e) {
$this->logError("Error sending order confirmation email");
$this->logError("Error message: {$e->getMessage()}");
$this->logError("Stack trace: {$e->getPrevious()->getTraceAsString()}");
$this->order->addCommentToStatusHistory(
"Callback for {$this->expectedEvent} encountered an error when trying to send order confirmation email",
false
);
}
}
}

protected function getSuccessComment(): string
{
return 'Checkout completed for payment ID: %s';
}
}
5 changes: 5 additions & 0 deletions Controller/Webhook/PaymentCharged.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function afterSave(): void
$paymentResponse = $this->dibsCheckoutContext->getDibsOrderHandler()->loadDibsPaymentById($this->paymentId, $this->storeId);
$responseHandler->saveOrder($paymentResponse, $this->order);
}

protected function getSuccessComment(): string
{
return 'Charge created for payment ID: %s';
}
}
8 changes: 8 additions & 0 deletions Controller/Webhook/PaymentRefund.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dibs\EasyCheckout\Controller\Webhook;

use Dibs\EasyCheckout\Model\Client\DTO\Payment\CreatePaymentWebhook;

class PaymentRefund extends Webhook {

protected function beforeSave() {
Expand Down Expand Up @@ -35,4 +37,10 @@ protected function beforeSave() {
}
}

protected function getSuccessComment(): string
{
return $this->expectedEvent === CreatePaymentWebhook::EVENT_PAYMENT_REFUND_COMPLETED
? 'Refund completed for payment ID: %s'
: 'Refund initiated for payment ID: %s';
}
}
29 changes: 29 additions & 0 deletions Controller/Webhook/ReservationCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,33 @@ protected function beforeSave()
}
}
}

/**
* @inheritDoc
*/
protected function afterSave()
{
// Send order confirmation if not sent already
if ($this->order->getEmailSent()) {
return;
}

try {
$this->dibsCheckoutContext->getOrderSender()->send($this->order);
$this->logInfo("Sent order confirmation");
} catch (\Exception $e) {
$this->logError("Error sending order confirmation email");
$this->logError("Error message: {$e->getMessage()}");
$this->logError("Stack trace: {$e->getPrevious()->getTraceAsString()}");
$this->order->addCommentToStatusHistory(
"Callback for {$this->expectedEvent} encountered an error when trying to send order confirmation email",
false
);
}
}

protected function getSuccessComment(): string
{
return 'Reservation created for payment ID: %s';
}
}
61 changes: 5 additions & 56 deletions Controller/Webhook/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,11 @@ abstract class Webhook implements HttpPostActionInterface, CsrfAwareActionInterf
//
// di.xml scalar properties

/**
* @var string
*/
protected $logPrefix;

/**
* @var string
*/
protected $expectedEvent;

/**
* @var string
*/
protected $successComment;

/**
* @var bool
*/
protected $sendEmail;
//
// Processing properties

Expand Down Expand Up @@ -116,9 +102,7 @@ public function __construct(
DibsCheckoutContext $dibsCheckoutContext,
JsonFactory $resultFactory,
OrderPaymentRepositoryInterface $paymentRepo,
string $expectedEvent,
string $successComment,
bool $sendEmail
string $expectedEvent
) {
$this->helper = $helper;
$this->paymentApi = $paymentApi;
Expand All @@ -128,8 +112,6 @@ public function __construct(
$this->resultFactory = $resultFactory;
$this->paymentRepo = $paymentRepo;
$this->expectedEvent = $expectedEvent;
$this->successComment = $successComment;
$this->sendEmail = $sendEmail;
}

public function execute() {
Expand Down Expand Up @@ -187,26 +169,6 @@ public function execute() {
$this->paymentRepo->save($this->order->getPayment());

$this->logInfo("Order is updated successfully");
$orderId = $this->order->getIncrementId();
$payment = $this->order->getPayment();
if ($payment->getMethod() == "dibseasycheckout") {
$this->logInfo('from the webhook ' . $this->order->getIncrementId());
// To sent email for Swish payment method
if (strtoupper($this->paymentMethod) == 'SWISH' && $data['event'] == 'payment.checkout.completed') {
try {
$this->dibsCheckoutContext->getOrderSender()->send($this->order);
$this->logInfo("Sent order confirmation");
} catch (\Exception $e) {
$this->logError("Error sending order confirmation email");
$this->logError("Error message: {$e->getMessage()}");
$this->logError("Stack trace: {$e->getPrevious()->getTraceAsString()}");
$this->order->addCommentToStatusHistory(
"Callback for {$this->expectedEvent} encountered an error when trying to send order confirmation email",
false
);
}
}
}
} catch (\Exception $e) {
$this->logError("Could not update order");
$this->logError("Error message: {$e->getMessage()}");
Expand All @@ -219,23 +181,8 @@ public function execute() {
return $result;
}

// Send order confirmation if not sent already
if ($this->sendEmail && !$this->order->getEmailSent()) {
try {
$this->dibsCheckoutContext->getOrderSender()->send($this->order);
$this->logInfo("Sent order confirmation");
} catch (\Exception $e) {
$this->logError("Error sending order confirmation email");
$this->logError("Error message: {$e->getMessage()}");
$this->logError("Stack trace: {$e->getPrevious()->getTraceAsString()}");
$this->order->addCommentToStatusHistory(
"Callback for {$this->expectedEvent} encountered an error when trying to send order confirmation email",
false
);
}
}

$this->afterSave();

$result->setHttpResponseCode(200);
return $result;
}
Expand Down Expand Up @@ -316,7 +263,7 @@ protected function getLogPrefix() {
* @return void
*/
protected function addSuccessCommentToOrder() {
$comment = sprintf($this->successComment, $this->paymentId);
$comment = sprintf($this->getSuccessComment(), $this->paymentId);
if ($this->order->getState() === Order::STATE_PENDING_PAYMENT) {
$this->order->setState(Order::STATE_PROCESSING);
$status = $this->dibsCheckoutContext->getHelper()->getProcessingOrderStatus($this->order->getStore());
Expand All @@ -326,4 +273,6 @@ protected function addSuccessCommentToOrder() {

$this->order->addCommentToStatusHistory($comment, false);
}

abstract protected function getSuccessComment(): string;
}
2 changes: 1 addition & 1 deletion Model/Client/Api/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function UpdatePaymentCart(UpdatePaymentCart $cart, $paymentId) {
* @return void
* @throws ClientException
*/
public function UpdatePaymentReference(UpdatePaymentReference $reference, $paymentId, $storeId) {
public function updatePaymentReference(UpdatePaymentReference $reference, $paymentId, $storeId) {
try {
$this->put("/v1/payments/" . $paymentId . "/referenceinformation", $reference, $storeId);
} catch (ClientException $e) {
Expand Down
26 changes: 26 additions & 0 deletions Model/Dibs/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Magento\Store\Model\StoreManagerInterface;
use Dibs\EasyCheckout\Api\CheckoutFlow;
use Dibs\EasyCheckout\Model\Client\DTO\TerminatePayment;
use Dibs\EasyCheckout\Model\Client\DTO\UpdatePaymentReference;
use Magento\Sales\Model\Order as OrderEntity;

class Order {

Expand Down Expand Up @@ -643,6 +645,21 @@ public function refundDibsPayment(\Magento\Payment\Model\InfoInterface $payment,
}
}

public function updatePaymentReference(OrderEntity $orderEntity): void {
$paymentId = $orderEntity->getDibsPaymentId();
$storeId = $orderEntity->getStoreId();
$payment = $this->loadDibsPaymentById($paymentId, $storeId);

if ($orderEntity->getIncrementId() === $payment->getOrderDetails()->getReference()) {
return;
}

$reference = new UpdatePaymentReference();
$reference->setReference($orderEntity->getIncrementId());
$reference->setCheckoutUrl($this->getCheckoutUrl($payment));
$this->paymentApi->updatePaymentReference($reference, $paymentId, $storeId);
}

/**
* @param $paymentId
* @param $storeId
Expand Down Expand Up @@ -713,4 +730,13 @@ private function generateConsumerType($quote) {
return $consumerType;
}

private function getCheckoutUrl(GetPaymentResponse $payment): string
{
if ($this->helper->getCheckoutFlow() === "HostedPaymentPage") {
return $payment->getCheckoutUrl();
}

return $this->helper->getCheckoutUrl();
}

}
40 changes: 15 additions & 25 deletions Observer/UpdatePaymentReferenceOnSubmitAllObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@

namespace Dibs\EasyCheckout\Observer;

use Dibs\EasyCheckout\Helper\Data;
use Dibs\EasyCheckout\Model\Client\Api\Payment;
use Dibs\EasyCheckout\Logger\Logger;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Dibs\EasyCheckout\Model\Client\DTO\UpdatePaymentReference;
use Dibs\EasyCheckout\Model\Dibs\Order;

class UpdatePaymentReferenceOnSubmitAllObserver implements ObserverInterface
{
private Payment $api;

private Data $helper;
private Order $dibsHandler;
private Logger $logger;

public function __construct(
Data $helper,
Payment $api
Order $dibsHandler,
Logger $logger
) {
$this->api = $api;
$this->helper = $helper;
$this->dibsHandler = $dibsHandler;
$this->logger = $logger;
}

public function execute(EventObserver $observer)
Expand All @@ -31,23 +29,15 @@ public function execute(EventObserver $observer)
if ($payment->getMethod() !== "dibseasycheckout") {
return;
}
$paymentId = $order->getDibsPaymentId();
$storeId = $order->getStoreId();
$reference = new UpdatePaymentReference();
$reference->setReference($order->getIncrementId());
$reference->setCheckoutUrl($this->getCheckoutUrl($paymentId, $storeId));
$this->api->UpdatePaymentReference($reference, $paymentId, $storeId);
}

private function getCheckoutUrl(string $paymentId, int $storeId): string
{
if ($this->helper->getCheckoutFlow() === "HostedPaymentPage") {
$payment = $this->api->getPayment($paymentId, $storeId);

return $payment->getCheckoutUrl();
try {
$this->dibsHandler->updatePaymentReference($order);
} catch (\Exception $e) {
$this->logger->error(
'updatePaymentReference in submit observer failed',
['paymentId' => $order->getDibsPaymentId(), 'exception' => $e]
);
}

return $this->helper->getCheckoutUrl();
}
}

15 changes: 15 additions & 0 deletions etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,20 @@
<value id="dibspayment" type="host">https://*.dibspayment.eu</value>
</values>
</policy>
<policy id="connect-src">
<values>
<value id="dibspayment" type="host">https://*.dibspayment.eu</value>
</values>
</policy>
<policy id="style-src">
<values>
<value id="dibspayment" type="host">https://*.dibspayment.eu</value>
</values>
</policy>
<policy id="img-src">
<values>
<value id="dibspayment" type="host">https://info.dibs.se</value>
</values>
</policy>
</policies>
</csp_whitelist>
Loading

0 comments on commit dfeb3d9

Please sign in to comment.