Skip to content

Commit

Permalink
Rapidez v3 support (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Dec 13, 2024
1 parent 7e6a84e commit 3d16cb7
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 84 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ composer require rapidez/mollie

### Configuration

You need to enable and set a custom return url in the Magento 2 configuration:
```
https://yourdomain.com/mollie-return/{{order_hash}}/{{payment_token}}
```

> If you're working locally and your environment isn't accessible from the web you need to disable the webhook setting as Mollie can't reach your environment. This results in getting the "pending payment" page after each payment.
### Views
Expand Down
92 changes: 74 additions & 18 deletions resources/js/mollie.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@
document.addEventListener('vue:loaded', () => {
window.app.$on('checkout-credentials-saved', () => {
window.app.magentoCart('get', 'mollie/payment-token').then(response => {
window.app.checkout.mollie = response
})
});

window.app.$on('checkout-payment-saved', (data) => {
if (!data.order.payment_method_code.includes('mollie_')) {
return;
import { cart } from 'Vendor/rapidez/core/resources/js/stores/useCart'
import { addBeforePaymentMethodHandler, addBeforePlaceOrderHandler, addAfterPlaceOrderHandler } from 'Vendor/rapidez/core/resources/js/stores/usePaymentHandlers'

document.addEventListener('vue:loaded', (event) => {
Vue.set(window.app.custom, 'mollie_selected_issuer', null)
});

addBeforePaymentMethodHandler(async function (query, variables, options) {
if (!variables.code.includes('mollie_') || !window?.app?.checkout?.mollie_selected_issuer)
{
return [query, variables, options];
}

// Add mollie_selected_issuer to setPaymentMethodOnCart
query = config.queries.cart +
`
mutation setMolliePaymentMethodOnCart(
$cart_id: String!,
$code: String!,
$mollie_selected_issuer: String
) {
setPaymentMethodOnCart(input: {
cart_id: $cart_id,
payment_method: {
code: $code,
mollie_selected_issuer: $mollie_selected_issuer
}
}) {
cart { ...cart }
}
window.app.checkout.doNotGoToTheNextStep = true
window.magentoAPI('post', 'mollie/transaction/start', {
token: window.app.checkout.mollie
}).then(response => {
window.location.replace(response)
})
});
})
}`

variables.mollie_selected_issuer = window.app.custom.mollie_selected_issuer

return [query, variables, options];
});

addBeforePlaceOrderHandler(async function (query, variables, options) {
if (!cart.value?.selected_payment_method?.code?.includes('mollie_')) {
return [query, variables, options];
}

// Add mollie_return_url to placeorder
query = config.queries.order + config.queries.orderV2 +
`
mutation molliePlaceOrder($cart_id: String!, $mollie_return_url: String) {
placeOrder(
input: {
cart_id: $cart_id,
mollie_return_url: $mollie_return_url
}
) {
order {
...order
}
orderV2 {
...orderV2
}
errors {
code
message
}
}
}`

variables.mollie_return_url = url('/checkout/success?payment_token={{payment_token}}');

return [query, variables, options]
});

addAfterPlaceOrderHandler(async function (response, mutationComponent) {
mutationComponent.redirect = response?.data?.placeOrder?.order?.mollie_redirect_url || mutationComponent.redirect;
});
7 changes: 0 additions & 7 deletions resources/views/complete.blade.php

This file was deleted.

3 changes: 2 additions & 1 deletion resources/views/graphql/process-transaction.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mutation MollieProcessTransaction($payment_token: String!) {
mollieProcessTransaction (input: { payment_token: $payment_token }) {
paymentStatus
redirect_to_success_page
}
}
}
13 changes: 0 additions & 13 deletions resources/views/pending_payment.blade.php

This file was deleted.

7 changes: 0 additions & 7 deletions resources/views/processing.blade.php

This file was deleted.

31 changes: 0 additions & 31 deletions routes/web.php

This file was deleted.

37 changes: 37 additions & 0 deletions src/Actions/CheckSuccessfulOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rapidez\Mollie\Actions;

use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class CheckSuccessfulOrder
{
/**
* @throws RequestException
*/
public function __invoke(Request $request)
{
$paymentToken = $request->get('payment_token');
if (empty($paymentToken)) {
return true;
}

$url = config('rapidez.magento_url').'/graphql';

$response = Http::post($url, [
'query' => view('mollie::graphql.process-transaction')->render(),
'variables' => [
'payment_token' => $paymentToken,
],
])->throw()->object()->data;

if ($response->mollieProcessTransaction && $response->mollieProcessTransaction->redirect_to_success_page) {
return true;
}

// mollieProcessTransaction has already re-activated the cart for us.
return false;
}
}
21 changes: 19 additions & 2 deletions src/MollieServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Rapidez\Mollie;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Rapidez\Mollie\Actions\CheckSuccessfulOrder;
use TorMorten\Eventy\Facades\Eventy;

class MollieServiceProvider extends ServiceProvider
{
Expand All @@ -13,8 +17,6 @@ class MollieServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');

$this->loadViewsFrom(__DIR__.'/../resources/views', 'mollie');

if ($this->app->runningInConsole()) {
Expand All @@ -26,5 +28,20 @@ public function boot()
__DIR__.'/../resources/payment-icons' => public_path('payment-icons'),
], 'payment-icons');
}

// Fallback for default mollie url
Route::get('mollie/checkout/process', fn() => redirect(route('checkout.success', request()->query()), 308));
// Fallback for legacy mollie url
Route::get('mollie-return/{orderHash}/{paymentToken}', fn ($orderHash, $paymentToken) => redirect(route('checkout.success', ['order_hash' => $orderHash, 'payment_token' => $paymentToken, ...request()->query()]), 308));

Eventy::addFilter('checkout.queries.order.data', function($attributes = []) {
$attributes[] = 'mollie_redirect_url';
$attributes[] = 'mollie_payment_token';
return $attributes;
});

Eventy::addFilter('checkout.checksuccess', function($success = true) {
return $success && App::call(CheckSuccessfulOrder::class);
});
}
}

0 comments on commit 3d16cb7

Please sign in to comment.