Skip to content

Commit

Permalink
UNZER-512 Fix ApplePay Cert
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Tkachev committed Nov 8, 2024
1 parent 0f8de03 commit 396d9f4
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 510 deletions.
81 changes: 46 additions & 35 deletions src/Controller/Admin/ModuleConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,28 @@
use OxidSolutionCatalysts\Unzer\Exception\UnzerException;
use OxidSolutionCatalysts\Unzer\Module;
use OxidSolutionCatalysts\Unzer\Service\ApiClient;
use OxidSolutionCatalysts\Unzer\Service\ModuleConfiguration\ApplePaymentProcessingCertificate;
use OxidSolutionCatalysts\Unzer\Service\ModuleSettings;
use OxidSolutionCatalysts\Unzer\Service\Translator;
use OxidSolutionCatalysts\Unzer\Service\UnzerWebhooks;
use OxidSolutionCatalysts\Unzer\Traits\Request;
use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer;
use Throwable;
use UnzerSDK\Exceptions\UnzerApiException;

/**
* Order class wrapper for Unzer module
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class ModuleConfiguration extends ModuleConfiguration_parent
{
use ServiceContainer;
use Request;

/** @var Translator $translator */
protected $translator = null;
/** @var ModuleSettings $moduleSettings */
protected $moduleSettings = null;
/** @var UnzerWebhooks $unzerWebhooks */
protected $unzerWebhooks = null;
protected ModuleSettings $moduleSettings;
protected UnzerWebhooks $unzerWebhooks;
protected string $_sModuleId; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

/**
* @inheritDoc
*/
public function __construct()
{
parent::__construct();
Expand All @@ -52,8 +47,6 @@ public function __construct()
}

/**
* @inheritDoc
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function render()
Expand All @@ -67,6 +60,11 @@ public function render()
$this->_aViewData['applePayNetworks'] = $this->moduleSettings->getApplePayNetworks();
$this->_aViewData['applePayMerchantCert'] = $this->moduleSettings->getApplePayMerchantCert();
$this->_aViewData['applePayMerchantCertKey'] = $this->moduleSettings->getApplePayMerchantCertKey();
$this->_aViewData['applePayPaymentProcessingCert'] = $this->moduleSettings->getApplePayPaymentCert();

$this->_aViewData['applePayPaymentProcessingCertKey'] =
$this->moduleSettings->getApplePayPaymentPrivateKey();

$this->_aViewData['systemMode'] = $this->moduleSettings->getSystemMode();
} catch (Throwable $loggerException) {
Registry::getUtilsView()->addErrorToDisplay(
Expand Down Expand Up @@ -112,25 +110,32 @@ public function unregisterWebhooks(): void
}

/**
* @throws GuzzleException
* @throws JsonException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ElseExpression)
*/
public function transferApplePayPaymentProcessingData(): void
{
/** @var string $key */
$key = Registry::getRequest()->getRequestEscapedParameter('applePayPaymentProcessingCertKey');
/** @var string $cert */
$cert = Registry::getRequest()->getRequestEscapedParameter('applePayPaymentProcessingCert');
$systemMode = $this->moduleSettings->getSystemMode();
$keyReqName = $systemMode . '-' . 'applePayPaymentProcessingCertKey';
$key = $this->getUnzerStringRequestParameter($keyReqName);
$certReqName = $systemMode . '-' . 'applePayPaymentProcessingCert';
$cert = $this->getUnzerStringRequestParameter($certReqName);
$errorMessage = !$key || !$cert ? 'OSCUNZER_ERROR_TRANSMITTING_APPLEPAY_PAYMENT_SET_CERT' : null;

$apiClient = $this->getServiceFromContainer(ApiClient::class);
$applePayKeyId = null;
$applePayCertId = null;

// save Apple Pay processing cert and key
if (is_null($errorMessage)) {
$applePaymentProcessingCertificateService = $this->getServiceFromContainer(
ApplePaymentProcessingCertificate::class
);
$applePaymentProcessingCertificateService->saveCertificate($cert);
$applePaymentProcessingCertificateService->saveCertificateKey($key);
}

// Upload Key
if (is_null($errorMessage)) {
try {
Expand Down Expand Up @@ -234,26 +239,17 @@ public function getApplePayPaymentProcessingCertExists(): bool
}

/**
* @return void
* @throws FileException
* @SuppressWarnings(PHPMD.StaticAccess)
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
public function saveConfVars()
public function saveConfVars(): void
{
$request = Registry::getRequest();
if (
$request->getRequestEscapedParameter('oxid') &&
$request->getRequestEscapedParameter('oxid') === 'osc-unzer'
) {
// the systemMode is very important, so we set it first ...
/** @var array $confselects */
$confselects = $request->getRequestEscapedParameter('confselects');
/** @var string $systemMode */
$systemMode = $confselects['UnzerSystemMode'];
$this->moduleSettings->setSystemMode($systemMode);

$this->resetContentCache();

$systemMode = $this->moduleSettings->getSystemMode();
$applePayMC = $request->getRequestEscapedParameter('applePayMC');
if (is_array($applePayMC)) {
$this->moduleSettings->saveApplePayMerchantCapabilities($applePayMC);
Expand All @@ -274,13 +270,28 @@ public function saveConfVars()
$this->moduleSettings->getApplePayMerchantCertKeyFilePath(),
$applePayMerchCertKey
);

$applePaymentProcessingCertificateService = $this->getServiceFromContainer(
ApplePaymentProcessingCertificate::class
);

$applePaymentProcessingCertificateService->saveCertificate(
$this->getUnzerStringRequestEscapedParameter(
$systemMode . '-' . 'applePayPaymentProcessingCert'
)
);

$applePaymentProcessingCertificateService->saveCertificateKey(
$this->getUnzerStringRequestEscapedParameter(
$systemMode . '-' . 'applePayPaymentProcessingCertKey'
)
);
}

$this->moduleSettings->saveWebhookConfiguration([]);
$this->registerWebhooks();

parent::saveConfVars();
if ($request->getRequestEscapedParameter('oxid') === 'osc-unzer') {
$this->moduleSettings->saveWebhookConfiguration([]);
$this->registerWebhooks();
}
}

