Add blik payment

This commit is contained in:
Emili Castells Guasch 2024-08-20 16:02:35 +02:00
parent 0a3aa70ea9
commit 11693c8df2
7 changed files with 170 additions and 14 deletions

View file

@ -0,0 +1,9 @@
export function Blik( { config, components } ) {
const { PaymentMethodIcons } = components;
return (
<div>
<PaymentMethodIcons icons={ [ config.icon ] } align="right" />
</div>
);
}

View file

@ -0,0 +1,18 @@
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
import { Blik } from './blik-block';
const config = wc.wcSettings.getSetting( 'ppcp-blik_data' );
registerPaymentMethod( {
name: config.id,
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
content: <Blik config={ config } />,
edit: <div></div>,
ariaLabel: config.title,
canMakePayment: () => {
return true;
},
supports: {
features: config.supports,
},
} );

View file

@ -46,4 +46,11 @@ return array(
$container->get( 'ppcp-local-apms.bancontact.wc-gateway' )
);
},
'ppcp-local-apms.blik.payment-method' => static function( ContainerInterface $container ): BlikPaymentMethod {
return new BlikPaymentMethod(
$container->get( 'ppcp-local-apms.url' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'ppcp-local-apms.blik.wc-gateway' )
);
},
);

View file

@ -11,6 +11,9 @@ namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
/**
* Class BancontactPaymentMethod
*/
class BancontactPaymentMethod extends AbstractPaymentMethodType {
/**
@ -34,16 +37,23 @@ class BancontactPaymentMethod extends AbstractPaymentMethodType {
*/
private $gateway;
/**
* BancontactPaymentMethod constructor.
*
* @param string $module_url The URL of this module.
* @param string $version The assets version.
* @param BancontactGateway $gateway Bancontact WC gateway.
*/
public function __construct(
string $module_url,
string $version,
BancontactGateway $gateway
) {
$this->module_url = $module_url;
$this->version = $version;
$this->gateway = $gateway;
$this->version = $version;
$this->gateway = $gateway;
$this->name = BancontactGateway::ID;
$this->name = BancontactGateway::ID;
}
/**
@ -78,10 +88,10 @@ class BancontactPaymentMethod extends AbstractPaymentMethodType {
*/
public function get_payment_method_data() {
return array(
'id' => $this->name,
'title' => $this->gateway->title,
'description' => $this->gateway->description,
'icon' => esc_url( 'https://www.paypalobjects.com/images/checkout/alternative_payments/paypal_bancontact_color.svg' ),
'id' => $this->name,
'title' => $this->gateway->title,
'description' => $this->gateway->description,
'icon' => esc_url( 'https://www.paypalobjects.com/images/checkout/alternative_payments/paypal_bancontact_color.svg' ),
);
}
}

View file

@ -0,0 +1,97 @@
<?php
/**
* Blik payment method.
*
* @package WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
/**
* Class BlikPaymentMethod
*/
class BlikPaymentMethod extends AbstractPaymentMethodType {
/**
* The URL of this module.
*
* @var string
*/
private $module_url;
/**
* The assets version.
*
* @var string
*/
private $version;
/**
* Blik WC gateway.
*
* @var BlikGateway
*/
private $gateway;
/**
* BlikPaymentMethod constructor.
*
* @param string $module_url The URL of this module.
* @param string $version The assets version.
* @param BlikGateway $gateway Blik WC gateway.
*/
public function __construct(
string $module_url,
string $version,
BlikGateway $gateway
) {
$this->module_url = $module_url;
$this->version = $version;
$this->gateway = $gateway;
$this->name = BlikGateway::ID;
}
/**
* {@inheritDoc}
*/
public function initialize() {}
/**
* {@inheritDoc}
*/
public function is_active() {
return true;
}
/**
* {@inheritDoc}
*/
public function get_payment_method_script_handles() {
wp_register_script(
'ppcp-blick-payment-method',
trailingslashit( $this->module_url ) . 'assets/js/blik-payment-method.js',
array(),
$this->version,
true
);
return array( 'ppcp-blick-payment-method' );
}
/**
* {@inheritDoc}
*/
public function get_payment_method_data() {
return array(
'id' => $this->name,
'title' => $this->gateway->title,
'description' => $this->gateway->description,
'icon' => esc_url( 'https://www.paypalobjects.com/images/checkout/alternative_payments/paypal_blik_color.svg' ),
);
}
}

View file

@ -37,12 +37,19 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
public function run( ContainerInterface $c ): void {
add_filter(
'woocommerce_payment_gateways',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function ( $methods ) use ( $c ) {
if ( is_admin() ) {
$methods[] = $c->get( 'ppcp-local-apms.bancontact.wc-gateway' );
$methods[] = $c->get( 'ppcp-local-apms.blik.wc-gateway' );
if ( ! is_array( $methods ) ) {
return $methods;
}
$methods[] = $c->get( 'ppcp-local-apms.bancontact.wc-gateway' );
$methods[] = $c->get( 'ppcp-local-apms.blik.wc-gateway' );
return $methods;
}
);
@ -55,15 +62,19 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
* @psalm-suppress MissingClosureParamType
*/
function ( $methods ) use ( $c ) {
if ( ! is_array( $methods ) ) {
return $methods;
}
if ( ! is_admin() ) {
$customer_country = WC()->customer->get_billing_country() ?: WC()->customer->get_shipping_country();
$site_currency = get_woocommerce_currency();
if ( $customer_country === 'BE' && $site_currency === 'EUR' ) {
$methods[ BancontactGateway::ID ] = $c->get( 'ppcp-local-apms.bancontact.wc-gateway' );
if ( $customer_country !== 'BE' || $site_currency !== 'EUR' ) {
unset( $methods[ BancontactGateway::ID ] );
}
if ( $customer_country === 'PL' && $site_currency === 'PLN' ) {
$methods[ BlikGateway::ID ] = $c->get( 'ppcp-local-apms.blik.wc-gateway' );
if ( $customer_country !== 'PL' || $site_currency !== 'PLN' ) {
unset( $methods[ BlikGateway::ID ] );
}
}
@ -75,6 +86,7 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
'woocommerce_blocks_payment_method_type_registration',
function( PaymentMethodRegistry $payment_method_registry ) use ( $c ): void {
$payment_method_registry->register( $c->get( 'ppcp-local-apms.bancontact.payment-method' ) );
$payment_method_registry->register( $c->get( 'ppcp-local-apms.blik.payment-method' ) );
}
);

View file

@ -12,6 +12,9 @@ module.exports = {
'bancontact-payment-method': path.resolve(
'./resources/js/bancontact-payment-method.js'
),
'blik-payment-method': path.resolve(
'./resources/js/blik-payment-method.js'
),
},
output: {
path: path.resolve( __dirname, 'assets/' ),