Skip to content

Commit

Permalink
Merge pull request #37 from answear/threeDsAuthentication
Browse files Browse the repository at this point in the history
support challangeRequested property on create order request
  • Loading branch information
konradkozaczenko authored May 22, 2024
2 parents 696112f + cf4ac42 commit c2ff604
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ $gateway->execute($captureRequest);

### Missing features

* `OrderRequest` params `recurring`, `mcpData`, `threeDsAuthentication`, `credit`
* `OrderRequest` params `recurring`, `mcpData`, `credit`
* ...
3 changes: 2 additions & 1 deletion src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ private function prepareOrderRequest(Capture $request, TokenInterface $token, Mo
$model->additionalDescription(),
$model->visibleDescription(),
$model->statementDescription(),
$model->cardOnFile()
$model->cardOnFile(),
threeDsAuthentication: $model->threeDsAuthentication(),
);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Enum/ChallengeRequestedType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Answear\Payum\PayU\Enum;

enum ChallengeRequestedType: string
{
case Yes = 'YES';
case No = 'NO';
case Mandate = 'MANDATE';
}
2 changes: 2 additions & 0 deletions src/Enum/ModelFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ enum ModelFields
public const EXT_REFUND_ID = 'extRefundId';
public const PROPERTIES = 'properties';
public const CARD_ON_FILE = 'cardOnFile';
public const THREE_DS_AUTHENTICATION = 'threeDsAuthentication';
public const CHALLENGE_REQUESTED = 'challangeRequested';
}
13 changes: 13 additions & 0 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Answear\Payum\PayU\Model;

use Answear\Payum\PayU\Enum\CardOnFileEnum;
use Answear\Payum\PayU\Enum\ChallengeRequestedType;
use Answear\Payum\PayU\Enum\ModelFields;
use Answear\Payum\PayU\Enum\OrderStatus;
use Answear\Payum\PayU\Enum\PayMethodType;
Expand All @@ -13,6 +14,7 @@
use Answear\Payum\PayU\ValueObject\Buyer;
use Answear\Payum\PayU\ValueObject\Product;
use Answear\Payum\PayU\ValueObject\Request\Order\PayMethod;
use Answear\Payum\PayU\ValueObject\Request\Order\ThreeDsAuthentication;
use Answear\Payum\PayU\ValueObject\Response;
use Payum\Core\Bridge\Spl\ArrayObject;

Expand Down Expand Up @@ -188,6 +190,17 @@ public function setCardOnFile(?CardOnFileEnum $cardOnFileEnum): void
$this[ModelFields::CARD_ON_FILE] = $cardOnFileEnum?->value;
}

public function threeDsAuthentication(): ?ThreeDsAuthentication
{
if (!isset($this[ModelFields::THREE_DS_AUTHENTICATION])) {
return null;
}

return new ThreeDsAuthentication(
ChallengeRequestedType::from($this[ModelFields::THREE_DS_AUTHENTICATION][ModelFields::CHALLENGE_REQUESTED])
);
}