private function addErrorTransmittingCertificate(): void
Expand Down
10 changes: 6 additions & 4 deletions src/Controller/ApplePayCallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\Unzer\Service\ApplePaySessionHandler;
use OxidSolutionCatalysts\Unzer\Service\DebugHandler;
use OxidSolutionCatalysts\Unzer\Service\ResponseHandler;
use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer;

class ApplePayCallbackController extends FrontendController
{
use ServiceContainer;

/**
* @throws JsonException
*/
public function validateMerchant(): void
protected $_sThisTemplate = '@osc-unzer/frontend/tpl/account/account_saved_payments';

public function validateMerchant()
{
/** @var string $merchValidUrlRaw */
$merchValidUrlRaw = Registry::getRequest()->getRequestEscapedParameter('merchantValidationUrl');
$merchValidUrl = urldecode($merchValidUrlRaw);

$responseHandler = $this->getServiceFromContainer(ResponseHandler::class);

$validationResponse = $this
->getServiceFromContainer(ApplePaySessionHandler::class)
->validateMerchant($merchValidUrl);

if (is_array($validationResponse)) {
$responseHandler
->response()
Expand Down
10 changes: 1 addition & 9 deletions src/PaymentExtensions/UnzerPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,17 @@ public function getUnzerOrderId(): string
{
return $this->unzerOrderId;
}
/**
* @return array
*/

public function getPaymentCurrencies(): array
{
return $this->allowedCurrencies;
}

/**
* @return bool
*/
public function redirectUrlNeedPending(): bool
{
return $this->needPending;
}

/**
* @return BasePaymentType
*/
abstract public function getUnzerPaymentTypeObject(): UnzerParentInterface;

/**
Expand Down
4 changes: 0 additions & 4 deletions src/Service/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public function requestApplePayPaymentKey(string $keyId): ?ResponseInterface
return $this->request('keypair/applepay/privatekeys/' . $keyId);
}

/**
* @throws GuzzleException
* @throws JsonException
*/
public function uploadApplePayPaymentKey(string $key): ?ResponseInterface
{
return $this->request('keypair/applepay/privatekeys', 'POST', [
Expand Down
8 changes: 2 additions & 6 deletions src/Service/ApplePaySessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ApplePaySessionHandler
private DebugHandler $logger;

/**
* @param ModuleSettings $moduleSettings
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
public function __construct(ModuleSettings $moduleSettings, DebugHandler $logger)
{
Expand All @@ -35,7 +35,7 @@ public function __construct(ModuleSettings $moduleSettings, DebugHandler $logger
}

/**
* @return void
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
private function initialize(): void
{
Expand Down Expand Up @@ -64,10 +64,6 @@ private function initialize(): void
}
}

/**
* @param string $validationUrl
* @return array|null
*/
public function validateMerchant(string $validationUrl): ?array
{
// if we have no credentials we could not validate Merchant
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace OxidSolutionCatalysts\Unzer\Service\ModuleConfiguration;

use OxidSolutionCatalysts\Unzer\Service\ModuleSettings;

class ApplePaymentProcessingCertificate
{
private ModuleSettings $moduleSettings;

public function __construct(ModuleSettings $moduleSettings)
{
$this->moduleSettings = $moduleSettings;
}
public function saveCertificate(?string $certificate): bool
{
if (is_null($certificate)) {
return false;
}

return (bool) file_put_contents(
$this->moduleSettings->getApplePayPaymentCertFilePath(),
$certificate
);
}

/**
* @throws \OxidEsales\EshopCommunity\Core\Exception\FileException
*/
public function saveCertificateKey(?string $certificateKey): bool
{
if (is_null($certificateKey)) {
return false;
}

return (bool) file_put_contents(
$this->moduleSettings->getApplePayPaymentPrivateKeyFilePath(),
$certificateKey
);
}
}
Loading

0 comments on commit 396d9f4

Please sign in to comment.