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

keep customer ip #38

Merged
merged 1 commit into from
May 24, 2024
Merged
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
40 changes: 27 additions & 13 deletions src/Action/ConvertPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,9 @@ public function execute($request): void
/** @var PaymentInterface $payment */
$payment = $request->getSource();
$details = Model::ensureArrayObject($payment->getDetails());
$details->replace(
[
ModelFields::TOTAL_AMOUNT => $payment->getTotalAmount(),
ModelFields::CURRENCY => $payment->getCurrencyCode(),
ModelFields::EXT_ORDER_ID => $details->extOrderId() ?? $payment->getNumber(),
ModelFields::DESCRIPTION => $payment->getDescription(),
ModelFields::CLIENT_EMAIL => $payment->getClientEmail(),
ModelFields::CLIENT_ID => $payment->getClientId(),
ModelFields::CUSTOMER_IP => $this->userIpService->getIp(),
ModelFields::CREDIT_CARD_MASKED_NUMBER => $payment->getCreditCard() ? $payment->getCreditCard()->getMaskedNumber() : null,
ModelFields::VALIDITY_TIME => $details->validityTime() ?? self::DEFAULT_VALIDITY_TIME,
]
);

$this->replaceModelFields($details, $payment);

if ($payment instanceof Payment) {
$details->setConfigKey($payment->getConfigKey());

Expand Down Expand Up @@ -105,4 +95,28 @@ public function supports($request): bool
&& $request->getSource() instanceof PaymentInterface
&& 'array' === $request->getTo();
}

private function replaceModelFields(Model $details, PaymentInterface $payment): void
{
if (null === $details->customerIp()) {
$details->replace(
[
ModelFields::CUSTOMER_IP => $this->userIpService->getIp(),
]
);
}

$details->replace(
[
ModelFields::TOTAL_AMOUNT => $payment->getTotalAmount(),
ModelFields::CURRENCY => $payment->getCurrencyCode(),
ModelFields::EXT_ORDER_ID => $details->extOrderId() ?? $payment->getNumber(),
ModelFields::DESCRIPTION => $payment->getDescription(),
ModelFields::CLIENT_EMAIL => $payment->getClientEmail(),
ModelFields::CLIENT_ID => $payment->getClientId(),
ModelFields::CREDIT_CARD_MASKED_NUMBER => $payment->getCreditCard() ? $payment->getCreditCard()->getMaskedNumber() : null,
ModelFields::VALIDITY_TIME => $details->validityTime() ?? self::DEFAULT_VALIDITY_TIME,
]
);
}
}
76 changes: 74 additions & 2 deletions tests/Integration/Action/ConvertActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Answear\Payum\Model\PaidForInterface;
use Answear\Payum\PayU\Action\ConvertPaymentAction;
use Answear\Payum\PayU\Enum\ModelFields;
use Answear\Payum\PayU\Service\UserIpService;
use Answear\Payum\PayU\Tests\Payment;
use Payum\Core\Model\CreditCard;
Expand Down Expand Up @@ -41,13 +42,13 @@ public function convertWithLowInfoTest(): void

self::assertSame(
[
'customerIp' => null,
'totalAmount' => null,
'currencyCode' => null,
'extOrderId' => null,
'description' => null,
'clientEmail' => null,
'clientId' => null,
'customerIp' => null,
'creditCardMaskedNumber' => null,
'validityTime' => 259200,
'configKey' => 'pos2',
Expand Down Expand Up @@ -107,13 +108,84 @@ public function convertWithFullDataTest(): void

self::assertSame(
[
'customerIp' => '127.0.0.1',
'totalAmount' => 1001,
'currencyCode' => 'PLN',
'extOrderId' => 'extOrderId',
'description' => 'Some description',
'clientEmail' => '[email protected]',
'clientId' => '2874',
'creditCardMaskedNumber' => $creditCard->getMaskedNumber(),
'validityTime' => 259200,
'configKey' => 'pos2',
'buyer' => [
'email' => '[email protected]',
'firstName' => 'Firstname',
'lastName' => 'Surname',
'phone' => '123123123',
'customerId' => null,
'extCustomerId' => '2874',
'nin' => null,
'language' => 'pl',
'delivery' => null,
],
'status' => 'NEW',
],
$convert->getResult()
);
}

/**
* @test
*/
public function convertButKeepCustomerIp(): void
{
$convertAction = new ConvertPaymentAction(new UserIpService());

$_SERVER['REMOTE_ADDR'] = '127.0.0.1';

$creditCard = new CreditCard();
$creditCard->setMaskedNumber('masked-number');

$paidFor = $this->createMock(PaidForInterface::class);
$paidFor->method('getEmail')
->willReturn('[email protected]');
$paidFor->method('getFirstName')
->willReturn('Firstname');
$paidFor->method('getSurname')
->willReturn('Surname');
$paidFor->method('getPhone')
->willReturn('123123123');

$payment = new Payment();
$payment->setTotalAmount(1001);
$payment->setCurrencyCode('PLN');
$payment->setNumber('extOrderId');
$payment->setDescription('Some description');
$payment->setClientEmail('[email protected]');
$payment->setClientId('2874');
$payment->setCreditCard($creditCard);
$payment->setLanguage('pl');
$payment->setConfigKey('pos2');
$payment->setPaidFor($paidFor);
$payment->setDetails(
[
ModelFields::CUSTOMER_IP => '111.222.333.444'
]
);
$convert = new Convert($payment, 'array');

$convertAction->execute($convert);

self::assertSame(
[
'customerIp' => '111.222.333.444',
'totalAmount' => 1001,
'currencyCode' => 'PLN',
'extOrderId' => 'extOrderId',
'description' => 'Some description',
'clientEmail' => '[email protected]',
'clientId' => '2874',
'customerIp' => '127.0.0.1',
'creditCardMaskedNumber' => $creditCard->getMaskedNumber(),
'validityTime' => 259200,
'configKey' => 'pos2',
Expand Down
Loading