PaypalConfig.php
175 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tutor\PaymentGateways\Configs; |
| 4 | |
| 5 | use Ollyo\PaymentHub\Contracts\Payment\ConfigContract; |
| 6 | use Ollyo\PaymentHub\Payments\Paypal\Config; |
| 7 | use phpDocumentor\Reflection\Types\Self_; |
| 8 | use Tutor\Ecommerce\Settings; |
| 9 | |
| 10 | /** |
| 11 | * PaypalConfig class. |
| 12 | * |
| 13 | * This class handles the configuration for the Paypal payment gateway. |
| 14 | * It extends the BaseConfig class and implements the ConfigContract interface. |
| 15 | * |
| 16 | * @since 3.0.0 |
| 17 | */ |
| 18 | class PaypalConfig extends Config implements ConfigContract { |
| 19 | |
| 20 | use PaymentUrlsTrait; |
| 21 | |
| 22 | /** |
| 23 | * Constants for API URLs. |
| 24 | * |
| 25 | * @since 3.0.0 |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | const API_URL_TEST = 'https://api-m.sandbox.paypal.com'; |
| 30 | const API_URL_LIVE = 'https://api-m.paypal.com'; |
| 31 | |
| 32 | /** |
| 33 | * PayPal environment key. |
| 34 | * |
| 35 | * @var string |
| 36 | * @since 3.0.0 |
| 37 | */ |
| 38 | private $environment; |
| 39 | |
| 40 | /** |
| 41 | * PayPal merchant email key. |
| 42 | * |
| 43 | * @var string |
| 44 | * @since 3.0.0 |
| 45 | */ |
| 46 | private $merchant_email; |
| 47 | |
| 48 | /** |
| 49 | * PayPal client ID key. |
| 50 | * |
| 51 | * @var string |
| 52 | * @since 3.0.0 |
| 53 | */ |
| 54 | private $client_id; |
| 55 | |
| 56 | /** |
| 57 | * PayPal client secret id. |
| 58 | * |
| 59 | * @var string |
| 60 | * @since 3.0.0 |
| 61 | */ |
| 62 | private $secret_id; |
| 63 | |
| 64 | /** |
| 65 | * PayPal webhook ID key. |
| 66 | * |
| 67 | * @var string |
| 68 | * @since 3.0.0 |
| 69 | */ |
| 70 | private $webhook_id; |
| 71 | |
| 72 | /** |
| 73 | * The name of the payment gateway. |
| 74 | * |
| 75 | * @since 3.0.0 |
| 76 | * |
| 77 | * @var string |
| 78 | */ |
| 79 | protected $name = 'paypal'; |
| 80 | |
| 81 | public function __construct() { |
| 82 | parent::__construct(); |
| 83 | |
| 84 | $settings = Settings::get_payment_gateway_settings( $this->name ); |
| 85 | |
| 86 | $config_keys = $this->get_config_keys(); |
| 87 | foreach ( $config_keys as $key ) { |
| 88 | $this->$key = $this->get_field_value( $settings, $key ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public function getMode(): string { |
| 93 | return 'test'; |
| 94 | } |
| 95 | |
| 96 | public function getClientSecret(): string { |
| 97 | return $this->secret_id; |
| 98 | } |
| 99 | |
| 100 | public function getWebhookID(): string { |
| 101 | return $this->webhook_id; |
| 102 | } |
| 103 | |
| 104 | public function getAdditionalInformation(): string { |
| 105 | return ''; |
| 106 | } |
| 107 | |
| 108 | public function getTitle(): string { |
| 109 | return ''; |
| 110 | } |
| 111 | |
| 112 | public function getName(): string { |
| 113 | return $this->name; |
| 114 | } |
| 115 | |
| 116 | public function getClientID() : string { |
| 117 | return $this->client_id; |
| 118 | } |
| 119 | |
| 120 | public function getMerchantEmail() : string { |
| 121 | return $this->merchant_email; |
| 122 | } |
| 123 | |
| 124 | public function getApiURL() { |
| 125 | return $this->environment === 'test' ? static::API_URL_TEST : static::API_URL_LIVE; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Creates and updates configuration settings specific to the PayPal integration. |
| 130 | * |
| 131 | * Retrieves necessary configuration values using getter methods for webhook ID, |
| 132 | * client ID, merchant email, API URL, and client secret. Updates the configuration |
| 133 | * storage with these values to ensure they are available for subsequent operations |
| 134 | * such as API requests and webhook handling. |
| 135 | * |
| 136 | * @since 1.0.0 |
| 137 | */ |
| 138 | public function createConfig(): void { |
| 139 | parent::createConfig(); |
| 140 | |
| 141 | $config = array( |
| 142 | 'webhook_id' => $this->getWebhookID(), |
| 143 | 'client_id' => $this->getClientID(), |
| 144 | 'merchant_email' => $this->getMerchantEmail(), |
| 145 | 'api_url' => $this->getApiURL(), |
| 146 | 'client_secret' => $this->getClientSecret(), |
| 147 | ); |
| 148 | |
| 149 | $this->updateConfig( $config ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Determine whether payment gateway configured properly |
| 154 | * |
| 155 | * @since 3.0.0 |
| 156 | * |
| 157 | * @return boolean |
| 158 | */ |
| 159 | public function is_configured() { |
| 160 | // Return true if all the settings are filled. |
| 161 | return $this->merchant_email && $this->client_id && $this->secret_id; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get config keys |
| 166 | * |
| 167 | * @since 3.0.0 |
| 168 | * |
| 169 | * @return array |
| 170 | */ |
| 171 | private function get_config_keys() { |
| 172 | return array_keys( Settings::get_paypal_config_keys() ); |
| 173 | } |
| 174 | } |
| 175 |