Skip to content

Commit

Permalink
POC - Reworked first-run-wizard extension
Browse files Browse the repository at this point in the history
  • Loading branch information
cyl3x committed Dec 2, 2024
1 parent d61b9e6 commit 18537fb
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 220 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Shopware.Component.override('sw-first-run-wizard-paypal-credentials', () => import('./sw-first-run-wizard/sw-first-run-wizard-paypal-credentials'));
if (Shopware.Feature.isActive('PAYPAL_SETTINGS_TWEAKS')) {
Shopware.Component.override('sw-first-run-wizard-paypal-credentials', () => import('./sw-first-run-wizard/sw-first-run-wizard-paypal-credentials'));
} else {
Shopware.Component.override('sw-first-run-wizard-paypal-credentials', () => import('./sw-first-run-wizard/sw-first-run-wizard-paypal-credentials-deprecated'));
}

Shopware.Component.override('sw-sales-channel-modal-detail', () => import('./sw-sales-channel-modal-detail'));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"swag-paypal-frw-credentials": {
"buttonGetCredentials": "Hole API Zugangsdaten",
"buttonGetSandboxCredentials": "Hole Sandbox API Zugangsdaten",
"textIntroPayPal": "Um PayPal zu nutzen müssen nur die API Zugangsdaten eingegeben werden.",
"labelClientId": "Client-ID",
"labelClientSecret": "Client-Secret",
Expand All @@ -15,6 +16,7 @@
"messageFetchedError": " Bitte versuche es erneut oder nutze die erweiterten Einstellungen um die Zugangsdaten direkt einzugeben.",
"textFetchedSuccessful": "Die Zugangsdaten wurden erfolgreich abgerufen.",
"messageNoCredentials": "Es wurden keine Zugangsdaten hinterlegt.",
"messageInvalidCredentials": "Die Zugangsdaten sind ungültig.",
"messageTestSuccess": "Die Zugangsdaten sind gültig."
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"swag-paypal-frw-credentials": {
"buttonGetCredentials": "Get API credentials",
"buttonGetSandboxCredentials": "Get sandbox API credentials",
"textIntroPayPal": "To get PayPal up and running you only need to provide your PayPal API credentials.",
"labelClientId": "Client ID",
"labelClientSecret": "Client secret",
Expand All @@ -15,6 +16,7 @@
"messageFetchedError": "Try again or use the advanced settings to provide your credentials.",
"textFetchedSuccessful": "Credentials have been fetched.",
"messageNoCredentials": "No credentials provided.",
"messageInvalidCredentials": "Credentials are invalid.",
"messageTestSuccess": "Credentials are valid."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import type * as PayPal from 'src/types';
import template from './sw-first-run-wizard-paypal-credentials.html.twig';
import './sw-first-run-wizard-paypal-credentials.scss';

export default Shopware.Component.wrapComponentConfig({
template,

inject: [
'systemConfigApiService',
'SwagPaypalPaymentMethodService',
],

mixins: [
Shopware.Mixin.getByName('swag-paypal-notification'),
Shopware.Mixin.getByName('swag-paypal-credentials-loader'),
],

data() {
return {
config: {} as PayPal.SystemConfig,
isLoading: false,
setDefault: false,
};
},

computed: {
sandboxMode() {
return this.config['SwagPayPal.settings.sandbox'] || false;
},

onboardingUrl() {
return this.sandboxMode ? this.onboardingUrlSandbox : this.onboardingUrlLive;
},

onboardingCallback() {
return this.sandboxMode ? 'onboardingCallbackSandbox' : 'onboardingCallbackLive';
},

buttonConfig() {
const prev = this.$super('buttonConfig') as { key: string; action: () => Promise<boolean> }[];

return prev.map((button) => {
if (button.key === 'next') {
button.action = this.onClickNext.bind(this);
}

return button;
});
},

credentialsProvided() {
return (!this.sandboxMode && this.credentialsProvidedLive)
|| (this.sandboxMode && this.credentialsProvidedSandbox);
},

credentialsProvidedLive() {
return !!this.config['SwagPayPal.settings.clientId']
&& !!this.config['SwagPayPal.settings.clientSecret'];
},

credentialsProvidedSandbox() {
return !!this.config['SwagPayPal.settings.clientIdSandbox']
&& !!this.config['SwagPayPal.settings.clientSecretSandbox'];
},
},

created() {
this.createdComponent();
},

methods: {
createdComponent() {
this.$super('createdComponent');
this.fetchPayPalConfig();
},

onPayPalCredentialsLoadSuccess(clientId: string, clientSecret: string, merchantPayerId: string, sandbox: boolean) {
this.setConfig(clientId, clientSecret, merchantPayerId, sandbox);
},

onPayPalCredentialsLoadFailed(sandbox: boolean) {
this.setConfig('', '', '', sandbox);
this.createNotificationError({
message: this.$tc('swag-paypal-frw-credentials.messageFetchedError'),
// @ts-expect-error - duration is not defined correctly
duration: 10000,
});
},

setConfig(clientId: string, clientSecret: string, merchantPayerId: string, sandbox: boolean) {
const suffix = sandbox ? 'Sandbox' : '';
this.$set(this.config, `SwagPayPal.settings.clientId${suffix}`, clientId);
this.$set(this.config, `SwagPayPal.settings.clientSecret${suffix}`, clientSecret);
this.$set(this.config, `SwagPayPal.settings.merchantPayerId${suffix}`, merchantPayerId);
},

async onClickNext(): Promise<boolean> {
if (!this.credentialsProvided) {
this.createNotificationError({
message: this.$tc('swag-paypal-frw-credentials.messageNoCredentials'),
});

return true;
}

try {
// Do not test the credentials if they have been fetched from the PayPal api
if (!this.isGetCredentialsSuccessful) {
await this.testApiCredentials();
}

await this.saveConfig();

this.$emit('frw-redirect', 'sw.first.run.wizard.index.plugins');

return false;
} catch {
return true;
}
},

fetchPayPalConfig() {
this.isLoading = true;
return this.systemConfigApiService.getValues('SwagPayPal.settings', null)
.then((values: PayPal.SystemConfig) => {
this.config = values;
})
.finally(() => {
this.isLoading = false;
});
},

async saveConfig() {
this.isLoading = true;
await this.systemConfigApiService.saveValues(this.config, null);

if (this.setDefault) {
await this.SwagPaypalPaymentMethodService.setDefaultPaymentForSalesChannel();
}

this.isLoading = false;
},

async testApiCredentials() {
this.isLoading = true;

const sandbox = this.config['SwagPayPal.settings.sandbox'] ?? false;
const sandboxSetting = sandbox ? 'Sandbox' : '';
const clientId = this.config[`SwagPayPal.settings.clientId${sandboxSetting}`];
const clientSecret = this.config[`SwagPayPal.settings.clientSecret${sandboxSetting}`];

const response = await this.SwagPayPalApiCredentialsService
.validateApiCredentials(clientId, clientSecret, sandbox)
.catch((errorResponse: PayPal.ServiceError) => {
this.createNotificationFromError({ errorResponse, title: 'swag-paypal.settingForm.messageTestError' });

return { credentialsValid: false };
});

this.isLoading = false;

return response.credentialsValid ? Promise.resolve() : Promise.reject();
},

onCredentialsChanged() {
this.isGetCredentialsSuccessful = null;
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{% block sw_first_run_wizard_paypal_credentials %}
<div class="sw-first-run-wizard-paypal-credentials">

{% block sw_first_run_wizard_paypal_credentials_inner %}
<sw-loader v-if="isLoading" />

{% block sw_first_run_wizard_paypal_credentials_intro %}
<p class="sw-first-run-wizard-paypal-credentials__headerText">
{{ $tc('swag-paypal-frw-credentials.textIntroPayPal') }}
</p>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_sandbox %}
<sw-switch-field
v-model:value="config['SwagPayPal.settings.sandbox']"
:label="$tc('swag-paypal-frw-credentials.labelSandbox')"
:helpText="$tc('swag-paypal-frw-credentials.tooltipSandbox')"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_button_container %}
<div class="sw-first-run-wizard-paypal-credentials__button-container">

{% block sw_first_run_wizard_paypal_credentials_button %}
<a
class="sw-button sw-button--primary swag-paypal-frw__signup-button"
target="_blank"
:data-paypal-onboard-complete="onboardingCallback"
:href="`${onboardingUrl}`"
data-paypal-button="true"
>
{{ $tc('swag-paypal-frw-credentials.buttonGetCredentials') }}
</a>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_indicator %}
<div class="sw-first-run-wizard-paypal-credentials__indicator">
<template v-if="isGetCredentialsSuccessful">

{% block sw_first_run_wizard_paypal_credentials_indicator_icon %}
<sw-icon
name="regular-checkmark-s"
class="sw-first-run-wizard-paypal-credentials__icon-successful"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_indicator_text %}
<span class="sw-first-run-wizard-paypal-credentials__text-indicator">
{{ $tc('swag-paypal-frw-credentials.textFetchedSuccessful') }}
</span>
{% endblock %}
</template>
</div>
{% endblock %}
</div>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_client_id %}
<sw-text-field
v-model:value="config['SwagPayPal.settings.clientId']"
v-show="!sandboxMode"
:label="$tc('swag-paypal-frw-credentials.labelClientId')"
@update:value="onCredentialsChanged"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_client_secret %}
<sw-text-field
v-model:value="config['SwagPayPal.settings.clientSecret']"
v-show="!sandboxMode"
:label="$tc('swag-paypal-frw-credentials.labelClientSecret')"
@update:value="onCredentialsChanged"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_merchant_id %}
<sw-text-field
v-model:value="config['SwagPayPal.settings.merchantPayerId']"
v-show="!sandboxMode"
:label="$tc('swag-paypal-frw-credentials.labelMerchantPayerId')"
@update:value="onCredentialsChanged"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_client_id_sandbox %}
<sw-text-field
v-model:value="config['SwagPayPal.settings.clientIdSandbox']"
v-show="sandboxMode"
:label="$tc('swag-paypal-frw-credentials.labelClientIdSandbox')"
@update:value="onCredentialsChanged"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_client_secret_sandbox %}
<sw-text-field
v-model:value="config['SwagPayPal.settings.clientSecretSandbox']"
v-show="sandboxMode"
:label="$tc('swag-paypal-frw-credentials.labelClientSecretSandbox')"
@update:value="onCredentialsChanged"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_merchant_id_sandbox %}
<sw-text-field
v-model:value="config['SwagPayPal.settings.merchantPayerIdSandbox']"
v-show="sandboxMode"
:label="$tc('swag-paypal-frw-credentials.labelMerchantPayerIdSandbox')"
@update:value="onCredentialsChanged"
/>
{% endblock %}

{% block sw_first_run_wizard_paypal_credentials_set_default %}
<sw-switch-field
v-model:value="setDefault"
:disabled="!credentialsProvided"
:label="$tc('swag-paypal-frw-credentials.labelSetDefault')"
:helpText="$tc('swag-paypal-frw-credentials.tooltipSetDefault')"
/>
{% endblock %}
{% endblock %}
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@import "~scss/variables";

.sw-first-run-wizard-paypal-credentials {
width: 100%;

.sw-first-run-wizard-paypal-credentials__headerText {
font-weight: bold;
color: $color-darkgray-200;
margin-bottom: 22px;
}

.sw-first-run-wizard-paypal-credentials__button-container {
display: flex;
align-items: center;
margin-bottom: 22px;

.sw-first-run-wizard-paypal-credentials__indicator {
display: inline;
margin-left: 25px;

.sw-first-run-wizard-paypal-credentials__icon-successful {
color: $color-emerald-500;
margin-top: -5px;
}

.sw-first-run-wizard-paypal-credentials__text-indicator {
margin-left: 8px;
}
}
}
}
Loading

0 comments on commit 18537fb

Please sign in to comment.