|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Paypal\Model; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\Locale\ResolverInterface; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides URL to PP SDK based on configuration |
| 16 | + */ |
| 17 | +class SdkUrl |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Base url for Paypal SDK |
| 21 | + */ |
| 22 | + private const BASE_URL = 'https://www.paypal.com/sdk/js?'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var ScopeConfigInterface |
| 26 | + */ |
| 27 | + private $scopeConfig; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Config |
| 31 | + */ |
| 32 | + private $config; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var StoreManagerInterface |
| 36 | + */ |
| 37 | + private $storeManager; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var array |
| 41 | + */ |
| 42 | + private $queryParams = []; |
| 43 | + |
| 44 | + /** |
| 45 | + * Maps the old checkout SDK configuration values to the current ones |
| 46 | + * |
| 47 | + * @var array |
| 48 | + */ |
| 49 | + private $disallowedFundingMap; |
| 50 | + |
| 51 | + /** |
| 52 | + * These payment methods will be added as parameters to the SDK url to disable them. |
| 53 | + * |
| 54 | + * @var array |
| 55 | + */ |
| 56 | + private $unsupportedPaymentMethods; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var ResolverInterface |
| 60 | + */ |
| 61 | + private $localeResolver; |
| 62 | + |
| 63 | + /** |
| 64 | + * Generated Url to PayPAl SDK |
| 65 | + * |
| 66 | + * @var string |
| 67 | + */ |
| 68 | + private $url; |
| 69 | + |
| 70 | + /** |
| 71 | + * @param ResolverInterface $localeResolver |
| 72 | + * @param ConfigFactory $configFactory |
| 73 | + * @param ScopeConfigInterface $scopeConfig |
| 74 | + * @param StoreManagerInterface $storeManager |
| 75 | + * @param array $disallowedFundingMap |
| 76 | + * @param array $unsupportedPaymentMethods |
| 77 | + */ |
| 78 | + public function __construct( |
| 79 | + ResolverInterface $localeResolver, |
| 80 | + ConfigFactory $configFactory, |
| 81 | + ScopeConfigInterface $scopeConfig, |
| 82 | + StoreManagerInterface $storeManager, |
| 83 | + $disallowedFundingMap = [], |
| 84 | + $unsupportedPaymentMethods = [] |
| 85 | + ) { |
| 86 | + $this->localeResolver = $localeResolver; |
| 87 | + $this->config = $configFactory->create(); |
| 88 | + $this->config->setMethod(Config::METHOD_EXPRESS); |
| 89 | + $this->scopeConfig = $scopeConfig; |
| 90 | + $this->storeManager = $storeManager; |
| 91 | + $this->disallowedFundingMap = $disallowedFundingMap; |
| 92 | + $this->unsupportedPaymentMethods = $unsupportedPaymentMethods; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Generate the url to download the Paypal SDK |
| 97 | + * |
| 98 | + * @return string |
| 99 | + */ |
| 100 | + public function getUrl(): string |
| 101 | + { |
| 102 | + if (empty($this->url)) { |
| 103 | + $components = []; |
| 104 | + $params = [ |
| 105 | + 'client-id' => $this->getClientId(), |
| 106 | + 'locale' => $this->localeResolver->getLocale(), |
| 107 | + 'currency' => $this->storeManager->getStore()->getBaseCurrencyCode(), |
| 108 | + ]; |
| 109 | + |
| 110 | + if ($this->areMessagesEnabled()) { |
| 111 | + $components[] = 'messages'; |
| 112 | + } |
| 113 | + if ($this->areButtonsEnabled()) { |
| 114 | + $components[] = 'buttons'; |
| 115 | + $params['commit'] = 'false'; |
| 116 | + $params['intent'] = $this->getIntent(); |
| 117 | + $params['merchant-id'] = $this->config->getValue('merchant_id'); |
| 118 | + $params['disable-funding'] = $this->getDisallowedFunding(); |
| 119 | + $params = array_replace($params, $this->queryParams); |
| 120 | + } |
| 121 | + $params['components'] = implode(',', $components); |
| 122 | + $this->url = self::BASE_URL . http_build_query(array_filter($params)); |
| 123 | + } |
| 124 | + return $this->url; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Set query params in PayPal SDK Url |
| 129 | + * |
| 130 | + * @param string $key |
| 131 | + * @param string $value |
| 132 | + */ |
| 133 | + public function setQueryParam(string $key, string $value) |
| 134 | + { |
| 135 | + $allowedParams = ['commit']; |
| 136 | + if (in_array($key, $allowedParams)) { |
| 137 | + $this->queryParams[$key] = $value; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Check if PP PayLater Messages enabled |
| 143 | + * |
| 144 | + * @return bool |
| 145 | + */ |
| 146 | + private function areMessagesEnabled() |
| 147 | + { |
| 148 | + //ToDo read from config |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Check if SmartButtons enabled |
| 154 | + * |
| 155 | + * @return bool |
| 156 | + */ |
| 157 | + private function areButtonsEnabled() |
| 158 | + { |
| 159 | + return (bool)(int) $this->config->getValue('in_context'); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Get configured value for PayPal client id |
| 164 | + * |
| 165 | + * @return mixed |
| 166 | + */ |
| 167 | + private function getClientId() |
| 168 | + { |
| 169 | + return (int)$this->config->getValue('sandbox_flag') ? |
| 170 | + $this->config->getValue('sandbox_client_id') : |
| 171 | + $this->config->getValue('client_id'); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns disallowed funding from configuration after updating values |
| 176 | + * |
| 177 | + * @return string |
| 178 | + */ |
| 179 | + private function getDisallowedFunding() |
| 180 | + { |
| 181 | + $disallowedFunding = $this->config->getValue('disable_funding_options'); |
| 182 | + $result = $disallowedFunding ? explode(',', $disallowedFunding) : []; |
| 183 | + |
| 184 | + // PayPal Guest Checkout Credit Card Icons only available when Guest Checkout option is enabled |
| 185 | + if ($this->isPaypalGuestCheckoutAllowed() === false && !in_array('CARD', $result)) { |
| 186 | + array_push($result, 'CARD'); |
| 187 | + } |
| 188 | + |
| 189 | + // Map old configuration values to current ones |
| 190 | + $result = array_map(function ($oldValue) { |
| 191 | + return $this->disallowedFundingMap[$oldValue] ?? $oldValue; |
| 192 | + }, $result); |
| 193 | + |
| 194 | + //disable unsupported payment methods |
| 195 | + $result = array_combine($result, $result); |
| 196 | + $result = array_merge($result, $this->unsupportedPaymentMethods); |
| 197 | + |
| 198 | + return implode(',', $result); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Returns if is allowed PayPal Guest Checkout. |
| 203 | + * |
| 204 | + * @return bool |
| 205 | + */ |
| 206 | + private function isPaypalGuestCheckoutAllowed(): bool |
| 207 | + { |
| 208 | + return $this->config->getValue('solution_type') === Config::EC_SOLUTION_TYPE_SOLE; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Return intent value from the configuration payment_action value |
| 213 | + * |
| 214 | + * @return string |
| 215 | + */ |
| 216 | + private function getIntent(): string |
| 217 | + { |
| 218 | + $paymentAction = $this->config->getValue('paymentAction'); |
| 219 | + $mappedIntentValues = [ |
| 220 | + Config::PAYMENT_ACTION_AUTH => 'authorize', |
| 221 | + Config::PAYMENT_ACTION_SALE => 'capture', |
| 222 | + Config::PAYMENT_ACTION_ORDER => 'order' |
| 223 | + ]; |
| 224 | + return $mappedIntentValues[$paymentAction]; |
| 225 | + } |
| 226 | +} |
0 commit comments