-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into apiki_alteracao_metodo_pagamento
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |