Skip to content

Commit

Permalink
Pay method on Payment
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-falda committed Nov 6, 2023
1 parent 3d7341c commit 657a92e
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 6 deletions.
4 changes: 4 additions & 0 deletions core/Model/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Answear\Payum\Model;

use Answear\Payum\PayU\ValueObject\Request\Order\PayMethod;

abstract class Payment extends \Payum\Core\Model\Payment
{
abstract public function getGatewayName(): string;
Expand All @@ -17,4 +19,6 @@ abstract public function getConfigKey(): ?string;
abstract public function getPaidFor(): PaidForInterface;

abstract public function getLanguage(): ?string;

abstract public function getPayMethodRequest(): ?PayMethod;
}
3 changes: 2 additions & 1 deletion src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private function updatePayment(
}
}

$model->filterSensitiveData();
$payment->setDetails($model);

$status = new GetHumanStatus($token);
Expand Down Expand Up @@ -165,7 +166,7 @@ private function setRecurringStandardPayment(OrderRequest $orderRequest, Model $
$orderRequest->setRequiring($model->recurring(), new PayMethod(PayMethodType::CardToken, $cardToken['value']));
}

private function findPreferredToken(array $tokens, ?string $creditCardMaskedNumber = null): ?array
private function findPreferredToken(array $tokens, string $creditCardMaskedNumber = null): ?array
{
$tokens = array_filter(
$tokens,
Expand Down
11 changes: 11 additions & 0 deletions src/Action/ConvertPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public function execute($request): void
);

$details->setBuyer($buyer);

$payMethod = $payment->getPayMethodRequest();
if (null !== $payMethod) {
$details->replace(
[
ModelFields::PAY_METHODS => [
ModelFields::PAY_METHOD => $payMethod->toArray(),
],
]
);
}
}

if (RecurringEnum::First === $details->recurring() && !empty($payment->getCreditCard()?->getToken())) {
Expand Down
2 changes: 2 additions & 0 deletions src/Enum/ModelFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum ModelFields
public const PAY_METHOD = 'payMethod';
public const PAY_METHOD_TYPE = 'type';
public const PAY_METHOD_VALUE = 'value';
public const PAY_METHOD_AUTHORIZATION_CODE = 'authorizationCode';
public const PAY_METHOD_SPECIFIC_DATA = 'specificData';
public const PRODUCT_LISTING_DATE = 'listingDate';
public const PRODUCT_VIRTUAL = 'virtual';
public const PRODUCT_QUANTITY = 'quantity';
Expand Down
1 change: 1 addition & 0 deletions src/Enum/PayMethodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ enum PayMethodType: string
case CardToken = 'CARD_TOKEN';
case Installments = 'INSTALLMENTS';
case PaymentWall = 'PAYMENT_WALL';
case BlikAuthorizationCode = 'BLIK_AUTHORIZATION_CODE';
}
22 changes: 22 additions & 0 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ public function payMethod(): ?PayMethod
return new PayMethod(
$type instanceof PayMethodType ? $type : PayMethodType::tryFrom($type),
$this[ModelFields::PAY_METHODS][ModelFields::PAY_METHOD][ModelFields::PAY_METHOD_VALUE],
$this[ModelFields::PAY_METHODS][ModelFields::PAY_METHOD][ModelFields::PAY_METHOD_AUTHORIZATION_CODE] ?? null,
$this[ModelFields::PAY_METHODS][ModelFields::PAY_METHOD][ModelFields::PAY_METHOD_SPECIFIC_DATA] ?? null,
);
}

public function filterSensitiveData(): void
{
if (!isset($this[ModelFields::PAY_METHODS])) {
return;
}

$this->replace(
[
ModelFields::PAY_METHODS => [
ModelFields::PAY_METHOD => [
ModelFields::PAY_METHOD_TYPE => $this[ModelFields::PAY_METHODS][ModelFields::PAY_METHOD][ModelFields::PAY_METHOD_TYPE],
ModelFields::PAY_METHOD_VALUE => '???',
ModelFields::PAY_METHOD_AUTHORIZATION_CODE => '???',
ModelFields::PAY_METHOD_SPECIFIC_DATA => $this[ModelFields::PAY_METHODS][ModelFields::PAY_METHOD][ModelFields::PAY_METHOD_SPECIFIC_DATA] ?? null,
],
],
]
);
}

Expand Down
73 changes: 68 additions & 5 deletions tests/Integration/Action/CaptureActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace Answear\Payum\PayU\Tests\Integration\Action;

use Answear\Payum\PayU\Action\CaptureAction;
use Answear\Payum\PayU\Enum\PayMethodType;
use Answear\Payum\PayU\Exception\PayUException;
use Answear\Payum\PayU\Model\Model;
use Answear\Payum\PayU\Request\OrderRequestService;
use Answear\Payum\PayU\Request\PayMethodsRequestService;
use Answear\Payum\PayU\Tests\Util\FileTestUtil;
use Answear\Payum\PayU\ValueObject\Request\Order\PayMethod;
use Answear\Payum\PayU\ValueObject\Request\OrderRequest;
use Answear\Payum\PayU\ValueObject\Response\OrderCreated\OrderCreatedStatus;
use Answear\Payum\PayU\ValueObject\Response\OrderCreated\StatusCode;
use Answear\Payum\PayU\ValueObject\Response\OrderCreatedResponse;
Expand Down Expand Up @@ -51,6 +54,61 @@ public function captureTest(): void
self::assertTrue($redirected);
}

