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-9196] Add optional quoteId to getShopperReference to allow tokenization for guest shoppers #2620

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion Gateway/Request/AdditionalDataLevel23DataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function build(array $buildSubject)

$prefix = 'enhancedSchemeData';
$requestBody['additionalData'][$prefix . '.totalTaxAmount'] = $this->adyenHelper->formatAmount($order->getTaxAmount(), $currencyCode);
$requestBody['additionalData'][$prefix . '.customerReference'] = $this->adyenRequestHelper->getShopperReference($order->getCustomerId(), $order->getIncrementId());
$requestBody['additionalData'][$prefix . '.customerReference'] = $this->adyenRequestHelper->getShopperReference($order->getCustomerId(), $order->getIncrementId(), $payment->getAdditionalInformation('shopperReference'));
if ($order->getIsNotVirtual()) {
$requestBody['additionalData'][$prefix . '.freightAmount'] = $this->adyenHelper->formatAmount($order->getBaseShippingAmount(), $currencyCode);
$requestBody['additionalData'][$prefix . '.destinationPostalCode'] = $order->getShippingAddress()->getPostcode();
Expand Down
9 changes: 6 additions & 3 deletions Helper/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function buildCustomerData(
$additionalData = null,
$request = []
) {
$request['shopperReference'] = $this->getShopperReference($customerId, $payment->getOrder()->getIncrementId());
$request['shopperReference'] = $this->getShopperReference($customerId, $payment->getOrder()->getIncrementId(), $payment->getAdditionalInformation('shopperReference'));

// In case of virtual product and guest checkout there is a workaround to get the guest's email address
if (!empty($additionalData['guestEmail'])) {
Expand Down Expand Up @@ -417,9 +417,12 @@ public function buildDonationData($buildSubject, $storeId): array
* @param string $orderIncrementId
* @return string
*/
public function getShopperReference($customerId, $orderIncrementId): string
public function getShopperReference($customerId, $orderIncrementId, $orderShopperReference): string
{
if ($customerId) {
if ($orderShopperReference) {
$shopperReference = $orderShopperReference;
}
else if ($customerId) {
$shopperReference = $this->adyenHelper->padShopperReference($customerId);
} else {
$uuid = Uuid::generateV4();
Expand Down
15 changes: 7 additions & 8 deletions Model/Api/AdyenDonations.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Adyen\Payment\Model\Sales\OrderRepository;
use Adyen\Payment\Model\Ui\AdyenCcConfigProvider;
use Adyen\Util\Uuid;
use Adyen\Payment\Helper\Requests;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
Expand All @@ -37,6 +38,7 @@ class AdyenDonations implements AdyenDonationsInterface
private Config $config;
private PaymentMethods $paymentMethodsHelper;
private OrderRepository $orderRepository;
private Requests $adyenRequestsHelper;

private $donationTryCount;

Expand All @@ -47,7 +49,8 @@ public function __construct(
ChargedCurrency $chargedCurrency,
Config $config,
PaymentMethods $paymentMethodsHelper,
OrderRepository $orderRepository
OrderRepository $orderRepository,
Requests $adyenRequestsHelper
) {
$this->commandPool = $commandPool;
$this->jsonSerializer = $jsonSerializer;
Expand All @@ -56,6 +59,7 @@ public function __construct(
$this->config = $config;
$this->paymentMethodsHelper = $paymentMethodsHelper;
$this->orderRepository = $orderRepository;
$this->adyenRequestsHelper = $adyenRequestsHelper;
}

/**
Expand Down Expand Up @@ -111,14 +115,9 @@ function ($amount) use ($formatter, $currencyCode) {
} else {
throw new LocalizedException(__('Donation failed!'));
}

$payment = $order->getPayment();
$request['shopperReference'] = $this->adyenRequestsHelper->getShopperReference($customerId, $payment->getOrder()->getIncrementId(), $payment->getAdditionalInformation('shopperReference'));
$customerId = $order->getCustomerId();
if ($customerId) {
$payload['shopperReference'] = $this->dataHelper->padShopperReference($customerId);
} else {
$guestCustomerId = $order->getIncrementId() . Uuid::generateV4();
$payload['shopperReference'] = $guestCustomerId;
}

try {
$donationsCaptureCommand = $this->commandPool->get('capture');
Expand Down
2 changes: 2 additions & 0 deletions Observer/AdyenCcDataAssignObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AdyenCcDataAssignObserver extends AbstractDataAssignObserver
const STORE_PAYMENT_METHOD = 'storePaymentMethod';
const RETURN_URL = 'returnUrl';
const RECURRING_PROCESSING_MODEL = 'recurringProcessingModel';
const SHOPPER_REFERENCE = 'shopperReference';

/**
* Approved root level keys from additional data array
Expand All @@ -46,6 +47,7 @@ class AdyenCcDataAssignObserver extends AbstractDataAssignObserver
self::CC_TYPE,
self::RETURN_URL,
self::RECURRING_PROCESSING_MODEL,
self::SHOPPER_REFERENCE,
HeaderDataBuilder::FRONTENDTYPE
];

Expand Down
2 changes: 1 addition & 1 deletion Plugin/PaymentVaultDeleteToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private function createDisableTokenRequest(PaymentTokenInterface $paymentToken):
$paymentToken->getPaymentMethodCode()
),
Requests::SHOPPER_REFERENCE =>
$this->requestsHelper->getShopperReference($paymentToken->getCustomerId(), null),
$this->requestsHelper->getShopperReference($paymentToken->getCustomerId(), null, null),
Requests::RECURRING_DETAIL_REFERENCE => $paymentToken->getGatewayToken()
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,53 @@ public function testVirtualOrder()
$this->assertEquals($expectedResult, $result);
}

public function testVirtualOrderGuest()
{
$storeId = 1;
$orderShopperReference = 123;
$currencyCode = 'USD';
$customerId = null;
$orderIncrementId = '000000123';
$shopperReference = 'guest-cart-123';
$taxAmount = 10.00;
$formattedTaxAmount = '1000';

$this->storeMock->method('getId')->willReturn($storeId);
$this->configMock->method('sendLevel23AdditionalData')->with($storeId)->willReturn(true);
$this->chargedCurrencyMock->method('getOrderAmountCurrency')->willReturn(new AdyenAmountCurrency(null, $currencyCode));
$this->adyenHelperMock->method('formatAmount')->willReturn($formattedTaxAmount);
$this->adyenRequestHelperMock->method('getShopperReference')->with(null, $orderIncrementId, $orderShopperReference)->willReturn($shopperReference);

$orderMock = $this->createConfiguredMock(Order::class, [
'getCustomerId' => $customerId,
'getIncrementId' => $orderIncrementId,
'getTaxAmount' => $taxAmount,
'getItems' => [],
'getIsNotVirtual' => false,
'getShippingAddress' => null,
'getBaseShippingAmount' => 0.00,
]);

$orderAdapterMock = $this->createMock(OrderAdapterInterface::class);
$paymentMock = $this->createMock(Payment::class);
$paymentMock->method('getOrder')->willReturn($orderMock);
$paymentMock->method('getAdditionalInformation')->willReturn($orderShopperReference);
$paymentDataObject = new PaymentDataObject($orderAdapterMock, $paymentMock);
$buildSubject = ['payment' => $paymentDataObject, 'order' => $orderMock];
$result = $this->additionalDataBuilder->build($buildSubject);

$expectedResult = [
'body' => [
'additionalData' => [
'enhancedSchemeData.totalTaxAmount' => '1000',
'enhancedSchemeData.customerReference' => $shopperReference
]
]
];

$this->assertEquals($expectedResult, $result);
}

public function testNonVirtualOrder()
{
$storeId = 1;
Expand Down
4 changes: 3 additions & 1 deletion Test/Unit/Model/Api/AdyenDonationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Helper\Requests;
use Adyen\Payment\Model\Api\AdyenDonations;
use Adyen\Payment\Model\Sales\OrderRepository;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
Expand Down Expand Up @@ -48,7 +49,8 @@ public function testDonate()
$this->createMock(ChargedCurrency::class),
$this->createMock(Config::class),
$this->createMock(PaymentMethods::class),
$orderRepositoryMock
$orderRepositoryMock,
$this->createMock(Requests::class)
])
->getMock();

Expand Down
2 changes: 2 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ input AdyenAdditionalDataCc {
stateData: String @doc(description: "JSON string of filled fields.")
returnUrl: String @doc(description: "The URL to return to in case of a redirection. The format depends on the channel. This URL can have a maximum of 1024 characters. It can include a placeholder `:merchantReference` to identify the order e.g. `https://your-company.com/checkout?shopperOrder=:merchantReference`.")
recurringProcessingModel: String @doc(description: "Recurring processing model to tokenize the payment method.")
shopperReference: String @doc(description: "Recurring processing model shopperReference value; defaults to customerID.")
}

input AdyenAdditionalData {
Expand All @@ -176,6 +177,7 @@ input AdyenAdditionalData {
guestEmail: String @doc(description: "Email address if customer is guest.")
returnUrl: String @doc(description: "The URL to return to in case of a redirection. The format depends on the channel. This URL can have a maximum of 1024 characters. It can include a placeholder `:merchantReference` to identify the order e.g. `https://your-company.com/checkout?shopperOrder=:merchantReference`.")
recurringProcessingModel: String @doc(description: "Recurring processing model to tokenize the payment method.")
shopperReference: String @doc(description: "Recurring processing model shopperReference value; defaults to customerID.")
}

input AdyenAdditionalDataPosCloud {
Expand Down
Loading