Skip to content

Commit

Permalink
Hosted checkout (#44)
Browse files Browse the repository at this point in the history
* Added hosted checkout

* Updated callback URLs

* Fixed integration with paypal plus
  • Loading branch information
harism2 authored Jun 16, 2023
1 parent 6f30331 commit 239ea6a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Plugin\MonduPayment\Src\Controllers\Frontend;
namespace Plugin\MonduPayment\Src\Services;

use Plugin\MonduPayment\Src\Helpers\BasketHelper;
use Plugin\MonduPayment\Src\Helpers\Text;
Expand All @@ -16,7 +16,7 @@
use JTL\Cart\CartItem;
use Plugin\MonduPayment\Src\Helpers\OrderHashHelper;

class CheckoutController
class OrderService
{
private MonduClient $monduClient;
private ConfigService $configService;
Expand All @@ -27,38 +27,34 @@ public function __construct()
$this->configService = new ConfigService();
}

public function token(Request $request, int $pluginId)
public function token($paymentMethod)
{
$paymentMethod = $request->all()['payment_method'] ?? null;
$formParams = $request->allRaw()['form_params'] ?? null;

$orderData = $this->getOrderData($paymentMethod, $formParams);
$orderData = $this->getOrderData($paymentMethod);
$order = $this->monduClient->createOrder($orderData);

$monduOrderUuid = @$order['order']['uuid'];
$hostedCheckoutUrl = '';

if ($monduOrderUuid != null) {
$_SESSION['monduOrderUuid'] = $monduOrderUuid;
$_SESSION['monduCartHash'] = OrderHashHelper::getOrderHash($orderData);
}

return Response::json(
[

if (isset($order['order']['hosted_checkout_url'])) {
$hostedCheckoutUrl = $order['order']['hosted_checkout_url'];
}

return [
'error' => @$order['error'] ?? false,
'token' => $monduOrderUuid
]
);
'token' => $monduOrderUuid,
'hosted_checkout_url' => $hostedCheckoutUrl
];
}

public function getOrderData($paymentMethod, $formParams = null)
public function getOrderData($paymentMethod)
{
if($formParams) {
\parse_str($formParams, $params);
$params = Text::filterXSS($params);

BasketHelper::addSurcharge($this->getPaymentId($paymentMethod), $params);
}

$basket = BasketHelper::getBasket();

$customer = Frontend::getCustomer();
Expand Down Expand Up @@ -92,10 +88,13 @@ public function getOrderData($paymentMethod, $formParams = null)
$buyer['is_registered'] = $customer->kKunde != null;

$currency = Frontend::getCurrency()->getCode();

$data = [
'currency' => $currency,
'state_flow' => $this->configService->getOrderFlow(),
'success_url' => $this->getPaymentSuccessURL(),
'cancel_url' => $this->getPaymentCancelURL(),
'declined_url' => $this->getPaymentDeclineURL(),
'payment_method' => $this->getPaymentMethod($paymentMethod),
'gross_amount_cents' => round($basket->total[1] * 100),
'source' => 'widget',
Expand Down Expand Up @@ -204,10 +203,24 @@ public function getPayment($cModulId)
);
}

public function getPaymentId($cModulId): int
public function getCheckoutURL(): string
{
return Shop::Container()->getLinkService()->getStaticRoute('bestellvorgang.php');

}

public function getPaymentSuccessURL(): string
{
$payment = $this->getPayment($cModulId);
return $this->getCheckoutURL() . '?payment=accepted';
}

return (int)($payment->kZahlungsart ?? 0);
public function getPaymentCancelURL(): string
{
return $this->getCheckoutURL() . '?editZahlungsart=1&payment=cancelled';
}

public function getPaymentDeclineURL(): string
{
return $this->getCheckoutURL() . '?editZahlungsart=1&payment=declined';
}
}
10 changes: 6 additions & 4 deletions frontend/hooks/CheckoutConfirmPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
namespace Plugin\MonduPayment\Hooks;

use JTL\Shop;
use Plugin\MonduPayment\Src\Services\OrderService;

