Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Added logo URL validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lucionescu committed Sep 15, 2023
1 parent 6598501 commit 6177dcd
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 32 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lunar/plugin-shopware-6",
"description": "Lunar Online Payments for Shopware",
"version": "2.1.0",
"version": "2.2.0",
"type": "shopware-platform-plugin",
"license": "MIT",
"authors": [
Expand Down
70 changes: 59 additions & 11 deletions src/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Lunar\Payment\Helpers\ValidationHelper;

/**
* @Route(defaults={"_routeScope"={"administration"}})
* @Route(defaults={"_routeScope"={"api"}})
*/
class SettingsController extends AbstractController
{
Expand All @@ -33,17 +33,70 @@ public function __construct(
}

/**
* Temporary added - we don't validate the keys
*
* @Route("/api/lunar/validate-api-keys", name="api.lunar.validate.api.keys", methods={"POST"})
* @Route("/api/lunar/validate-settings", name="api.lunar.validate.settings", methods={"POST"})
*/
public function validateApiKeys(Request $request, Context $context): JsonResponse
public function validatesettings(Request $request, Context $context): JsonResponse
{
$settingsData = $request->request->all()['keys'] ?? [];

$cardLogoURLKey = 'cardLogoURL';
$mobilePayLogoURLKey = 'mobilePayLogoURL';
$this->validateLogoURL($settingsData[$cardLogoURLKey], $cardLogoURLKey);
$this->validateLogoURL($settingsData[$mobilePayLogoURLKey], $mobilePayLogoURLKey);


if (!empty($this->errors)) {
return new JsonResponse([
'message' => 'Error',
'errors'=> $this->errors,
], 400);
}

return new JsonResponse([
'message' => 'Success',
], 200);
}


/**
* @return void
*/
private function validateLogoURL($url, $errorKey)
{
if (!preg_match('/^https:\/\//', $url)) {
$this->errors[$errorKey] = 'Logo URL must start with https://. ' . "($errorKey)";
return;
}

if (!$this->fileExists($url)) {
$this->errors[$errorKey] = 'Logo URL is invalid. ' . "($errorKey)";
}
}


/**
* @return bool
*/
private function fileExists($url)
{
$valid = true;

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_NOBODY, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FRESH_CONNECT, 1);

if(!curl_exec($c)){
$valid = false;
}

curl_close($c);

return $valid;
}

/**
* @TODO add validate logo URL logic
*/
Expand All @@ -66,12 +119,7 @@ public function validateApiKeys(Request $request, Context $context): JsonRespons
// $this->validateTestAppKey($settingsKeys[$testAppKeyName] ?? '');
// $this->validateTestPublicKey($settingsKeys[$testPublicKeyName] ?? '');

// if (!empty($this->errors)) {
// return new JsonResponse([
// 'message' => 'Error',
// 'errors'=> $this->errors,
// ], 400);
// }



// return new JsonResponse([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class LunarPaymentSettingsService extends ApiService {
this.apiRoute = `${this.getApiBasePath()}`;
}

