Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/37296 payment methods #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 3.2.1
- Shipping cost as separated parameter

# 3.2.0
- New payment methods: Bank Transfer, P24, Twint, Carte Bancaire, PostFinane

# 3.1.2
- Webhook fix

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mediaopt/worldline",
"description": "Worldline Online Payments",
"version": "3.1.2",
"version": "3.2.1",
"type": "shopware-platform-plugin",
"license": "proprietary",
"config": {
Expand Down
109 changes: 97 additions & 12 deletions src/Adapter/WorldlineSDKAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OnlinePayments\Sdk\Domain\MerchantAction;
use OnlinePayments\Sdk\Domain\Order;
use OnlinePayments\Sdk\Domain\OrderLineDetails;
use OnlinePayments\Sdk\Domain\OrderReferences;
use OnlinePayments\Sdk\Domain\PaymentDetailsResponse;
use OnlinePayments\Sdk\Domain\PaymentProductFilter;
use OnlinePayments\Sdk\Domain\PaymentProductFiltersHostedCheckout;
Expand All @@ -52,6 +53,7 @@
use OnlinePayments\Sdk\Domain\RefundResponse;
use OnlinePayments\Sdk\Domain\Shipping;
use OnlinePayments\Sdk\Domain\ShoppingCart;
use OnlinePayments\Sdk\Domain\ShoppingCartExtension;
use OnlinePayments\Sdk\Domain\ThreeDSecure;
use OnlinePayments\Sdk\Merchant\MerchantClient;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
Expand All @@ -69,6 +71,8 @@
use OnlinePayments\Sdk\Merchant\Products\GetPaymentProductsParams;
use OnlinePayments\Sdk\Domain\GetPaymentProductsResponse;
use OnlinePayments\Sdk\Domain\CreateHostedCheckoutResponse;
use OnlinePayments\Sdk\Domain\PaymentProduct130SpecificThreeDSecure;
use OnlinePayments\Sdk\Domain\RedirectPaymentProduct5408SpecificInput;

/**
* This is the adaptor for Worldline's API
Expand Down Expand Up @@ -119,6 +123,13 @@ public function getMerchantClient(array $credentials = []): MerchantClient
$credentials = $this->getCredentials();
}

$shoppingCartExtension = new ShoppingCartExtension(
MoptWorldline::PLUGIN_CREATOR,
MoptWorldline::PLUGIN_NAME,
MoptWorldline::PLUGIN_VERSION,
MoptWorldline::PLUGIN_ID
);

$communicatorConfiguration = new CommunicatorConfiguration(
$credentials['apiKey'],
$credentials['apiSecret'],
Expand All @@ -127,6 +138,8 @@ public function getMerchantClient(array $credentials = []): MerchantClient
null
);

$communicatorConfiguration->setShoppingCartExtension($shoppingCartExtension);

$connection = new DefaultConnection();
$communicator = new Communicator($connection, $communicatorConfiguration);
$client = new Client($communicator);
Expand Down Expand Up @@ -190,6 +203,9 @@ public function createPayment(

$order = new Order();
$order->setAmountOfMoney($amountOfMoney);
$orderRef = new OrderReferences();
$orderRef->setMerchantReference($orderEntity->getOrderNumber());
$order->setReferences($orderRef);

$hostedCheckoutSpecificInput = new HostedCheckoutSpecificInput();
$ReturnUrlController = new ReturnUrlController($this->systemConfigService);
Expand All @@ -201,14 +217,12 @@ public function createPayment(
if ($this->isDirectSales()) {
$cardPaymentMethodSpecificInput->setAuthorizationMode(Payment::DIRECT_SALE);
}

$groupCardsConfig = $this->getPluginConfig(Form::GROUP_CARDS);
if ($groupCardsConfig) {
$cardPaymentMethodSpecificInputForHostedCheckout = new CardPaymentMethodSpecificInputForHostedCheckout();
$cardPaymentMethodSpecificInputForHostedCheckout->setGroupCards(true);
$hostedCheckoutSpecificInput->setCardPaymentMethodSpecificInput($cardPaymentMethodSpecificInputForHostedCheckout);
}

$hostedCheckoutRequest = new CreateHostedCheckoutRequest();
if ($worldlinePaymentProductId != 0) {
$paymentProductFilter = new PaymentProductFilter();
Expand Down Expand Up @@ -236,12 +250,11 @@ public function createPayment(
$hostedCheckoutRequest->setHostedCheckoutSpecificInput($hostedCheckoutSpecificInput);
$hostedCheckoutRequest->setCardPaymentMethodSpecificInput($cardPaymentMethodSpecificInput);
$hostedCheckoutClient = $merchantClient->hostedCheckout();

return $hostedCheckoutClient->createHostedCheckout($hostedCheckoutRequest);
}

/**
* @param string $worldlinePaymentProductId
* @param int $worldlinePaymentProductId
* @param string $currencyISO
* @param OrderEntity|null $orderEntity
* @param CardPaymentMethodSpecificInput $cardPaymentMethodSpecificInput
Expand Down Expand Up @@ -297,6 +310,37 @@ private function setCustomProperties(
$redirectPaymentMethodSpecificInput->setPaymentOption($this->getPluginConfig(Form::ONEY_PAYMENT_OPTION_FIELD));
break;
}
case PaymentProducts::PAYMENT_PRODUCT_PRZELEWY24:
{
$this->addCustomerEmail($orderEntity, $order);
$redirectPaymentMethodSpecificInput = new RedirectPaymentMethodSpecificInput();
$redirectPaymentMethodSpecificInput->setPaymentProductId($worldlinePaymentProductId);
$cardPaymentMethodSpecificInput = null;
break;
}
case PaymentProducts::PAYMENT_PRODUCT_CARTE_BANCAIRE:
{
$this->addCarteBancaireData($orderEntity, $cardPaymentMethodSpecificInput);
break;
}
case PaymentProducts::PAYMENT_PRODUCT_BANK_TRANSFER:
{
$instantPayment = $this->getPluginConfig(Form::BANK_TRANSFER_INSTANT_PAYMENT_FIELD);

$specificInput = new RedirectPaymentProduct5408SpecificInput();
$specificInput->setInstantPaymentOnly($instantPayment);

$redirectionData = new RedirectionData();
$redirectionData->setReturnUrl($hostedCheckoutSpecificInput->getReturnUrl());

$cardPaymentMethodSpecificInput = null;

$redirectPaymentMethodSpecificInput = new RedirectPaymentMethodSpecificInput();
$redirectPaymentMethodSpecificInput->setPaymentProductId($worldlinePaymentProductId);
$redirectPaymentMethodSpecificInput->setPaymentProduct5408SpecificInput($specificInput);
$redirectPaymentMethodSpecificInput->setRedirectionData($redirectionData);
break;
}
}

if (isset($redirectPaymentMethodSpecificInput)) {
Expand Down Expand Up @@ -342,18 +386,20 @@ public function createHostedTokenization(array $iframeData): GetHostedTokenizati
}

/**
* @param float $amountTotal
* @param int $amountTotal
* @param string $currencyISO
* @param array $iframeData
* @param GetHostedTokenizationResponse $hostedTokenization
* @param ?OrderEntity $orderEntity
* @return CreatePaymentResponse
* @throws \Exception
*/
public function createHostedTokenizationPayment(
int $amountTotal,
string $currencyISO,
array $iframeData,
GetHostedTokenizationResponse $hostedTokenization
GetHostedTokenizationResponse $hostedTokenization,
?OrderEntity $orderEntity
): CreatePaymentResponse
{
$token = $hostedTokenization->getToken()->getId();
Expand Down Expand Up @@ -408,6 +454,10 @@ public function createHostedTokenizationPayment(
$createPaymentRequest->setOrder($order);
$createPaymentRequest->setCardPaymentMethodSpecificInput($cardPaymentMethodSpecificInput);

if ($paymentProductId == PaymentProducts::PAYMENT_PRODUCT_CARTE_BANCAIRE) {
$this->addCarteBancaireData($orderEntity, $cardPaymentMethodSpecificInput);
}

// Get the response for the PaymentsClient
$paymentsClient = $merchantClient->payments();
$createPaymentResponse = $paymentsClient->createPayment($createPaymentRequest);
Expand Down Expand Up @@ -666,6 +716,7 @@ private function addCartToRequest(
$isNetPrice
)
);
$shipping->setShippingCost($orderEntity->getShippingCosts()->getTotalPrice() * 100);

$order->setShoppingCart($shoppingCart);
$order->setShipping($shipping);
Expand All @@ -682,6 +733,46 @@ private function addCartToRequest(
$cardPaymentMethodSpecificInput = null;
}

/**
* @param OrderEntity $orderEntity
* @param Order $order
* @return void
*/
private function addCustomerEmail(OrderEntity $orderEntity, Order $order): void
{
$orderCustomer = $orderEntity->getOrderCustomer();
$contactDetails = new ContactDetails();
$contactDetails->setEmailAddress($orderCustomer->getEmail());

$customer = new Customer();
$customer->setContactDetails($contactDetails);

$order->setCustomer($customer);
}

/**
* @param OrderEntity $orderEntity
* @param CardPaymentMethodSpecificInput $cardPaymentMethodSpecificInput
* @return void
*/
private function addCarteBancaireData(
OrderEntity $orderEntity,
CardPaymentMethodSpecificInput &$cardPaymentMethodSpecificInput
): void
{
$count = 0;
foreach ($orderEntity->getLineItems() as $lineItem) {
$count += $lineItem->getQuantity();
}
$useCase = $this->isDirectSales() ? 'single-amount' : 'payment-upon-shipment';
$threeDSecure = new PaymentProduct130SpecificThreeDSecure();
$threeDSecure->setUsecase($useCase);
$threeDSecure->setNumberOfItems(min($count, 99));
$cardPaymentMethodSpecificInput->setPaymentProduct130SpecificInput(
$threeDSecure
);
}

/**
* @param OrderLineItemCollection $lineItemCollection
* @param CalculatedPrice $shippingPrice
Expand Down Expand Up @@ -730,12 +821,6 @@ private function createRequestLineItems(
if ($shippingPrice > 0) {
$grandPrice += $shippingPrice;
$grandCount++;
$shippingElementId = 'shipping_element';
if ($maxPrices['unit']['price'] < $shippingPrice) {
$maxPrices['unit']['price'] = $shippingPrice;
$maxPrices['unit']['id'] = $shippingElementId;
}
$requestLineItems[$shippingElementId] = self::createLineItem(self::SHIPPING_LABEL, $currencyISO, $shippingPrice, $shippingPrice, 1);
}

if ($discount > 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/Bootstrap/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Form
const AUTO_CAPTURE_5_DAYS = '5_days';
const GROUP_CARDS = 'MoptWorldline.config.groupCards';
const PARTIAL_OPERATIONS_ENABLED = 'MoptWorldline.config.partialOperationsEnabled';
const BANK_TRANSFER_INSTANT_PAYMENT_FIELD = 'MoptWorldline.config.bankTransferInstantPayment';

/** @var string Fieldset name for the plugin custom field */
const CUSTOM_FIELD_WORLDLINE_PAYMENT_TRANSACTION_FIELDSET = 'payment_transaction_fieldset';
Expand Down Expand Up @@ -89,7 +90,7 @@ class Form
const WORLDLINE_CART_FORM_REDIRECT_TOKEN = 'moptWorldlineRedirectToken';

/** @var string Field name for the plugin session key */
const SESSION_OPERATIONS_LOCK = 'order_locked';
const SESSION_OPERATIONS_LOCK = 'worldline_order_locked';
const SESSION_IFRAME_DATA = 'worldline_iframe_data';
const SESSION_SERVER_URL = 'worldline_server_url';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use MoptWorldline\Controller\PaymentMethod\PaymentMethodController;

#[Route(defaults: ['_routeScope' => ['api']])]
class ApiTestController extends AbstractController
class PluginConfigController extends AbstractController
{
private SystemConfigService $systemConfigService;
private EntityRepository $salesChannelRepository;
Expand All @@ -32,6 +32,8 @@ class ApiTestController extends AbstractController
private EntityRepository $mediaRepository;
private MediaService $mediaService;
private FileSaver $fileSaver;
private EntityRepository $ruleRepository;
private EntityRepository $ruleConditionRepository;

private array $credentialKeys = [
'sandbox' => [
Expand All @@ -57,6 +59,8 @@ class ApiTestController extends AbstractController
* @param EntityRepository $mediaRepository
* @param MediaService $mediaService
* @param FileSaver $fileSaver
* @param EntityRepository $ruleRepository
* @param EntityRepository $ruleConditionRepository
*/
public function __construct(
SystemConfigService $systemConfigService,
Expand All @@ -66,7 +70,9 @@ public function __construct(
PluginIdProvider $pluginIdProvider,
EntityRepository $mediaRepository,
MediaService $mediaService,
FileSaver $fileSaver
FileSaver $fileSaver,
EntityRepository $ruleRepository,
EntityRepository $ruleConditionRepository,
)
{
$this->systemConfigService = $systemConfigService;
Expand All @@ -77,11 +83,13 @@ public function __construct(
$this->mediaRepository = $mediaRepository;
$this->mediaService = $mediaService;
$this->fileSaver = $fileSaver;
$this->ruleRepository = $ruleRepository;
$this->ruleConditionRepository = $ruleConditionRepository;
}

#[Route(
path: '/api/_action/api-test/test-connection',
name: 'api.action.test.connection',
path: '/api/_action/worldline/api-test/test-connection',
name: 'api.action.worldline.test.connection',
methods: ['POST']
)]
public function testConnection(Request $request, Context $context): JsonResponse
Expand Down Expand Up @@ -120,8 +128,8 @@ public function testConnection(Request $request, Context $context): JsonResponse
}

#[Route(
path: '/api/_action/api-test/savemethod',
name: 'api.action.test.savemethod',
path: '/api/_action/worldline/api-test/savemethod',
name: 'api.action.worldline.test.savemethod',
methods: ['POST']
)]
public function saveMethod(Request $request, Context $context): JsonResponse
Expand All @@ -146,7 +154,9 @@ private function getPaymentMethodController()
$this->mediaRepository,
$this->mediaService,
$this->fileSaver,
$this->salesChannelRepository
$this->salesChannelRepository,
$this->ruleRepository,
$this->ruleConditionRepository,
);
}

Expand Down
Loading