[API] Fix configuration of Payment Request resource

This commit is contained in:
Grzegorz Sadowski 2024-10-11 10:35:51 +02:00
parent fd2b99e775
commit 105a196e3f
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
4 changed files with 147 additions and 76 deletions

View file

@ -25,7 +25,11 @@
>
<normalizationContext>
<values>
<value>sylius:shop:payment_request:show</value>
<value name="groups">
<values>
<value>sylius:shop:payment_request:show</value>
</values>
</value>
</values>
</normalizationContext>
</operation>
@ -40,17 +44,29 @@
>
<normalizationContext>
<values>
<value>sylius:shop:payment_request:show</value>
<value name="groups">
<values>
<value>sylius:shop:payment_request:show</value>
</values>
</value>
</values>
</normalizationContext>
<denormalizationContext>
<values>
<value>sylius:shop:payment_request:create</value>
<value name="groups">
<values>
<value>sylius:shop:payment_request:create</value>
</values>
</value>
</values>
</denormalizationContext>
<validationContext>
<values>
<value>sylius</value>
<value name="groups">
<values>
<value>sylius</value>
</values>
</value>
</values>
</validationContext>
</operation>
@ -65,17 +81,29 @@
>
<normalizationContext>
<values>
<value>sylius:shop:payment_request:show</value>
<value name="groups">
<values>
<value>sylius:shop:payment_request:show</value>
</values>
</value>
</values>
</normalizationContext>
<denormalizationContext>
<values>
<value>sylius:shop:payment_request:update</value>
<value name="groups">
<values>
<value>sylius:shop:payment_request:update</value>
</values>
</value>
</values>
</denormalizationContext>
<validationContext>
<values>
<value>sylius</value>
<value name="groups">
<values>
<value>sylius</value>
</values>
</value>
</values>
</validationContext>
</operation>

View file

@ -27,7 +27,7 @@ sylius:
not_available: 'The payment method %name% is not available for this order. Please choose another one.'
not_exist: 'The payment method with %code% code does not exist.'
payment_request:
action_not_available: 'The payment request (method code "%code%" and payment id "%id%") has no handler. Please choose another payment method.'
action_not_available: 'The payment request (method code: %code% and payment id: %id%) has no handler. Please choose another payment method.'
not_available: 'The payment request is not supporting this action.'
product:
not_exist: 'The product %productName% does not exist.'

View file

@ -43,8 +43,8 @@ final class ChosenPaymentRequestActionEligibilityValidator extends ConstraintVal
$gatewayFactoryCommandProvider = $this->getCommandProvider($value);
if (null === $gatewayFactoryCommandProvider) {
$this->context->addViolation($constraint->notAvailable, [
'%code%' => $value->getPaymentMethodCode(),
'%id%' => $value->getPaymentId(),
'%code%' => $value->paymentMethodCode,
'%id%' => $value->paymentId,
]);
}
@ -52,12 +52,15 @@ final class ChosenPaymentRequestActionEligibilityValidator extends ConstraintVal
return;
}
$actionsCommandProvider = $gatewayFactoryCommandProvider->getCommandProvider($value->getAction());
$actionsCommandProvider = $gatewayFactoryCommandProvider->getCommandProvider($value->action);
if (null !== $actionsCommandProvider) {
return;
}
$this->context->addViolation($constraint->notAvailable);
$this->context->addViolation($constraint->notAvailable, [
'%code%' => $value->paymentMethodCode,
'%id%' => $value->paymentId,
]);
}
private function getCommandProvider(AddPaymentRequest $addPaymentRequest): ?PaymentRequestCommandProviderInterface

View file

@ -93,39 +93,6 @@ final class PaymentRequestsTest extends JsonApiTestCase
);
}
public function createPaymentRequestProvider(): iterable
{
$environment = getenv('APP_ENV');
if ($environment === 'test_cached_payum') {
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'cart.yaml',
'country.yaml',
'shipping_method.yaml',
'payment_method.yaml',
],
'shop/payment_request/post_payment_request_payum',
];
return;
}
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'cart.yaml',
'country.yaml',
'shipping_method.yaml',
'payment_method.yaml',
],
'shop/payment_request/post_payment_request',
];
}
/** @test */
public function it_does_not_create_a_payment_request_without_required_data(): void
{
@ -154,6 +121,46 @@ final class PaymentRequestsTest extends JsonApiTestCase
);
}
/** @test */
public function it_does_not_create_a_payment_request_with_not_existent_action(): void
{
$this->loadFixturesFromFiles([
'authentication/shop_user.yaml',
'channel/channel.yaml',
'cart.yaml',
'country.yaml',
'shipping_method.yaml',
'payment_method.yaml',
]);
$order = $this->placeOrder('nAWw2jewpA', 'oliver@doe.com');
$payment = $order->getLastPayment();
$this->placeOrder('test', 'oliver@doe.com');
$this->client->request(
method: 'POST',
uri: '/api/v2/shop/payment-requests',
server: $this->headerBuilder()->withJsonLdAccept()->withJsonLdContentType()->withShopUserAuthorization('oliver@doe.com')->build(),
content: json_encode([
'paymentId' => $payment->getId(),
'paymentMethodCode' => $payment->getMethod()->getCode(),
'action' => 'invalid_action',
'payload' => [
'target_path' => 'https://myshop.tld/target-path',
'after_path' => 'https://myshop.tld/after-path',
],
], \JSON_THROW_ON_ERROR),
);
$this->assertResponseViolations(
$this->client->getResponse(),
[
['propertyPath' => '', 'message' => sprintf('The payment request (method code: %s and payment id: %d) has no handler. Please choose another payment method.', $payment->getMethod()->getCode(), $payment->getId())],
],
);
}
/**
* @test
*
@ -187,37 +194,6 @@ final class PaymentRequestsTest extends JsonApiTestCase
$this->assertResponseSuccessful($responsePath);
}
public function updatePaymentRequestProvider(): iterable
{
$environment = getenv('APP_ENV');
if ($environment === 'test_cached_payum') {
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'payment_method.yaml',
'payment_request/payment_request_payum.yaml',
'payment_request/order_with_customer.yaml',
],
'shop/payment_request/put_payment_request_payum',
];
return;
}
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'payment_method.yaml',
'payment_request/payment_request.yaml',
'payment_request/order_with_customer.yaml',
],
'shop/payment_request/put_payment_request',
];
}
/** @test */
public function it_does_not_update_a_payment_request_in_wrong_state(): void
{
@ -249,4 +225,68 @@ final class PaymentRequestsTest extends JsonApiTestCase
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
}
public function createPaymentRequestProvider(): iterable
{
$environment = getenv('APP_ENV');
if ($environment === 'test_cached_payum') {
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'cart.yaml',
'country.yaml',
'shipping_method.yaml',
'payment_method.yaml',
],
'shop/payment_request/post_payment_request_payum',
];
return;
}
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'cart.yaml',
'country.yaml',
'shipping_method.yaml',
'payment_method.yaml',
],
'shop/payment_request/post_payment_request',
];
}
public function updatePaymentRequestProvider(): iterable
{
$environment = getenv('APP_ENV');
if ($environment === 'test_cached_payum') {
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'payment_method.yaml',
'payment_request/payment_request_payum.yaml',
'payment_request/order_with_customer.yaml',
],
'shop/payment_request/put_payment_request_payum',
];
return;
}
yield [
[
'authentication/shop_user.yaml',
'channel/channel.yaml',
'payment_method.yaml',
'payment_request/payment_request.yaml',
'payment_request/order_with_customer.yaml',
],
'shop/payment_request/put_payment_request',
];
}
}