InstructionsConfigProvider.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\OfflinePayments\Model;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Framework\Escaper;
  9. use Magento\Payment\Helper\Data as PaymentHelper;
  10. class InstructionsConfigProvider implements ConfigProviderInterface
  11. {
  12. /**
  13. * @var string[]
  14. */
  15. protected $methodCodes = [
  16. Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE,
  17. Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE,
  18. ];
  19. /**
  20. * @var \Magento\Payment\Model\Method\AbstractMethod[]
  21. */
  22. protected $methods = [];
  23. /**
  24. * @var Escaper
  25. */
  26. protected $escaper;
  27. /**
  28. * @param PaymentHelper $paymentHelper
  29. * @param Escaper $escaper
  30. */
  31. public function __construct(
  32. PaymentHelper $paymentHelper,
  33. Escaper $escaper
  34. ) {
  35. $this->escaper = $escaper;
  36. foreach ($this->methodCodes as $code) {
  37. $this->methods[$code] = $paymentHelper->getMethodInstance($code);
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getConfig()
  44. {
  45. $config = [];
  46. foreach ($this->methodCodes as $code) {
  47. if ($this->methods[$code]->isAvailable()) {
  48. $config['payment']['instructions'][$code] = $this->getInstructions($code);
  49. }
  50. }
  51. return $config;
  52. }
  53. /**
  54. * Get instructions text from config
  55. *
  56. * @param string $code
  57. * @return string
  58. */
  59. protected function getInstructions($code)
  60. {
  61. return nl2br($this->escaper->escapeHtml($this->methods[$code]->getInstructions()));
  62. }
  63. }