From 353be0485dc1f78482e0e72b0ed5ca2e0bf3355c Mon Sep 17 00:00:00 2001 From: indykoning <15870933+indykoning@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:11:48 +0100 Subject: [PATCH] Rapidez v3 support (#1) --- composer.json | 2 +- resources/js/eventlisteners.js | 136 +++++-------------- resources/views/additional-info.blade.php | 66 +++++---- src/RivertyServiceProvider.php | 4 + src/View/Composers/PaymentMethodComposer.php | 21 +++ 5 files changed, 102 insertions(+), 127 deletions(-) create mode 100644 src/View/Composers/PaymentMethodComposer.php diff --git a/composer.json b/composer.json index 2a6a154..c521ac4 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ ], "require": { "php": "^8.1|^8.2", - "rapidez/core": "^2.8" + "rapidez/core": "^3.0" }, "config": { "sort-packages": true diff --git a/resources/js/eventlisteners.js b/resources/js/eventlisteners.js index 1673c51..1c3020e 100644 --- a/resources/js/eventlisteners.js +++ b/resources/js/eventlisteners.js @@ -1,106 +1,44 @@ -import { token } from 'Vendor/rapidez/core/resources/js/stores/useUser' -import { mask } from 'Vendor/rapidez/core/resources/js/stores/useMask' +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', () => { - async function placeOrder() { - if (!token.value && window.app.guestEmail) { - await window.magentoGraphQL( - `mutation setGuestEmailOnCart($cart_id: String!, $email: String!) { - setGuestEmailOnCart(input: { - cart_id: $cart_id - email: $email - }) { - cart { - email - } - } - }`, - { - cart_id: mask.value, - email: window.app.guestEmail - } - ) - } - - await window.magentoGraphQL( - `mutation setRivertyPaymentMethodOnCart( - $cart_id: String! - $code: String! - $terms_and_conditions: Int! - $customer_dob: String - $customer_gender: Int - $customer_telephone: String - ) { - setPaymentMethodOnCart( - input: { - cart_id: $cart_id - payment_method: { - code: $code - afterpay: { - customer_dob: $customer_dob - customer_gender: $customer_gender - customer_telephone: $customer_telephone - terms_and_conditions: $terms_and_conditions - } - } - } - ) { - cart { - selected_payment_method { - code - } - } - } - }`, - { - cart_id: mask.value, - code: window.app.checkout.payment_method, - terms_and_conditions: 1, - customer_gender: window.app.custom?.gender ?? window.app.checkout.billing_address?.gender ?? window.app.checkout.shipping_address?.gender, - customer_dob: window.app.custom?.dob ?? window.app.checkout.billing_address?.dob ?? window.app.checkout.shipping_address?.dob ?? window.app.checkout.billing_address?.date_of_birth ?? window.app.checkout.shipping_address?.date_of_birth ?? '1999-11-11', - customer_telephone: window.app.checkout.billing_address?.telephone ?? window.app.checkout.shipping_address?.telephone - } - ) +addBeforePaymentMethodHandler(async function (query, variables, options) { + if (!variables.code.includes('riverty_') && !variables.code.includes('afterpay_')) { + return [query, variables, options]; + } - return await window.magentoGraphQL( - `mutation rivertyPlaceOrder($cart_id: String!) { - placeOrder( - input: { - cart_id: $cart_id - } - ) { - order { - order_number - } - errors { - code - message - } + // Add afterpay data to setPaymentMethodOnCart + query = config.queries.cart + + ` + mutation setRivertyPaymentMethodOnCart( + $cart_id: String! + $code: String! + $terms_and_conditions: Int! + $customer_dob: String + $customer_gender: Int + $customer_telephone: String + ) { + setPaymentMethodOnCart( + input: { + cart_id: $cart_id + payment_method: { + code: $code + afterpay: { + customer_dob: $customer_dob + customer_gender: $customer_gender + customer_telephone: $customer_telephone + terms_and_conditions: $terms_and_conditions } - }`, - { - 'cart_id': mask.value } - ).then(response => { - if (response?.data?.placeOrder?.order?.order_number) { - return true; - } - - if (response?.data?.placeOrder?.errors) { - response?.data?.placeOrder?.errors?.forEach((error) => Notify(error.message)); } - - return false; - }) - } - - window.app.$on('before-checkout-payment-saved', (data) => { - if (!data.order.payment_method_code.includes('riverty_') && !data.order.payment_method_code.includes('afterpay_')) { - return; + ) { + cart { ...cart } } - window.app.checkout.preventOrder = true - window.app.checkout.doNotGoToTheNextStep = true + }` + + variables.terms_and_conditions = 1 + variables.customer_gender = window.app.custom?.gender ?? cart.value?.billing_address?.gender ?? cart.value?.shipping_address?.gender + variables.customer_dob = window.app.custom?.dob ?? cart.value?.billing_address?.dob ?? cart.value?.shipping_address?.dob ?? cart.value?.billing_address?.date_of_birth ?? cart.value?.shipping_address?.date_of_birth ?? '1999-11-11' + variables.customer_telephone = cart.value?.billing_address?.telephone ?? cart.value?.shipping_address?.telephone - placeOrder(data).then(success => success ? window.app.checkout.step = window.app.getCheckoutStep('success') : ''); - }); -}) + return [query, variables, options]; +}); \ No newline at end of file diff --git a/resources/views/additional-info.blade.php b/resources/views/additional-info.blade.php index 57340d5..c415f57 100644 --- a/resources/views/additional-info.blade.php +++ b/resources/views/additional-info.blade.php @@ -1,29 +1,41 @@ -
- @lang('In order to use Riverty to pay, we need the following information.') - - + - - - -
- +
+ @lang('In order to use Riverty to pay, we need the following information.') + + -
@lang('Male')
- - -
@lang('Female')
-
-
- -
+ +
+ +
+ +
@lang('Male')
+
+ +
@lang('Female')
+
+
+
+
+ diff --git a/src/RivertyServiceProvider.php b/src/RivertyServiceProvider.php index 828b63f..774d406 100644 --- a/src/RivertyServiceProvider.php +++ b/src/RivertyServiceProvider.php @@ -3,6 +3,8 @@ namespace Rapidez\Riverty; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Facades; +use Rapidez\Riverty\View\Composers\PaymentMethodComposer; class RivertyServiceProvider extends ServiceProvider { @@ -15,6 +17,8 @@ public function boot() { $this->loadViewsFrom(__DIR__.'/../resources/views', 'riverty'); + Facades\View::composer('rapidez::checkout.steps.payment_method', PaymentMethodComposer::class); + if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/../resources/views' => resource_path('views/vendor/riverty'), diff --git a/src/View/Composers/PaymentMethodComposer.php b/src/View/Composers/PaymentMethodComposer.php new file mode 100644 index 0000000..7dc9979 --- /dev/null +++ b/src/View/Composers/PaymentMethodComposer.php @@ -0,0 +1,21 @@ +getFactory()->startPush('payment_methods', view('riverty::additional-info')->render()); + } +}