validateApiKeys(keys) {
validateSettings(keys) {
return this.httpClient.post(
this.apiRoute + `/validate-api-keys`,
this.apiRoute + `/validate-settings`,
{
keys: keys,
headers: this.getBasicHeaders()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Component.register('lunar-settings', {
isSaveSuccessful: false,
config: {},
configPath: 'LunarPayment.settings.',
showApiErrors: false,
apiResponseErrors: {},
cardLogoURLKey: 'cardLogoURL',
mobilePayLogoURLKey: 'mobilePayLogoURL',
};
},

Expand All @@ -47,46 +51,141 @@ Component.register('lunar-settings', {
onConfigChange(config) {
this.config = config;
},

/**
*
*/
getConfigValue(field) {
const defaultConfig = this.$refs.systemConfig.actualConfigData.null;
const salesChannelId = this.$refs.systemConfig.currentSalesChannelId;

if (salesChannelId === null) {
return this.config[`${this.configPath}${field}`];
}
return this.config[`${this.configPath}${field}`] || defaultConfig[`${this.configPath}${field}`];
},

/**
*
*/
onSave() {
this.isLoading = true;
const titleError = this.$tc('lunar-payment.settings.titleError');

this.isSaveSuccessful = false;

let data = {
cardLogoURL: this.getConfigValue(this.cardLogoURLKey),
mobilePayLogoURL: this.getConfigValue(this.mobilePayLogoURLKey),
// transactionMode: this.getConfigValue(this.transactionModeConfigKey),
// liveModeAppKey: this.getConfigValue(this.liveAppConfigKey),
// liveModePublicKey: this.getConfigValue(this.livePublicConfigKey),
// testModeAppKey: this.getConfigValue(this.testAppConfigKey),
// testModePublicKey: this.getConfigValue(this.testPublicConfigKey),
}

/**
* Save configuration.
* Validate API keys
*/
this.$refs.systemConfig.saveAll().then(() => {
this.createNotificationSuccess({
title: this.$tc('lunar-payment.settings.titleSuccess'),
message: this.$tc('lunar-payment.settings.generalSuccess'),
});
this.LunarPaymentSettingsService.validateSettings(data)
.then((response) => {

/** Clear errors. */
this.$refs.systemConfig.config.forEach((configElement) => {
configElement.elements.forEach((child) => {
delete child.config.error;
});
});

/**
* Save configuration.
*/
this.$refs.systemConfig.saveAll().then(() => {
this.createNotificationSuccess({
title: this.$tc('lunar-payment.settings.titleSuccess'),
message: this.$tc('lunar-payment.settings.generalSuccess'),
});

this.isSaveSuccessful = true;

}).catch((errorResponse) => {
console.log(errorResponse);

this.createNotificationError({
title: titleError,
message: this.$tc('lunar-payment.settings.generalSaveError'),
});
});

this.isLoading = false;

this.isSaveSuccessful = true;

}).catch((errorResponse) => {
console.log(errorResponse);
let apiResponseErrors = errorResponse.response.data.errors;

this.createNotificationError({
title: titleError,
message: this.$tc('lunar-payment.settings.generalSaveError'),
Object.entries(apiResponseErrors).forEach(([key, errorMessage]) => {
this.createNotificationError({
title: titleError,
message: errorMessage,
})
});
});

this.isLoading = false;
this.showApiErrors = true;
this.apiResponseErrors = apiResponseErrors;
this.isLoading = false;
this.isSaveSuccessful = false;
});

},

/**
*
*/
getBind(element, config) {
if (config !== this.config) {
this.config = config;
// if (config !== this.config) {
// this.config = config;
// }


if (this.showApiErrors) {
if (
element.name === `${this.configPath}${this.cardLogoURLKey}`
&& this.apiResponseErrors.hasOwnProperty(this.cardLogoURLKey)
) {
element.config.error = {code: 1, detail: this.$tc('lunar-payment.settings.cardLogoURLInvalid')};
}
if (
element.name === `${this.configPath}${this.mobilePayLogoURLKey}`
&& this.apiResponseErrors.hasOwnProperty(this.mobilePayLogoURLKey)
) {
element.config.error = {code: 1, detail: this.$tc('lunar-payment.settings.mobilePayLogoURLInvalid')};
}
// if (
// element.name === `${this.configPath}${this.liveAppConfigKey}`
// && this.apiResponseErrors.hasOwnProperty(this.liveAppConfigKey)
// ) {
// element.config.error = {code: 1, detail: this.$tc('lunar-payment.settings.liveAppKeyInvalid')};
// }
// if (
// element.name === `${this.configPath}${this.livePublicConfigKey}`
// && this.apiResponseErrors.hasOwnProperty(this.livePublicConfigKey)
// ) {
// element.config.error = {code: 1, detail: this.$tc('lunar-payment.settings.livePublicKeyInvalid')};
// }
// if (
// element.name === `${this.configPath}${this.testAppConfigKey}`
// && this.apiResponseErrors.hasOwnProperty(this.testAppConfigKey)
// ) {
// element.config.error = {code: 1, detail: this.$tc('lunar-payment.settings.testAppKeyInvalid')};
// }
// if (
// element.name === `${this.configPath}${this.testPublicConfigKey}`
// && this.apiResponseErrors.hasOwnProperty(this.testPublicConfigKey)
// ) {
// element.config.error = {code: 1, detail: this.$tc('lunar-payment.settings.testPublicKeyInvalid')};
// }
}


let originalElement;

if (config !== this.config) {
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/app/administration/src/snippet/en_GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"liveAppKeyInvalid": "The App key is invalid",
"livePublicKeyInvalid": "The Public key is invalid",
"testAppKeyInvalid": "The test App key is invalid",
"testPublicKeyInvalid": "The test Public key is invalid"
"testPublicKeyInvalid": "The test Public key is invalid",
"cardLogoURLInvalid": "Cards logo URL is invalid",
"mobilePayLogoURLInvalid": "Mobile Pay logo URL is invalid"
},
"messageNotBlank": "This field must not be empty.",
"transactionId": "transaction ID",
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/administration/js/lunar-payment.js

Large diffs are not rendered by default.

0 comments on commit 6177dcd

Please sign in to comment.