Add p24 gateway

This commit is contained in:
Emili Castells Guasch 2024-08-21 15:47:23 +02:00
parent cf97f0becd
commit 83cb9d607a
15 changed files with 378 additions and 49 deletions

View file

@ -1,4 +1,4 @@
export function EPS( { config, components } ) {
export function APM( { config, components } ) {
const { PaymentMethodIcons } = components;
return (

View file

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

View file

@ -1,12 +1,12 @@
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
import { Bancontact } from './bancontact-block';
import { APM } from './apm-block';
const config = wc.wcSettings.getSetting( 'ppcp-bancontact_data' );
registerPaymentMethod( {
name: config.id,
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
content: <Bancontact config={ config } />,
content: <APM config={ config } />,
edit: <div></div>,
ariaLabel: config.title,
canMakePayment: () => {

View file

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

View file

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

View file

@ -1,12 +1,12 @@
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
import { EPS } from './eps-block';
import { APM } from './apm-block';
const config = wc.wcSettings.getSetting( 'ppcp-eps_data' );
registerPaymentMethod( {
name: config.id,
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
content: <EPS config={ config } />,
content: <APM config={ config } />,
edit: <div></div>,
ariaLabel: config.title,
canMakePayment: () => {

View file

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

View file

@ -1,12 +1,12 @@
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
import { IDeal } from './ideal-block';
import { APM } from './apm-block';
const config = wc.wcSettings.getSetting( 'ppcp-ideal_data' );
registerPaymentMethod( {
name: config.id,
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
content: <IDeal config={ config } />,
content: <APM config={ config } />,
edit: <div></div>,
ariaLabel: config.title,
canMakePayment: () => {

View file

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

View file

@ -1,12 +1,12 @@
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
import { MyBank } from './mybank-block';
import { APM } from './apm-block';
const config = wc.wcSettings.getSetting( 'ppcp-mybank_data' );
registerPaymentMethod( {
name: config.id,
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
content: <MyBank config={ config } />,
content: <APM config={ config } />,
edit: <div></div>,
ariaLabel: config.title,
canMakePayment: () => {

View file

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

View file

@ -24,7 +24,7 @@ return array(
);
},
'ppcp-local-apms.payment-methods' => static function( ContainerInterface $container): array {
return [
return array(
'bancontact' => array(
'id' => BancontactGateway::ID,
'country' => 'BE',
@ -50,7 +50,12 @@ return array(
'country' => 'IT',
'currency' => 'EUR',
),
];
'p24' => array(
'id' => P24Gateway::ID,
'country' => 'PL',
'currency' => 'EUR',
),
);
},
'ppcp-local-apms.bancontact.wc-gateway' => static function ( ContainerInterface $container ): BancontactGateway {
return new BancontactGateway(
@ -92,6 +97,14 @@ return array(
$container->get( 'wcgateway.transaction-url-provider' )
);
},
'ppcp-local-apms.p24.wc-gateway' => static function ( ContainerInterface $container ): P24Gateway {
return new P24Gateway(
$container->get( 'api.endpoint.orders' ),
$container->get( 'api.factory.purchase-unit' ),
$container->get( 'wcgateway.processor.refunds' ),
$container->get( 'wcgateway.transaction-url-provider' )
);
},
'ppcp-local-apms.bancontact.payment-method' => static function( ContainerInterface $container ): BancontactPaymentMethod {
return new BancontactPaymentMethod(
$container->get( 'ppcp-local-apms.url' ),
@ -127,4 +140,11 @@ return array(
$container->get( 'ppcp-local-apms.mybank.wc-gateway' )
);
},
'ppcp-local-apms.p24.payment-method' => static function( ContainerInterface $container ): P24PaymentMethod {
return new P24PaymentMethod(
$container->get( 'ppcp-local-apms.url' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'ppcp-local-apms.p24.wc-gateway' )
);
},
);

View file

@ -0,0 +1,227 @@
<?php
/**
* The P24 payment gateway.
*
* @package WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use WC_Payment_Gateway;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
/**
* Class P24Gateway
*/
class P24Gateway extends WC_Payment_Gateway {
const ID = 'ppcp-p24';
/**
* PayPal Orders endpoint.
*
* @var Orders
*/
private $orders_endpoint;
/**
* Purchase unit factory.
*
* @var PurchaseUnitFactory
*/
private $purchase_unit_factory;
/**
* The Refund Processor.
*
* @var RefundProcessor
*/
private $refund_processor;
/**
* Service able to provide transaction url for an order.
*
* @var TransactionUrlProvider
*/
protected $transaction_url_provider;
/**
* P24Gateway constructor.
*
* @param Orders $orders_endpoint PayPal Orders endpoint.
* @param PurchaseUnitFactory $purchase_unit_factory Purchase unit factory.
* @param RefundProcessor $refund_processor The Refund Processor.
* @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order.
*/
public function __construct(
Orders $orders_endpoint,
PurchaseUnitFactory $purchase_unit_factory,
RefundProcessor $refund_processor,
TransactionUrlProvider $transaction_url_provider
) {
$this->id = self::ID;
$this->supports = array(
'refunds',
'products',
);
$this->method_title = __( 'Przelewy24', 'woocommerce-paypal-payments' );
$this->method_description = __( 'Przelewy24', 'woocommerce-paypal-payments' );
$this->title = $this->get_option( 'title', __( 'Przelewy24', 'woocommerce-paypal-payments' ) );
$this->description = $this->get_option( 'description', '' );
$this->icon = esc_url( 'https://www.paypalobjects.com/images/checkout/alternative_payments/paypal_przelewy24_color.svg' );
$this->init_form_fields();
$this->init_settings();
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
$this->orders_endpoint = $orders_endpoint;
$this->purchase_unit_factory = $purchase_unit_factory;
$this->refund_processor = $refund_processor;
$this->transaction_url_provider = $transaction_url_provider;
}
/**
* Initialize the form fields.
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'label' => __( 'Przelewy24', 'woocommerce-paypal-payments' ),
'default' => 'no',
'desc_tip' => true,
'description' => __( 'Enable/Disable Przelewy24 payment gateway.', 'woocommerce-paypal-payments' ),
),
'title' => array(
'title' => __( 'Title', 'woocommerce-paypal-payments' ),
'type' => 'text',
'default' => $this->title,
'desc_tip' => true,
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-paypal-payments' ),
),
'description' => array(
'title' => __( 'Description', 'woocommerce-paypal-payments' ),
'type' => 'text',
'default' => $this->description,
'desc_tip' => true,
'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce-paypal-payments' ),
),
);
}
/**
* Processes the order.
*
* @param int $order_id The WC order ID.
* @return array
*/
public function process_payment( $order_id ) {
$wc_order = wc_get_order( $order_id );
$wc_order->update_status( 'on-hold', __( 'Awaiting Przelewy24 to confirm the payment.', 'woocommerce-paypal-payments' ) );
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
$amount = $purchase_unit->amount()->to_array();
$request_body = array(
'intent' => 'CAPTURE',
'payment_source' => array(
'p24' => array(
'country_code' => 'PL',
'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(),
'email' => $wc_order->get_billing_email(),
),
),
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
'purchase_units' => array(
array(
'reference_id' => $purchase_unit->reference_id(),
'amount' => array(
'currency_code' => $amount['currency_code'],
'value' => $amount['value'],
),
'custom_id' => $purchase_unit->custom_id(),
'invoice_id' => $purchase_unit->invoice_id(),
),
),
'application_context' => array(
'locale' => 'en-PL',
'return_url' => $this->get_return_url( $wc_order ),
'cancel_url' => add_query_arg( 'cancelled', 'true', $this->get_return_url( $wc_order ) ),
),
);
try {
$response = $this->orders_endpoint->create( $request_body );
} catch ( RuntimeException $exception ) {
$wc_order->update_status(
'failed',
$exception->getMessage()
);
return array(
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
);
}
$body = json_decode( $response['body'] );
$payer_action = '';
foreach ( $body->links as $link ) {
if ( $link->rel === 'payer-action' ) {
$payer_action = $link->href;
}
}
WC()->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => esc_url( $payer_action ),
);
}
/**
* Process refund.
*
* If the gateway declares 'refunds' support, this will allow it to refund.
* a passed in amount.
*
* @param int $order_id Order ID.
* @param float $amount Refund amount.
* @param string $reason Refund reason.
* @return boolean True or false based on success, or a WP_Error object.
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
$order = wc_get_order( $order_id );
if ( ! is_a( $order, \WC_Order::class ) ) {
return false;
}
return $this->refund_processor->process( $order, (float) $amount, (string) $reason );
}
/**
* Return transaction url for this gateway and given order.
*
* @param \WC_Order $order WC order to get transaction url by.
*
* @return string
*/
public function get_transaction_url( $order ): string {
$this->view_transaction_url = $this->transaction_url_provider->get_transaction_url_base( $order );
return parent::get_transaction_url( $order );
}
}

View file

@ -0,0 +1,97 @@
<?php
/**
* Przelewy24 payment method.
*
* @package WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
/**
* Class P24PaymentMethod
*/
class P24PaymentMethod extends AbstractPaymentMethodType {
/**
* The URL of this module.
*
* @var string
*/
private $module_url;
/**
* The assets version.
*
* @var string
*/
private $version;
/**
* P24Gateway WC gateway.
*
* @var P24Gateway
*/
private $gateway;
/**
* P24PaymentMethod constructor.
*
* @param string $module_url The URL of this module.
* @param string $version The assets version.
* @param P24Gateway $gateway Przelewy24 WC gateway.
*/
public function __construct(
string $module_url,
string $version,
P24Gateway $gateway
) {
$this->module_url = $module_url;
$this->version = $version;
$this->gateway = $gateway;
$this->name = P24Gateway::ID;
}
/**
* {@inheritDoc}
*/
public function initialize() {}
/**
* {@inheritDoc}
*/
public function is_active() {
return true;
}
/**
* {@inheritDoc}
*/
public function get_payment_method_script_handles() {
wp_register_script(
'ppcp-p24-payment-method',
trailingslashit( $this->module_url ) . 'assets/js/p24-payment-method.js',
array(),
$this->version,
true
);
return array( 'ppcp-p24-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_przelewy24_color.svg' ),
);
}
}

View file

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