PluginProbe
MultiSafepay plugin for WooCommerce / trunk
MultiSafepay plugin for WooCommerce vtrunk
6.11.1 6.12.0 6.13.0 6.2.0 6.2.1 6.3.0 6.3.1 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.6.0 6.6.1 6.6.2 6.7.0 6.7.1 6.7.2 6.7.3 6.8.0 6.8.1 6.8.2 6.8.3 6.9.0 All 84 releases
multisafepay / src / Services / SdkService.php

SdkService.php in MultiSafepay plugin for WooCommerce trunk, at src/Services/SdkService.php

220 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace MultiSafepay\WooCommerce\Services;
4
5 use Exception;
6 use MultiSafepay\Api\ApiTokenManager;
7 use MultiSafepay\Api\GatewayManager;
8 use MultiSafepay\Api\Gateways\Gateway;
9 use MultiSafepay\Api\IssuerManager;
10 use MultiSafepay\Api\PaymentMethodManager;
11 use MultiSafepay\Api\TransactionManager;
12 use MultiSafepay\Exception\ApiException;
13 use MultiSafepay\Exception\InvalidApiKeyException;
14 use MultiSafepay\Sdk;
15 use MultiSafepay\WooCommerce\Client\MultiSafepayClient;
16 use MultiSafepay\WooCommerce\Utils\Logger;
17 use Nyholm\Psr7\Factory\Psr17Factory;
18 use Psr\Http\Client\ClientExceptionInterface;
19 use WP_Error;
20
21 /**
22 * This class returns the SDK object.
23 *
24 * Class SdkService
25 *
26 * @package MultiSafepay\WooCommerce\Services
27 */
28 class SdkService {
29
30 /**
31 * @var string The API Key
32 */
33 private $api_key;
34
35 /**
36 * @var boolean The environment.
37 */
38 private $test_mode;
39
40 /**
41 * @var Sdk Sdk.
42 */
43 private $sdk = null;
44
45 /**
46 * @var Logger
47 */
48 private $logger;
49
50 /**
51 * SdkService constructor.
52 *
53 * @param string $api_key
54 * @param boolean $test_mode
55 * @param Logger|null $logger
56 */
57 public function __construct( ?string $api_key = null, ?bool $test_mode = null, ?Logger $logger = null ) {
58 $this->api_key = $api_key ?? $this->get_api_key();
59 $this->test_mode = $test_mode ?? $this->get_test_mode();
60 $this->logger = $logger ?? new Logger();
61 $psr_factory = new Psr17Factory();
62 $client = new MultiSafepayClient();
63 try {
64 $this->sdk = new Sdk( $this->api_key, ( $this->test_mode ) ? false : true, $client, $psr_factory, $psr_factory );
65 } catch ( InvalidApiKeyException $invalid_api_key_exception ) {
66 set_transient( 'multisafepay_payment_methods', array() );
67 $this->logger->log_error( $invalid_api_key_exception->getMessage() );
68 }
69 }
70
71 /**
72 * Returns if test mode is enable
73 *
74 * @return boolean
75 */
76 public function get_test_mode(): bool {
77 return (bool) get_option( 'multisafepay_testmode', false );
78 }
79
80 /**
81 * Returns api key set in settings page according with
82 * the environment selected
83 *
84 * @return string
85 */
86 public function get_api_key(): string {
87 if ( $this->get_test_mode() ) {
88 return get_option( 'multisafepay_test_api_key', '' );
89 }
90 return get_option( 'multisafepay_api_key', '' );
91 }
92
93
94 /**
95 * Returns gateway manager
96 *
97 * @return GatewayManager|WP_Error
98 */
99 public function get_gateway_manager() {
100 try {
101 return $this->sdk->getGatewayManager();
102 } catch ( ApiException $api_exception ) {
103 $this->logger->log_error( $api_exception->getMessage() );
104 return new WP_Error( 'multisafepay-warning', $api_exception->getMessage() );
105 }
106 }
107
108
109 /**
110 * Returns an array of the gateways available on the merchant account
111 *
112 * @return Gateway[]|WP_Error
113 */
114 public function get_gateways() {
115 try {
116 return $this->get_gateway_manager()->getGateways( true );
117 } catch ( ApiException $api_exception ) {
118 $this->logger->log_error( $api_exception->getMessage() );
119 return new WP_Error( 'multisafepay-warning', $api_exception->getMessage() );
120 }
121 }
122
123 /**
124 * Returns transaction manager
125 *
126 * @return TransactionManager
127 */
128 public function get_transaction_manager(): TransactionManager {
129 return $this->sdk->getTransactionManager();
130 }
131
132 /**
133 * Returns issuer manager
134 *
135 * @return IssuerManager
136 */
137 public function get_issuer_manager(): IssuerManager {
138 return $this->sdk->getIssuerManager();
139 }
140
141
142 /**
143 * @return Sdk
144 */
145 public function get_sdk(): Sdk {
146 return $this->sdk;
147 }
148
149 /**
150 * Returns api token manager
151 *
152 * @return ApiTokenManager
153 */
154 public function get_api_token_manager(): ?ApiTokenManager {
155 if ( null === $this->sdk ) {
156 $this->logger->log_error( 'SDK is not initialized' );
157 return null;
158 }
159 return $this->sdk->getApiTokenManager();
160 }
161
162 /**
163 * Returns a PaymentMethodManager instance
164 *
165 * @return PaymentMethodManager|null
166 */
167 public function get_payment_method_manager(): ?PaymentMethodManager {
168 if ( null === $this->sdk ) {
169 $this->logger->log_error( 'SDK is not initialized' );
170 return null;
171 }
172 try {
173 return $this->sdk->getPaymentMethodManager();
174 } catch ( ApiException $api_exception ) {
175 $this->logger->log_error( $api_exception->getMessage() );
176 return null;
177 }
178 }
179
180 /**
181 * Returns an array of tokens for the given customer reference and gateway code
182 *
183 * @param string $customer_reference
184 * @param string $gateway_code
185 * @return array
186 */
187 public function get_payment_tokens( string $customer_reference, string $gateway_code ): array {
188 try {
189 $tokens = $this->sdk->getTokenManager()->getListByGatewayCodeAsArray( $customer_reference, $gateway_code );
190 } catch ( ApiException $api_exception ) {
191 $this->logger->log_error( $api_exception->getMessage() );
192 return array();
193 } catch ( ClientExceptionInterface $client_exception ) {
194 $this->logger->log_error( $client_exception->getMessage() );
195 return array();
196 } catch ( Exception $exception ) {
197 $this->logger->log_error( $exception->getMessage() );
198 return array();
199 }
200
201 return $tokens;
202 }
203
204 /**
205 * Return the MultiSafepay Merchant Account ID
206 *
207 * @return int
208 */
209 public function get_multisafepay_account_id(): int {
210 try {
211 $account_manager = $this->sdk->getAccountManager();
212 $gateway_merchant_id = $account_manager->get()->getAccountId();
213 } catch ( ApiException |ClientExceptionInterface |Exception $exception ) {
214 $this->logger->log_error( 'Error when try to set the merchant credentials: ' . $exception->getMessage() );
215 }
216
217 return $gateway_merchant_id ?? 0;
218 }
219 }
220