Skip to content

Commit

Permalink
Add tests for API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Jun 25, 2024
1 parent 578ac3e commit 2c7b70c
Show file tree
Hide file tree
Showing 37 changed files with 467 additions and 168 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:

-
name: Run ECS
run: vendor/bin/ecs check src
run: vendor/bin/ecs check
if: always() && steps.end-of-setup.outcome == 'success'

-
Expand Down
5 changes: 5 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
return static function (ECSConfig $ecsConfig): void {
$ecsConfig->import(__DIR__ . '/vendor/sylius-labs/coding-standard/ecs.php');

$ecsConfig->paths([
'src',
'tests/Behat',
]);

$services = $ecsConfig->services();

// PHP 7 compatibility
Expand Down
33 changes: 33 additions & 0 deletions features/shop/stripe_checkout_session/api.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@stripe_checkout_session_payment_configuration
Feature: Paying with Stripe Checkout Session during checkout
In order to buy products
As a Customer
I want to be able to pay with "Stripe Checkout Session" payment gateway
I want to be able to use the payment configuration endpoint

Background:
Given the store operates on a single channel in "United States"
And there is a user "[email protected]" identified by "password123"
And the store has a product "PHP T-Shirt" priced at "€19.99"
And the store ships everywhere for free
And I am logged in as "[email protected]"

@api
Scenario: Getting payment configuration
Given the store has a payment method "Stripe" with a code "stripe" and Stripe Checkout Session payment gateway without using authorize
And I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe" payment method
When I see the payment configuration
Then I should be able to get "publishable_key" with value "pk_test_publishablekey"
And I should be able to get "use_authorize" with a boolean value 0
And I should be able to get "stripe_checkout_session_url" with value "https://checkout.stripe.com/c/pay/cs_1"

@api
Scenario: Getting payment configuration using authorize
Given the store has a payment method "Stripe authorize" with a code "stripe_authorize" and Stripe Checkout Session payment gateway using authorize
And I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe authorize" payment method
When I see the payment configuration
Then I should be able to get "publishable_key" with value "pk_test_publishablekey"
And I should be able to get "use_authorize" with a boolean value 1
And I should be able to get "stripe_checkout_session_url" with value "https://checkout.stripe.com/c/pay/cs_1"
33 changes: 33 additions & 0 deletions features/shop/stripe_js/api.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@stripe_js_payment_configuration
Feature: Paying with Stripe JS during checkout
In order to buy products
As a Customer
I want to be able to pay with "Stripe JS" payment gateway
I want to be able to use the payment configuration endpoint

Background:
Given the store operates on a single channel in "United States"
And there is a user "[email protected]" identified by "password123"
And the store has a product "PHP T-Shirt" priced at "€19.99"
And the store ships everywhere for free
And I am logged in as "[email protected]"

@api
Scenario: Getting payment configuration
Given the store has a payment method "Stripe" with a code "stripe" and Stripe JS payment gateway without using authorize
And I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe" payment method
When I see the payment configuration for Stripe JS
Then I should be able to get "publishable_key" with value "pk_test_publishablekey"
And I should be able to get "use_authorize" with a boolean value 0
And I should be able to get "stripe_payment_intent_client_secret" with value "1234567890"

@api
Scenario: Getting payment configuration using authorize
Given the store has a payment method "Stripe authorize" with a code "stripe_authorize" and Stripe JS payment gateway using authorize
And I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe authorize" payment method
When I see the payment configuration for Stripe JS
Then I should be able to get "publishable_key" with value "pk_test_publishablekey"
And I should be able to get "use_authorize" with a boolean value 1
And I should be able to get "stripe_payment_intent_client_secret" with value "1234567890"
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace FluxSE\SyliusPayumStripePlugin\Api\PaymentConfiguration;

use FluxSE\SyliusPayumStripePlugin\Api\Payum\CaptureProcessorInterface;
use Stripe\Checkout\Session;
use FluxSE\SyliusPayumStripePlugin\Api\Payum\ProcessorInterface;
use Payum\Core\Reply\HttpRedirect;
use Sylius\Bundle\ApiBundle\Payment\PaymentConfigurationProviderInterface;
use Sylius\Component\Core\Model\PaymentInterface;

Expand All @@ -15,10 +15,10 @@ final class StripeCheckoutSessionPaymentConfigProvider implements PaymentConfigu
StripePaymentConfigProviderTrait::__construct as private __stripePaymentConfigProviderConstruct;
}

