Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExpressConfigProvider.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Paypal\Model;
7 
12 use Magento\Payment\Helper\Data as PaymentHelper;
13 use Magento\Paypal\Helper\Data as PaypalHelper;
14 
20 {
21  const IN_CONTEXT_BUTTON_ID = 'paypal-express-in-context-button';
22 
26  protected $localeResolver;
27 
31  protected $config;
32 
36  protected $currentCustomer;
37 
41  protected $paypalHelper;
42 
46  protected $methodCodes = [
51  ];
52 
56  protected $methods = [];
57 
61  protected $paymentHelper;
62 
66  protected $urlBuilder;
67 
78  public function __construct(
79  ConfigFactory $configFactory,
82  PaypalHelper $paypalHelper,
83  PaymentHelper $paymentHelper,
85  ) {
86  $this->localeResolver = $localeResolver;
87  $this->config = $configFactory->create();
88  $this->currentCustomer = $currentCustomer;
89  $this->paypalHelper = $paypalHelper;
90  $this->paymentHelper = $paymentHelper;
91  $this->urlBuilder = $urlBuilder;
92 
93  foreach ($this->methodCodes as $code) {
94  $this->methods[$code] = $this->paymentHelper->getMethodInstance($code);
95  }
96  }
97 
101  public function getConfig()
102  {
103  $locale = $this->localeResolver->getLocale();
104 
105  $config = [
106  'payment' => [
107  'paypalExpress' => [
108  'paymentAcceptanceMarkHref' => $this->config->getPaymentMarkWhatIsPaypalUrl(
109  $this->localeResolver
110  ),
111  'paymentAcceptanceMarkSrc' => $this->config->getPaymentMarkImageUrl(
112  $locale
113  ),
114  'isContextCheckout' => false,
115  'inContextConfig' => []
116  ]
117  ]
118  ];
119 
120  $isInContext = $this->isInContextCheckout();
121  if ($isInContext) {
122  $config['payment']['paypalExpress']['isContextCheckout'] = $isInContext;
123  $config['payment']['paypalExpress']['inContextConfig'] = [
124  'inContextId' => self::IN_CONTEXT_BUTTON_ID,
125  'merchantId' => $this->config->getValue('merchant_id'),
126  'path' => $this->urlBuilder->getUrl('paypal/express/gettoken', ['_secure' => true]),
127  'clientConfig' => [
128  'environment' => ((int) $this->config->getValue('sandbox_flag') ? 'sandbox' : 'production'),
129  'locale' => $locale,
130  'button' => [
131  self::IN_CONTEXT_BUTTON_ID
132  ]
133  ],
134  ];
135  }
136 
137  foreach ($this->methodCodes as $code) {
138  if ($this->methods[$code]->isAvailable()) {
139  $config['payment']['paypalExpress']['redirectUrl'][$code] = $this->getMethodRedirectUrl($code);
140  $config['payment']['paypalExpress']['billingAgreementCode'][$code] =
141  $this->getBillingAgreementCode($code);
142  }
143  }
144 
145  return $config;
146  }
147 
151  protected function isInContextCheckout()
152  {
153  $this->config->setMethod(Config::METHOD_EXPRESS);
154 
155  return (bool)(int) $this->config->getValue('in_context');
156  }
157 
164  protected function getMethodRedirectUrl($code)
165  {
166  return $this->methods[$code]->getCheckoutRedirectUrl();
167  }
168 
175  protected function getBillingAgreementCode($code)
176  {
177  $customerId = $this->currentCustomer->getCustomerId();
178  $this->config->setMethod($code);
179  return $this->paypalHelper->shouldAskToCreateBillingAgreement($this->config, $customerId)
181  }
182 }
__construct(ConfigFactory $configFactory, ResolverInterface $localeResolver, CurrentCustomer $currentCustomer, PaypalHelper $paypalHelper, PaymentHelper $paymentHelper, UrlInterface $urlBuilder)
$configFactory
Definition: config_data.php:43
$code
Definition: info.phtml:12