Skip to content

Commit

Permalink
Merge pull request #4 from logeecom/master
Browse files Browse the repository at this point in the history
PISYL-87 Add Billie payment method
  • Loading branch information
sebastian-mollie authored Oct 3, 2023
2 parents 459f842 + e6ebd7b commit a3ad196
Show file tree
Hide file tree
Showing 73 changed files with 1,882 additions and 1,756 deletions.
1 change: 0 additions & 1 deletion features/admin/managing_mollie_payment_method.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Feature: Adding a new mollie payment method
Given I want to create a new Mollie payment method
When I name it "Mollie" in "English (United States)"
And I specify its code as "mollie_test"
And I fill the Profile ID with "MOLLIE_PROFILE_KEY"
And I fill the API key with "MOLLIE_TEST_API_KEY"
And make it available in channel "Web-EUR"
And I add it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Feature: Adding a new mollie recurring subscription
Given I want to create a new Mollie recurring subscription
When I name it "Mollie Recurring Subscription" in "English (United States)"
And I specify its code as "mollie_recurring_subscription_test"
And I fill the Profile ID with "MOLLIE_PROFILE_KEY"
And I fill the API key with "MOLLIE_TEST_API_KEY"
And make it available in channel "Web-EUR"
And I add it
Expand Down
1 change: 0 additions & 1 deletion features/admin/mollie_payment_method_validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Feature: Mollie payment method validation
Scenario: Trying to add a new mollie payment method without the correct api key
Given I want to create a new Mollie payment method
When I name it "Mollie" in "English (United States)"
And I fill the Profile ID with "MOLLIE_PROFILE_KEY"
And I fill the API key with "tttt_jdqkCbp55GRnfb9nFRz7R555pJMW4"
And I add it
And I should be notified with error 'Invalid API key. An API key must start with "test_".' message
Expand Down
18 changes: 15 additions & 3 deletions src/Action/ConvertMolliePaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
use SyliusMolliePlugin\Factory\ApiCustomerFactoryInterface;
use SyliusMolliePlugin\Helper\ConvertOrderInterface;
use SyliusMolliePlugin\Helper\IntToStringConverterInterface;
use SyliusMolliePlugin\Helper\PaymentDescriptionInterface;
use SyliusMolliePlugin\Payments\PaymentTerms\Options;
use SyliusMolliePlugin\Provider\Divisor\DivisorProviderInterface;
use SyliusMolliePlugin\Resolver\PaymentLocaleResolverInterface;
use Mollie\Api\Types\PaymentMethod;
use Payum\Core\Action\ActionInterface;
Expand Down Expand Up @@ -49,20 +51,30 @@ final class ConvertMolliePaymentAction extends BaseApiAwareAction implements Act
/** @var ApiCustomerFactoryInterface */
private $apiCustomerFactory;

/** @var IntToStringConverterInterface */
private $intToStringConverter;

/** @var DivisorProviderInterface */
private $divisorProvider;

public function __construct(
PaymentDescriptionInterface $paymentDescription,
RepositoryInterface $mollieMethodsRepository,
ConvertOrderInterface $orderConverter,
CustomerContextInterface $customerContext,
PaymentLocaleResolverInterface $paymentLocaleResolver,
ApiCustomerFactoryInterface $apiCustomerFactory
ApiCustomerFactoryInterface $apiCustomerFactory,
IntToStringConverterInterface $intToStringConverter,
DivisorProviderInterface $divisorProvider
) {
$this->paymentDescription = $paymentDescription;
$this->mollieMethodsRepository = $mollieMethodsRepository;
$this->orderConverter = $orderConverter;
$this->customerContext = $customerContext;
$this->paymentLocaleResolver = $paymentLocaleResolver;
$this->apiCustomerFactory = $apiCustomerFactory;
$this->intToStringConverter = $intToStringConverter;
$this->divisorProvider = $divisorProvider;
}

/** @param Convert|mixed $request */
Expand All @@ -82,10 +94,10 @@ public function execute($request): void
Assert::notNull($payment->getCurrencyCode());
$this->gateway->execute($currency = new GetCurrency($payment->getCurrencyCode()));

