Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-9602]Adding Uplift API keys #2909

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Gateway/Request/PaymentDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Adyen\Exception\MissingDataException;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Requests;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Payment\Gateway\Helper\SubjectReader;
Expand All @@ -31,18 +32,26 @@ class PaymentDataBuilder implements BuilderInterface
*/
private $chargedCurrency;

/**
* @var CheckoutSession
*/
private CheckoutSession $checkoutSession;

/**
* PaymentDataBuilder constructor.
*
* @param Requests $adyenRequestsHelper
* @param ChargedCurrency $chargedCurrency
* @param CheckoutSession $checkoutSession
*/
public function __construct(
Requests $adyenRequestsHelper,
ChargedCurrency $chargedCurrency
ChargedCurrency $chargedCurrency,
CheckoutSession $checkoutSession
) {
$this->adyenRequestsHelper = $adyenRequestsHelper;
$this->chargedCurrency = $chargedCurrency;
$this->checkoutSession = $checkoutSession;
}

/**
Expand All @@ -55,20 +64,23 @@ public function build(array $buildSubject): array
{
/** @var PaymentDataObject $paymentDataObject */
$paymentDataObject = SubjectReader::readPayment($buildSubject);

$order = $paymentDataObject->getOrder();
$payment = $paymentDataObject->getPayment();
$fullOrder = $payment->getOrder();

$quote = $this->checkoutSession->getQuote();
$amountCurrency = $this->chargedCurrency->getOrderAmountCurrency($fullOrder);
$currencyCode = $amountCurrency->getCurrencyCode();
$amount = $amountCurrency->getAmount();
$reference = $order->getOrderIncrementId();
$reference = $fullOrder->getIncrementId();

$shopperConversionIdData = $quote->getPayment()->getAdditionalInformation('shopper_conversion_id');

$shopperConversionId = !empty($shopperConversionIdData) ? (string) json_decode($shopperConversionIdData, true) : '';

$request['body'] = $this->adyenRequestsHelper->buildPaymentData(
$amount,
$currencyCode,
$reference,
$shopperConversionId,
[]
);

Expand Down
52 changes: 52 additions & 0 deletions Helper/GenerateShopperConversionId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Adyen\Payment\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Adyen\Payment\Helper\Util\Uuid;
use Magento\Checkout\Model\Session as CheckoutSession;

class GenerateShopperConversionId extends AbstractHelper
{
const SHOPPER_CONVERSION_ID = 'shopper_conversion_id';

/**
* @var CheckoutSession
*/
private $checkoutSession;

/**
* @param Context $context
* @param CheckoutSession $checkoutSession
*/
public function __construct(
Context $context,
CheckoutSession $checkoutSession,
) {
parent::__construct($context);
$this->checkoutSession = $checkoutSession;
}

/**
* Get or generate a ShopperConversionID
*
* @return string
*/
public function getShopperConversionId(): string
{
$shopperConversionId = Uuid::generateV4();

$quote = $this->checkoutSession->getQuote();
$payment = $quote->getPayment();

// Store shopperConversionId in additional information
$payment->setAdditionalInformation(self::SHOPPER_CONVERSION_ID, json_encode($shopperConversionId));

// Save the quote to persist additional_information
$quote->setPayment($payment);
$quote->save();

return $shopperConversionId;
}
}
31 changes: 30 additions & 1 deletion Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Magento\Store\Model\Store;
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Checkout\Model\Session as CheckoutSession;

