PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Entity/Payment.php

https://gitlab.com/Drulenium-bot/payment
PHP | 472 lines | 268 code | 54 blank | 150 comment | 15 complexity | d9cd135606411d647ef21c26454c4fd0 MD5 | raw file
  1. <?php
  2. namespace Drupal\payment\Entity;
  3. use Drupal\Core\Entity\ContentEntityBase;
  4. use Drupal\Core\Entity\EntityChangedTrait;
  5. use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Entity\EntityTypeInterface;
  7. use Drupal\Core\Field\BaseFieldDefinition;
  8. use Drupal\Core\Field\FieldDefinitionInterface;
  9. use Drupal\payment\PaymentAwareInterface;
  10. use Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface;
  11. use Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface as PluginPaymentMethodInterface;
  12. use Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface as PluginPaymentStatusInterface;
  13. use Drupal\user\UserInterface;
  14. /**
  15. * Defines a payment entity.
  16. *
  17. * @ContentEntityType(
  18. * base_table = "payment",
  19. * bundle_label = @Translation("Payment type"),
  20. * handlers = {
  21. * "access" = "Drupal\payment\Entity\Payment\PaymentAccessControlHandler",
  22. * "form" = {
  23. * "delete" = "Drupal\payment\Entity\Payment\PaymentDeleteForm",
  24. * "update_status" = "Drupal\payment\Entity\Payment\PaymentStatusForm",
  25. * "capture" = "Drupal\payment\Entity\Payment\PaymentCaptureForm",
  26. * "refund" = "Drupal\payment\Entity\Payment\PaymentRefundForm"
  27. * },
  28. * "list_builder" = "Drupal\payment\Entity\Payment\PaymentListBuilder",
  29. * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
  30. * "views_data" = "Drupal\payment\Entity\Payment\PaymentViewsData",
  31. * "storage" = "Drupal\payment\Entity\Payment\PaymentStorage",
  32. * "storage_schema" = "Drupal\payment\Entity\Payment\PaymentStorageSchema",
  33. * },
  34. * entity_keys = {
  35. * "bundle" = "bundle",
  36. * "id" = "id",
  37. * "uuid" = "uuid",
  38. * },
  39. * field_ui_base_route = "payment.payment_type",
  40. * id = "payment",
  41. * label = @Translation("Payment"),
  42. * links = {
  43. * "canonical" = "/payment/{payment}",
  44. * "collection" = "/admin/content/payment",
  45. * "complete" = "/payment/{payment}/complete",
  46. * "delete-form" = "/payment/{payment}/delete",
  47. * "update-status-form" = "/payment/{payment}/update-status",
  48. * "capture-form" = "/payment/{payment}/capture",
  49. * "refund-form" = "/payment/{payment}/refund"
  50. * }
  51. * )
  52. */
  53. class Payment extends ContentEntityBase implements PaymentInterface {
  54. use EntityChangedTrait;
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) {
  59. // Unserialize the values for fields that are stored in the base table,
  60. // because the entity storage does not do that.
  61. $property_names = ['payment_method', 'payment_type'];
  62. foreach ($property_names as $property_name) {
  63. if (isset($values[$property_name])) {
  64. foreach ($values[$property_name] as &$item_values) {
  65. $item_values['plugin_configuration'] = unserialize($item_values['plugin_configuration']);
  66. }
  67. }
  68. }
  69. parent::__construct($values, $entity_type, $bundle, $translations);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getCreatedTime() {
  75. return $this->get('created')->value;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getChangedTimeAcrossTranslations() {
  81. // Payments are not translatable, so there are no per-translation changed
  82. // times.
  83. return $this->getChangedTime();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function label() {
  89. return (string) $this->getPaymentType()->getPaymentDescription();
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getPaymentType() {
  95. /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $field_item */
  96. $field_item = $this->get('payment_type')->first();
  97. return $field_item ? $field_item->getContainedPluginInstance() : NULL;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getCurrency() {
  103. return $this->get('currency')->entity;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function setCurrencyCode($currency_code) {
  109. $this->set('currency', $currency_code);
  110. return $this;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function getCurrencyCode() {
  116. return $this->get('currency')->target_id;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function setLineItems(array $line_items) {
  122. foreach ($line_items as $line_item) {
  123. $this->setLineItem($line_item);
  124. }
  125. return $this;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function setLineItem(PaymentLineItemInterface $line_item) {
  131. $line_item->setPayment($this);
  132. $this->unsetLineItem($line_item->getName());
  133. $this->get('line_items')->appendItem($line_item);
  134. return $this;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function unsetLineItem($name) {
  140. foreach ($this->get('line_items') as $delta => $line_item_item) {
  141. if ($line_item_item->getContainedPluginInstance()->getName() == $name) {
  142. $this->get('line_items')->removeItem($delta);
  143. }
  144. }
  145. return $this;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getLineItems() {
  151. $line_items = [];
  152. /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $field_item */
  153. foreach ($this->get('line_items') as $field_item) {
  154. /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface $line_item */
  155. $line_item = $field_item->getContainedPluginInstance();
  156. if ($line_item) {
  157. $line_items[$line_item->getName()] = $line_item;
  158. }
  159. }
  160. return $line_items;
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function getLineItem($name) {
  166. $line_items = $this->getLineItems();
  167. foreach ($line_items as $delta => $line_item) {
  168. if ($line_item->getName() == $name) {
  169. return $line_item;
  170. }
  171. }
  172. return NULL;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getLineItemsByType($plugin_id) {
  178. $line_items = [];
  179. foreach ($this->getLineItems() as $name => $line_item) {
  180. if ($line_item->getPluginId() == $plugin_id) {
  181. $line_items[$name] = $line_item;
  182. }
  183. }
  184. return $line_items;
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function setPaymentStatuses(array $payment_statuses) {
  190. $payment_status_list = $this->get('payment_statuses');
  191. // Remove all existing field items.
  192. foreach ($payment_status_list as $index => $payment_status) {
  193. $payment_status_list->removeItem($index);
  194. }
  195. // Set each individual new status.
  196. /** @var \Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface[] $payment_statuses */
  197. foreach ($payment_statuses as $payment_status) {
  198. $payment_status->setPayment($this);
  199. $payment_status_list->appendItem($payment_status);
  200. }
  201. return $this;
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function setPaymentStatus(PluginPaymentStatusInterface $payment_status) {
  207. $previous_status = $this->getPaymentStatus();
  208. $payment_status->setPayment($this);
  209. // Prevent duplicate statuses.
  210. if (!$this->getPaymentStatus() || $this->getPaymentStatus()->getPluginId() != $payment_status->getPluginId()) {
  211. $this->get('payment_statuses')->appendItem($payment_status);
  212. }
  213. /** @var \Drupal\payment\EventDispatcherInterface $event_dispatcher */
  214. $event_dispatcher = \Drupal::service('payment.event_dispatcher');
  215. $event_dispatcher->setPaymentStatus($this, $previous_status);
  216. return $this;
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function getPaymentStatuses() {
  222. $payment_statuses = [];
  223. /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $field_item */
  224. foreach ($this->get('payment_statuses') as $field_item) {
  225. $payment_statuses[] = $field_item->getContainedPluginInstance();
  226. }
  227. return array_filter($payment_statuses);
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function getPaymentStatus() {
  233. $deltas = [];
  234. /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $field_item */
  235. foreach ($this->get('payment_statuses') as $delta => $field_item) {
  236. $deltas[] = $delta;
  237. }
  238. if ($deltas) {
  239. return $this->get('payment_statuses')[max($deltas)]->getContainedPluginInstance();
  240. }
  241. }
  242. /**
  243. * {@inheritdoc}
  244. */
  245. public function setPaymentMethod(PluginPaymentMethodInterface $payment_method) {
  246. // The payment method might have been unserialized with an old payment
  247. // object, trying to save that as new will result in exceptions. Set the
  248. // current object again.
  249. $payment_method->setPayment($this);
  250. /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $field_item */
  251. $this->get('payment_method')->applyDefaultValue();
  252. $this->get('payment_method')->appendItem($payment_method);
  253. return $this;
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function getPaymentMethod() {
  259. /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $field_item */
  260. $field_item = $this->get('payment_method')->first();
  261. return $field_item ? $field_item->getContainedPluginInstance() : NULL;
  262. }
  263. /**
  264. * {@inheritdoc}
  265. */
  266. public function setOwnerId($id) {
  267. $this->owner[0]->setValue($id);
  268. return $this;
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function setOwner(UserInterface $user) {
  274. $this->owner[0]->setValue($user->id());
  275. return $this;
  276. }
  277. /**
  278. * {@inheritdoc}
  279. */
  280. public function getOwnerId() {
  281. return $this->owner[0]->get('target_id')->getValue();
  282. }
  283. /**
  284. * {@inheritdoc}
  285. */
  286. public function getOwner() {
  287. return $this->owner[0]->get('entity')->getValue();
  288. }
  289. /**
  290. * {@inheritdoc}
  291. */
  292. public function getAmount() {
  293. $total = 0;
  294. foreach ($this->getLineItems() as $line_item) {
  295. $total = bcadd($total, $line_item->getTotalAmount(), 6);
  296. }
  297. return $total;
  298. }
  299. /**
  300. * {@inheritdoc}
  301. */
  302. public function execute() {
  303. if ($this->getPaymentMethod()) {
  304. return $this->getPaymentMethod()->executePayment();
  305. }
  306. }
  307. /**
  308. * {@inheritdoc}
  309. */
  310. public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  311. $fields['bundle'] = BaseFieldDefinition::create('string')
  312. ->setLabel(t('Bundle'))
  313. ->setReadOnly(TRUE);
  314. $fields['changed'] = BaseFieldDefinition::create('changed')
  315. ->setLabel(t('Changed'))
  316. ->setDescription(t('The time the payment was last edited.'))
  317. ->setDisplayOptions('view', array(
  318. 'type' => 'timestamp',
  319. 'weight' => 0,
  320. ))
  321. ->setDisplayConfigurable('view', TRUE);
  322. $fields['created'] = BaseFieldDefinition::create('created')
  323. ->setLabel(t('Created'))
  324. ->setDescription(t('The time the payment was created.'))
  325. ->setDisplayOptions('view', array(
  326. 'type' => 'timestamp',
  327. 'weight' => 0,
  328. ))
  329. ->setDisplayConfigurable('view', TRUE);
  330. $fields['currency'] = BaseFieldDefinition::create('entity_reference')
  331. ->setLabel(t('Currency'))
  332. ->setRequired(TRUE)
  333. ->setDefaultValue(0)
  334. ->setSetting('target_type', 'currency')
  335. ->setSetting('handler', 'default')
  336. ->setDisplayOptions('view', array(
  337. 'type' => 'entity_reference_label',
  338. 'weight' => 0,
  339. ))
  340. ->setDisplayConfigurable('view', TRUE);
  341. $fields['id'] = BaseFieldDefinition::create('integer')
  342. ->setLabel(t('Payment ID'))
  343. ->setReadOnly(TRUE);
  344. $fields['owner'] = BaseFieldDefinition::create('entity_reference')
  345. ->setLabel(t('Payer'))
  346. ->setDefaultValue(0)
  347. ->setSetting('target_type', 'user')
  348. ->setSetting('handler', 'default')
  349. ->setDefaultValueCallback(Payment::class . '::getCurrentUserId')
  350. ->setTranslatable(TRUE)
  351. ->setDisplayOptions('view', array(
  352. 'type' => 'author',
  353. 'weight' => 0,
  354. ))
  355. ->setDisplayConfigurable('view', TRUE);
  356. $fields['line_items'] = BaseFieldDefinition::create('plugin:payment_line_item')
  357. ->setLabel(t('Line items'))
  358. ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  359. ->setDisplayOptions('view', array(
  360. 'type' => 'payment_line_item_overview',
  361. 'weight' => 0,
  362. ))
  363. ->setDisplayConfigurable('view', TRUE);
  364. $fields['payment_method'] = BaseFieldDefinition::create('plugin:payment_method')
  365. ->setLabel(t('Payment method'))
  366. ->setDisplayOptions('view', array(
  367. 'type' => 'plugin_label',
  368. 'weight' => 0,
  369. ))
  370. ->setDisplayConfigurable('view', TRUE);
  371. $fields['payment_statuses'] = BaseFieldDefinition::create('plugin:payment_status')
  372. ->setLabel(t('Payment statuses'))
  373. ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  374. ->setDisplayOptions('view', array(
  375. 'type' => 'payment_status_overview',
  376. 'weight' => 0,
  377. ))
  378. ->setDisplayConfigurable('view', TRUE);
  379. $fields['payment_type'] = BaseFieldDefinition::create('plugin:payment_type')
  380. ->setLabel(t('Payment type'))
  381. ->setDisplayOptions('view', array(
  382. 'type' => 'plugin_label',
  383. 'weight' => 0,
  384. ))
  385. ->setDisplayConfigurable('view', TRUE);
  386. $fields['uuid'] = BaseFieldDefinition::create('uuid')
  387. ->setLabel(t('Universally Unique ID'))
  388. ->setReadOnly(TRUE);
  389. return $fields;
  390. }
  391. /**
  392. * {@inheritdoc}
  393. */
  394. protected function getTranslatedField($name, $langcode) {
  395. $field_item_list = parent::getTranslatedField($name, $langcode);
  396. $plugin_bag_field_names = ['line_items', 'payment_method', 'payment_statuses', 'payment_type'];
  397. if (in_array($name, $plugin_bag_field_names)) {
  398. foreach ($field_item_list as $field_item) {
  399. $plugin_instance = $field_item->get('plugin_instance')->getValue();
  400. if ($plugin_instance instanceof PaymentAwareInterface) {
  401. $plugin_instance->setPayment($this);
  402. }
  403. }
  404. }
  405. return $field_item_list;
  406. }
  407. /**
  408. * Default value callback for 'owner' base field definition.
  409. *
  410. * @see ::baseFieldDefinitions()
  411. *
  412. * @return array
  413. * An array of default values.
  414. */
  415. public static function getCurrentUserId(EntityInterface $entity, FieldDefinitionInterface $field_definition) {
  416. return array(\Drupal::currentUser()->id());
  417. }
  418. }