Skip to content

Commit 239ea6a

Browse files
authored
Hosted checkout (#44)
* Added hosted checkout * Updated callback URLs * Fixed integration with paypal plus
1 parent 6f30331 commit 239ea6a

File tree

5 files changed

+49
-85
lines changed

5 files changed

+49
-85
lines changed

Src/Controllers/Frontend/CheckoutController.php renamed to Src/Services/OrderService.php

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Plugin\MonduPayment\Src\Controllers\Frontend;
3+
namespace Plugin\MonduPayment\Src\Services;
44

55
use Plugin\MonduPayment\Src\Helpers\BasketHelper;
66
use Plugin\MonduPayment\Src\Helpers\Text;
@@ -16,7 +16,7 @@
1616
use JTL\Cart\CartItem;
1717
use Plugin\MonduPayment\Src\Helpers\OrderHashHelper;
1818

19-
class CheckoutController
19+
class OrderService
2020
{
2121
private MonduClient $monduClient;
2222
private ConfigService $configService;
@@ -27,38 +27,34 @@ public function __construct()
2727
$this->configService = new ConfigService();
2828
}
2929

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

35-
$orderData = $this->getOrderData($paymentMethod, $formParams);
33+
$orderData = $this->getOrderData($paymentMethod);
3634
$order = $this->monduClient->createOrder($orderData);
3735

3836
$monduOrderUuid = @$order['order']['uuid'];
37+
$hostedCheckoutUrl = '';
3938

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

45-
return Response::json(
46-
[
44+
45+
if (isset($order['order']['hosted_checkout_url'])) {
46+
$hostedCheckoutUrl = $order['order']['hosted_checkout_url'];
47+
}
48+
49+
return [
4750
'error' => @$order['error'] ?? false,
48-
'token' => $monduOrderUuid
49-
]
50-
);
51+
'token' => $monduOrderUuid,
52+
'hosted_checkout_url' => $hostedCheckoutUrl
53+
];
5154
}
5255

53-
public function getOrderData($paymentMethod, $formParams = null)
56+
public function getOrderData($paymentMethod)
5457
{
55-
if($formParams) {
56-
\parse_str($formParams, $params);
57-
$params = Text::filterXSS($params);
58-
59-
BasketHelper::addSurcharge($this->getPaymentId($paymentMethod), $params);
60-
}
61-
6258
$basket = BasketHelper::getBasket();
6359

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

9490
$currency = Frontend::getCurrency()->getCode();
95-
91+
9692
$data = [
9793
'currency' => $currency,
9894
'state_flow' => $this->configService->getOrderFlow(),
95+
'success_url' => $this->getPaymentSuccessURL(),
96+
'cancel_url' => $this->getPaymentCancelURL(),
97+
'declined_url' => $this->getPaymentDeclineURL(),
9998
'payment_method' => $this->getPaymentMethod($paymentMethod),
10099
'gross_amount_cents' => round($basket->total[1] * 100),
101100
'source' => 'widget',
@@ -204,10 +203,24 @@ public function getPayment($cModulId)
204203
);
205204
}
206205

207-
public function getPaymentId($cModulId): int
206+
public function getCheckoutURL(): string
207+
{
208+
return Shop::Container()->getLinkService()->getStaticRoute('bestellvorgang.php');
209+
210+
}
211+
212+
public function getPaymentSuccessURL(): string
208213
{
209-
$payment = $this->getPayment($cModulId);
214+
return $this->getCheckoutURL() . '?payment=accepted';
215+
}
210216

211-
return (int)($payment->kZahlungsart ?? 0);
217+
public function getPaymentCancelURL(): string
218+
{
219+
return $this->getCheckoutURL() . '?editZahlungsart=1&payment=cancelled';
220+
}
221+
222+
public function getPaymentDeclineURL(): string
223+
{
224+
return $this->getCheckoutURL() . '?editZahlungsart=1&payment=declined';
212225
}
213226
}

frontend/hooks/CheckoutConfirmPage.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
namespace Plugin\MonduPayment\Hooks;
44

55
use JTL\Shop;
6+
use Plugin\MonduPayment\Src\Services\OrderService;
67

