Skip to content

Commit

Permalink
bill payment initial
Browse files Browse the repository at this point in the history
  • Loading branch information
netzkollektiv committed Jul 22, 2024
1 parent 349c91d commit 3801879
Show file tree
Hide file tree
Showing 31 changed files with 630 additions and 171 deletions.
15 changes: 11 additions & 4 deletions src/Frontend/NetzkollektivEasyCredit/Api/QuoteBuilder.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Shopware\Plugins\NetzkollektivEasyCredit\Api;

use Teambank\RatenkaufByEasyCreditApiV3\Integration;
use Teambank\RatenkaufByEasyCreditApiV3\Model\Transaction;
use Teambank\RatenkaufByEasyCreditApiV3\Model\ShippingAddress;
use Teambank\RatenkaufByEasyCreditApiV3\Model\InvoiceAddress;
Expand Down Expand Up @@ -154,7 +153,9 @@ protected function isExpress() {
}

public function build(): Transaction {
return new Transaction([
$transaction = new Transaction([
'paymentType' => $this->helper->getPaymentType($this->helper->getSelectedPayment()),
'paymentSwitchPossible' => count($this->helper->getActivePaymentMethods()) > 1, // Switch between installment & bill payment should be possible if both methods are enabled
'financingTerm' => $this->getDuration(),
'orderDetails' => new \Teambank\RatenkaufByEasyCreditApiV3\Model\OrderDetails([
'orderValue' => $this->getGrandTotal(),
Expand All @@ -164,16 +165,22 @@ public function build(): Transaction {
'shippingAddress' => $this->isExpress() ? null : $this->getShippingAddress(),
'shoppingCartInformation' => $this->getItems(),
'withoutFlexprice' => (new \EasyCredit_FlexpriceService())->shouldDisableFlexprice()
]),
]),
'shopsystem' => $this->getSystem(),
'customer' => $this->getCustomer()->build(),
'customerRelationship' => new \Teambank\RatenkaufByEasyCreditApiV3\Model\CustomerRelationship([
'customerSince' => ($this->getCustomer()->getCreatedAt() instanceof \DateTime) ? $this->getCustomer()->getCreatedAt()->format('Y-m-d') : null,
'orderDoneWithLogin' => $this->getCustomer()->isLoggedIn(),
'numberOfOrders' => $this->getCustomer()->getOrderCount(),
'logisticsServiceProvider' => $this->getShippingMethod()
'logisticsServiceProvider' => $this->getShippingMethod()
]),
'redirectLinks' => $this->getRedirectLinks()
]);

$transaction = $this->helper->getContainer()
->get('events')
->filter('NetzkollektivEasyCredit_Api_QuoteBuilder_Transaction', $transaction, ['subject' => $this]);

return $transaction;
}
}
68 changes: 49 additions & 19 deletions src/Frontend/NetzkollektivEasyCredit/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Shopware_Plugins_Frontend_NetzkollektivEasyCredit_Bootstrap

public function getLabel()
{
return 'easyCredit-Ratenkauf';
return 'easyCredit';
}

public function getVersion()
Expand Down Expand Up @@ -78,6 +78,7 @@ public function update($version)
$this->_createMenuItem();
$this->_createAttributes();
$this->_applyBrandRelaunch();
$this->_applyBillPaymentMigration();