$divisor = 10 ** $currency->exp;
$divisor = $this->divisorProvider->getDivisorForCurrency($currency);

Assert::notNull($payment->getAmount());
$amount = number_format(abs($payment->getAmount() / $divisor), 2, '.', '');
$amount = $this->intToStringConverter->convertIntToString($payment->getAmount(), $divisor);

$paymentOptions = $payment->getDetails();

Expand Down
24 changes: 18 additions & 6 deletions src/Action/ConvertMollieSubscriptionPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
use SyliusMolliePlugin\Entity\OrderInterface;
use SyliusMolliePlugin\Helper\ConvertOrderInterface;
use SyliusMolliePlugin\Helper\IntToStringConverterInterface;
use SyliusMolliePlugin\Helper\PaymentDescriptionInterface;
use SyliusMolliePlugin\Provider\Divisor\DivisorProviderInterface;
use SyliusMolliePlugin\Request\Api\CreateCustomer;
use SyliusMolliePlugin\Resolver\PaymentLocaleResolverInterface;
use Mollie\Api\Types\PaymentMethod;
Expand All @@ -19,9 +21,9 @@
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Convert;
use Payum\Core\Request\GetCurrency;
use Sylius\Bundle\CoreBundle\Context\CustomerContext;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Customer\Context\CustomerContextInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Webmozart\Assert\Assert;

Expand All @@ -38,24 +40,34 @@ final class ConvertMollieSubscriptionPaymentAction extends BaseApiAwareAction im
/** @var ConvertOrderInterface */
private $orderConverter;

/** @var CustomerContext */
/** @var CustomerContextInterface */
private $customerContext;

/** @var PaymentLocaleResolverInterface */
private $paymentLocaleResolver;

/** @var IntToStringConverterInterface */
private $intToStringConverter;

/** @var DivisorProviderInterface */
private $divisorProvider;

public function __construct(
PaymentDescriptionInterface $paymentDescription,
RepositoryInterface $mollieMethodsRepository,
ConvertOrderInterface $orderConverter,
CustomerContext $customerContext,
PaymentLocaleResolverInterface $paymentLocaleResolver
CustomerContextInterface $customerContext,
PaymentLocaleResolverInterface $paymentLocaleResolver,
IntToStringConverterInterface $intToStringConverter,
DivisorProviderInterface $divisorProvider
) {
$this->paymentDescription = $paymentDescription;
$this->mollieMethodsRepository = $mollieMethodsRepository;
$this->orderConverter = $orderConverter;
$this->customerContext = $customerContext;
$this->paymentLocaleResolver = $paymentLocaleResolver;
$this->intToStringConverter = $intToStringConverter;
$this->divisorProvider = $divisorProvider;
}

/** @param Convert|mixed $request */
Expand All @@ -78,10 +90,10 @@ public function execute($request): void
Assert::notNull($payment->getCurrencyCode());
$this->gateway->execute($currency = new GetCurrency($payment->getCurrencyCode()));

$divisor = 10 ** $currency->exp;
$divisor = $this->divisorProvider->getDivisorForCurrency($currency);

Assert::notNull($payment->getAmount());
$amount = number_format(abs($payment->getAmount() / $divisor), 2, '.', '');
$amount = $this->intToStringConverter->convertIntToString($payment->getAmount(), $divisor);
$paymentOptions = $payment->getDetails();

$cartToken = $paymentOptions['cartToken'];
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator/CalculateTaxAmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function calculate(float $taxRateAmount, int $amount): string
{
$shippingTaxAmount = round($amount - ($amount / (1 + $taxRateAmount)));

return $this->converter->convertIntToString((int) $shippingTaxAmount, 100);
return $this->converter->convertIntToString((int) $shippingTaxAmount);
}
}
10 changes: 8 additions & 2 deletions src/Creator/PaymentRefundCommandCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace SyliusMolliePlugin\Creator;

