Skip to content
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
14 changes: 10 additions & 4 deletions Model/Client/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Address;
use Magento\Sales\Model\OrderRepository;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Payment as MolliePayment;
use Mollie\Api\Types\PaymentStatus;
Expand Down Expand Up @@ -201,10 +202,10 @@ public function __construct(

/**
* @param OrderInterface $order
* @param \Mollie\Api\MollieApiClient $mollieApi
* @param MollieApiClient $mollieApi
*
* @return string
* @throws \Mollie\Api\Exceptions\ApiException
* @throws ApiException
*/
public function startTransaction(OrderInterface $order, $mollieApi)
{
Expand Down Expand Up @@ -266,7 +267,12 @@ public function startTransaction(OrderInterface $order, $mollieApi)

public function getAddressLine(OrderAddressInterface $address): array
{
// Phone is added in \Mollie\Payment\Service\Order\TransactionPart\PhoneNumber

$output = [
'givenName' => $address->getFirstname(),
'familyName' => $address->getLastname(),
'organizationName' => $address->getCompany(),
'streetAndNumber' => rtrim(implode(' ', $address->getStreet()), ' '),
'postalCode' => $address->getPostcode(),
'city' => $address->getCity(),
Expand Down Expand Up @@ -331,13 +337,13 @@ public function processResponse(OrderInterface $order, $payment)

/**
* @param Order $order
* @param \Mollie\Api\MollieApiClient $mollieApi
* @param MollieApiClient $mollieApi
* @param string $type
* @param null $paymentToken
*
* @return array
* @throws LocalizedException
* @throws \Mollie\Api\Exceptions\ApiException
* @throws ApiException
*/
public function processTransaction(Order $order, $mollieApi, $type = 'webhook', $paymentToken = null)
{
Expand Down
40 changes: 24 additions & 16 deletions Service/Order/TransactionPart/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Mollie\Payment\Service\Order\TransactionPart;

use InvalidArgumentException;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Model\Client\Payments;
use Mollie\Payment\Model\Methods\In3;
use Mollie\Payment\Service\Order\TransactionPartInterface;

class PhoneNumber implements TransactionPartInterface
Expand Down Expand Up @@ -261,35 +261,43 @@ class PhoneNumber implements TransactionPartInterface

public function process(OrderInterface $order, $apiMethod, array $transaction)
{
if ($order->getPayment()->getMethod() != In3::CODE) {
return $transaction;
}
$transaction['billingAddress'] = $this->formatForAddress(
$order->getBillingAddress(),
$transaction['billingAddress'],
);

if ($apiMethod == Payments::CHECKOUT_TYPE) {
return $transaction;
}
$transaction['shippingAddress'] = $this->formatForAddress(
$order->getShippingAddress(),
$transaction['shippingAddress'],
);

return $transaction;
}

$address = $order->getBillingAddress();
private function formatForAddress(OrderAddressInterface $address, array $transactionAddress): array
{
$countryCode = $address->getCountryId();
$phoneNumber = $address->getTelephone();

if (empty($phoneNumber)) {
return $transaction;
if (!$countryCode || !$phoneNumber) {
return $transactionAddress;
}

try {
$transaction['billingAddress']['phone'] = $this->formatInE164($countryCode, $phoneNumber);
} catch (\InvalidArgumentException $exception) {
$transactionAddress['phone'] = $this->formatInE164($countryCode, $phoneNumber);

return $transactionAddress;
} catch (InvalidArgumentException $exception) {
// Silently ignore the exception
}

return $transaction;
return $transactionAddress;
}

private function formatInE164(string $countryCodeIso2, string $phoneNumber): string
{
if (!array_key_exists($countryCodeIso2, self::COUNTRY_CODE_MAPPING)) {
throw new \InvalidArgumentException(sprintf('Country code "%s" is not supported', $countryCodeIso2));
throw new InvalidArgumentException(sprintf('Country code "%s" is not supported', $countryCodeIso2));
}

$countryCode = self::COUNTRY_CODE_MAPPING[$countryCodeIso2];
Expand All @@ -306,7 +314,7 @@ private function formatInE164(string $countryCodeIso2, string $phoneNumber): str
$formattedNumber = '+' . $countryCode . $formattedNumber;

if (strlen($formattedNumber) <= 3 || !preg_match('/^\+[1-9]\d{1,14}$/', $formattedNumber)) {
throw new \InvalidArgumentException(__('Phone number "%s" is not valid', $formattedNumber));
throw new InvalidArgumentException(__('Phone number "%s" is not valid', $formattedNumber));
}

return $formattedNumber;
Expand Down
46 changes: 3 additions & 43 deletions Test/Integration/Service/Order/TransactionPart/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,13 @@
namespace Mollie\Payment\Test\Integration\Service\Order\TransactionPart;

use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Mollie\Payment\Model\Client\Orders;
use Mollie\Payment\Model\Client\Payments;
use Mollie\Payment\Service\Order\TransactionPart\PhoneNumber;
use Mollie\Payment\Test\Integration\IntegrationTestCase;

class PhoneNumberTest extends IntegrationTestCase
{
public function testDoesNothingWhenPaymentMethodIsNotIn3(): void
{
/** @var PhoneNumber $instance */
$instance = $this->objectManager->create(PhoneNumber::class);

$order = $this->objectManager->create(OrderInterface::class);
$order->setPayment($this->objectManager->create(OrderPaymentInterface::class));
$order->getPayment()->setMethod('mollie_methods_ideal');

$transaction = $instance->process(
$order,
Payments::CHECKOUT_TYPE,
['billingAddress' => []]
);

$this->assertArrayNotHasKey('phone', $transaction['billingAddress']);
}

public function testDoesNothingWhenPaymentsApiIsUsed(): void
{
$transaction = [];

/** @var PhoneNumber $instance */
$instance = $this->objectManager->create(PhoneNumber::class);

$order = $this->objectManager->create(OrderInterface::class);
$order->setPayment($this->objectManager->create(OrderPaymentInterface::class));
$order->getPayment()->setMethod('mollie_methods_in3');

$newTransaction = $instance->process(
$order,
Payments::CHECKOUT_TYPE,
$transaction
);

$this->assertSame($transaction, $newTransaction);
}

/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @dataProvider convertsPhoneNumbersToTheCorrectFormatDataProvider
Expand Down Expand Up @@ -77,7 +37,7 @@ public function testConvertsPhoneNumbersToTheCorrectFormat(
$transaction = $instance->process(
$order,
Orders::CHECKOUT_TYPE,
['billingAddress' => []]
['billingAddress' => [], 'shippingAddress' => []]
);

$this->assertSame($expected, $transaction['billingAddress']['phone']);
Expand All @@ -103,7 +63,7 @@ public function testDoesNotAddThePhoneNumberWhenItsEmpty(): void
$transaction = $instance->process(
$order,
Orders::CHECKOUT_TYPE,
['billingAddress' => []]
['billingAddress' => [], 'shippingAddress' => []]
);

$this->assertArrayNotHasKey('phone', $transaction['billingAddress']);
Expand All @@ -129,7 +89,7 @@ public function testHandlesNullAsPhonenumber(): void
$transaction = $instance->process(
$order,
Orders::CHECKOUT_TYPE,
['billingAddress' => []]
['billingAddress' => [], 'shippingAddress' => []]
);

$this->assertArrayNotHasKey('phone', $transaction['billingAddress']);
Expand Down
Loading