Skip to content

Commit

Permalink
Merge pull request #32 from answear/pay-method-on-payment
Browse files Browse the repository at this point in the history
Pay method on Payment
  • Loading branch information
lukasz-falda authored Nov 15, 2023
2 parents 3d7341c + c9c4d3d commit dc2b293
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 13 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
],
'sort_algorithm' => 'alpha',
],
'nullable_type_declaration_for_default_null_value' => true,
]
);
23 changes: 23 additions & 0 deletions core/Action/Request/Capture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Answear\Payum\Action\Request;

use Answear\Payum\PayU\ValueObject\Request\Order\PayMethod;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Security\TokenInterface;

class Capture extends \Payum\Core\Request\Capture
{
public function __construct(
TokenInterface $captureToken,
PaymentInterface $payment,
public readonly ?PayMethod $payMethod = null,
) {
parent::__construct($captureToken);

$this->setModel($payment);
$this->setModel($payment->getDetails());
}
}
13 changes: 9 additions & 4 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function execute($request): void
}

$configKey = PaymentHelper::getConfigKey($model, $firstModel);
$orderRequest = $this->prepareOrderRequest($token, $model);
$orderRequest = $this->prepareOrderRequest($request, $token, $model);
if (RecurringEnum::Standard === $model->recurring()) {
$this->setRecurringStandardPayment($orderRequest, $model, $configKey);
}
Expand All @@ -69,7 +69,7 @@ public function execute($request): void
$this->updatePayment($model, $orderCreatedResponse, $firstModel, $token);
$request->setModel($model);

throw new HttpRedirect($orderCreatedResponse->redirectUri ?? $token->getTargetUrl());
throw new HttpRedirect($orderCreatedResponse->redirectUri ?? $token->getAfterUrl());
}

if (StatusCode::WarningContinue3ds === $orderCreatedResponse->status->statusCode) {
Expand Down Expand Up @@ -125,8 +125,13 @@ private function updatePayment(
/** Payment will be auto-updated on @see \Payum\Core\Extension\StorageExtension::onPostExecute */
}

private function prepareOrderRequest(TokenInterface $token, Model $model): OrderRequest
private function prepareOrderRequest(Capture $request, TokenInterface $token, Model $model): OrderRequest
{
$payMethod = $model->payMethod();
if ($request instanceof \Answear\Payum\Action\Request\Capture && null !== $request->payMethod) {
$payMethod = $request->payMethod;
}

return new OrderRequest(
$model->description(),
$model->currencyCode(),
Expand All @@ -144,7 +149,7 @@ private function prepareOrderRequest(TokenInterface $token, Model $model): Order
$model->extOrderId(),
$token->getAfterUrl(),
$model->buyer(),
$model->payMethod(),
$payMethod,
$model->additionalDescription(),
$model->visibleDescription(),
$model->statementDescription()
Expand Down
4 changes: 2 additions & 2 deletions src/Action/StatusAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function execute($request): void
public function supports($request): bool
{
return
$request instanceof GetStatusInterface &&
$request->getModel() instanceof \ArrayAccess;
$request instanceof GetStatusInterface
&& $request->getModel() instanceof \ArrayAccess;
}
}
4 changes: 2 additions & 2 deletions src/Action/SyncPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function execute($request): void
public function supports($request): bool
{
return
$request instanceof SyncPayment &&
$request->getModel() instanceof PaymentInterface;
$request instanceof SyncPayment
&& $request->getModel() instanceof PaymentInterface;
}

protected function updatePayment(PaymentInterface $payment, Model $model): void
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';
}
2 changes: 2 additions & 0 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ 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,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/ValueObject/Response/OrderCreated/StatusCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ enum StatusCode: string
case WarningContinue3ds = 'WARNING_CONTINUE_3DS';
case WarningContinueCVV = 'WARNING_CONTINUE_CVV';
case Error_syntax = 'ERROR_SYNTAX';
case ErrorAuthorizationCode = 'ERROR_AUTHORIZATION_CODE';
case ErrorValueInvalid = 'ERROR_VALUE_INVALID';
case ErrorValueMissing = 'ERROR_VALUE_MISSING';
case ErrorToken = 'ERROR_TOKEN';
case ErrorOrderNotUnique = 'ERROR_ORDER_NOT_UNIQUE';
case ErrorInternal = 'ERROR_INTERNAL';
case BusinessError = 'BUSINESS_ERROR';
Expand Down
4 changes: 2 additions & 2 deletions src/ValueObject/Response/OrderCreatedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OrderCreatedResponse
{
public function __construct(
public readonly OrderCreatedStatus $status,
public readonly string $redirectUri,
public readonly ?string $redirectUri,
public readonly string $orderId,
public readonly ?string $extOrderId = null,
public readonly ?array $payMethods = null,
Expand All @@ -21,7 +21,7 @@ public static function fromResponse(array $response): self
{
return new self(
OrderCreatedStatus::fromResponse($response['status']),
$response['redirectUri'],
$response['redirectUri'] ?? null,
$response['orderId'],
$response['extOrderId'] ?? null,
$response['payMethods'] ?? null
Expand Down
59 changes: 56 additions & 3 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 All @@ -33,7 +36,9 @@ class CaptureActionTest extends TestCase
*/
public function captureTest(): void
{
$captureAction = $this->getCaptureAction();
$captureAction = $this->getCaptureAction(
expectedCreateRequest: FileTestUtil::decodeJsonFromFile(__DIR__ . '/data/expectedOrderRequest.json')
);

$captureToken = new Token();
$capture = new Capture($captureToken);
Expand All @@ -51,6 +56,38 @@ public function captureTest(): void
self::assertTrue($redirected);
}

/**
* @test
*/
public function captureWithPayMethodTest(): void
{
$captureAction = $this->getCaptureAction(
expectedCreateRequest: FileTestUtil::decodeJsonFromFile(__DIR__ . '/data/expectedOrderRequestWithPayMethod.json')
);

$payment = new \Answear\Payum\PayU\Tests\Payment();
$payment->setDetails(FileTestUtil::decodeJsonFromFile(__DIR__ . '/data/details.json'));

$payMethod = new PayMethod(
PayMethodType::Pbl,
'some value',
'some authorisation code'
);

$captureToken = new Token();
$capture = new \Answear\Payum\Action\Request\Capture($captureToken, $payment, $payMethod);

$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 +192,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,
?array $expectedCreateRequest = null
): CaptureAction {
$response = $response ?? new OrderCreatedResponse(
new OrderCreatedStatus(
StatusCode::Success,
Expand All @@ -169,6 +209,19 @@ private function getCaptureAction(?OrderCreatedResponse $response = null, ?array

$orderRequestService = $this->createMock(OrderRequestService::class);
$orderRequestService->method('create')
->with(
$this->callback(
static function (OrderRequest $createRequest) use ($expectedCreateRequest) {

if ($expectedCreateRequest) {
$jsonE = json_encode($createRequest->toArray('posId'));
self::assertSame($createRequest->toArray('posId'), $expectedCreateRequest);
}

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

$captureAction = new CaptureAction(
Expand Down
37 changes: 37 additions & 0 deletions tests/Integration/Action/data/expectedOrderRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"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,
"cardOnFile": null,
"recurring": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"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": {
"payMethod": {
"type": "PBL",
"value": "some value",
"authorizationCode": "some authorisation code",
"specificData": null
}
},
"cardOnFile": null,
"recurring": null
}

0 comments on commit dc2b293

Please sign in to comment.