Skip to content

Commit

Permalink
Merge pull request #217 from OXID-eSales/UNZER-150_Save_Payment_Data
Browse files Browse the repository at this point in the history
Unzer 150 save payment data
  • Loading branch information
mariolorenz authored Nov 29, 2023
2 parents 42d6924 + f8e1849 commit f41a115
Show file tree
Hide file tree
Showing 20 changed files with 949 additions and 169 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ php-cs-fixer.phar
/Tests/Codeception/_output/
/Tests/Codeception/_support/
/files/*
!/files/.htaccess
!/files/.htaccess
.idea
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Discounts with time restrictions may not be invalidated directly in the checkout...
- provided additional Order-Number is searchable
- increase line spacing for the error messages
- Save Payment Data for registered Users

## [1.1.3] - 2023-11-14

Expand Down
17 changes: 16 additions & 1 deletion metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

use OxidSolutionCatalysts\Unzer\Model\DiscountList;
use OxidSolutionCatalysts\Unzer\Controller\AccountSavedPaymentController;
use OxidSolutionCatalysts\Unzer\Controller\Admin\AdminOrderController;
use OxidSolutionCatalysts\Unzer\Controller\Admin\ModuleConfiguration;
use OxidSolutionCatalysts\Unzer\Controller\Admin\OrderMain;
Expand Down Expand Up @@ -52,7 +53,7 @@
</ul>',
],
'thumbnail' => 'logo.svg',
'version' => '1.1.4-rc.1',
'version' => '1.1.4-rc.2',
'author' => 'OXID eSales AG',
'url' => 'https://www.oxid-esales.com',
'email' => '[email protected]',
Expand All @@ -75,6 +76,7 @@
'unzer_dispatcher' => DispatcherController::class,
'unzer_installment' => InstallmentController::class,
'unzer_applepay_callback' => ApplePayCallbackController::class,
'unzer_saved_payments' => AccountSavedPaymentController::class,
],
'templates' => [
// admin
Expand All @@ -83,6 +85,7 @@
// frontend
'modules/osc/unzer/unzer_assets.tpl' => 'osc/unzer/views/frontend/tpl/order/unzer_assets.tpl',
'modules/osc/unzer/unzer_card.tpl' => 'osc/unzer/views/frontend/tpl/order/unzer_card.tpl',
'modules/osc/unzer/unzer_paypal.tpl' => 'osc/unzer/views/frontend/tpl/order/unzer_paypal.tpl',
'modules/osc/unzer/unzer_eps_charge.tpl' => 'osc/unzer/views/frontend/tpl/order/unzer_eps_charge.tpl',
'modules/osc/unzer/unzer_installment.tpl' => 'osc/unzer/views/frontend/tpl/order/unzer_installment.tpl',
'modules/osc/unzer/unzer_installment_confirm.tpl' => 'osc/unzer/views/frontend/tpl/order/unzer_installment_confirm.tpl',
Expand All @@ -99,6 +102,7 @@
'modules/osc/unzer/payment/applepay_availibility_check.tpl' => 'osc/unzer/views/frontend/tpl/payment/applepay_availibility_check.tpl',
'modules/osc/unzer/payment/payment_unzer.tpl' => 'osc/unzer/views/frontend/tpl/payment/payment_unzer.tpl',
'modules/osc/unzer/order/applepay_button.tpl' => 'osc/unzer/views/frontend/tpl/order/applepay_button.tpl',
'modules/osc/unzer/account_saved_payments.tpl' => 'osc/unzer/views/frontend/tpl/account/account_saved_payments.tpl',
],
'blocks' => [
//frontend
Expand Down Expand Up @@ -129,6 +133,11 @@
'block' => 'select_payment',
'file' => 'views/frontend/blocks/page/checkout/select_payment.tpl'
],
[
'template' => 'page/account/inc/account_menu.tpl',
'block' => 'account_menu',
'file' => 'views/frontend/blocks/page/account/inc/account_menu.tpl'
],
//admin
[
'template' => 'module_config.tpl',
Expand Down Expand Up @@ -166,6 +175,12 @@
'type' => 'bool',
'value' => '0',
],
[
'group' => 'unzerenvironment',
'name' => 'UnzerSavePaymentsForUser',
'type' => 'bool',
'value' => '0',
],
[
'group' => 'unzermerchant',
'name' => 'sandbox-UnzerPublicKey',
Expand Down
37 changes: 37 additions & 0 deletions migration/data/Version20230927114315.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace OxidSolutionCatalysts\Unzer\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230927114315 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->updateTransactionTable($schema);
}
protected function updateTransactionTable(Schema $schema): void
{
$transaction = $schema->getTable('oscunzertransaction');

if (!$transaction->hasColumn('PAYMENTTYPEID')) {
$transaction->addColumn('PAYMENTTYPEID', Types::STRING, ['default' => ""]);
}
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
}
}
90 changes: 90 additions & 0 deletions src/Controller/AccountSavedPaymentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidSolutionCatalysts\Unzer\Controller;

use OxidEsales\Eshop\Application\Controller\AccountController;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidSolutionCatalysts\Unzer\Service\UnzerSDKLoader;
use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer;
use OxidEsales\Eshop\Core\Registry;

class AccountSavedPaymentController extends AccountController
{
use ServiceContainer;

public function render()
{
parent::render();
$this->redirectAfterLogin();

$user = $this->getUser();
if ($user && $user->getFieldData('oxpassword')) {
$this->setPaymentListsToView();
return "modules/osc/unzer/account_saved_payments.tpl";
}
return $this->_sThisLoginTemplate;
}

protected function setPaymentListsToView(): void
{
$UnzerSdk = $this->getServiceFromContainer(UnzerSDKLoader::class);
$unzerSDK = $UnzerSdk->getUnzerSDK();

$ids = $this->getTransactionIds();
$paymentTypes = false;
foreach ($ids as $typeId) {
if (empty($typeId['PAYMENTTYPEID'])) {
continue;
}
$paymentType = $unzerSDK->fetchPaymentType($typeId['PAYMENTTYPEID']);

if (strpos($typeId['PAYMENTTYPEID'], 'crd') && method_exists($paymentType, 'getBrand')) {
$paymentTypes[$paymentType->getBrand()][$typeId['OXID']] = $paymentType->expose();
}
if (strpos($typeId['PAYMENTTYPEID'], 'ppl')) {
$paymentTypes['paypal'][$typeId['OXID']] = $paymentType->expose();
}
if (strpos($typeId['PAYMENTTYPEID'], 'sdd')) {
$paymentTypes['sepa'][$typeId['OXID']] = $paymentType->expose();
}
}

$this->_aViewData['unzerPaymentType'] = $paymentTypes;
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function getTransactionIds(): array
{
$result = [];
if ($this->getUser()) {
$oDB = DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC);
$result = $oDB->getAll(
"SELECT OXID, PAYMENTTYPEID from oscunzertransaction
where OXUSERID = :oxuserid AND PAYMENTTYPEID IS NOT NULL GROUP BY PAYMENTTYPEID ",
[':oxuserid' => $this->getUser()->getId()]
);
}
return $result;
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function deletePayment(): void
{
$paymenttypeid = Registry::getRequest()->getRequestParameter('paymenttypeid');
$oDB = DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC);
$oDB->getAll(
"UPDATE oscunzertransaction SET PAYMENTTYPEID = NULL
WHERE OXUSERID = :oxuserid AND OXID = :oxid",
[':oxuserid' => $this->getUser()->getId(), 'oxid' => $paymenttypeid]
);
}
}
63 changes: 63 additions & 0 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use OxidSolutionCatalysts\Unzer\Service\ModuleSettings;
use OxidSolutionCatalysts\Unzer\Service\Payment as PaymentService;
use OxidSolutionCatalysts\Unzer\Service\ResponseHandler;
use OxidSolutionCatalysts\Unzer\Service\Transaction;
use OxidSolutionCatalysts\Unzer\Service\Translator;
use OxidSolutionCatalysts\Unzer\Service\Unzer;
use OxidSolutionCatalysts\Unzer\Service\UnzerSDKLoader;
use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer;
use OxidSolutionCatalysts\Unzer\Service\UnzerDefinitions;
use OxidSolutionCatalysts\Unzer\Core\UnzerDefinitions as CoreUnzerDefinitions;
Expand Down Expand Up @@ -60,6 +62,7 @@ public function render()
// generate always a new threat metrix session id
$unzer = $this->getServiceFromContainer(Unzer::class);
$this->_aViewData['unzerThreatMetrixSessionID'] = $unzer->generateUnzerThreatMetrixIdInSession();
$this->getSavedPayment();
return parent::render();
}

Expand Down Expand Up @@ -236,6 +239,21 @@ public function getActualOrder(): Order
return $this->actualOrder;
}

/**
* @return int|mixed
*/
public function getPaymentSaveSetting()
{
$bSavedPayment = Registry::getConfig()->getConfigParam('UnzerSavePaymentsForUser');

// no guests allowed
$user = $this->getUser();
if (!$user || (!$user->getFieldData('oxpassword'))) {
$bSavedPayment = 0;
return $bSavedPayment;
}
return $bSavedPayment;
}
/**
* @return array
*
Expand Down Expand Up @@ -297,4 +315,49 @@ public function getExecuteFnc()
}
return parent::getExecuteFnc();
}
protected function getSavedPayment(): void
{
$UnzerSdk = $this->getServiceFromContainer(UnzerSDKLoader::class);
$unzerSDK = $UnzerSdk->getUnzerSDK();

$ids = $this->getTrancactionIds();
$paymentTypes = false;
if ($ids) {
foreach ($ids as $typeId) {
if (!empty($typeId['PAYMENTTYPEID'])) {
$paymentType = $unzerSDK->fetchPaymentType($typeId['PAYMENTTYPEID']);

if (strpos($typeId['PAYMENTTYPEID'], 'crd')) {
$paymentTypes['card'][$typeId['PAYMENTTYPEID']] = $paymentType->expose();
}
if (strpos($typeId['PAYMENTTYPEID'], 'ppl')) {
$paymentTypes['paypal'][$typeId['PAYMENTTYPEID']] = $paymentType->expose();
}
if (strpos($typeId['PAYMENTTYPEID'], 'sdd')) {
$paymentTypes['sepa'][$typeId['PAYMENTTYPEID']] = $paymentType->expose();
}
}
}
}

$this->_aViewData['unzerPaymentType'] = $paymentTypes;
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function getTrancactionIds(): array
{
$result = [];
if ($this->getUser()->getId() !== null) {
$oDB = DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC);
$result = $oDB->getAll(
"SELECT PAYMENTTYPEID from oscunzertransaction
where OXUSERID = :oxuserid AND PAYMENTTYPEID IS NOT NULL
GROUP BY PAYMENTTYPEID ",
[':oxuserid' => $this->getUser()->getId()]
);
}
return $result;
}
}
40 changes: 38 additions & 2 deletions src/PaymentExtensions/UnzerPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@
use Exception;
use OxidEsales\Eshop\Application\Model\Basket;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\Unzer\Model\Transaction;
use OxidSolutionCatalysts\Unzer\Service\Payment as PaymentService;
use OxidSolutionCatalysts\Unzer\Service\Unzer as UnzerService;
use OxidSolutionCatalysts\Unzer\Service\Transaction as TransactionService;
use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer;
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Resources\PaymentTypes\BasePaymentType;
use UnzerSDK\Resources\TransactionTypes\Authorization;
use UnzerSDK\Unzer;
use JsonException;

/**
* @SuppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class UnzerPayment
{
use ServiceContainer;

/** @var Unzer */
protected $unzerSDK;

