View source
<?php
namespace Drupal\payment\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\payment\PaymentAwareInterface;
use Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface as PluginPaymentMethodInterface;
use Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface as PluginPaymentStatusInterface;
use Drupal\user\UserInterface;
class Payment extends ContentEntityBase implements PaymentInterface {
use EntityChangedTrait;
public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) {
$property_names = [
'payment_method',
'payment_type',
];
foreach ($property_names as $property_name) {
if (isset($values[$property_name])) {
foreach ($values[$property_name] as &$item_values) {
if (is_string($item_values['plugin_configuration'])) {
$item_values['plugin_configuration'] = unserialize($item_values['plugin_configuration']);
}
}
}
}
parent::__construct($values, $entity_type, $bundle, $translations);
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function getChangedTimeAcrossTranslations() {
return $this
->getChangedTime();
}
public function label() {
return (string) $this
->getPaymentType()
->getPaymentDescription();
}
public function getPaymentType() {
$field_item = $this
->get('payment_type')
->first();
return $field_item ? $field_item
->getContainedPluginInstance() : NULL;
}
public function getCurrency() {
return $this
->get('currency')->entity;
}
public function setCurrencyCode($currency_code) {
$this
->set('currency', $currency_code);
return $this;
}
public function getCurrencyCode() {
return $this
->get('currency')->target_id;
}
public function setLineItems(array $line_items) {
foreach ($line_items as $line_item) {
$this
->setLineItem($line_item);
}
return $this;
}
public function setLineItem(PaymentLineItemInterface $line_item) {
$line_item
->setPayment($this);
$this
->unsetLineItem($line_item
->getName());
$this
->get('line_items')
->appendItem($line_item);
return $this;
}
public function unsetLineItem($name) {
foreach ($this
->get('line_items') as $delta => $line_item_item) {
if ($line_item_item
->getContainedPluginInstance()
->getName() == $name) {
$this
->get('line_items')
->removeItem($delta);
}
}
return $this;
}
public function getLineItems() {
$line_items = [];
foreach ($this
->get('line_items') as $field_item) {
$line_item = $field_item
->getContainedPluginInstance();
if ($line_item) {
$line_items[$line_item
->getName()] = $line_item;
}
}
return $line_items;
}
public function getLineItem($name) {
$line_items = $this
->getLineItems();
foreach ($line_items as $delta => $line_item) {
if ($line_item
->getName() == $name) {
return $line_item;
}
}
return NULL;
}
public function getLineItemsByType($plugin_id) {
$line_items = [];
foreach ($this
->getLineItems() as $name => $line_item) {
if ($line_item
->getPluginId() == $plugin_id) {
$line_items[$name] = $line_item;
}
}
return $line_items;
}
public function setPaymentStatuses(array $payment_statuses) {
$payment_status_list = $this
->get('payment_statuses');
foreach ($payment_status_list as $index => $payment_status) {
$payment_status_list
->removeItem($index);
}
foreach ($payment_statuses as $payment_status) {
$payment_status
->setPayment($this);
$payment_status_list
->appendItem($payment_status);
}
return $this;
}
public function setPaymentStatus(PluginPaymentStatusInterface $payment_status) {
$previous_status = $this
->getPaymentStatus();
$payment_status
->setPayment($this);
if (!$this
->getPaymentStatus() || $this
->getPaymentStatus()
->getPluginId() != $payment_status
->getPluginId()) {
$this
->get('payment_statuses')
->appendItem($payment_status);
}
$event_dispatcher = \Drupal::service('payment.event_dispatcher');
$event_dispatcher
->setPaymentStatus($this, $previous_status);
return $this;
}
public function getPaymentStatuses() {
$payment_statuses = [];
foreach ($this
->get('payment_statuses') as $field_item) {
$payment_statuses[] = $field_item
->getContainedPluginInstance();
}
return array_filter($payment_statuses);
}
public function getPaymentStatus() {
$deltas = [];
foreach ($this
->get('payment_statuses') as $delta => $field_item) {
$deltas[] = $delta;
}
if ($deltas) {
return $this
->get('payment_statuses')[max($deltas)]
->getContainedPluginInstance();
}
}
public function setPaymentMethod(PluginPaymentMethodInterface $payment_method) {
$payment_method
->setPayment($this);
$this
->get('payment_method')
->applyDefaultValue();
$this
->get('payment_method')
->appendItem($payment_method);
return $this;
}
public function getPaymentMethod() {
$field_item = $this
->get('payment_method')
->first();
return $field_item ? $field_item
->getContainedPluginInstance() : NULL;
}
public function setOwnerId($id) {
$this->owner[0]
->setValue($id);
return $this;
}
public function setOwner(UserInterface $user) {
$this->owner[0]
->setValue($user
->id());
return $this;
}
public function getOwnerId() {
return $this->owner[0]
->get('target_id')
->getValue();
}
public function getOwner() {
return $this->owner[0]
->get('entity')
->getValue();
}
public function getAmount() {
$total = 0;
foreach ($this
->getLineItems() as $line_item) {
$total = bcadd($total, $line_item
->getTotalAmount(), 6);
}
return $total;
}
public function execute() {
if ($this
->getPaymentMethod()) {
return $this
->getPaymentMethod()
->executePayment();
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['bundle'] = BaseFieldDefinition::create('string')
->setLabel(t('Bundle'))
->setReadOnly(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time the payment was last edited.'))
->setDisplayOptions('view', array(
'type' => 'timestamp',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time the payment was created.'))
->setDisplayOptions('view', array(
'type' => 'timestamp',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['currency'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Currency'))
->setRequired(TRUE)
->setDefaultValue(0)
->setSetting('target_type', 'currency')
->setSetting('handler', 'default')
->setDisplayOptions('view', array(
'type' => 'entity_reference_label',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Payment ID'))
->setReadOnly(TRUE);
$fields['owner'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Payer'))
->setDefaultValue(0)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback(Payment::class . '::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'type' => 'author',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['line_items'] = BaseFieldDefinition::create('plugin:payment_line_item')
->setLabel(t('Line items'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', array(
'type' => 'payment_line_item_overview',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['payment_method'] = BaseFieldDefinition::create('plugin:payment_method')
->setLabel(t('Payment method'))
->setDisplayOptions('view', array(
'type' => 'plugin_label',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['payment_statuses'] = BaseFieldDefinition::create('plugin:payment_status')
->setLabel(t('Payment statuses'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', array(
'type' => 'payment_status_overview',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['payment_type'] = BaseFieldDefinition::create('plugin:payment_type')
->setLabel(t('Payment type'))
->setDisplayOptions('view', array(
'type' => 'plugin_label',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('Universally Unique ID'))
->setReadOnly(TRUE);
return $fields;
}
protected function getTranslatedField($name, $langcode) {
$field_item_list = parent::getTranslatedField($name, $langcode);
$plugin_bag_field_names = [
'line_items',
'payment_method',
'payment_statuses',
'payment_type',
];
if (in_array($name, $plugin_bag_field_names)) {
foreach ($field_item_list as $field_item) {
$plugin_instance = $field_item
->get('plugin_instance')
->getValue();
if ($plugin_instance instanceof PaymentAwareInterface) {
$plugin_instance
->setPayment($this);
}
}
}
return $field_item_list;
}
public static function getCurrentUserId(EntityInterface $entity, FieldDefinitionInterface $field_definition) {
return array(
\Drupal::currentUser()
->id(),
);
}
}