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