IframeConfigProvider.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Framework\UrlInterface;
  9. use Magento\Payment\Helper\Data as PaymentHelper;
  10. class IframeConfigProvider implements ConfigProviderInterface
  11. {
  12. /**
  13. * @var string[]
  14. */
  15. protected $methodCodes = [
  16. Config::METHOD_PAYFLOWADVANCED,
  17. Config::METHOD_PAYFLOWLINK,
  18. Config::METHOD_HOSTEDPRO,
  19. ];
  20. /**
  21. * @var \Magento\Payment\Model\Method\AbstractMethod[]
  22. */
  23. protected $methods = [];
  24. /**
  25. * @var PaymentHelper
  26. */
  27. protected $paymentHelper;
  28. /**
  29. * @var UrlInterface
  30. */
  31. protected $urlBuilder;
  32. /**
  33. * @param PaymentHelper $paymentHelper
  34. * @param UrlInterface $urlBuilder
  35. */
  36. public function __construct(
  37. PaymentHelper $paymentHelper,
  38. UrlInterface $urlBuilder
  39. ) {
  40. $this->paymentHelper = $paymentHelper;
  41. $this->urlBuilder = $urlBuilder;
  42. foreach ($this->methodCodes as $code) {
  43. $this->methods[$code] = $this->paymentHelper->getMethodInstance($code);
  44. }
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getConfig()
  50. {
  51. $config = [
  52. 'payment' => [
  53. 'paypalIframe' => [],
  54. ],
  55. ];
  56. foreach ($this->methodCodes as $code) {
  57. if ($this->methods[$code]->isAvailable()) {
  58. $config['payment']['paypalIframe']['actionUrl'][$code] = $this->getFrameActionUrl($code);
  59. }
  60. }
  61. return $config;
  62. }
  63. /**
  64. * Get frame action URL
  65. *
  66. * @param string $code
  67. * @return string
  68. */
  69. protected function getFrameActionUrl($code)
  70. {
  71. $url = '';
  72. switch ($code) {
  73. case Config::METHOD_PAYFLOWADVANCED:
  74. $url = $this->urlBuilder->getUrl('paypal/payflowadvanced/form', ['_secure' => true]);
  75. break;
  76. case Config::METHOD_PAYFLOWLINK:
  77. $url = $this->urlBuilder->getUrl('paypal/payflow/form', ['_secure' => true]);
  78. break;
  79. case Config::METHOD_HOSTEDPRO:
  80. $url = $this->urlBuilder->getUrl('paypal/hostedpro/redirect', ['_secure' => true]);
  81. break;
  82. }
  83. return $url;
  84. }
  85. }