Expand Down Expand Up @@ -85,14 +90,25 @@ abstract public function getUnzerPaymentTypeObject(): BasePaymentType;
* @param Basket $basketModel
* @return bool
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.StaticAccess)
* @throws JsonException
* @throws UnzerApiException
*/
public function execute(
User $userModel,
Basket $basketModel
): bool {
$request = Registry::getRequest();
$paymentType = $this->getUnzerPaymentTypeObject();
if ($paymentType instanceof \UnzerSDK\Resources\PaymentTypes\Paypal) {
$paymentData = $request->getRequestParameter('paymentData');
$paymentData = is_string($paymentData) ? $paymentData : '';
$aPaymentData = json_decode($paymentData, true, 512, JSON_THROW_ON_ERROR);
if (is_array($aPaymentData) && isset($aPaymentData['id'])) {
$paymentType->setId($aPaymentData['id']);
}
}
/** @var string $companyType */
$companyType = $request->getRequestParameter('unzer_company_form', '');

Expand Down Expand Up @@ -136,7 +152,27 @@ public function execute(
if ($request->getRequestParameter('birthdate')) {
$userModel->save();
}
$savePayment = Registry::getRequest()->getRequestParameter('oscunzersavepayment');

if ($savePayment === "1" && $userModel->getId()) {
/** @var TransactionService $transactionService */
$transactionService = $this->getServiceFromContainer(
Transaction::class
);
$payment = $this->getServiceFromContainer(PaymentService::class)
->getSessionUnzerPayment();
try {
$transactionService->writeTransactionToDB(
Registry::getSession()->getSessionChallengeToken(),
$userModel->getId(),
$payment
);
} catch (Exception $e) {
Registry::getLogger()->info(
'Could not save Transaction for PaymentID (savePayment): ' . $e->getMessage()
);
}
}
return true;
}
}
Loading

0 comments on commit f41a115

Please sign in to comment.