class PaymentMethods extends AbstractHelper
{
Expand Down Expand Up @@ -171,6 +172,16 @@ class PaymentMethods extends AbstractHelper
*/
private SearchCriteriaBuilder $searchCriteriaBuilder;

/**
* @var GenerateShopperConversionId
*/
private GenerateShopperConversionId $generateShopperConversionId;

/**
* @var CheckoutSession
*/
private CheckoutSession $checkoutSession;

/**
* @param Context $context
* @param CartRepositoryInterface $quoteRepository
Expand All @@ -190,6 +201,8 @@ class PaymentMethods extends AbstractHelper
* @param Data $adyenDataHelper
* @param PaymentTokenRepositoryInterface $paymentTokenRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param GenerateShopperConversionId $generateShopperConversionId
* @param CheckoutSession $checkoutSession
*/
public function __construct(
Context $context,
Expand All @@ -209,7 +222,9 @@ public function __construct(
SerializerInterface $serializer,
AdyenDataHelper $adyenDataHelper,
PaymentTokenRepositoryInterface $paymentTokenRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
SearchCriteriaBuilder $searchCriteriaBuilder,
GenerateShopperConversionId $generateShopperConversionId,
CheckoutSession $checkoutSession,
) {
parent::__construct($context);
$this->quoteRepository = $quoteRepository;
Expand All @@ -229,6 +244,8 @@ public function __construct(
$this->adyenDataHelper = $adyenDataHelper;
$this->paymentTokenRepository = $paymentTokenRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->generateShopperConversionId = $generateShopperConversionId;
$this->checkoutSession = $checkoutSession;
}

/**
Expand Down Expand Up @@ -581,6 +598,18 @@ protected function getPaymentMethodsRequest(
$this->adyenDataHelper->padShopperReference($this->getCurrentShopperReference());
}

$quote = $this->checkoutSession->getQuote();

$shopperConversionIdData = $quote->getPayment()->getAdditionalInformation('shopper_conversion_id');

$shopperConversionId = !empty($shopperConversionIdData) ? (string) json_decode($shopperConversionIdData, true) : '';

if(!empty($shopperConversionId)) {
$shopperConversionId = $this->generateShopperConversionId->getShopperConversionId();
$paymentMethodRequest["shopperConversionId"] = $shopperConversionId;
}


$amountValue = $this->adyenHelper->formatAmount($this->getCurrentPaymentAmount(), $currencyCode);

if (!empty($amountValue)) {
Expand Down
9 changes: 6 additions & 3 deletions Helper/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Adyen\Payment\Model\Config\Source\CcType;
use Adyen\Payment\Model\Ui\AdyenCcConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
use Adyen\Util\Uuid;
use Adyen\Payment\Helper\Util\Uuid;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Request\Http as Http;

Expand Down Expand Up @@ -276,20 +276,23 @@ public function buildAddressData($billingAddress, $shippingAddress, $storeId, $r
}

/**
* @param array $request
* @param $amount
* @param $currencyCode
* @param $reference
* @param $shopperConversionId
* @param array $request
* @return array
*/
public function buildPaymentData($amount, $currencyCode, $reference, array $request = [])
public function buildPaymentData($amount, $currencyCode, $reference, $shopperConversionId,array $request = []): array
{
var_dump($amount);
$request['amount'] = [
'currency' => $currencyCode,
'value' => $this->adyenHelper->formatAmount($amount, $currencyCode)
];

$request["reference"] = $reference;
$request["shopperConversionId"] = $shopperConversionId;

return $request;
}
Expand Down
49 changes: 49 additions & 0 deletions Helper/Util/Uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Adyen\Payment\Helper\Util;

use RuntimeException;

/**
* Class Uuid
*
* Utility class for generating UUID version 4.
*
* @package Adyen\Payment\Helper\Util
*/
class Uuid
{
/**
* Generate a UUID v4 (random-based).
*
* A UUID v4 is a universally unique identifier that is generated using
* random numbers. It follows the RFC 4122 standard.
*
* @return string A randomly generated UUID v4.
* @throws RuntimeException If secure random generation fails.
*/
public static function generateV4(): string
{
try {
$random = random_bytes(16);
} catch (\Exception $e) {
throw new RuntimeException('Failed to generate a secure UUID: ' . $e->getMessage(), 0, $e);
}

// Set the version to 4 (0100)
$random[6] = chr((ord($random[6]) & 0x0f) | 0x40);

// Set the variant to RFC 4122 (10xx)
$random[8] = chr((ord($random[8]) & 0x3f) | 0x80);

// Convert binary to hexadecimal and format as UUID
return sprintf(
'%08s-%04s-%04s-%04s-%12s',
bin2hex(substr($random, 0, 4)),
bin2hex(substr($random, 4, 2)),
bin2hex(substr($random, 6, 2)),
bin2hex(substr($random, 8, 2)),
bin2hex(substr($random, 10, 6))
);
}
}
Loading
Loading