class CheckoutConfirmPage
{
public function execute(): void
{
if (isset($_SESSION['Zahlungsart']) && $_SESSION['Zahlungsart']->cAnbieter == 'Mondu') {
if (isset($GLOBALS['step']) && $GLOBALS['step'] == 'Bestaetigung') {
if (!isset($_SESSION['monduOrderUuid']) || empty($_SESSION['monduOrderUuid'])) {
$linkHelper = Shop::Container()->getLinkService();
if (!isset($_SESSION['monduOrderUuid']) || empty($_SESSION['monduOrderUuid']) || $_GET['payment'] != 'accepted') {
$orderService = new OrderService();
$orderData = $orderService->token($_SESSION['Zahlungsart']->cModulId);

header('Location: ' . $linkHelper->getStaticRoute('bestellvorgang.php') . '?editZahlungsart=1', true, 303);
exit;
header('Location: ' . $orderData['hosted_checkout_url'], true, 303);
exit;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/hooks/CheckoutPaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function __construct() {
*/
public function execute($args_arr = []): void
{
unset($_SESSION['monduOrderUuid']);
unset($_SESSION['monduCartHash']);

$this->filterPaymentMethods();

if (!$this->isPaymentGroupingEnabled()){
Expand Down
59 changes: 2 additions & 57 deletions frontend/js/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class MonduCheckoutPlugin {
const value = jQuery('input[name="Zahlungsart"]:checked').val(); //110
const monduPaymentMethods = window.MONDU_CONFIG.payment_methods;
const isMondu = Object.keys(monduPaymentMethods).includes(value);
const formParams = $(this).serialize();

if (!submittedForm && that._paypalEnabled()) {
e.preventDefault();
Expand All @@ -73,24 +72,15 @@ class MonduCheckoutPlugin {
if (!isMondu) return this.submit();

$('.checkout-shipping-form .submit_once').attr('disabled', 'disabled');
that._handleSubmit(monduPaymentMethods[value], formParams);
this.submit();
} else {
$('.mondu-payment-method-groups').remove();
ppp.doContinue();
}
}

if (!submittedForm && !that._paypalEnabled()) {
e.preventDefault();

submittedForm = true;
if (!isMondu) return this.submit();

$('.checkout-shipping-form .submit_once').attr('disabled', 'disabled');
that._handleSubmit(monduPaymentMethods[value], formParams);
}
});


window.addEventListener("message", (event) => {
var isPaypal = event.origin.includes('paypal');

Expand All @@ -110,55 +100,10 @@ class MonduCheckoutPlugin {
}
}

async _handleSubmit(paymentMethod = null, formParams = '') {
const that = this;
const token = await this._getMonduToken(paymentMethod, formParams);
const removeWidgetContainer = this._removeWidgetContainer.bind(this);

window.monduCheckout.render({
token,
onClose() {
removeWidgetContainer();

if (that.state.isSuccess) {
that._submitForm();
} else {
window.location.href.reload();
}
},
onSuccess() {
that.state.isSuccess = true;
}
});
}

async _getMonduToken(paymentMethod, formParams) {
const client = new HttpRequest();
const tokenUrl = window.MONDU_CONFIG.token_url;
var tokenObject = await client.post('/' + tokenUrl, { payment_method: paymentMethod, form_params: formParams });

if (!tokenObject.data.error) {
return tokenObject.data.token;
}
}

_removeWidgetContainer() {
const widgetContainer = document.getElementById("mondu-checkout-widget");

if (widgetContainer) {
widgetContainer.remove();
window.monduCheckout.destroy();

window.location.reload();
}
}

_isMonduPaymentSelected() {
return window.MONDU_CONFIG != undefined && window.MONDU_CONFIG.selected;
}

_submitForm() {
document.getElementsByClassName('checkout-shipping-form')[0].submit();
}

}
3 changes: 2 additions & 1 deletion paymentmethod/MonduPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use JTL\Session\Frontend;
use JTL\Shop;
use PHPMailer\PHPMailer\Exception;
use Plugin\MonduPayment\Src\Services\OrderService;
use stdClass;
use JTL\Checkout\Bestellung;
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function createInvoice(int $orderID, int $languageID): object

private function confirmOrder($order)
{
$checkoutController = new CheckoutController();
$checkoutController = new OrderService();
$orderData = $checkoutController->getOrderData($order->Zahlungsart->cModulId);

if(OrderHashHelper::getOrderHash($orderData) !== $_SESSION['monduCartHash']) {
Expand Down

0 comments on commit 239ea6a

Please sign in to comment.