Source of file Prepayment.php
Size: 19,161 Bytes - Last Modified: 2021-12-23T10:21:29+00:00
/var/www/docs.ssmods.com/process/src/code/Model/Prepayment.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 | <?php namespace SilverCart\Prepayment\Model; use SilverCart\Admin\Forms\GridField\GridFieldConfig_ExclusiveRelationEditor; use SilverCart\Forms\FormFields\FieldGroup; use SilverCart\Forms\FormFields\TextField; use SilverCart\Model\Order\Order; use SilverCart\Model\Payment\PaymentMethod; use SilverCart\Model\ShopEmail; use SilverCart\Model\Translation\TranslationTools; use SilverCart\Prepayment\Model\PrepaymentTranslation; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTP; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\ReadonlyField; use SilverStripe\Forms\Tab; use SilverStripe\Forms\GridField\GridField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\Filters\ExactMatchFilter; use SilverStripe\ORM\Filters\GreaterThanFilter; use SilverStripe\ORM\Filters\LessThanFilter; use SilverStripe\ORM\Filters\PartialMatchFilter; use SilverStripe\View\ArrayData; use SilverStripe\View\SSViewer_FromString; /** * prepayment module * * @package SilverCart * @subpackage Prepayment_Model * @author Sebastian Diel <sdiel@pixeltricks.de> * @copyright 2017 pixeltricks GmbH * @since 22.08.2017 * @license see license file in modules root directory * * @property string $PaymentChannel Payment channel * @property string $BankAccountData Bank account data * * @method \SilverStripe\ORM\HasManyList PrepaymentTranslations() Returns the related PrepaymentTranslations. */ class Prepayment extends PaymentMethod { /** * A list of possible payment channels. * * @var array */ private static $possible_payment_channels = [ 'prepayment' => 'Prepayment', 'invoice' => 'Invoice', ]; /** * classes attributes * * @var array */ private static $db = [ 'PaymentChannel' => 'Enum("prepayment,invoice","prepayment")', 'BankAccountData' => 'Text', ]; /** * 1:n relationships. * * @var array */ private static $has_many = [ 'PrepaymentTranslations' => PrepaymentTranslation::class, ]; /** * Casted attributes * * @var array */ private static $casting = [ 'TextBankAccountInfo' => 'Text', 'InvoiceInfo' => 'Text', 'BankAccounts' => ArrayList::class, ]; /** * DB table name * * @var string */ private static $table_name = 'SilvercartPaymentPrepayment'; /** * module name to be shown in backend interface * * @var string */ protected $moduleName = 'Prepayment'; /** * Field labels for display in tables. * * @param boolean $includerelations A boolean value to indicate if the labels returned include relation fields * * @return array */ public function fieldLabels($includerelations = true) : array { $fieldLabels = array_merge( parent::fieldLabels($includerelations), [ 'TextBankAccountInfo' => PrepaymentTranslation::singleton()->fieldLabel('TextBankAccountInfo'), 'InvoiceInfo' => PrepaymentTranslation::singleton()->fieldLabel('InvoiceInfo'), 'PrepaymentTranslations' => PrepaymentTranslation::singleton()->plural_name(), 'PaymentChannel' => _t(self::class . '.PAYMENT_CHANNEL', 'Payment Channel'), 'TextTemplates' => _t(self::class . '.TextTemplates', 'text templates'), 'InfoMailSubject' => _t(self::class . '.InfoMailSubject', 'payment information regarding your order'), 'BankAccount' => _t(self::class . '.BankAccount', 'Bank Account'), 'BankAccounts' => _t(self::class . '.BankAccounts', 'Bank Accounts'), 'BankAccountOwner' => _t(self::class . '.BankAccountOwner', 'Account Holder'), 'BankAccountName' => _t(self::class . '.BankAccountName', 'Bank'), 'BankAccountIBAN' => _t(self::class . '.BankAccountIBAN', 'IBAN'), 'BankAccountBIC' => _t(self::class . '.BankAccountBIC', 'BIC / SWIFT'), ] ); $this->extend('updateFieldLabels', $fieldLabels); return $fieldLabels; } /** * input fields for editing * * @param mixed $params optional * * @return FieldList */ public function getCMSFields() : FieldList { $this->beforeUpdateCMSFields(function(FieldList $fields) { $fields->removeByName('InvoiceInfo'); $fields->removeByName('TextBankAccountInfo'); $fields->removeByName('BankAccountData'); $tabTextTemplates = Tab::create($this->fieldLabel('TextTemplates')); $fields->fieldByName('Root')->push($tabTextTemplates); $languageFields = TranslationTools::prepare_cms_fields($this->getTranslationClassName()); switch ($this->PaymentChannel) { case 'invoice': $tabTextTemplates->setChildren(FieldList::create($languageFields->fieldByName('InvoiceInfo'))); break; case 'prepayment': $tabTextTemplates->setChildren(FieldList::create($languageFields->fieldByName('TextBankAccountInfo'))); break; default: break; } $translations = GridField::create( 'PrepaymentTranslations', $this->fieldLabel('PrepaymentTranslations'), $this->PrepaymentTranslations(), GridFieldConfig_ExclusiveRelationEditor::create() ); $fields->addFieldToTab('Root.Translations', $translations); $this->addBankAccountCMSFields($fields); }); return parent::getCMSFieldsForModules(); } /** * creates default objects * * @return void */ public function requireDefaultRecords() : void { parent::requireDefaultRecords(); $infoMail = ShopEmail::get()->filter('TemplateName', 'PaymentPrepaymentBankAccountInfo')->first(); if (is_null($infoMail) || !$infoMail->exists() ) { $infoMail = ShopEmail::create(); $infoMail->TemplateName = 'PaymentPrepaymentBankAccountInfo'; $infoMail->Subject = $this->fieldLabel('InfoMailSubject'); $infoMail->write(); } } /** * Searchable fields * * @return array */ public function searchableFields() : array { $searchableFields = [ "PrepaymentTranslations.Name" => [ 'title' => $this->fieldLabel('Title'), 'filter' => PartialMatchFilter::class, ], 'isActive' => [ 'title' => $this->fieldLabel('isActive'), 'filter' => ExactMatchFilter::class, ], 'minAmountForActivation' => [ 'title' => $this->fieldLabel('MinAmountForActivation'), 'filter' => GreaterThanFilter::class, ], 'maxAmountForActivation' => [ 'title' => $this->fieldLabel('MaxAmountForActivation'), 'filter' => LessThanFilter::class, ], 'Zone.ID' => [ 'title' => $this->fieldLabel('AttributedZones'), 'filter' => ExactMatchFilter::class, ], 'Countries.ID' => [ 'title' => $this->fieldLabel('AttributedCountries'), 'filter' => ExactMatchFilter::class, ] ]; $this->extend('updateSearchableFields', $searchableFields); return $searchableFields; } /** * Called on before write. * * @return void */ public function onBeforeWrite() : void { parent::onBeforeWrite(); $this->writeBankAccounts(); } /*********************************************************************************************** *********************************************************************************************** ** ** ** Mutator methods for casted attributes ** ** ** *********************************************************************************************** **********************************************************************************************/ /** * getter for the multilingual attribute TextBankAccountInfo * * @return string */ public function getTextBankAccountInfo() { return $this->getTranslationFieldValue('TextBankAccountInfo'); } /** * getter for the multilingual attribute InvoiceInfo * * @return string */ public function getInvoiceInfo() { return $this->getTranslationFieldValue('InvoiceInfo'); } /*********************************************************************************************** *********************************************************************************************** ** ** ** Bank account handling methods ** ** ** *********************************************************************************************** **********************************************************************************************/ /** * Returns the bank accounts. * * @return ArrayList */ public function getBankAccounts() : ArrayList { if ($this->PaymentChannel != 'prepayment') { return ArrayList::create(); } $bankAccounts = ArrayList::create(); $bankAccountData = unserialize($this->BankAccountData); if (is_array($bankAccountData)) { foreach ($bankAccountData as $ID => $data) { if (empty($data['Owner']) && empty($data['Name']) && empty($data['IBAN']) && empty($data['BIC']) ) { continue; } $bankAccounts->add(ArrayData::create([ 'ID' => $ID, 'Owner' => $data['Owner'], 'Name' => $data['Name'], 'IBAN' => $data['IBAN'], 'BIC' => $data['BIC'], ])); } } return $bankAccounts; } /** * Adds the bank account related CMS fields. * * @param FieldList $fields Field list * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 18.04.2018 */ protected function addBankAccountCMSFields(FieldList $fields) : void { if ($this->PaymentChannel != 'prepayment') { return; } $fields->findOrMakeTab('Root.BankAccounts', $this->fieldLabel('BankAccounts')); $bankAccounts = $this->getBankAccounts(); $highestID = 0; if ($bankAccounts->exists()) { $highestID = $bankAccounts->sort('ID', 'DESC')->first()->ID; } $bankAccounts->add(ArrayData::create(['ID' => $highestID+1, 'Name' => '', 'IBAN' => '', 'BIC' => ''])); $index = 1; foreach ($bankAccounts as $bankAccount) { $bankAccountGroup = FieldGroup::create('BankAccountGroup' . $bankAccount->ID, '', $fields); $bankAccountGroup->push(ReadonlyField::create('BankAccuntLabel' . $bankAccount->ID, "{$this->fieldLabel('BankAccount')} {$index}", ' ')); $bankAccountGroup->push(TextField::create('BankAccounts[' . $bankAccount->ID . '][Owner]', $this->fieldLabel('BankAccountOwner'), $bankAccount->Owner)); $bankAccountGroup->push(TextField::create('BankAccounts[' . $bankAccount->ID . '][Name]', $this->fieldLabel('BankAccountName'), $bankAccount->Name)); $bankAccountGroup->push(TextField::create('BankAccounts[' . $bankAccount->ID . '][IBAN]', $this->fieldLabel('BankAccountIBAN'), $bankAccount->IBAN)); $bankAccountGroup->push(TextField::create('BankAccounts[' . $bankAccount->ID . '][BIC]', $this->fieldLabel('BankAccountBIC'), $bankAccount->BIC)); $fields->addFieldToTab('Root.BankAccounts', $bankAccountGroup); $index++; } } /** * Writes the bank account data on before write. * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 18.04.2018 */ protected function writeBankAccounts() : void { if ($this->PaymentChannel != 'prepayment') { return; } $requests = Controller::curr()->getRequest(); $bankAccounts = $requests->postVar('BankAccounts'); $bankAccountData = []; if (is_array($bankAccounts)) { foreach ($bankAccounts as $ID => $data) { if (empty($data['Owner']) && empty($data['Name']) && empty($data['IBAN']) && empty($data['BIC']) ) { continue; } $bankAccountData[$ID] = [ 'Owner' => $data['Owner'], 'Name' => $data['Name'], 'IBAN' => $data['IBAN'], 'BIC' => $data['BIC'], ]; } if (!empty($bankAccountData)) { $this->BankAccountData = serialize($bankAccountData); } } } /*********************************************************************************************** *********************************************************************************************** ** ** ** Payment processing section. SilverCart checkout will call these methods: ** ** ** ** - canProcessBeforePaymentProvider ** ** - canProcessAfterPaymentProvider ** ** - canProcessBeforeOrder ** ** - canProcessAfterOrder ** ** - canPlaceOrder ** ** - processBeforePaymentProvider ** ** - processAfterPaymentProvider ** ** - processBeforeOrder ** ** - processAfterOrder ** ** - processNotification ** ** - processConfirmationText ** ** ** *********************************************************************************************** **********************************************************************************************/ /** * Returns whether the checkout is ready to call self::processAfterOrder(). * * @param array $checkoutData Checkout data * * @return bool * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 24.04.2018 */ public function canProcessAfterOrder(Order $order, array $checkoutData) : bool { return true; } /** * Is called by default checkout right before placing an order. * If this returns false, the order won't be placed and the checkout won't be finalized. * * @param array $checkoutData Checkout data * * @return bool * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 24.04.2018 */ public function canPlaceOrder(array $checkoutData) : bool { return true; } /** * Is called by default checkout right after placing an order. * If payed by prepayment, an email containing the bank account data will be sent to the * customer. * * @param \SilverCart\Model\Order\Order $order Order * @param array $checkoutData Checkout data * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 13.04.2018 */ public function processAfterOrder(Order $order, array $checkoutData) : void { if ($this->PaymentChannel == 'prepayment' && !empty($this->TextBankAccountInfo) || $this->getBankAccounts()->exists() ) { // send email with payment information to the customer ShopEmail::send( 'PaymentPrepaymentBankAccountInfo', $order->CustomersEmail, ['Order' => $order], [], $order->Member()->Locale ); } } /** * Is called before rendering the order confirmation page right after the order placement is * finalized. * Expects an optional string to display additional information to the customer (e.g. showing * the shop owners bank account data if the customer chose prepayment). * * @param \SilverCart\Model\Order\Order $order Order * @param array $checkoutData Checkout data * * @return string * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 13.04.2018 */ public function processConfirmationText(Order $order, array $checkoutData) : string { $text = ''; $textTemplate = null; switch ($this->PaymentChannel) { case 'invoice': $textTemplate = SSViewer_FromString::create($this->InvoiceInfo); break; case 'prepayment': $textTemplate = SSViewer_FromString::create($this->TextBankAccountInfo); break; default: break; } if (!is_null($textTemplate)) { $text = HTTP::absoluteURLs($textTemplate->process(ArrayData::create(['Order' => $order]))); } return (string) $text; } } |