return array(
'success' => true,
Expand All @@ -103,8 +104,7 @@ public function afterInit()
*/
public function enable()
{
$payment = $this->getPayment();
if ($payment !== null) {
foreach ($this->getPaymentMethods() as $payment) {
$payment->setActive(true);
$this->get('models')->flush($payment);
}
Expand All @@ -125,12 +125,11 @@ public function updateSpecialFieldTypes($type = 'easycreditIntro') {

public function disable()
{
$payment = $this->getPayment();
if ($payment !== null) {
foreach ($this->getPaymentMethods() as $payment) {
$payment->setActive(false);
$this->get('models')->flush($payment);
$this->updateSpecialFieldTypes('button');
}
$this->updateSpecialFieldTypes('button');

return array(
'success' => true,
Expand Down Expand Up @@ -270,23 +269,36 @@ public function registerTemplateDir($viewDir = 'Views') {
$template->addTemplateDir($this->Path() . $viewDir.'/');
}

protected $paymentsCache = null;
public function getPayment()
protected $paymentMethodsCache = null;

public function getPaymentMethods()
{
if (null === $this->paymentsCache) {
$this->paymentsCache = $this->Payments()->findOneBy(
array('name' => 'easycredit')
if (null === $this->paymentMethodsCache) {
$this->paymentMethodsCache = $this->Payments()->findBy(
['class' => 'easycredit']
);
}
return $this->paymentsCache;
return $this->paymentMethodsCache;
}

protected function _createPayment()
{
$this->createPayment(
array(
'name' => self::PAYMENT_NAME,
'description' => $this->getLabel(),
'name' => self::PAYMENT_NAME.'_ratenkauf',
'description' => 'easyCredit-Ratenkauf',
'action' => 'payment_easycredit',
'active' => 0,
'position' => 0,
'additionalDescription' => '',
'template' => 'easycredit.tpl',
'class' => 'easycredit',
)
);
$this->createPayment(
array(
'name' => self::PAYMENT_NAME.'_rechnung',
'description' => 'easyCredit-Rechnung',
'action' => 'payment_easycredit',
'active' => 0,
'position' => 0,
Expand All @@ -306,7 +318,7 @@ protected function _createMenuItem()

$this->createMenuItem(
array(
'label' => 'easyCredit-Ratenkauf',
'label' => 'easyCredit',
'controller' => 'EasycreditMerchant',
'action' => 'Index',
'class' => 'easycredit--icon',
Expand Down Expand Up @@ -372,6 +384,20 @@ public function _applyBrandRelaunch() {
");
}

public function _applyBillPaymentMigration()
{
Shopware()->Db()->query("
UPDATE s_core_plugins Set description = REPLACE(description, 'easyCredit-Ratenkauf','easyCredit') WHERE name = 'NetzkollektivEasyCredit';
UPDATE s_core_config_forms Set
description = REPLACE(description, 'easyCredit-Ratenkauf','easyCredit'),
label = REPLACE(label, 'easyCredit-Ratenkauf','easyCredit')
WHERE name = 'NetzkollektivEasyCredit';
/* UPDATE menu item in backend */
");
}

public function getCheckout() {
return $this->get('easyCreditCheckout');
}
Expand Down Expand Up @@ -421,11 +447,15 @@ public function isResponsive() {

public function isSelected($paymentId = null) {
if ($paymentId == null) {
$helper = new \EasyCredit_Helper();
$user = $helper->getUser();
$paymentId = $user['additional']['payment']['id'];
$paymentId = (new \EasyCredit_Helper())->getSelectedPayment();
}

foreach ($this->getPaymentMethods() as $payment) {
if ($payment->getId() === (int)$paymentId) {
return true;
}
}
return $paymentId == $this->getPayment()->getId();
return false;
}

public function addInterest($refresh = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function build ($form) {
'required' => false,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'stripCharsRe' => ' ',
'description' => 'Die API Signatur sichert die Datenübertragung gegen Datenmanipulation von Dritten ab. Sie können die API-Signatur im easyCredit-Ratenkauf Partnerportal aktivieren.',
'description' => 'Die API Signatur sichert die Datenübertragung gegen Datenmanipulation von Dritten ab. Sie können die API-Signatur im easyCredit Partnerportal aktivieren.',
'position' => $position++
)
);
Expand Down Expand Up @@ -168,7 +168,7 @@ public function build ($form) {
'label' => '„Lieferung melden“ automatisch durchführen?',
'value' => false,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Bei Aktivierung dieser Option wird die Lieferung bei dem in der folgenden Option eingestellten Bestellstatus automatisch an easyCredit-Ratenkauf übermittelt.',
'description' => 'Bei Aktivierung dieser Option wird die Lieferung bei dem in der folgenden Option eingestellten Bestellstatus automatisch an easyCredit übermittelt.',
'position' => $position++
)
);
Expand All @@ -194,7 +194,7 @@ public function build ($form) {
'label' => 'Rückabwicklung automatisch durchführen?',
'value' => false,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Bei Aktivierung dieser Option wird die Rückabwicklung bei dem in der folgenden Option eingestellten Bestellstatus automatisch an easyCredit-Ratenkauf übermittelt.',
'description' => 'Bei Aktivierung dieser Option wird die Rückabwicklung bei dem in der folgenden Option eingestellten Bestellstatus automatisch an easyCredit übermittelt.',
'position' => $position++
)
);
Expand Down Expand Up @@ -243,7 +243,7 @@ public function build ($form) {
'label' => 'Zeige Express-Button auf Detailseite',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Steigern Sie ihre Conversion, indem Sie Kunden ermöglichen mit dem easyCredit-Ratenkauf direkt von der Produktseite aus zu bezahlen.',
'description' => 'Steigern Sie ihre Conversion, indem Sie Kunden ermöglichen mit easyCredit direkt von der Produktseite aus zu bezahlen.',
'position' => $position++
)
);
Expand All @@ -255,7 +255,7 @@ public function build ($form) {
'label' => 'Zeige Express-Button im Warenkorb',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Steigern Sie ihre Conversion, indem Sie Kunden ermöglichen mit dem easyCredit-Ratenkauf direkt aus dem Warenkorb zu bezahlen.',
'description' => 'Steigern Sie ihre Conversion, indem Sie Kunden ermöglichen mit easyCredit direkt aus dem Warenkorb heraus zu bezahlen.',
'position' => $position++
)
);
Expand All @@ -267,7 +267,7 @@ public function build ($form) {
'label' => 'Zeige Express-Button im Off-Canvas Warenkorb',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Steigern Sie ihre Conversion, indem Sie Kunden ermöglichen mit dem easyCredit-Ratenkauf direkt aus dem Off-Canvas Warenkorb zu bezahlen.',
'description' => 'Steigern Sie ihre Conversion, indem Sie Kunden ermöglichen mit easyCredit direkt aus dem Off-Canvas Warenkorb zu bezahlen.',
'position' => $position++
)
);
Expand All @@ -290,7 +290,7 @@ public function build ($form) {
'label' => 'Zeige Ratenrechner-Widget neben Produktpreis',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Für den größten Erfolg mit dem easyCredit-Ratenkauf empfehlen wir, das Widget zu aktivieren.',
'description' => 'Für den größten Erfolg mit easyCredit empfehlen wir, das Widget zu aktivieren.',
'position' => $position++
)
);
Expand All @@ -302,7 +302,7 @@ public function build ($form) {
'label' => 'Zeige Ratenrechner-Widget im Warenkorb',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Für den größten Erfolg mit dem easyCredit-Ratenkauf empfehlen wir, das Widget zu aktivieren.',
'description' => 'Für den größten Erfolg mit easyCredit empfehlen wir, das Widget zu aktivieren.',
'position' => $position++
)
);
Expand All @@ -314,7 +314,7 @@ public function build ($form) {
'label' => 'Zeige Ratenrechner-Widget im Off-Canvas Warenkorb',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'description' => 'Für den größten Erfolg mit dem easyCredit-Ratenkauf empfehlen wir, das Widget zu aktivieren.',
'description' => 'Für den größten Erfolg mit easyCredit empfehlen wir, das Widget zu aktivieren.',
'position' => $position++
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,22 @@ public function createCustomer(TransactionInformation $transaction)
'country' => $countryId,
'state' => null,
'phone' => $contact->getMobilePhoneNumber(),
]);
], $transaction);
return $customerModel;
}

/**
* @return Customer
*/
private function registerCustomer(array $customerData)
private function registerCustomer(array $customerData, TransactionInformation $transaction)
{
$customer = new Customer();
$personalForm = $this->formFactory->create(PersonalFormType::class, $customer);
$personalForm->submit($customerData);

$customer->setPaymentId($this->helper->getPayment()->getId());
$customer->setPaymentId($this->helper->getMethodByPaymentType(
$transaction->getTransaction()->getPaymentType()
)->getId());

$address = new Address();
$addressForm = $this->formFactory->create(AddressFormType::class, $address);
Expand Down
55 changes: 53 additions & 2 deletions src/Frontend/NetzkollektivEasyCredit/Components/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ public function getEntityManager() {
return $this->container->get('models');
}

public function getPayment () {
return $this->getPlugin()->getPayment();
public function getPaymentMethods () {
return $this->getPlugin()->getPaymentMethods();
}

public function getPaymentMethodIds () {
return array_map(function ($payment) {
return $payment->getId();
}, $this->getPaymentMethods());
}

public function getActivePaymentMethods()
{
return array_filter($this->getPaymentMethods(), function ($payment) {
return $payment->getActive();
});
}

protected function getVariable($var, $module) {
Expand All @@ -56,4 +69,42 @@ public function getUser() {
public function getSelectedDispatch() {
return Shopware()->Front()->Plugins()->ViewRenderer()->Action()->View()->getAssign('sDispatch');
}

public function getSelectedPayment()
{
if ($paymentId = $this->getSession()->offsetGet('sPaymentID')) {
return $paymentId;
}
if ($paymentId = $this->getUser()['additional']['payment']['id']) {
return $paymentId;
}
}

public function getPaymentType($paymentId) {
$methods = $this->getPaymentMethods();
foreach ($methods as $method) {
if ($method->getId() !== (int) $paymentId) {
continue;
}

if ($method->getName() == 'easycredit_ratenkauf') {
return 'INSTALLMENT_PAYMENT';
}
if ($method->getName() == 'easycredit_rechnung') {
return 'BILL_PAYMENT';
}
}
}

public function getMethodByPaymentType($paymentType) {
return current(array_filter($this->getPaymentMethods(), function ($payment) use ($paymentType) {
$type = $this->getPaymentType($payment->getId());
if ($paymentType === $type ||
$paymentType === str_replace('_PAYMENT', '', $type)
) {
return true;
}
return false;
}));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
use Shopware\Models\Order\Order;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Query\Expr\Join;
use Shopware\Components\Model\QueryBuilder;
use Teambank\RatenkaufByEasyCreditApiV3\ApiException;
Expand All @@ -24,14 +25,14 @@ abstract class Shopware_Controllers_Backend_EasycreditMerchant_Abstract extends
private function prepareOrderQueryBuilder(QueryBuilder $builder)
{
$helper = new EasyCredit_Helper();
$paymentId = $helper->getPayment()->getId();

$builder->innerJoin(
'sOrder.payment',
'payment',
Join::WITH,
'payment.id = :paymentId'
)->setParameter('paymentId', $paymentId, \PDO::PARAM_INT);
$builder->expr()->in('payment.id', ':paymentIds')
);
$builder->setParameter('paymentIds', $helper->getPaymentMethodIds(), Connection::PARAM_INT_ARRAY);

$builder->leftJoin('sOrder.languageSubShop', 'languageSubShop')
->leftJoin('sOrder.customer', 'customer')
Expand Down Expand Up @@ -205,4 +206,3 @@ class Shopware_Controllers_Backend_EasycreditMerchant extends Shopware_Controlle

}
}

Loading

0 comments on commit 3801879

Please sign in to comment.