33
44use Magento \Checkout \Model \ConfigProviderInterface ;
55use Magento \Payment \Helper \Data as PaymentHelper ;
6- use Magento \Store \Model \Store as Store ;
6+ use Magento \Store \Model \StoreManagerInterface ;
7+ use Psr \Log \LoggerInterface ;
78
8- /**
9- * Class ConfigProvider
10- */
119class ConfigProvider implements ConfigProviderInterface
1210{
1311
14- protected $ method ;
15- protected $ store ;
12+ protected $ paymentHelper ;
13+ protected $ storeManager ;
14+ protected $ logger ;
15+ private $ method ;
1616
17- public function __construct (PaymentHelper $ paymentHelper , Store $ store )
17+ public function __construct (
18+ PaymentHelper $ paymentHelper ,
19+ StoreManagerInterface $ storeManager ,
20+ LoggerInterface $ logger
21+ ) {
22+ $ this ->paymentHelper = $ paymentHelper ;
23+ $ this ->storeManager = $ storeManager ;
24+ $ this ->logger = $ logger ;
25+ }
26+
27+ /**
28+ * @return \Magento\Payment\Model\MethodInterface
29+ */
30+ private function getMethod ()
1831 {
19- $ this ->method = $ paymentHelper ->getMethodInstance (\Pstk \Paystack \Model \Payment \Paystack::CODE );
20- $ this ->store = $ store ;
32+ if ($ this ->method === null ) {
33+ $ this ->method = $ this ->paymentHelper ->getMethodInstance (\Pstk \Paystack \Model \Payment \Paystack::CODE );
34+ }
35+ return $ this ->method ;
2136 }
2237
2338 /**
@@ -27,51 +42,57 @@ public function __construct(PaymentHelper $paymentHelper, Store $store)
2742 */
2843 public function getConfig ()
2944 {
30- $ publicKey = $ this ->method ->getConfigData ('live_public_key ' );
31- if ($ this ->method ->getConfigData ('test_mode ' )) {
32- $ publicKey = $ this ->method ->getConfigData ('test_public_key ' );
33- }
34-
35- $ integrationType = $ this ->method ->getConfigData ('integration_type ' )?: 'inline ' ;
45+ try {
46+ $ method = $ this ->getMethod ();
47+
48+ $ publicKey = $ method ->getConfigData ('live_public_key ' );
49+ if ($ method ->getConfigData ('test_mode ' )) {
50+ $ publicKey = $ method ->getConfigData ('test_public_key ' );
51+ }
52+
53+ $ integrationType = $ method ->getConfigData ('integration_type ' ) ?: 'inline ' ;
54+ $ baseUrl = $ this ->storeManager ->getStore ()->getBaseUrl ();
3655
37- return [
38- 'payment ' => [
39- \Pstk \Paystack \Model \Payment \Paystack::CODE => [
40- 'public_key ' => $ publicKey ,
41- 'integration_type ' => $ integrationType ,
42- 'api_url ' => $ this ->store ->getBaseUrl () . 'rest/ ' ,
43- 'integration_type_standard_url ' => $ this ->store ->getBaseUrl () . 'paystack/payment/setup ' ,
44- 'recreate_quote_url ' => $ this ->store ->getBaseUrl () . 'paystack/payment/recreate ' ,
56+ return [
57+ 'payment ' => [
58+ \Pstk \Paystack \Model \Payment \Paystack::CODE => [
59+ 'public_key ' => $ publicKey ,
60+ 'integration_type ' => $ integrationType ,
61+ 'api_url ' => $ baseUrl . 'rest/ ' ,
62+ 'integration_type_standard_url ' => $ baseUrl . 'paystack/payment/setup ' ,
63+ 'recreate_quote_url ' => $ baseUrl . 'paystack/payment/recreate ' ,
64+ ]
4565 ]
46- ]
47- ];
48- }
49-
50- public function getStore () {
51- return $ this ->store ;
66+ ];
67+ } catch (\Exception $ e ) {
68+ $ this ->logger ->error ('Paystack ConfigProvider: ' . $ e ->getMessage ());
69+ return [];
70+ }
5271 }
53-
72+
5473 /**
5574 * Get secret key for webhook process
56- *
75+ *
5776 * @return array
5877 */
59- public function getSecretKeyArray (){
60- $ data = ["live " => $ this ->method ->getConfigData ('live_secret_key ' )];
61- if ($ this ->method ->getConfigData ('test_mode ' )) {
62- $ data = ["test " => $ this ->method ->getConfigData ('test_secret_key ' )];
78+ public function getSecretKeyArray ()
79+ {
80+ $ method = $ this ->getMethod ();
81+ $ data = ["live " => $ method ->getConfigData ('live_secret_key ' )];
82+ if ($ method ->getConfigData ('test_mode ' )) {
83+ $ data = ["test " => $ method ->getConfigData ('test_secret_key ' )];
6384 }
64-
85+
6586 return $ data ;
6687 }
6788
68- public function getPublicKey (){
69- $ publicKey = $ this ->method ->getConfigData ('live_public_key ' );
70- if ($ this ->method ->getConfigData ('test_mode ' )) {
71- $ publicKey = $ this ->method ->getConfigData ('test_public_key ' );
89+ public function getPublicKey ()
90+ {
91+ $ method = $ this ->getMethod ();
92+ $ publicKey = $ method ->getConfigData ('live_public_key ' );
93+ if ($ method ->getConfigData ('test_mode ' )) {
94+ $ publicKey = $ method ->getConfigData ('test_public_key ' );
7295 }
7396 return $ publicKey ;
7497 }
75-
76-
7798}
0 commit comments