PaymentToken.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Model\ResourceModel;
  7. use Magento\Vault\Setup\InstallSchema;
  8. use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
  9. /**
  10. * Vault Payment Token Resource Model
  11. */
  12. class PaymentToken extends AbstractDb
  13. {
  14. /**
  15. * Resource initialization
  16. *
  17. * @return void
  18. */
  19. protected function _construct()
  20. {
  21. $this->_init('vault_payment_token', 'entity_id');
  22. }
  23. /**
  24. * Get payment token by order payment Id.
  25. *
  26. * @param int $paymentId
  27. * @return array
  28. */
  29. public function getByOrderPaymentId($paymentId)
  30. {
  31. $connection = $this->getConnection();
  32. $select = $connection
  33. ->select()
  34. ->from($this->getMainTable())
  35. ->joinInner(
  36. $this->getTable('vault_payment_token_order_payment_link'),
  37. 'payment_token_id = entity_id',
  38. null
  39. )
  40. ->where('order_payment_id = ?', (int) $paymentId);
  41. return $connection->fetchRow($select);
  42. }
  43. /**
  44. * Get payment token by gateway token.
  45. *
  46. * @param string $token The gateway token.
  47. * @param string $paymentMethodCode
  48. * @param int $customerId Customer ID.
  49. * @return array
  50. * @throws \Magento\Framework\Exception\LocalizedException
  51. */
  52. public function getByGatewayToken($token, $paymentMethodCode, $customerId = 0)
  53. {
  54. $connection = $this->getConnection();
  55. $select = $connection
  56. ->select()
  57. ->from($this->getMainTable())
  58. ->where('gateway_token = ?', $token)
  59. ->where('payment_method_code = ?', $paymentMethodCode);
  60. if ($customerId > 0) {
  61. $select = $select->where('customer_id = ?', $customerId);
  62. } else {
  63. $select = $select->where('customer_id IS NULL');
  64. }
  65. return $connection->fetchRow($select);
  66. }
  67. /**
  68. * Get payment token by public hash.
  69. *
  70. * @param string $hash Public hash.
  71. * @param int $customerId Customer ID.
  72. * @return array
  73. * @throws \Magento\Framework\Exception\LocalizedException
  74. */
  75. public function getByPublicHash($hash, $customerId = 0)
  76. {
  77. $connection = $this->getConnection();
  78. $select = $connection
  79. ->select()
  80. ->from($this->getMainTable())
  81. ->where('public_hash = ?', $hash);
  82. if ($customerId > 0) {
  83. $select = $select->where('customer_id = ?', $customerId);
  84. } else {
  85. $select = $select->where('customer_id IS NULL');
  86. }
  87. return $connection->fetchRow($select);
  88. }
  89. /**
  90. * Add link between payment token and order payment.
  91. *
  92. * @param int $paymentTokenId
  93. * @param int $orderPaymentId
  94. * @return bool
  95. */
  96. public function addLinkToOrderPayment($paymentTokenId, $orderPaymentId)
  97. {
  98. $connection = $this->getConnection();
  99. $select = $connection->select()
  100. ->from($this->getTable('vault_payment_token_order_payment_link'))
  101. ->where('order_payment_id = ?', (int) $orderPaymentId)
  102. ->where('payment_token_id =?', (int) $paymentTokenId);
  103. if (!empty($connection->fetchRow($select))) {
  104. return true;
  105. }
  106. return 1 === $connection->insert(
  107. $this->getTable('vault_payment_token_order_payment_link'),
  108. ['order_payment_id' => (int) $orderPaymentId, 'payment_token_id' => (int) $paymentTokenId]
  109. );
  110. }
  111. }