use SyliusMolliePlugin\Exceptions\OfflineRefundPaymentMethodNotFound;
use SyliusMolliePlugin\Provider\Divisor\DivisorProviderInterface;
use SyliusMolliePlugin\Refund\Units\PaymentUnitsItemRefundInterface;
use SyliusMolliePlugin\Refund\Units\ShipmentUnitRefundInterface;
use Mollie\Api\Resources\Payment;
Expand Down Expand Up @@ -36,20 +37,25 @@ final class PaymentRefundCommandCreator implements PaymentRefundCommandCreatorIn
/** @var RefundPaymentMethodsProviderInterface */
private $refundPaymentMethodProvider;

/** @var DivisorProviderInterface */
private $divisorProvider;

public function __construct(
RepositoryInterface $orderRepository,
RepositoryInterface $refundUnitsRepository,
PaymentUnitsItemRefundInterface $itemRefund,
ShipmentUnitRefundInterface $shipmentRefund,
AdjustmentFactoryInterface $adjustmentFactory,
RefundPaymentMethodsProviderInterface $refundPaymentMethodProvider
RefundPaymentMethodsProviderInterface $refundPaymentMethodProvider,
DivisorProviderInterface $divisorProvider
) {
$this->orderRepository = $orderRepository;
$this->refundUnitsRepository = $refundUnitsRepository;
$this->itemRefund = $itemRefund;
$this->shipmentRefund = $shipmentRefund;
$this->adjustmentFactory = $adjustmentFactory;
$this->refundPaymentMethodProvider = $refundPaymentMethodProvider;
$this->divisorProvider = $divisorProvider;
}

public function fromPayment(Payment $payment): RefundUnits
Expand All @@ -65,7 +71,7 @@ public function fromPayment(Payment $payment): RefundUnits
$refunded = $this->getSumOfAmountExistingRefunds($allRefunded);

Assert::notNull($payment->amountRefunded);
$mollieRefund = (float) $payment->amountRefunded->value * 100;
$mollieRefund = (float) $payment->amountRefunded->value * $this->divisorProvider->getDivisor();
$toRefund = (int) $mollieRefund - $refunded;

Assert::notNull($order->getChannel());
Expand Down
11 changes: 0 additions & 11 deletions src/Documentation/DocumentationLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ final class DocumentationLinks implements DocumentationLinksInterface
'single_click' => 'https://help.mollie.com/hc/en-us/articles/115000671249-What-are-single-click-payments-and-how-does-it-work-',
'mollie_components' => 'https://www.mollie.com/en/news/post/better-checkout-flows-with-mollie-components',
'payment_methods' => 'https://docs.mollie.com/orders/why-use-orders',
'profile_id' => 'https://www.mollie.com/dashboard/developers/api-keys',
'api_key' => 'https://www.mollie.com/dashboard/developers/api-keys',
];

Expand Down Expand Up @@ -62,16 +61,6 @@ public function getPaymentMethodDoc(): string
);
}

public function getProfileIdDoc(): string
{
return \sprintf(
'%s <a target="_blank" href="%s"> %s </a>',
$this->translator->trans('sylius_mollie_plugin.ui.you_can_find_you_profile_id'),
self::DOCUMENTATION_LINKS['profile_id'],
$this->translator->trans('sylius_mollie_plugin.ui.mollie_profile_id')
);
}

public function getApiKeyDoc(): string
{
return \sprintf(
Expand Down
2 changes: 0 additions & 2 deletions src/Documentation/DocumentationLinksInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ public function getMollieComponentsDoc(): string;

public function getPaymentMethodDoc(): string;

public function getProfileIdDoc(): string;

public function getApiKeyDoc(): string;
}
5 changes: 5 additions & 0 deletions src/Factory/MollieGatewayConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public function create(

return $mollieGatewayConfig;
}

public function createNew(): MollieGatewayConfigInterface
{
return $this->mollieGatewayConfigFactory->createNew();
}
}
3 changes: 2 additions & 1 deletion src/Factory/MollieGatewayConfigFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

namespace SyliusMolliePlugin\Factory;

use Sylius\Component\Resource\Factory\FactoryInterface;
use SyliusMolliePlugin\Entity\GatewayConfigInterface;
use SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
use SyliusMolliePlugin\Payments\Methods\MethodInterface;

