Skip to content

Commit

Permalink
handel classic orders webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-saad committed May 6, 2020
1 parent 3ca5ce4 commit b331cd6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Components/Services/PaymentStatusService.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
22 changes: 22 additions & 0 deletions WebhookHandlers/SaleRefunded.php
Expand Up @@ -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()]
Expand All @@ -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;
}
}
}

0 comments on commit b331cd6

Please sign in to comment.