Skip to content

Commit

Permalink
Rapidez v3 support (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Dec 13, 2024
1 parent 5044ec0 commit 353be04
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 127 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": "^8.1|^8.2",
"rapidez/core": "^2.8"
"rapidez/core": "^3.0"
},
"config": {
"sort-packages": true
Expand Down
136 changes: 37 additions & 99 deletions resources/js/eventlisteners.js
Original file line number Diff line number Diff line change
@@ -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];
});
66 changes: 39 additions & 27 deletions resources/views/additional-info.blade.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
<div v-if="checkout.payment_method.includes('riverty_') || checkout.payment_method.includes('afterpay_')" class="mt-2">
<span class="my-1">@lang('In order to use Riverty to pay, we need the following information.')</span>
<x-rapidez::label :label="__('Date of Birth')" class="my-1">
<x-rapidez::input
name="dob"
type="date"
v-model="$root.custom.dob"
<template v-else-if="method.code.includes('riverty_') || method.code.includes('afterpay_')">
<x-rapidez::radio
name="payment_method"
v-model="variables.code"
v-bind:value="method.code"
v-bind:dusk="'method-'+index"
v-on:change="mutate"
required
>
</x-rapidez::input>
</x-rapidez::label>
<x-rapidez::label :label="__('Gender')" class="my-1">
<div class="flex">
<x-rapidez::radio
name="gender"
value="1"
v-model="$root.custom.gender"
@{{ method.title }}
</x-rapidez::radio>
<div class="mt-2" v-if="variables.code === method.code">
<span class="my-1">@lang('In order to use Riverty to pay, we need the following information.')</span>
<x-rapidez::label :label="__('Date of Birth')" class="my-1">
<x-rapidez::input
name="dob"
type="date"
v-model="$root.custom.dob"
>
<div>@lang('Male')</div>
</x-rapidez::radio>
<x-rapidez::radio
name="gender"
value="2"
v-model="$root.custom.gender"
>
<div>@lang('Female')</div>
</x-rapidez::radio>
</div>
</x-rapidez::label>
</div>
</x-rapidez::input>
</x-rapidez::label>
<x-rapidez::label :label="__('Gender')" class="my-1">
<div class="flex">
<x-rapidez::radio
name="gender"
value="1"
v-model="$root.custom.gender"
>
<div>@lang('Male')</div>
</x-rapidez::radio>
<x-rapidez::radio
name="gender"
value="2"
v-model="$root.custom.gender"
>
<div>@lang('Female')</div>
</x-rapidez::radio>
</div>
</x-rapidez::label>
</div>
</template>
4 changes: 4 additions & 0 deletions src/RivertyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'),
Expand Down
21 changes: 21 additions & 0 deletions src/View/Composers/PaymentMethodComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rapidez\Riverty\View\Composers;

use Illuminate\View\View;

class PaymentMethodComposer
{
/**
* Create a new profile composer.
*/
public function __construct() {}

/**
* Bind data to the view.
*/
public function compose(View $view): void
{
$view->getFactory()->startPush('payment_methods', view('riverty::additional-info')->render());
}
}

0 comments on commit 353be04

Please sign in to comment.