| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager; |
| 6 |
use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\API; |
| 7 |
use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\Webhook; |
| 8 |
use FluentCart\App\Modules\PaymentMethods\PayPalGateway\PayPalSettingsBase; |
| 9 |
use FluentCart\Framework\Http\Request\Request; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
use FluentCart\App\Hooks\Handlers\GlobalPaymentHandler; |
| 12 |
use FluentCart\App\Models\Order; |
| 13 |
use FluentCart\App\Services\PluginInstaller\PaymentAddonManager; |
| 14 |
|
| 15 |
|
| 16 |
class PaymentMethodController extends Controller |
| 17 |
{ |
| 18 |
public function index(Request $request, GlobalPaymentHandler $globalHandler) |
| 19 |
{ |
| 20 |
try { |
| 21 |
$gateways = $globalHandler->getAll(); |
| 22 |
$categorizedGateways = [ |
| 23 |
'available' => [], |
| 24 |
'offline' => [], |
| 25 |
'requires_pro' => [], |
| 26 |
'upcoming' => [] |
| 27 |
]; |
| 28 |
|
| 29 |
|
| 30 |
foreach ($gateways as $gateway) { |
| 31 |
if (isset($gateway['requires_pro'])) { |
| 32 |
$categorizedGateways['requires_pro'][] = $gateway; |
| 33 |
} elseif (Arr::get($gateway, 'upcoming', false) === true) { |
| 34 |
$categorizedGateways['upcoming'][] = $gateway; |
| 35 |
} else { |
| 36 |
$categorizedGateways['available'][] = $gateway; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return [ |
| 41 |
'gateways' => array_merge( |
| 42 |
$categorizedGateways['available'], |
| 43 |
$categorizedGateways['requires_pro'], |
| 44 |
$categorizedGateways['upcoming'] |
| 45 |
) |
| 46 |
]; |
| 47 |
|
| 48 |
} catch (\Exception $error) { |
| 49 |
return $this->sendError([ |
| 50 |
'message' => $error->getMessage() |
| 51 |
], 423); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function store(Request $request, GlobalPaymentHandler $globalHandler) |
| 56 |
{ |
| 57 |
$data = $request->settings; |
| 58 |
$method = sanitize_text_field($request->method); |
| 59 |
if (GatewayManager::has($method)) { |
| 60 |
$methodInstance = GatewayManager::getInstance($method); |
| 61 |
wp_send_json( |
| 62 |
$methodInstance->updateSettings($data), |
| 63 |
200 |
| 64 |
); |
| 65 |
} else { |
| 66 |
throw new \Exception(esc_html__('No valid payment method found!', 'fluent-cart')); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function getSettings(Request $request, GlobalPaymentHandler $globalHandler) |
| 71 |
{ |
| 72 |
try { |
| 73 |
return $globalHandler->getSettings(sanitize_text_field($request->method)); |
| 74 |
} catch (\Exception $error) { |
| 75 |
return $this->sendError([ |
| 76 |
'message' => $error->getMessage() |
| 77 |
], 423); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public function saveDesign(Request $request) |
| 82 |
{ |
| 83 |
try { |
| 84 |
$method = sanitize_text_field($request->get('method')); |
| 85 |
if (!GatewayManager::has($method)) { |
| 86 |
return $this->sendError(['message' => __('Invalid payment method', 'fluent-cart')], 422); |
| 87 |
} |
| 88 |
|
| 89 |
$checkoutLabel = $request->getSafe('checkout_label', 'sanitize_text_field'); |
| 90 |
$checkoutLogo = $request->getSafe('checkout_logo', 'sanitize_url'); |
| 91 |
$checkoutInstructions = $request->getSafe('checkout_instructions', 'wp_kses_post'); |
| 92 |
$thankYouPageInstructions = $request->getSafe('thank_you_page_instructions', 'wp_kses_post'); |
| 93 |
$methodInstance = GatewayManager::getInstance($method); |
| 94 |
|
| 95 |
if (!$methodInstance) { |
| 96 |
return $this->sendError(['message' => __('Invalid payment method instance', 'fluent-cart')], 422); |
| 97 |
} |
| 98 |
|
| 99 |
// Merge into existing settings and persist via gateway's updateSettings |
| 100 |
$current = (array) $methodInstance->settings->get(); |
| 101 |
$current['checkout_label'] = $checkoutLabel; |
| 102 |
$current['checkout_logo'] = $checkoutLogo; |
| 103 |
$current['checkout_instructions'] = $checkoutInstructions; |
| 104 |
$current['thank_you_page_instructions'] = $thankYouPageInstructions; |
| 105 |
|
| 106 |
if (method_exists($methodInstance, 'updateSettings')) { |
| 107 |
$saved = $methodInstance->updateSettings($current); |
| 108 |
} else { |
| 109 |
return $this->sendError(['message' => __('Gateway cannot update settings', 'fluent-cart')], 500); |
| 110 |
} |
| 111 |
|
| 112 |
return $this->sendSuccess([ |
| 113 |
'message' => __('Checkout design settings saved', 'fluent-cart'), |
| 114 |
'settings' => $saved |
| 115 |
]); |
| 116 |
} catch (\Throwable $e) { |
| 117 |
return $this->sendError(['message' => $e->getMessage()], 500); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function connectInfo(Request $request, GlobalPaymentHandler $globalHandler) |
| 122 |
{ |
| 123 |
if (GatewayManager::has($request->getSafe('method', 'sanitize_text_field'))) { |
| 124 |
$methodInstance = GatewayManager::getInstance($request->getSafe('method', 'sanitize_text_field')); |
| 125 |
if (method_exists($methodInstance, 'getConnectInfo')) { |
| 126 |
wp_send_json( |
| 127 |
$methodInstance->getConnectInfo(), |
| 128 |
200 |
| 129 |
); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public function disconnect(Request $request, GlobalPaymentHandler $globalHandler) |
| 135 |
{ |
| 136 |
return $globalHandler->disconnect( |
| 137 |
sanitize_text_field($request->method), |
| 138 |
sanitize_text_field($request->mode), |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
public function setPayPalWebhook(Request $request) |
| 143 |
{ |
| 144 |
$setupWebhook = (new Webhook())->registerWebhook($request->getSafe('mode', 'sanitize_text_field')); |
| 145 |
if (is_wp_error($setupWebhook)) { |
| 146 |
return $this->sendError([ |
| 147 |
'message' => $setupWebhook->get_error_message() |
| 148 |
], 423); |
| 149 |
} |
| 150 |
|
| 151 |
return $this->sendSuccess([ |
| 152 |
'message' => __('Webhook setup successfully! Please reload the page.', 'fluent-cart') |
| 153 |
]); |
| 154 |
} |
| 155 |
|
| 156 |
public function checkPayPalWebhook(Request $request) |
| 157 |
{ |
| 158 |
return (new Webhook())->maybeSetWebhook($request->getSafe('mode', 'sanitize_text_field')); |
| 159 |
} |
| 160 |
|
| 161 |
public function reorder(Request $request) |
| 162 |
{ |
| 163 |
try { |
| 164 |
$order = $request->get('order'); |
| 165 |
if (!is_array($order)) { |
| 166 |
return $this->sendError([ |
| 167 |
'message' => __('Invalid order format', 'fluent-cart') |
| 168 |
], 422); |
| 169 |
} |
| 170 |
|
| 171 |
// Sanitize the order array |
| 172 |
$order = array_map('sanitize_text_field', $order); |
| 173 |
|
| 174 |
// Save to WordPress options table |
| 175 |
update_option('fluent_cart_payment_methods_order', $order); |
| 176 |
|
| 177 |
return $this->sendSuccess([ |
| 178 |
'message' => __('Payment methods order saved successfully', 'fluent-cart'), |
| 179 |
'order' => $order |
| 180 |
]); |
| 181 |
} catch (\Exception $e) { |
| 182 |
return $this->sendError([ |
| 183 |
'message' => $e->getMessage() |
| 184 |
], 500); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
public function installAddon(Request $request) |
| 189 |
{ |
| 190 |
try { |
| 191 |
$sourceType = $request->getSafe('source_type', 'sanitize_text_field'); |
| 192 |
$sourceLink = $request->getSafe('source_link', 'sanitize_url'); |
| 193 |
$pluginSlug = $request->getSafe('plugin_slug', 'sanitize_text_field'); |
| 194 |
|
| 195 |
if (empty($pluginSlug) || empty($sourceType)) { |
| 196 |
return $this->sendError([ |
| 197 |
'message' => __('Plugin slug, source type are required', 'fluent-cart') |
| 198 |
], 422); |
| 199 |
} |
| 200 |
|
| 201 |
if ($sourceType === 'github' && empty($sourceLink)) { |
| 202 |
return $this->sendError([ |
| 203 |
'message' => __('Source link is required', 'fluent-cart') |
| 204 |
], 422); |
| 205 |
} |
| 206 |
|
| 207 |
$manager = new PaymentAddonManager(); |
| 208 |
$result = $manager->installAddon($sourceType, $sourceLink, $pluginSlug); |
| 209 |
|
| 210 |
if (is_wp_error($result)) { |
| 211 |
return $this->sendError([ |
| 212 |
'message' => $result->get_error_message() |
| 213 |
], 423); |
| 214 |
} |
| 215 |
|
| 216 |
return $result; |
| 217 |
} catch (\Exception $e) { |
| 218 |
return $this->sendError([ |
| 219 |
'message' => $e->getMessage() |
| 220 |
], 500); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
public function activateAddon(Request $request) |
| 225 |
{ |
| 226 |
try { |
| 227 |
$pluginFile = $request->getSafe('plugin_file', 'sanitize_text_field'); |
| 228 |
|
| 229 |
if (empty($pluginFile)) { |
| 230 |
return $this->sendError([ |
| 231 |
'message' => __('Plugin file path is required', 'fluent-cart') |
| 232 |
], 422); |
| 233 |
} |
| 234 |
|
| 235 |
$manager = new PaymentAddonManager(); |
| 236 |
$result = $manager->activateAddon($pluginFile); |
| 237 |
|
| 238 |
if (is_wp_error($result)) { |
| 239 |
return $this->sendError([ |
| 240 |
'message' => $result->get_error_message() |
| 241 |
], 423); |
| 242 |
} |
| 243 |
|
| 244 |
// Customize message for FluentCart Pro |
| 245 |
$message = __('Payment addon activated successfully!', 'fluent-cart'); |
| 246 |
if ($pluginFile === 'fluent-cart-pro/fluent-cart-pro.php') { |
| 247 |
$message = __('FluentCart Pro activated successfully! All premium features are now available.', 'fluent-cart'); |
| 248 |
} |
| 249 |
|
| 250 |
return $this->sendSuccess([ |
| 251 |
'message' => $message |
| 252 |
]); |
| 253 |
} catch (\Exception $e) { |
| 254 |
return $this->sendError([ |
| 255 |
'message' => $e->getMessage() |
| 256 |
], 500); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|