Skip to content

Commit

Permalink
Merge branch 'master' into apiki_alteracao_metodo_pagamento
Browse files Browse the repository at this point in the history
  • Loading branch information
lucastgama authored Nov 28, 2024
2 parents 299de2a + f84c90e commit a3e12eb
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/includes/checkout/WCSOrderSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace VindiPaymentGateways;

class OrderSetup
{
public function __construct()
{
add_action('template_redirect', [$this, 'set_payment_gateway']);
add_filter('woocommerce_payment_gateways', [$this, 'restrict_payment_gateways_for_vindi_payment_link']);
}

public function set_payment_gateway()
{
$isPaymentLink = filter_input(INPUT_GET, 'vindi-payment-link', FILTER_VALIDATE_BOOLEAN) ?? false;
$gateway = filter_input(INPUT_GET, 'vindi-gateway', FILTER_SANITIZE_SPECIAL_CHARS) ?? '';

if ($isPaymentLink) {
WC()->session->set('vindi-payment-link', $isPaymentLink);
}

if ($gateway) {
WC()->session->set('vindi-gateway', $gateway);
}
}

public function restrict_payment_gateways_for_vindi_payment_link($available_gateways)
{
if ($this->is_vindi_payment_link()) {
$allowed_gateways = $this->get_allowed_gateways();
$available_gateways = $this->filter_gateways($available_gateways, $allowed_gateways);
}

return $available_gateways;
}

private function is_vindi_payment_link()
{
$isPaymentLink = filter_input(INPUT_GET, 'vindi-payment-link') ?? false;
return is_admin() && $isPaymentLink;
}

private function get_allowed_gateways()
{
return [
'vindi-bank-slip',
'vindi-bolepix',
'vindi-credit-card',
'vindi-pix',
];
}

private function filter_gateways($available_gateways, $allowed_gateways)
{
return array_filter($available_gateways, function ($gateway_id) use ($allowed_gateways) {
return in_array($gateway_id, $allowed_gateways);
}, ARRAY_FILTER_USE_KEY);
}
}

0 comments on commit a3e12eb

Please sign in to comment.