import { redirectToUrl } from '../util/Redirect';
import { getPaymentToken, setPaymentToken } from '../util/PaymentTokenPersistence';
const savePaymentMethodAndPlaceOrder = async (args, callback, instance) => {
const [paymentInformation] = args;
const { paymentMethod: { code, additional_data } } = paymentInformation;
const guest_cart_id = !isSignedIn() ? getGuestQuoteId() : '';
// It's important to check if the user has selected a Mollie payment method.
// We don't want to affect other methods, so we just use the callback directly in that case.
if (!MOLLIE_METHODS.includes(code)) {
return await callback(...args);
// Since this is a Mollie method, now we need some custom logic
// Just like the original function, we set the payment method
await fetchMutation(CheckoutQuery.getSetPaymentMethodOnCartMutation({
// However, when placing the order, there is some additional data we need to consider
const orderData = await fetchMutation(CheckoutQuery.getPlaceOrderMutation(guest_cart_id));
const { placeOrder: { order: { mollie_redirect_url, mollie_payment_token } } } = orderData;
if (Boolean(mollie_payment_token)) {
// We'll need this token later, so let's save it to the browser's storage
setPaymentToken(mollie_payment_token);
// It is a good idea to "fail early" if some data you expect to be present
// is not there. Also, provide good error messages.
// That way, your code will be easier to debug if something goes wrong.
throw Error("Expected mollie_payment_token in order data, none found", orderData)
// We take the redirect URL we requested in the previous step and redirect to it
if (Boolean(mollie_redirect_url)) {
redirectToUrl(mollie_redirect_url)
throw Error("Expected mollie_redirect_url in order data, none found", orderData)
instance._handleError(e);
// Afain, we need to export the plugin configuration:
"Route/Checkout/Container": {
savePaymentMethodAndPlaceOrder,