PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.3
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.3
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Modules / PaymentMethods / PayPalGateway / ConnectConfig.php

ConnectConfig.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.5.3, at app/Modules/PaymentMethods/PayPalGateway/ConnectConfig.php

213 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\PaymentMethods\PayPalGateway;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\API;
7 use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\PayPalPartner;
8 use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\Webhook;
9 use FluentCart\App\Vite;
10 use FluentCart\Framework\Http\Request\Request;
11 use FluentCart\Framework\Support\Arr;
12
13 class ConnectConfig
14 {
15 public static function getConnectConfig()
16 {
17 $PaypalSettings = new PayPalSettingsBase();
18 $settings = $PaypalSettings->get();
19
20 $testAccountInfo = self::getAccountInfo($settings, 'test');
21 $liveAccountInfo = self::getAccountInfo($settings, 'live');
22
23 $testConnectRedirect = admin_url('?fluent-cart=fluent_cart_payment_authenticate&payment_method=paypal&type=connect&mode=test');
24 $liveConnectRedirect = admin_url('?fluent-cart=fluent_cart_payment_authenticate&payment_method=paypal&type=connect&mode=live');
25
26 return [
27 'connect_config' => [
28 'test_redirect' => $testConnectRedirect,
29 'live_redirect' => $liveConnectRedirect,
30 'image_url' => Vite::getAssetUrl('images/payment-methods/paypal-icon.svg'),
31 'disconnect_note' => __('Disconnecting your PayPal account will prevent you from offering PayPal services and products on your website. Do you wish to continue?', 'fluent-cart')
32 ],
33 'test_account' => $testAccountInfo,
34 'live_account' => $liveAccountInfo,
35 'settings' => $settings
36 ];
37 }
38
39 public static function parseConnectInfos($vendorData)
40 {
41 if (!$vendorData || !Arr::get($vendorData, 'permissionsGranted')) {
42 echo '<div class="fct_message fct_message_error">' . esc_html(__('Invalid PayPal Request. Please try configuring paypal payment gateway again!', 'fluent-cart')) . '</div>';
43 die();
44 }
45
46 $settingsInstance = App::gateway('paypal')->settings;
47
48 $mode = Arr::get($vendorData, 'mode');
49
50 /*
51 * @todo will verify later
52 * we need to verify merchant manually after they create account
53 *
54 // start verifications if merchant able to receive payments
55 $sellerAccessToken = fluent_cart_get_option('_paypal_partner_connect_access_token_' . Arr::get($vendorData, 'mode'));
56
57 $accountData = (new PayPalPartner($mode))->verifyMerchant(Arr::get($sellerAccessToken, 'access_token'), Arr::get($vendorData, 'merchantIdInPayPal'));
58
59 if (is_wp_error($accountData)) {
60 echo '<div class="fct_message fct_message_error">' . esc_html($accountData->get_error_message()) . '</div>';
61 }
62
63 if (!Arr::get($accountData, 'payments_receivable')) {
64 echo '<div class="fct_message fct_message_error">
65 <p style="color: #b94a48; background: #f2dede; padding: 12px 16px; border-radius: 4px; border: 1px solid #ebccd1; margin: 0; max-width: 460px; margin:0 auto; margin-top: 10%;">
66 Attention: You currently cannot receive payments due to restriction on your PayPal account. Please reach out to PayPal Customer Support or connect to <a href="https://www.paypal.com" style="color: #31708f; text-decoration: underline;">https://www.paypal.com</a> for more information.
67 </p>
68 </div>';
69 die();
70 }
71
72 if (!Arr::get($accountData, 'primary_email_confirmed')) {
73 echo '<div class="fct_message fct_message_error">
74 <p style="color: #b94a48; background: #f2dede; padding: 12px 16px; border-radius: 4px; border: 1px solid #ebccd1; margin: 0; max-width: 460px; margin:0 auto; margin-top: 10%;line-height: 1.7rem;">
75 Attention: Please confirm your email address on <a href="https://www.paypal.com/businessprofile/settings" style="color: #31708f; text-decoration: underline;">https://www.paypal.com/businessprofile/settings</a> in order to receive payments! You currently cannot receive payments.
76 </p>
77 </div>';
78 die();
79 }
80
81 *
82 */
83
84 // update all data that verified
85 $data = [
86 $mode . '_email_address' => sanitize_text_field(Arr::get($vendorData, 'merchantId')),
87 $mode . '_account_status' => sanitize_text_field(Arr::get($vendorData, 'accountStatus')),
88 ];
89 $settingsInstance->updateNonSensitiveData($data);
90
91 wp_redirect(admin_url('admin.php?page=fluent-cart#/settings/payments/paypal'));
92 }
93
94 public function getSellerAuthToken(Request $request)
95 {
96
97 $authCode = $request->getSafe('authCode', 'sanitize_text_field');
98 $clientId = $request->getSafe('sharedId', 'sanitize_text_field');
99 $mode = $request->getSafe('mode', 'sanitize_text_field');
100 $paypalConnect = new PayPalPartner($mode);
101
102 try {
103
104 $response = $paypalConnect->exchangeAuthCode($clientId, $authCode);
105
106 if (isset($response['access_token'])) {
107 $endpoint = "/v1/customer/partners/" . $paypalConnect->getPartnerId() . "/merchant-integrations/credentials/";
108
109 // retrieve the merchant client ID and client secret
110 $credentials = $paypalConnect->makeMerchantRequest($endpoint, $response['access_token']);
111
112 if (is_wp_error($credentials)) {
113 wp_send_json([
114 'message' => $credentials->get_error_message()
115 ], 422);
116 }
117 if (is_array($credentials)) {
118 static::prepareSettingToSave($credentials, $mode);
119 }
120 //update the existing access token for after redirect verification
121 fluent_cart_update_option('_paypal_partner_connect_access_token_' . $mode, $response);
122 }
123
124 if (isset($response['error'])) {
125 throw new \Exception($response['error_description']);
126 }
127 } catch (\Exception $exception) {
128 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
129 throw new \Exception($exception->getMessage());
130 }
131 }
132
133
134 public static function prepareSettingToSave($credentials, $mode = 'live')
135 {
136 $clientId = Arr::get($credentials, 'client_id');
137 $clientSecret = Arr::get($credentials, 'client_secret');
138
139 $data = [
140 $mode . '_client_id' => $clientId,
141 $mode . '_client_secret' => $clientSecret,
142 $mode . '_account_id' => Arr::get($credentials, 'payer_id'),
143 'provider' => 'connect',
144 'is_active' => 'yes',
145 'payment_mode' => $mode
146 ];
147
148 $payPalInstance = App::gateway('paypal');
149
150 $oldSettings = $payPalInstance->settings->get();
151 $settingsData = wp_parse_args($data, $oldSettings);
152 $payPalInstance->updateSettings($settingsData);
153
154 //create webhook and setup endpoints
155 (new Webhook())->registerWebhook($mode);
156 }
157
158 private static function getAccountInfo($settings, $mode)
159 {
160 if (Arr::get($settings, 'provider') !== 'connect') {
161 return false;
162 }
163 return API::retrieveAccount($settings, $mode);
164 }
165
166 public static function disconnect($mode, $sendResponse = true)
167 {
168 $payPalInstance = App::gateway('paypal');
169 $paypalSettings = $payPalInstance->settings->get();
170
171 if (empty($paypalSettings[$mode . '_account_id'])) {
172 if ($sendResponse) {
173 wp_send_json([
174 'message' => __('Selected Account does not exist', 'fluent-cart')
175 ], 422);
176 }
177 return false;
178 }
179
180 $paypalSettings[$mode . '_account_id'] = '';
181 $paypalSettings[$mode . '_publishable_key'] = '';
182 $paypalSettings[$mode . '_secret_key'] = '';
183 $paypalSettings[$mode . '_webhook_events'] = [];
184 $paypalSettings[$mode . '_webhook_id'] = '';
185 $paypalSettings[$mode . '_client_id'] = '';
186 $paypalSettings[$mode . '_client_secret'] = '';
187
188 if ($mode == 'live') {
189 $alternateMode = 'test';
190 } else {
191 $alternateMode = 'live';
192 }
193
194 if (empty($paypalSettings[$alternateMode . '_account_id'])) {
195 $paypalSettings['is_active'] = 'no';
196 $paypalSettings['payment_mode'] = 'test';
197 } else {
198 $paypalSettings['payment_mode'] = $alternateMode;
199 }
200
201 $sendResponse = $payPalInstance->updateSettings($paypalSettings);
202
203 if ($sendResponse) {
204 wp_send_json([
205 'message' => __('PayPal settings has been disconnected', 'fluent-cart'),
206 'settings' => $paypalSettings
207 ], 200);
208 }
209
210 return true;
211 }
212 }
213