interface MollieGatewayConfigFactoryInterface
interface MollieGatewayConfigFactoryInterface extends FactoryInterface
{
public function create(
MethodInterface $method,
Expand Down
2 changes: 2 additions & 0 deletions src/Factory/MollieGatewayFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface MollieGatewayFactoryInterface
/** @deprecated since 3.7.0 use MollieGatewayFactoryInterface::LOCALES_AVAILABLE_MAP instead */
public const LOCALES_AVAILABLE = [
'en_US',
'en_GB',
'nl_NL',
'nl_BE',
'fr_FR',
Expand All @@ -34,6 +35,7 @@ interface MollieGatewayFactoryInterface

public const LOCALES_AVAILABLE_MAP = [
'en_US' => 'en_US',
'en_GB' => 'en_GB',
'nl_NL' => 'nl_NL',
'nl_BE' => 'nl_BE',
'fr_FR' => 'fr_FR',
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/MollieSubscriptionGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function populateConfig(ArrayObject $config): void
$mollieApiClient->setConfig($config->toUnsafeArray());
$mollieApiClient->setIsRecurringSubscription(true);
$mollieApiClient->addVersionString(\sprintf('Sylius/%s', Kernel::VERSION));
$mollieApiClient->addVersionString(\sprintf('SyliusMolliePlugin/%s', $mollieApiClient->getVersion()));
$mollieApiClient->addVersionString(\sprintf('SubscriptionSyliusMolliePlugin/%s', $mollieApiClient->getVersion()));
$mollieApiClient->addVersionString(\sprintf('uap/%s', $mollieApiClient->getUserAgentToken()));

return $mollieApiClient;
Expand Down
44 changes: 35 additions & 9 deletions src/Form/Type/MollieGatewayConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

namespace SyliusMolliePlugin\Form\Type;

use SyliusMolliePlugin\Client\MollieApiClient;
use SyliusMolliePlugin\Documentation\DocumentationLinksInterface;
use SyliusMolliePlugin\Payments\PaymentTerms\Options;
use SyliusMolliePlugin\Validator\Constraints\LiveApiKeyIsNotBlank;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
Expand All @@ -28,10 +30,13 @@ final class MollieGatewayConfigurationType extends AbstractType

/** @var DocumentationLinksInterface */
private $documentationLinks;
/** @var MollieApiClient */
private $apiClient;

public function __construct(DocumentationLinksInterface $documentationLinks)
public function __construct(DocumentationLinksInterface $documentationLinks, MollieApiClient $apiClient)
{
$this->documentationLinks = $documentationLinks;
$this->apiClient = $apiClient;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -44,14 +49,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'sylius_mollie_plugin.ui.api_key_choice_live' => true,
],
])
->add('profile_id', TextType::class, [
'label' => $this->documentationLinks->getProfileIdDoc(),
'constraints' => [
new NotBlank([
'message' => 'sylius_mollie_plugin.profile_id.not_blank',
'groups' => ['sylius'],
]),
],
->add('profile_id', HiddenType::class, [
])
->add(self::API_KEY_TEST, PasswordType::class, [
'always_empty' => false,
Expand Down Expand Up @@ -127,6 +125,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

$data['payum.http_client'] = '@sylius_mollie_plugin.mollie_api_client';

$event->setData($data);
})
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
$data = $event->getData();

if (array_key_exists('environment', $data)) {
$apiKey = $this->getMollieApiKey($data);

$this->apiClient->setApiKey($apiKey);
$profile = $this->apiClient->profiles->getCurrent();

$data['profile_id'] = $profile->id;
}

$event->setData($data);
});
}
Expand All @@ -147,4 +159,18 @@ public function configureOptions(OptionsResolver $resolver): void
new LiveApiKeyIsNotBlank($defaults),
]);
}

/**
* @param array $config
*
* @return string
*/
private function getMollieApiKey(array $config): string
{
if ($config['environment']) {
return $config[MollieGatewayConfigurationType::API_KEY_LIVE];
}

return $config[MollieGatewayConfigurationType::API_KEY_TEST];
}
}
Loading

0 comments on commit a3ad196

Please sign in to comment.