78
class CheckoutConfirmPage
89
{
910
public function execute(): void
1011
{
1112
if (isset($_SESSION['Zahlungsart']) && $_SESSION['Zahlungsart']->cAnbieter == 'Mondu') {
1213
if (isset($GLOBALS['step']) && $GLOBALS['step'] == 'Bestaetigung') {
13-
if (!isset($_SESSION['monduOrderUuid']) || empty($_SESSION['monduOrderUuid'])) {
14-
$linkHelper = Shop::Container()->getLinkService();
14+
if (!isset($_SESSION['monduOrderUuid']) || empty($_SESSION['monduOrderUuid']) || $_GET['payment'] != 'accepted') {
15+
$orderService = new OrderService();
16+
$orderData = $orderService->token($_SESSION['Zahlungsart']->cModulId);
1517

16-
header('Location: ' . $linkHelper->getStaticRoute('bestellvorgang.php') . '?editZahlungsart=1', true, 303);
17-
exit;
18+
header('Location: ' . $orderData['hosted_checkout_url'], true, 303);
19+
exit;
1820
}
1921
}
2022
}

frontend/hooks/CheckoutPaymentMethod.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function __construct() {
3838
*/
3939
public function execute($args_arr = []): void
4040
{
41+
unset($_SESSION['monduOrderUuid']);
42+
unset($_SESSION['monduCartHash']);
43+
4144
$this->filterPaymentMethods();
4245

4346
if (!$this->isPaymentGroupingEnabled()){

frontend/js/plugin.js

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class MonduCheckoutPlugin {
6060
const value = jQuery('input[name="Zahlungsart"]:checked').val(); //110
6161
const monduPaymentMethods = window.MONDU_CONFIG.payment_methods;
6262
const isMondu = Object.keys(monduPaymentMethods).includes(value);
63-
const formParams = $(this).serialize();
6463

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

7574
$('.checkout-shipping-form .submit_once').attr('disabled', 'disabled');
76-
that._handleSubmit(monduPaymentMethods[value], formParams);
75+
this.submit();
7776
} else {
7877
$('.mondu-payment-method-groups').remove();
7978
ppp.doContinue();
8079
}
8180
}
82-
83-
if (!submittedForm && !that._paypalEnabled()) {
84-
e.preventDefault();
85-
86-
submittedForm = true;
87-
if (!isMondu) return this.submit();
88-
89-
$('.checkout-shipping-form .submit_once').attr('disabled', 'disabled');
90-
that._handleSubmit(monduPaymentMethods[value], formParams);
91-
}
9281
});
9382

83+
9484
window.addEventListener("message", (event) => {
9585
var isPaypal = event.origin.includes('paypal');
9686

@@ -110,55 +100,10 @@ class MonduCheckoutPlugin {
110100
}
111101
}
112102

113-
async _handleSubmit(paymentMethod = null, formParams = '') {
114-
const that = this;
115-
const token = await this._getMonduToken(paymentMethod, formParams);
116-
const removeWidgetContainer = this._removeWidgetContainer.bind(this);
117103

118-
window.monduCheckout.render({
119-
token,
120-
onClose() {
121-
removeWidgetContainer();
122-
123-
if (that.state.isSuccess) {
124-
that._submitForm();
125-
} else {
126-
window.location.href.reload();
127-
}
128-
},
129-
onSuccess() {
130-
that.state.isSuccess = true;
131-
}
132-
});
133-
}
134-
135-
async _getMonduToken(paymentMethod, formParams) {
136-
const client = new HttpRequest();
137-
const tokenUrl = window.MONDU_CONFIG.token_url;
138-
var tokenObject = await client.post('/' + tokenUrl, { payment_method: paymentMethod, form_params: formParams });
139-
140-
if (!tokenObject.data.error) {
141-
return tokenObject.data.token;
142-
}
143-
}
144-
145-
_removeWidgetContainer() {
146-
const widgetContainer = document.getElementById("mondu-checkout-widget");
147-
148-
if (widgetContainer) {
149-
widgetContainer.remove();
150-
window.monduCheckout.destroy();
151-
152-
window.location.reload();
153-
}
154-
}
155104

156105
_isMonduPaymentSelected() {
157106
return window.MONDU_CONFIG != undefined && window.MONDU_CONFIG.selected;
158107
}
159108

160-
_submitForm() {
161-
document.getElementsByClassName('checkout-shipping-form')[0].submit();
162-
}
163-
164109
}

paymentmethod/MonduPayment.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use JTL\Session\Frontend;
99
use JTL\Shop;
1010
use PHPMailer\PHPMailer\Exception;
11+
use Plugin\MonduPayment\Src\Services\OrderService;
1112
use stdClass;
1213
use JTL\Checkout\Bestellung;
1314
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
@@ -48,7 +49,7 @@ public function createInvoice(int $orderID, int $languageID): object
4849

4950
private function confirmOrder($order)
5051
{
51-
$checkoutController = new CheckoutController();
52+
$checkoutController = new OrderService();
5253
$orderData = $checkoutController->getOrderData($order->Zahlungsart->cModulId);
5354

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

0 commit comments

Comments
 (0)