EncryptionUpdate.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Sales\Model\ResourceModel\Order\Payment;
  8. /**
  9. * Resource for updating encrypted credit card data to the latest cipher
  10. */
  11. class EncryptionUpdate
  12. {
  13. const LEGACY_PATTERN = '^[[:digit:]]+:[^%s]:.*$';
  14. /**
  15. * @var \Magento\Sales\Model\ResourceModel\Order\Payment
  16. */
  17. private $paymentResource;
  18. /**
  19. * @var \Magento\Framework\Encryption\Encryptor
  20. */
  21. private $encryptor;
  22. /**
  23. * @param \Magento\Sales\Model\ResourceModel\Order\Payment $paymentResource
  24. * @param \Magento\Framework\Encryption\Encryptor $encryptor
  25. */
  26. public function __construct(
  27. \Magento\Sales\Model\ResourceModel\Order\Payment $paymentResource,
  28. \Magento\Framework\Encryption\Encryptor $encryptor
  29. ) {
  30. $this->paymentResource = $paymentResource;
  31. $this->encryptor = $encryptor;
  32. }
  33. /**
  34. * Fetch encrypted credit card numbers using legacy ciphers and re-encrypt with latest cipher
  35. * @throws \Magento\Framework\Exception\LocalizedException
  36. */
  37. public function reEncryptCreditCardNumbers()
  38. {
  39. $connection = $this->paymentResource->getConnection();
  40. $table = $this->paymentResource->getMainTable();
  41. $select = $connection->select()->from($table, ['entity_id', 'cc_number_enc'])
  42. ->where(
  43. 'cc_number_enc REGEXP ?',
  44. sprintf(self::LEGACY_PATTERN, \Magento\Framework\Encryption\Encryptor::CIPHER_LATEST)
  45. )->limit(1000);
  46. while ($attributeValues = $connection->fetchPairs($select)) {
  47. // save new values
  48. foreach ($attributeValues as $valueId => $value) {
  49. $connection->update(
  50. $table,
  51. ['cc_number_enc' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
  52. ['entity_id = ?' => (int)$valueId, 'cc_number_enc = ?' => (string)$value]
  53. );
  54. }
  55. }
  56. }
  57. }