public function setPayUResponse(Response\OrderCreatedResponse $orderCreatedResponse): void
{
$this[ModelFields::PAYU_RESPONSE] = $orderCreatedResponse->toArray();
Expand Down
25 changes: 25 additions & 0 deletions src/ValueObject/Request/Order/ThreeDsAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Answear\Payum\PayU\ValueObject\Request\Order;

use Answear\Payum\PayU\Enum\ChallengeRequestedType;

class ThreeDsAuthentication
{
public function __construct(
public readonly ChallengeRequestedType $challengeRequested,
) {
}

/**
* @return array<string, string|int|array|null>
*/
public function toArray(): array
{
return [
'challangeRequested' => $this->challengeRequested->value,
];
}
}
3 changes: 3 additions & 0 deletions src/ValueObject/Request/OrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Answear\Payum\PayU\ValueObject\Buyer;
use Answear\Payum\PayU\ValueObject\Product;
use Answear\Payum\PayU\ValueObject\Request\Order\PayMethod;
use Answear\Payum\PayU\ValueObject\Request\Order\ThreeDsAuthentication;
use Webmozart\Assert\Assert;

class OrderRequest
Expand Down Expand Up @@ -38,6 +39,7 @@ public function __construct(
public readonly ?string $statementDescription = null,
public ?CardOnFileEnum $cardOnFile = null,
public ?string $recurring = null,
public ?ThreeDsAuthentication $threeDsAuthentication = null,
) {
Assert::notEmpty($this->products);
Assert::allIsInstanceOf($this->products, Product::class);
Expand All @@ -64,6 +66,7 @@ public function toArray(string $merchantPosId): array
'buyer' => $this->buyer?->toArray(),
'products' => array_map(static fn(Product $product) => $product->toArray(), $this->products),
'payMethods' => null === $this->payMethod ? null : ['payMethod' => $this->payMethod->toArray()],
'threeDsAuthentication' => $this->threeDsAuthentication?->toArray(),
'cardOnFile' => $this->cardOnFile?->value,
'recurring' => $this->recurring,
];
Expand Down
31 changes: 31 additions & 0 deletions tests/Integration/Action/CaptureActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Answear\Payum\PayU\Tests\Integration\Action;

use Answear\Payum\PayU\Action\CaptureAction;
use Answear\Payum\PayU\Enum\ChallengeRequestedType;
use Answear\Payum\PayU\Enum\ModelFields;
use Answear\Payum\PayU\Enum\PayMethodType;
use Answear\Payum\PayU\Exception\PayUException;
use Answear\Payum\PayU\Model\Model;
Expand Down Expand Up @@ -56,6 +58,35 @@ public function captureTest(): void
self::assertTrue($redirected);
}

/**
* @test
*/
public function captureTestWithThreeDSAuthentication(): void
{
$details = FileTestUtil::decodeJsonFromFile(__DIR__ . '/data/details.json');
$details[ModelFields::THREE_DS_AUTHENTICATION][ModelFields::CHALLENGE_REQUESTED] = ChallengeRequestedType::Mandate->value;

$captureAction = $this->getCaptureAction(
expectedCreateRequest: FileTestUtil::decodeJsonFromFile(__DIR__ . '/data/expectedOrderRequestWithThreeDSAuthentication.json'),
details: $details
);

$captureToken = new Token();
$capture = new Capture($captureToken);
$capture->setModel(new \Answear\Payum\PayU\Tests\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
1 change: 1 addition & 0 deletions tests/Integration/Action/data/expectedOrderRequest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}
],
"payMethods": null,
"threeDsAuthentication": null,
"cardOnFile": null,
"recurring": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"specificData": null
}
},
"threeDsAuthentication": null,
"cardOnFile": null,
"recurring": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"extOrderId": "221214-0026UJ-CZ",
"notifyUrl": "http:\/\/notify.url",
"customerIp": "10.0.13.152",
"merchantPosId": "posId",
"validityTime": 259200,
"description": "Platnost za objedn\u00e1vku \u010d.: 221214-0026UJ-CZ",
"additionalDescription": null,
"visibleDescription": null,
"statementDescription": null,
"currencyCode": "CZK",
"totalAmount": 95500,
"continueUrl": null,
"buyer": {
"email": "[email protected]",
"firstName": "Testy",
"lastName": "Mjzykdwmh",
"phone": "+420733999019",
"customerId": null,
"extCustomerId": null,
"nin": null,
"language": "cs",
"delivery": null
},
"products": [
{
"name": "Platnost za objedn\u00e1vku \u010d.: 221214-0026UJ-CZ",
"unitPrice": 95500,
"quantity": 1,
"virtual": null,
"listingDate": null
}
],
"payMethods": null,
"threeDsAuthentication": {
"challangeRequested": "MANDATE"
},
"cardOnFile": null,
"recurring": null
}

0 comments on commit c2ff604

Please sign in to comment.