Add bancontact block checkout boilerplate

This commit is contained in:
Emili Castells Guasch 2024-08-09 18:27:00 +02:00
parent 633ae1aed0
commit 60885b2ad9
10 changed files with 2472 additions and 0 deletions

View file

@ -0,0 +1,14 @@
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": "3.25.0"
}
],
[
"@babel/preset-react"
]
]
}

View file

@ -0,0 +1,3 @@
node_modules
assets/js
assets/css

View file

@ -0,0 +1,33 @@
{
"name": "ppcp-local-alternative-payment-methods",
"version": "1.0.0",
"license": "GPL-3.0-or-later",
"browserslist": [
"> 0.5%",
"Safari >= 8",
"Chrome >= 41",
"Firefox >= 43",
"Edge >= 14"
],
"dependencies": {
"core-js": "^3.25.0"
},
"devDependencies": {
"@babel/core": "^7.19",
"@babel/preset-env": "^7.19",
"@babel/preset-react": "^7.18.6",
"@woocommerce/dependency-extraction-webpack-plugin": "2.2.0",
"babel-loader": "^8.2",
"cross-env": "^7.0.3",
"file-loader": "^6.2.0",
"sass": "^1.42.1",
"sass-loader": "^12.1.0",
"webpack": "^5.76",
"webpack-cli": "^4.10"
},
"scripts": {
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
"watch": "cross-env BABEL_ENV=default NODE_ENV=production webpack --watch",
"dev": "cross-env BABEL_ENV=default webpack --watch"
}
}

View file

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

View file

@ -12,10 +12,28 @@ namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
'ppcp-local-apms.url' => static function ( ContainerInterface $container ): string {
/**
* The path cannot be false.
*
* @psalm-suppress PossiblyFalseArgument
*/
return plugins_url(
'/modules/ppcp-local-alternative-payment-methods/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},
'ppcp-local-apms.bancontact.wc-gateway' => static function ( ContainerInterface $container ): BancontactGateway {
return new BancontactGateway(
$container->get( 'api.endpoint.orders' ),
$container->get( 'api.factory.purchase-unit' )
);
},
'ppcp-local-apms.bancontact.payment-method' => static function(ContainerInterface $container): BancontactPaymentMethod {
return new BancontactPaymentMethod(
$container->get('ppcp-local-apms.url'),
$container->get( 'ppcp.asset-version' ),
$container->get('ppcp-local-apms.bancontact.wc-gateway')
);
}
);

View file

@ -0,0 +1,86 @@
<?php
/**
* Bancontact payment method.
*
* @package WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
class BancontactPaymentMethod extends AbstractPaymentMethodType {
/**
* The URL of this module.
*
* @var string
*/
private $module_url;
/**
* The assets version.
*
* @var string
*/
private $version;
/**
* Bancontact WC gateway.
*
* @var BancontactGateway
*/
private $gateway;
public function __construct(
string $module_url,
string $version,
BancontactGateway $gateway
) {
$this->module_url = $module_url;
$this->version = $version;
$this->gateway = $gateway;
$this->name = BancontactGateway::ID;
}
/**
* {@inheritDoc}
*/
public function initialize() {}
/**
* {@inheritDoc}
*/
public function is_active() {
return true;
}
/**
* {@inheritDoc}
*/
public function get_payment_method_script_handles() {
wp_register_script(
'ppcp-bancontact-checkout-block',
trailingslashit( $this->module_url ) . 'assets/js/bancontact-checkout-block.js',
array(),
$this->version,
true
);
return array( 'ppcp-bancontact-checkout-block' );
}
/**
* {@inheritDoc}
*/
public function get_payment_method_data() {
return array(
'id' => $this->name,
'title' => $this->gateway->title,
'description' => $this->gateway->description,
);
}
}

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
@ -35,5 +36,12 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
return $methods;
});
add_action(
'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' ) );
}
);
}
}

View file

@ -0,0 +1,42 @@
const path = require( 'path' );
const isProduction = process.env.NODE_ENV === 'production';
const DependencyExtractionWebpackPlugin = require( '@woocommerce/dependency-extraction-webpack-plugin' );
module.exports = {
devtool: isProduction ? 'source-map' : 'eval-source-map',
mode: isProduction ? 'production' : 'development',
target: 'web',
plugins: [ new DependencyExtractionWebpackPlugin() ],
entry: {
'bancontact-checkout-block': path.resolve(
'./resources/js/bancontact-checkout-block.js'
),
},
output: {
path: path.resolve( __dirname, 'assets/' ),
filename: 'js/[name].js',
},
module: {
rules: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name: 'css/[name].css',
},
},
{ loader: 'sass-loader' },
],
},
],
},
};

File diff suppressed because it is too large Load diff