/**
* @test
*/
public function captureWithPayMethodTest(): void
{
$response = new OrderCreatedResponse(
new OrderCreatedStatus(
StatusCode::Success,
'Żądanie zostało wykonane poprawnie.'
),
'http://redirect-after-create-payment.url',
'WZHF5FFDRJ140731GUEST000P01',
'vjis3d90tsozmuj0rjgs3i'
);

$expectedPayMethod = new PayMethod(
PayMethodType::Pbl,
'ap',
'authorization-code'
);

$orderRequestService = $this->createMock(OrderRequestService::class);
$orderRequestService->method('create')
->with(
self::callback(function (OrderRequest $orderRequest) use ($expectedPayMethod) {
self::assertNotNull($orderRequest->payMethod);
self::assertSame($expectedPayMethod->toArray(), $orderRequest->payMethod->toArray());

return true;
})
)
->willReturn($response);

$details = FileTestUtil::decodeJsonFromFile(__DIR__ . '/data/detailsWithPayMethod.json');

$captureAction = $this->getCaptureAction(details: $details, orderRequestService: $orderRequestService);

$captureToken = new Token();
$capture = new Capture($captureToken);
$payment = new \Answear\Payum\PayU\Tests\Payment();
$payment->storePayMethodRequest($expectedPayMethod);
$capture->setModel($payment);
$capture->setModel($details);

$redirected = false;
try {
$captureAction->execute($capture);
} catch (HttpRedirect $httpRedirect) {
$redirected = true;
self::assertSame('http://redirect-after-create-payment.url', $httpRedirect->getUrl());
}

self::assertTrue($redirected);
}

/**
* @test
*/
Expand Down Expand Up @@ -155,8 +213,11 @@ public function captureWithOrderIdFailsTest(): void
$captureAction->execute($capture);
}

private function getCaptureAction(?OrderCreatedResponse $response = null, ?array $details = null): CaptureAction
{
private function getCaptureAction(
OrderCreatedResponse $response = null,
array $details = null,
OrderRequestService $orderRequestService = null
): CaptureAction {
$response = $response ?? new OrderCreatedResponse(
new OrderCreatedStatus(
StatusCode::Success,
Expand All @@ -167,9 +228,11 @@ private function getCaptureAction(?OrderCreatedResponse $response = null, ?array
'vjis3d90tsozmuj0rjgs3i'
);

$orderRequestService = $this->createMock(OrderRequestService::class);
$orderRequestService->method('create')
->willReturn($response);
if (null === $orderRequestService) {
$orderRequestService = $this->createMock(OrderRequestService::class);
$orderRequestService->method('create')
->willReturn($response);
}

$captureAction = new CaptureAction(
$orderRequestService,
Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/Action/ConvertActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use Answear\Payum\Model\PaidForInterface;
use Answear\Payum\PayU\Action\ConvertPaymentAction;
use Answear\Payum\PayU\Enum\PayMethodType;
use Answear\Payum\PayU\Service\UserIpService;
use Answear\Payum\PayU\Tests\Payment;
use Answear\Payum\PayU\ValueObject\Request\Order\PayMethod;
use Payum\Core\Model\CreditCard;
use Payum\Core\Request\Convert;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -101,6 +103,13 @@ public function convertWithFullDataTest(): void
$payment->setLanguage('pl');
$payment->setConfigKey('pos2');
$payment->setPaidFor($paidFor);
$payment->storePayMethodRequest(
new PayMethod(
PayMethodType::Pbl,
'ap',
'authorization-code'
)
);
$convert = new Convert($payment, 'array');

$convertAction->execute($convert);
Expand Down Expand Up @@ -128,6 +137,14 @@ public function convertWithFullDataTest(): void
'language' => 'pl',
'delivery' => null,
],
'payMethods' => [
'payMethod' => [
'type' => 'PBL',
'value' => 'ap',
'authorizationCode' => 'authorization-code',
'specificData' => null,
],
],
'status' => 'NEW',
],
$convert->getResult()
Expand Down
29 changes: 29 additions & 0 deletions tests/Integration/Action/data/detailsWithPayMethod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"totalAmount": 95500,
"firstName": "Testy",
"lastName": "Mjzykdwmh",
"description": "Platnost za objedn\u00e1vku \u010d.: 221214-0026UJ-CZ",
"currencyCode": "CZK",
"language": "cs",
"validityTime": 259200,
"buyer": {
"email": "[email protected]",
"firstName": "Testy",
"lastName": "Mjzykdwmh",
"phone": "+420733999019",
"language": "cs"
},
"payMethods": {
"payMethod": {
"type": "PBL",
"value": "ap",
"authorizationCode": "authorization-code",
"specificData": null
}
},
"extOrderId": "221214-0026UJ-CZ",
"client_email": "[email protected]",
"client_id": "124077",
"customerIp": "10.0.13.152",
"creditCardMaskedNumber": null
}
12 changes: 12 additions & 0 deletions tests/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Answear\Payum\PayU\Tests;

use Answear\Payum\Model\PaidForInterface;
use Answear\Payum\PayU\ValueObject\Request\Order\PayMethod;

class Payment extends \Answear\Payum\Model\Payment
{
Expand All @@ -13,6 +14,7 @@ class Payment extends \Answear\Payum\Model\Payment
private string $configKey;
private PaidForInterface $paidFor;
private string $language;
private ?PayMethod $payMethodRequest = null;

public function getGatewayName(): string
{
Expand Down Expand Up @@ -63,4 +65,14 @@ public function setLanguage(string $language): void
{
$this->language = $language;
}

public function getPayMethodRequest(): ?PayMethod
{
return $this->payMethodRequest;
}

public function storePayMethodRequest(PayMethod $payMethod): void
{
$this->payMethodRequest = $payMethod;
}
}

0 comments on commit 657a92e

Please sign in to comment.