Skip to content

Commit

Permalink
Merge pull request #166 from bold-commerce/signifyd
Browse files Browse the repository at this point in the history
Fix empty public order id during capture/refund/cancel the order.
  • Loading branch information
p-bystritsky authored Oct 26, 2023
2 parents 0d3cb3f + 53e74d4 commit e7d0a8a
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions Model/Payment/Gateway/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Bold\Checkout\Model\Payment\Gateway;

use Bold\Checkout\Api\Http\ClientInterface;
use Bold\Checkout\Model\Order\OrderExtensionDataFactory;
use Bold\Checkout\Model\ResourceModel\Order\OrderExtensionData;
use Exception;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Math\Random;
Expand Down Expand Up @@ -33,14 +35,32 @@ class Service
*/
private $random;

/**
* @var OrderExtensionDataFactory
*/
private $orderExtensionDataFactory;

/**
* @var OrderExtensionData
*/
private $orderExtensionDataResource;

/**
* @param ClientInterface $httpClient
* @param Random $random
* @param OrderExtensionDataFactory $orderExtensionDataFactory
* @param OrderExtensionData $orderExtensionDataResource
*/
public function __construct(ClientInterface $httpClient, Random $random)
{
public function __construct(
ClientInterface $httpClient,
Random $random,
OrderExtensionDataFactory $orderExtensionDataFactory,
OrderExtensionData $orderExtensionDataResource
) {
$this->httpClient = $httpClient;
$this->random = $random;
$this->orderExtensionDataFactory = $orderExtensionDataFactory;
$this->orderExtensionDataResource = $orderExtensionDataResource;
}

/**
Expand All @@ -58,7 +78,7 @@ public function captureFull(OrderInterface $order): string
'reauth' => true,
'idempotent_key' => $this->random->getRandomString(10),
];
$url = sprintf(self::CAPTURE_FULL_URL, $order->getExtensionAttributes()->getPublicId());
$url = sprintf(self::CAPTURE_FULL_URL, $this->getOrderPublicId($order));
return $this->sendCaptureRequest($websiteId, $url, $body);
}

Expand All @@ -74,14 +94,12 @@ public function capturePartial(OrderInterface $order, float $amount): string
{
$websiteId = (int)$order->getStore()->getWebsiteId();
$this->keepTransactionAdditionalData($order);
$orderPublicId = $order->getExtensionAttributes()->getPublicId();
$body = [
'reauth' => true,
'amount' => $amount * 100,
'idempotent_key' => $this->random->getRandomString(10),
];
$url = sprintf(self::CAPTURE_PARTIALLY_URL, $orderPublicId);

$url = sprintf(self::CAPTURE_PARTIALLY_URL, $this->getOrderPublicId($order));
return $this->sendCaptureRequest($websiteId, $url, $body);
}

Expand All @@ -97,8 +115,7 @@ public function cancel(OrderInterface $order, string $operation = self::CANCEL)
{
$websiteId = (int)$order->getStore()->getWebsiteId();
$this->keepTransactionAdditionalData($order);
$orderPublicId = $order->getExtensionAttributes()->getPublicId();
$url = sprintf(self::CANCEL_URL, $orderPublicId);
$url = sprintf(self::CANCEL_URL, $this->getOrderPublicId($order));
$body = [
'reason' => $operation === self::CANCEL
? __('Order has been canceled.')
Expand Down Expand Up @@ -131,12 +148,11 @@ public function refundFull(OrderInterface $order): string
{
$websiteId = (int)$order->getStore()->getWebsiteId();
$this->keepTransactionAdditionalData($order);
$orderPublicId = $order->getExtensionAttributes()->getPublicId();
$body = [
'email_notification' => false,
'reason' => 'Magento credit memo created.',
];
$url = sprintf(self::REFUND_FULL_URL, $orderPublicId);
$url = sprintf(self::REFUND_FULL_URL, $this->getOrderPublicId($order));
return $this->sendRefundRequest($websiteId, $url, $body);
}

Expand All @@ -152,13 +168,12 @@ public function refundPartial(OrderInterface $order, float $amount): string
{
$websiteId = (int)$order->getStore()->getWebsiteId();
$this->keepTransactionAdditionalData($order);
$orderPublicId = $order->getExtensionAttributes()->getPublicId();
$body = [
'email_notification' => false,
'reason' => 'Magento credit memo created.',
'amount' => $amount * 100,
];
$url = sprintf(self::REFUND_PARTIALLY_URL, $orderPublicId);
$url = sprintf(self::REFUND_PARTIALLY_URL, $this->getOrderPublicId($order));
return $this->sendRefundRequest($websiteId, $url, $body);
}

Expand Down Expand Up @@ -231,4 +246,21 @@ private function keepTransactionAdditionalData(OrderInterface $order): void
$order->getPayment()->setTransactionAdditionalInfo($key, $value);
}
}

/**
* Retrieve order public id.
*
* @param OrderInterface $order
* @return string|null
*/
private function getOrderPublicId(OrderInterface $order): ?string
{
$publicId = $order->getExtensionAttributes()->getPublicId();
if (!$publicId) {
$orderExtensionData = $this->orderExtensionDataFactory->create();
$this->orderExtensionDataResource->load($orderExtensionData, $order->getId(), OrderExtensionData::ORDER_ID);
$publicId = $orderExtensionData->getPublicId();
}
return $publicId;
}
}

0 comments on commit e7d0a8a

Please sign in to comment.