| 1 |
<?php |
| 2 |
/** |
| 3 |
* Payment Controller |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Controllers |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Controllers\Hooks\Payment; |
| 10 |
|
| 11 |
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry; |
| 12 |
use SeQura\Core\Infrastructure\ServiceRegister; |
| 13 |
use SeQura\WC\Controllers\Controller; |
| 14 |
use SeQura\WC\Services\Interface_Logger_Service; |
| 15 |
use SeQura\WC\Services\Order\Interface_Order_Service; |
| 16 |
use SeQura\WC\Services\Payment\Sequra_Payment_Gateway; |
| 17 |
use SeQura\WC\Services\Payment\Sequra_Payment_Gateway_Block_Support; |
| 18 |
use WC_Order; |
| 19 |
|
| 20 |
/** |
| 21 |
* Respond to payment hooks |
| 22 |
*/ |
| 23 |
class Payment_Controller extends Controller implements Interface_Payment_Controller { |
| 24 |
|
| 25 |
/** |
| 26 |
* Order service |
| 27 |
* |
| 28 |
* @var Interface_Order_Service |
| 29 |
*/ |
| 30 |
private $order_service; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor |
| 34 |
*/ |
| 35 |
public function __construct( |
| 36 |
Interface_Logger_Service $logger, |
| 37 |
string $templates_path, |
| 38 |
Interface_Order_Service $order_service |
| 39 |
) { |
| 40 |
parent::__construct( $logger, $templates_path ); |
| 41 |
$this->order_service = $order_service; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Register the gateway classes so that they can be used by WooCommerce |
| 46 |
* |
| 47 |
* @param string[] $gateways |
| 48 |
* @return string[] |
| 49 |
*/ |
| 50 |
public function register_gateway_classes( $gateways ) { |
| 51 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 52 |
if ( ! in_array( Sequra_Payment_Gateway::class, $gateways, true ) ) { |
| 53 |
$gateways[] = Sequra_Payment_Gateway::class; |
| 54 |
} |
| 55 |
return $gateways; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Register the payment gateway block so that it can be used by Gutenberg |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function register_gateway_gutenberg_block() { |
| 64 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 65 |
\add_action( 'woocommerce_blocks_payment_method_type_registration', array( $this, 'register_gateway_gutenberg_block_class' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Register the payment gateway block class |
| 70 |
* |
| 71 |
* @param PaymentMethodRegistry $payment_method_registry The payment method registry. |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function register_gateway_gutenberg_block_class( $payment_method_registry ) { |
| 75 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 76 |
$payment_method_registry->register( ServiceRegister::getService( Sequra_Payment_Gateway_Block_Support::class ) ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Append text after the thank you message on the order received page |
| 81 |
* |
| 82 |
* @param string $text The text to append. |
| 83 |
* @param mixed $order The order object. |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
public function order_received_text( $text, $order ) { |
| 87 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 88 |
$notices = \wc_print_notices( true ); |
| 89 |
return $notices . $text; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Set the proper payment method description in the order |
| 94 |
* |
| 95 |
* @param string $value The payment method title. |
| 96 |
* @param mixed $order The order object. |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function order_get_payment_method_title( $value, $order ) { |
| 100 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 101 |
$sequra_payment_method = $order instanceof WC_Order ? $this->order_service->get_payment_method_title( $order ) : null; |
| 102 |
return empty( $sequra_payment_method ) ? $value : $sequra_payment_method; |
| 103 |
} |
| 104 |
} |
| 105 |
|