private CaptureProcessorInterface $captureProcessor;
private ProcessorInterface $captureProcessor;

public function __construct(
CaptureProcessorInterface $captureProcessor,
ProcessorInterface $captureProcessor,
string $factoryName
) {
$this->captureProcessor = $captureProcessor;
Expand All @@ -29,9 +29,12 @@ public function provideConfiguration(PaymentInterface $payment): array
{
$config = $this->provideDefaultConfiguration($payment);

$details = $this->captureProcessor->__invoke($payment);
$session = Session::constructFrom($details);
$config['stripe_checkout_session_url'] = $session->url;
$data = $this->captureProcessor->__invoke($payment, $config['use_authorize']);
$reply = $data['reply'];

if ($reply instanceof HttpRedirect) {
$config['stripe_checkout_session_url'] = $reply->getUrl();
}

return $config;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Api/PaymentConfiguration/StripeJsPaymentConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FluxSE\SyliusPayumStripePlugin\Api\PaymentConfiguration;

use FluxSE\SyliusPayumStripePlugin\Api\Payum\CaptureProcessorInterface;
use FluxSE\SyliusPayumStripePlugin\Api\Payum\ProcessorInterface;
use Stripe\PaymentIntent;
use Sylius\Bundle\ApiBundle\Payment\PaymentConfigurationProviderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
Expand All @@ -15,10 +15,10 @@ final class StripeJsPaymentConfigProvider implements PaymentConfigurationProvide
StripePaymentConfigProviderTrait::__construct as private __stripePaymentConfigProviderConstruct;
}

private CaptureProcessorInterface $captureProcessor;
private ProcessorInterface $captureProcessor;

public function __construct(
CaptureProcessorInterface $captureProcessor,
ProcessorInterface $captureProcessor,
string $factoryName
) {
$this->captureProcessor = $captureProcessor;
Expand All @@ -29,8 +29,8 @@ public function provideConfiguration(PaymentInterface $payment): array
{
$config = $this->provideDefaultConfiguration($payment);

$details = $this->captureProcessor->__invoke($payment);
$paymentIntent = PaymentIntent::constructFrom($details);
$data = $this->captureProcessor->__invoke($payment, $config['use_authorize']);
$paymentIntent = PaymentIntent::constructFrom($data['details']);
$config['stripe_payment_intent_client_secret'] = $paymentIntent->client_secret;

return $config;
Expand Down
12 changes: 0 additions & 12 deletions src/Api/Payum/CaptureProcessorInterface.php

This file was deleted.

48 changes: 33 additions & 15 deletions src/Api/Payum/CaptureProcessor.php → src/Api/Payum/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,70 @@

namespace FluxSE\SyliusPayumStripePlugin\Api\Payum;

use FluxSE\SyliusPayumStripePlugin\Factory\CaptureRequestFactoryInterface;
use FluxSE\SyliusPayumStripePlugin\Factory\ModelAggregateFactoryInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Payum;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Webmozart\Assert\Assert;

final class CaptureProcessor implements CaptureProcessorInterface
final class Processor implements ProcessorInterface
{
private Payum $payum;

private CaptureRequestFactoryInterface $captureRequestFactory;
private ModelAggregateFactoryInterface $captureRequestFactory;

private ModelAggregateFactoryInterface $authorizeRequestFactory;

private AfterUrlProviderInterface $afterUrlProvider;

public function __construct(
Payum $payum,
CaptureRequestFactoryInterface $captureRequestFactory,
ModelAggregateFactoryInterface $captureRequestFactory,
ModelAggregateFactoryInterface $authorizeRequestFactory,
AfterUrlProviderInterface $afterUrlProvider
) {
$this->payum = $payum;
$this->captureRequestFactory = $captureRequestFactory;
$this->authorizeRequestFactory = $authorizeRequestFactory;
$this->afterUrlProvider = $afterUrlProvider;
}

public function __invoke(PaymentInterface $payment): array
public function __invoke(PaymentInterface $payment, bool $useAuthorize): array
{
$tokenFactory = $this->payum->getTokenFactory();
$gatewayName = $this->getGatewayConfig($payment)->getGatewayName();

$token = $tokenFactory->createCaptureToken(
$gatewayName,
$payment,
$this->afterUrlProvider->getAfterPath($payment),
$this->afterUrlProvider->getAfterParameters($payment)
);
$gateway = $this->payum->getGateway($gatewayName);

$captureRequest = $this->captureRequestFactory->createNewWithToken($token);
$gateway->execute($captureRequest, true);
if ($useAuthorize) {
$token = $tokenFactory->createAuthorizeToken(
$gatewayName,
$payment,
$this->afterUrlProvider->getAfterPath($payment),
$this->afterUrlProvider->getAfterParameters($payment)
);
$request = $this->authorizeRequestFactory->createNewWithToken($token);
} else {
$token = $tokenFactory->createCaptureToken(
$gatewayName,
$payment,
$this->afterUrlProvider->getAfterPath($payment),
$this->afterUrlProvider->getAfterParameters($payment)
);
$request = $this->captureRequestFactory->createNewWithToken($token);
}

$reply = $gateway->execute($request, true);

/** @var ArrayObject $details */
$details = $captureRequest->getModel();
$details = $request->getModel();

return $details->getArrayCopy();
return [
'reply' => $reply,
'details' => $details->getArrayCopy(),
];
}

private function getGatewayConfig(PaymentInterface $payment): GatewayConfigInterface
Expand Down
16 changes: 16 additions & 0 deletions src/Api/Payum/ProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\Payum;

use Payum\Core\Reply\ReplyInterface;
use Sylius\Component\Core\Model\PaymentInterface;

interface ProcessorInterface
{
/**
* @return array{'reply': ReplyInterface|null, "details": array}
*/
public function __invoke(PaymentInterface $payment, bool $useAuthorize): array;
}
6 changes: 3 additions & 3 deletions src/CommandHandler/CaptureAuthorizedPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
namespace FluxSE\SyliusPayumStripePlugin\CommandHandler;

use FluxSE\SyliusPayumStripePlugin\Command\CaptureAuthorizedPayment;
use FluxSE\SyliusPayumStripePlugin\Factory\CaptureRequestFactoryInterface;
use FluxSE\SyliusPayumStripePlugin\Factory\ModelAggregateFactoryInterface;
use Payum\Core\Payum;
use Payum\Core\Reply\ReplyInterface;
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
use Webmozart\Assert\Assert;

final class CaptureAuthorizedPaymentHandler extends AbstractPayumPaymentHandler
{
/** @var CaptureRequestFactoryInterface */
/** @var ModelAggregateFactoryInterface */
private $captureRequestFactory;

/**
* @param string[] $supportedGateways
*/
public function __construct(
CaptureRequestFactoryInterface $captureRequestFactory,
ModelAggregateFactoryInterface $captureRequestFactory,
PaymentRepositoryInterface $paymentRepository,
Payum $payum,
array $supportedGateways
Expand Down
17 changes: 17 additions & 0 deletions src/Factory/AuthorizeRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Factory;

use Payum\Core\Model\ModelAggregateInterface;
use Payum\Core\Request\Authorize;
use Payum\Core\Security\TokenInterface;

final class AuthorizeRequestFactory implements ModelAggregateFactoryInterface
{
public function createNewWithToken(TokenInterface $token): ModelAggregateInterface
{
return new Authorize($token);
}
}
2 changes: 1 addition & 1 deletion src/Factory/CaptureRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Payum\Core\Request\Capture;
use Payum\Core\Security\TokenInterface;

final class CaptureRequestFactory implements CaptureRequestFactoryInterface
final class CaptureRequestFactory implements ModelAggregateFactoryInterface
{
public function createNewWithToken(TokenInterface $token): ModelAggregateInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Payum\Core\Model\ModelAggregateInterface;
use Payum\Core\Security\TokenInterface;

interface CaptureRequestFactoryInterface
interface ModelAggregateFactoryInterface
{
public function createNewWithToken(TokenInterface $token): ModelAggregateInterface;
}
3 changes: 3 additions & 0 deletions src/Resources/config/services/factory.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ services:
flux_se.sylius_payum_stripe.factory.refund_request:
class: FluxSE\SyliusPayumStripePlugin\Factory\RefundRequestFactory

flux_se.sylius_payum_stripe.factory.authorize_request:
class: FluxSE\SyliusPayumStripePlugin\Factory\AuthorizeRequestFactory

flux_se.sylius_payum_stripe.factory.capture_request:
class: FluxSE\SyliusPayumStripePlugin\Factory\CaptureRequestFactory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ services:
flux_se.sylius_payum_stripe.api.payment_provider.stripe_checkout_session:
class: FluxSE\SyliusPayumStripePlugin\Api\PaymentConfiguration\StripeCheckoutSessionPaymentConfigProvider
arguments:
$captureProcessor: '@flux_se.sylius_payum_stripe.api.payum.capture_processor.stripe_js'
$captureProcessor: '@flux_se.sylius_payum_stripe.api.payum.processor.stripe_js'
$factoryName: 'stripe_checkout_session'
tags:
- name: sylius.api.payment_method_handler

flux_se.sylius_payum_stripe.api.payum.capture_processor.stripe_checkout_session:
class: FluxSE\SyliusPayumStripePlugin\Api\Payum\CaptureProcessor
flux_se.sylius_payum_stripe.api.payum.processor.stripe_checkout_session:
class: FluxSE\SyliusPayumStripePlugin\Api\Payum\Processor
arguments:
$payum: '@payum'
$captureRequestFactory: '@flux_se.sylius_payum_stripe.factory.capture_request'
$authorizeRequestFactory: '@flux_se.sylius_payum_stripe.factory.authorize_request'
$afterUrlProvider: '@flux_se.sylius_payum_stripe.api.payum.after_url.stripe_checkout_session'

flux_se.sylius_payum_stripe.api.payum.after_url.stripe_checkout_session:
Expand Down
7 changes: 4 additions & 3 deletions src/Resources/config/services/stripe_js/api_platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ services:
flux_se.sylius_payum_stripe.api.payment_provider.stripe_js:
class: FluxSE\SyliusPayumStripePlugin\Api\PaymentConfiguration\StripeJsPaymentConfigProvider
arguments:
$captureProcessor: '@flux_se.sylius_payum_stripe.api.payum.capture_processor.stripe_js'
$captureProcessor: '@flux_se.sylius_payum_stripe.api.payum.processor.stripe_js'
$factoryName: 'stripe_js'
tags:
- name: sylius.api.payment_method_handler

flux_se.sylius_payum_stripe.api.payum.capture_processor.stripe_js:
class: FluxSE\SyliusPayumStripePlugin\Api\Payum\CaptureProcessor
flux_se.sylius_payum_stripe.api.payum.processor.stripe_js:
class: FluxSE\SyliusPayumStripePlugin\Api\Payum\Processor
arguments:
$payum: '@payum'
$captureRequestFactory: '@flux_se.sylius_payum_stripe.factory.capture_request'
$authorizeRequestFactory: '@flux_se.sylius_payum_stripe.factory.authorize_request'
$afterUrlProvider: '@flux_se.sylius_payum_stripe.api.payum.after_url.stripe_js'

flux_se.sylius_payum_stripe.api.payum.after_url.stripe_js:
Expand Down
Loading

0 comments on commit 2c7b70c

Please sign in to comment.